blob: 1b3d3e8b7a28841670b9d6b4d41cef004a67f7ae [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"
19#include "X86TargetMachine.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000020#include "X86TargetObjectFile.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000021#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000022#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000023#include "llvm/DerivedTypes.h"
Chris Lattnerb903bed2009-06-26 21:20:29 +000024#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000025#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000026#include "llvm/Function.h"
Chris Lattnerb8105652009-07-20 17:51:36 +000027#include "llvm/Instructions.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000028#include "llvm/Intrinsics.h"
Owen Andersona90b3dc2009-07-15 21:51:10 +000029#include "llvm/LLVMContext.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000030#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000031#include "llvm/CodeGen/MachineFunction.h"
32#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner5e1df8d2010-01-25 23:38:14 +000033#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000034#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000035#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000036#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner589c6f62010-01-26 06:28:43 +000037#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000038#include "llvm/MC/MCContext.h"
Daniel Dunbar4e815f82010-03-15 23:51:06 +000039#include "llvm/MC/MCExpr.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000040#include "llvm/MC/MCSymbol.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000041#include "llvm/ADT/BitVector.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000042#include "llvm/ADT/SmallSet.h"
Evan Chengb1712452010-01-27 06:25:16 +000043#include "llvm/ADT/Statistic.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000044#include "llvm/ADT/StringExtras.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000045#include "llvm/ADT/VectorExtras.h"
Mon P Wang3c81d352008-11-23 04:37:22 +000046#include "llvm/Support/CommandLine.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000047#include "llvm/Support/Debug.h"
Bill Wendlingec041eb2010-03-12 19:20:40 +000048#include "llvm/Support/Dwarf.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000049#include "llvm/Support/ErrorHandling.h"
50#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000051#include "llvm/Support/raw_ostream.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000052using namespace llvm;
Bill Wendlingec041eb2010-03-12 19:20:40 +000053using namespace dwarf;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000054
Evan Chengb1712452010-01-27 06:25:16 +000055STATISTIC(NumTailCalls, "Number of tail calls");
56
Mon P Wang3c81d352008-11-23 04:37:22 +000057static cl::opt<bool>
Mon P Wang9f22a4a2008-11-24 02:10:43 +000058DisableMMX("disable-mmx", cl::Hidden, cl::desc("Disable use of MMX"));
Mon P Wang3c81d352008-11-23 04:37:22 +000059
Evan Cheng10e86422008-04-25 19:11:04 +000060// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000061static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000062 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000063
Chris Lattnerf0144122009-07-28 03:13:23 +000064static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
Eric Christopher62f35a22010-07-05 19:26:33 +000065
66 bool is64Bit = TM.getSubtarget<X86Subtarget>().is64Bit();
67
68 if (TM.getSubtarget<X86Subtarget>().isTargetDarwin()) {
69 if (is64Bit) return new X8664_MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +000070 return new TargetLoweringObjectFileMachO();
Eric Christopher62f35a22010-07-05 19:26:33 +000071 } else if (TM.getSubtarget<X86Subtarget>().isTargetELF() ){
72 if (is64Bit) return new X8664_ELFTargetObjectFile(TM);
Anton Korobeynikov9184b252010-02-15 22:35:59 +000073 return new X8632_ELFTargetObjectFile(TM);
Eric Christopher62f35a22010-07-05 19:26:33 +000074 } else if (TM.getSubtarget<X86Subtarget>().isTargetCOFF()) {
Chris Lattnerf0144122009-07-28 03:13:23 +000075 return new TargetLoweringObjectFileCOFF();
Eric Christopher62f35a22010-07-05 19:26:33 +000076 }
77 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +000078}
79
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +000080X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +000081 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +000082 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesenf1fc3a82007-09-23 14:52:20 +000083 X86ScalarSSEf64 = Subtarget->hasSSE2();
84 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng25ab6902006-09-08 06:48:29 +000085 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000086
Anton Korobeynikov2365f512007-07-14 14:06:15 +000087 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000088 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000089
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000090 // Set up the TargetLowering object.
91
92 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Owen Anderson825b72b2009-08-11 20:47:22 +000093 setShiftAmountType(MVT::i8);
Duncan Sands03228082008-11-23 15:47:28 +000094 setBooleanContents(ZeroOrOneBooleanContent);
Evan Cheng211ffa12010-05-19 20:19:50 +000095 setSchedulingPreference(Sched::RegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +000096 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +000097
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000098 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +000099 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000100 setUseUnderscoreSetJmp(false);
101 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000102 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000103 // MS runtime is weird: it exports _setjmp, but longjmp!
104 setUseUnderscoreSetJmp(true);
105 setUseUnderscoreLongJmp(false);
106 } else {
107 setUseUnderscoreSetJmp(true);
108 setUseUnderscoreLongJmp(true);
109 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000110
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000111 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000112 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman71edb242010-04-30 18:30:26 +0000113 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000114 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000115 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000116 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000117
Owen Anderson825b72b2009-08-11 20:47:22 +0000118 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000119
Scott Michelfdc40a02009-02-17 22:15:04 +0000120 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000121 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000122 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000123 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000124 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000125 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
126 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000127
128 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000129 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
130 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
131 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
132 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
133 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
134 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000135
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000136 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
137 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000138 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
139 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
140 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000141
Evan Cheng25ab6902006-09-08 06:48:29 +0000142 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000143 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
144 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000145 } else if (!UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000146 // We have an algorithm for SSE2->double, and we turn this into a
147 // 64-bit FILD followed by conditional FADD for other targets.
148 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000149 // We have an algorithm for SSE2, and we turn this into a 64-bit
150 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000151 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000152 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000153
154 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
155 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000156 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
157 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000158
Devang Patel6a784892009-06-05 18:48:29 +0000159 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000160 // SSE has no i16 to fp conversion, only i32
161 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000162 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000163 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000164 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000165 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000166 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
167 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000168 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000169 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000170 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
171 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000172 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000173
Dale Johannesen73328d12007-09-19 23:55:34 +0000174 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
175 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000176 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
177 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000178
Evan Cheng02568ff2006-01-30 22:13:22 +0000179 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
180 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000181 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
182 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000183
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000184 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000185 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000186 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000187 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000188 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000189 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
190 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000191 }
192
193 // Handle FP_TO_UINT by promoting the destination to a larger signed
194 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000195 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
196 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
197 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000198
Evan Cheng25ab6902006-09-08 06:48:29 +0000199 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000200 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
201 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000202 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000203 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000204 // Expand FP_TO_UINT into a select.
205 // FIXME: We would like to use a Custom expander here eventually to do
206 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000207 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000208 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000209 // With SSE3 we can use fisttpll to convert to a signed i64; without
210 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000211 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000212 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000213
Chris Lattner399610a2006-12-05 18:22:22 +0000214 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesenacbf6342010-05-21 18:44:47 +0000215 if (!X86ScalarSSEf64) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000216 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
217 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000218 if (Subtarget->is64Bit()) {
Dale Johannesen7d07b482010-05-21 00:52:33 +0000219 setOperationAction(ISD::BIT_CONVERT , MVT::f64 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000220 // Without SSE, i64->f64 goes through memory; i64->MMX is Legal.
221 if (Subtarget->hasMMX() && !DisableMMX)
222 setOperationAction(ISD::BIT_CONVERT , MVT::i64 , Custom);
223 else
224 setOperationAction(ISD::BIT_CONVERT , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000225 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000226 }
Chris Lattner21f66852005-12-23 05:15:23 +0000227
Dan Gohmanb00ee212008-02-18 19:34:53 +0000228 // Scalar integer divide and remainder are lowered to use operations that
229 // produce two results, to match the available instructions. This exposes
230 // the two-result form to trivial CSE, which is able to combine x/y and x%y
231 // into a single instruction.
232 //
233 // Scalar integer multiply-high is also lowered to use two-result
234 // operations, to match the available instructions. However, plain multiply
235 // (low) operations are left as Legal, as there are single-result
236 // instructions for this in x86. Using the two-result multiply instructions
237 // when both high and low results are needed must be arranged by dagcombine.
Owen Anderson825b72b2009-08-11 20:47:22 +0000238 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
239 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
240 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
241 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
242 setOperationAction(ISD::SREM , MVT::i8 , Expand);
243 setOperationAction(ISD::UREM , MVT::i8 , Expand);
244 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
245 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
246 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
247 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
248 setOperationAction(ISD::SREM , MVT::i16 , Expand);
249 setOperationAction(ISD::UREM , MVT::i16 , Expand);
250 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
251 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
252 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
253 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
254 setOperationAction(ISD::SREM , MVT::i32 , Expand);
255 setOperationAction(ISD::UREM , MVT::i32 , Expand);
256 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
257 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
258 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
259 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
260 setOperationAction(ISD::SREM , MVT::i64 , Expand);
261 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohmana37c9f72007-09-25 18:23:27 +0000262
Owen Anderson825b72b2009-08-11 20:47:22 +0000263 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
264 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
265 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
266 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000267 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000268 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
269 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
270 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
271 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
272 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
273 setOperationAction(ISD::FREM , MVT::f32 , Expand);
274 setOperationAction(ISD::FREM , MVT::f64 , Expand);
275 setOperationAction(ISD::FREM , MVT::f80 , Expand);
276 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000277
Owen Anderson825b72b2009-08-11 20:47:22 +0000278 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
279 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
280 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
281 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000282 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
283 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000284 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
285 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
286 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000287 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000288 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
289 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
290 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000291 }
292
Owen Anderson825b72b2009-08-11 20:47:22 +0000293 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
294 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000295
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000296 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000297 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000298 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000299 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000300 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000301 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
302 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
303 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
304 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
305 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000306 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000307 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
308 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
309 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
310 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000311 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000312 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
313 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000314 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000315 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000316
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000317 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000318 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
319 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
320 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
321 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000322 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000323 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
324 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000325 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000326 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000327 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
328 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
329 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
330 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000331 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000332 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000333 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000334 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
335 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
336 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000337 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000338 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
339 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
340 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000341 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000342
Evan Chengd2cde682008-03-10 19:38:10 +0000343 if (Subtarget->hasSSE1())
Owen Anderson825b72b2009-08-11 20:47:22 +0000344 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000345
Eric Christopher9a9d2752010-07-22 02:48:34 +0000346 // We may not have a libcall for MEMBARRIER so we should lower this.
347 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
348
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000349 // On X86 and X86-64, atomic operations are lowered to locked instructions.
350 // Locked instructions, in turn, have implicit fence semantics (all memory
351 // operations are flushed before issuing the locked instruction, and they
352 // are not buffered), so we can fold away the common pattern of
353 // fence-atomic-fence.
354 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000355
Mon P Wang63307c32008-05-05 19:05:59 +0000356 // Expand certain atomics
Owen Anderson825b72b2009-08-11 20:47:22 +0000357 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Custom);
358 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Custom);
359 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
360 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Bill Wendling5bf1b4e2008-08-20 00:28:16 +0000361
Owen Anderson825b72b2009-08-11 20:47:22 +0000362 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Custom);
363 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Custom);
364 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
365 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000366
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000367 if (!Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000368 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
369 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
370 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
371 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
372 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
373 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
374 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000375 }
376
Evan Cheng3c992d22006-03-07 02:02:57 +0000377 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000378 if (!Subtarget->isTargetDarwin() &&
379 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000380 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000381 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000382 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000383
Owen Anderson825b72b2009-08-11 20:47:22 +0000384 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
385 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
386 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
387 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000388 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000389 setExceptionPointerRegister(X86::RAX);
390 setExceptionSelectorRegister(X86::RDX);
391 } else {
392 setExceptionPointerRegister(X86::EAX);
393 setExceptionSelectorRegister(X86::EDX);
394 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000395 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
396 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000397
Owen Anderson825b72b2009-08-11 20:47:22 +0000398 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000399
Owen Anderson825b72b2009-08-11 20:47:22 +0000400 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000401
Nate Begemanacc398c2006-01-25 18:21:52 +0000402 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000403 setOperationAction(ISD::VASTART , MVT::Other, Custom);
404 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000405 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000406 setOperationAction(ISD::VAARG , MVT::Other, Custom);
407 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000408 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000409 setOperationAction(ISD::VAARG , MVT::Other, Expand);
410 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000411 }
Evan Chengae642192007-03-02 23:16:35 +0000412
Owen Anderson825b72b2009-08-11 20:47:22 +0000413 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
414 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000415 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000416 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000417 if (Subtarget->isTargetCygMing())
Owen Anderson825b72b2009-08-11 20:47:22 +0000418 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000419 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000420 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000421
Evan Chengc7ce29b2009-02-13 22:36:38 +0000422 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000423 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000424 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000425 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
426 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000427
Evan Cheng223547a2006-01-31 22:28:30 +0000428 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000429 setOperationAction(ISD::FABS , MVT::f64, Custom);
430 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000431
432 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000433 setOperationAction(ISD::FNEG , MVT::f64, Custom);
434 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000435
Evan Cheng68c47cb2007-01-05 07:55:56 +0000436 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000437 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
438 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000439
Evan Chengd25e9e82006-02-02 00:28:23 +0000440 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000441 setOperationAction(ISD::FSIN , MVT::f64, Expand);
442 setOperationAction(ISD::FCOS , MVT::f64, Expand);
443 setOperationAction(ISD::FSIN , MVT::f32, Expand);
444 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000445
Chris Lattnera54aa942006-01-29 06:26:08 +0000446 // Expand FP immediates into loads from the stack, except for the special
447 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000448 addLegalFPImmediate(APFloat(+0.0)); // xorpd
449 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000450 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000451 // Use SSE for f32, x87 for f64.
452 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000453 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
454 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000455
456 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000457 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000458
459 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000460 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000461
Owen Anderson825b72b2009-08-11 20:47:22 +0000462 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000463
464 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000465 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
466 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000467
468 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000469 setOperationAction(ISD::FSIN , MVT::f32, Expand);
470 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000471
Nate Begemane1795842008-02-14 08:57:00 +0000472 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000473 addLegalFPImmediate(APFloat(+0.0f)); // xorps
474 addLegalFPImmediate(APFloat(+0.0)); // FLD0
475 addLegalFPImmediate(APFloat(+1.0)); // FLD1
476 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
477 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
478
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000479 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000480 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
481 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000482 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000483 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000484 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000485 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000486 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
487 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000488
Owen Anderson825b72b2009-08-11 20:47:22 +0000489 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
490 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
491 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
492 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000493
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000494 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000495 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
496 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000497 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000498 addLegalFPImmediate(APFloat(+0.0)); // FLD0
499 addLegalFPImmediate(APFloat(+1.0)); // FLD1
500 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
501 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000502 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
503 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
504 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
505 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000506 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000507
Dale Johannesen59a58732007-08-05 18:49:15 +0000508 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000509 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000510 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
511 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
512 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000513 {
514 bool ignored;
515 APFloat TmpFlt(+0.0);
516 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
517 &ignored);
518 addLegalFPImmediate(TmpFlt); // FLD0
519 TmpFlt.changeSign();
520 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
521 APFloat TmpFlt2(+1.0);
522 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
523 &ignored);
524 addLegalFPImmediate(TmpFlt2); // FLD1
525 TmpFlt2.changeSign();
526 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
527 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000528
Evan Chengc7ce29b2009-02-13 22:36:38 +0000529 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000530 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
531 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000532 }
Dale Johannesen2f429012007-09-26 21:10:55 +0000533 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000534
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000535 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000536 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
537 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
538 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000539
Owen Anderson825b72b2009-08-11 20:47:22 +0000540 setOperationAction(ISD::FLOG, MVT::f80, Expand);
541 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
542 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
543 setOperationAction(ISD::FEXP, MVT::f80, Expand);
544 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000545
Mon P Wangf007a8b2008-11-06 05:31:54 +0000546 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000547 // (for widening) or expand (for scalarization). Then we will selectively
548 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000549 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
550 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
551 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
552 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
553 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
554 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
555 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
556 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
557 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
558 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
559 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
560 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
561 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
562 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
563 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
564 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
565 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
566 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
567 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
568 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
569 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
570 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
571 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
572 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
573 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
574 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
575 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
576 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
577 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
578 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
579 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
580 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
581 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
582 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
583 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
584 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
585 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
586 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
587 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
588 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
589 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
590 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
591 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
592 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
593 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
594 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
595 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
596 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
597 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
598 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000599 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000600 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
601 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
602 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
603 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
604 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
605 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
606 setTruncStoreAction((MVT::SimpleValueType)VT,
607 (MVT::SimpleValueType)InnerVT, Expand);
608 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
609 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
610 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000611 }
612
Evan Chengc7ce29b2009-02-13 22:36:38 +0000613 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
614 // with -msoft-float, disable use of MMX as well.
Evan Cheng92722532009-03-26 23:06:32 +0000615 if (!UseSoftFloat && !DisableMMX && Subtarget->hasMMX()) {
Dale Johannesen76090172010-04-20 22:34:09 +0000616 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass, false);
617 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass, false);
618 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass, false);
Chris Lattnere35d9842010-07-04 22:57:10 +0000619
Dale Johannesen76090172010-04-20 22:34:09 +0000620 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass, false);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000621
Owen Anderson825b72b2009-08-11 20:47:22 +0000622 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
623 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
624 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
625 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000626
Owen Anderson825b72b2009-08-11 20:47:22 +0000627 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
628 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
629 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
630 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000631
Owen Anderson825b72b2009-08-11 20:47:22 +0000632 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
633 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
Bill Wendling74027e92007-03-15 21:24:36 +0000634
Owen Anderson825b72b2009-08-11 20:47:22 +0000635 setOperationAction(ISD::AND, MVT::v8i8, Promote);
636 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
637 setOperationAction(ISD::AND, MVT::v4i16, Promote);
638 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
639 setOperationAction(ISD::AND, MVT::v2i32, Promote);
640 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
641 setOperationAction(ISD::AND, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000642
Owen Anderson825b72b2009-08-11 20:47:22 +0000643 setOperationAction(ISD::OR, MVT::v8i8, Promote);
644 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
645 setOperationAction(ISD::OR, MVT::v4i16, Promote);
646 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
647 setOperationAction(ISD::OR, MVT::v2i32, Promote);
648 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
649 setOperationAction(ISD::OR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000650
Owen Anderson825b72b2009-08-11 20:47:22 +0000651 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
652 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
653 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
654 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
655 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
656 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
657 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000658
Owen Anderson825b72b2009-08-11 20:47:22 +0000659 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
660 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
661 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
662 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
663 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
664 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
Owen Anderson825b72b2009-08-11 20:47:22 +0000665 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000666
Owen Anderson825b72b2009-08-11 20:47:22 +0000667 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
668 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
669 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000670 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
Bill Wendlinga348c562007-03-22 18:42:45 +0000671
Owen Anderson825b72b2009-08-11 20:47:22 +0000672 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
673 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
674 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
675 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000676
Owen Anderson825b72b2009-08-11 20:47:22 +0000677 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
678 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
679 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Bill Wendling3180e202008-07-20 02:32:23 +0000680
Owen Anderson825b72b2009-08-11 20:47:22 +0000681 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
Mon P Wang9e5ecb82008-12-12 01:25:51 +0000682
Owen Anderson825b72b2009-08-11 20:47:22 +0000683 setOperationAction(ISD::SELECT, MVT::v8i8, Promote);
684 setOperationAction(ISD::SELECT, MVT::v4i16, Promote);
685 setOperationAction(ISD::SELECT, MVT::v2i32, Promote);
686 setOperationAction(ISD::SELECT, MVT::v1i64, Custom);
687 setOperationAction(ISD::VSETCC, MVT::v8i8, Custom);
688 setOperationAction(ISD::VSETCC, MVT::v4i16, Custom);
689 setOperationAction(ISD::VSETCC, MVT::v2i32, Custom);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000690
691 if (!X86ScalarSSEf64 && Subtarget->is64Bit()) {
692 setOperationAction(ISD::BIT_CONVERT, MVT::v8i8, Custom);
693 setOperationAction(ISD::BIT_CONVERT, MVT::v4i16, Custom);
694 setOperationAction(ISD::BIT_CONVERT, MVT::v2i32, Custom);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000695 setOperationAction(ISD::BIT_CONVERT, MVT::v1i64, Custom);
696 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000697 }
698
Evan Cheng92722532009-03-26 23:06:32 +0000699 if (!UseSoftFloat && Subtarget->hasSSE1()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000700 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000701
Owen Anderson825b72b2009-08-11 20:47:22 +0000702 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
703 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
704 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
705 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
706 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
707 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
708 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
709 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
710 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
711 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
712 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
713 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000714 }
715
Evan Cheng92722532009-03-26 23:06:32 +0000716 if (!UseSoftFloat && Subtarget->hasSSE2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000717 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000718
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000719 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
720 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000721 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
722 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
723 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
724 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000725
Owen Anderson825b72b2009-08-11 20:47:22 +0000726 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
727 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
728 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
729 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
730 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
731 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
732 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
733 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
734 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
735 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
736 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
737 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
738 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
739 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
740 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
741 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000742
Owen Anderson825b72b2009-08-11 20:47:22 +0000743 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
744 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
745 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
746 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000747
Owen Anderson825b72b2009-08-11 20:47:22 +0000748 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
749 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
750 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
751 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
752 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000753
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000754 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
755 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
756 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
757 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
758 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
759
Evan Cheng2c3ae372006-04-12 21:21:57 +0000760 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000761 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
762 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000763 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000764 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000765 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000766 // Do not attempt to custom lower non-128-bit vectors
767 if (!VT.is128BitVector())
768 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000769 setOperationAction(ISD::BUILD_VECTOR,
770 VT.getSimpleVT().SimpleTy, Custom);
771 setOperationAction(ISD::VECTOR_SHUFFLE,
772 VT.getSimpleVT().SimpleTy, Custom);
773 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
774 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000775 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000776
Owen Anderson825b72b2009-08-11 20:47:22 +0000777 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
778 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
779 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
780 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
781 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
782 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000783
Nate Begemancdd1eec2008-02-12 22:51:28 +0000784 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000785 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
786 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000787 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000788
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000789 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000790 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
791 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000792 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000793
794 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000795 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000796 continue;
Eric Christopher4bd24c22010-03-30 01:04:59 +0000797
Owen Andersond6662ad2009-08-10 20:46:15 +0000798 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000799 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000800 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000801 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000802 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000803 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000804 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000805 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000806 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000807 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000808 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000809
Owen Anderson825b72b2009-08-11 20:47:22 +0000810 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000811
Evan Cheng2c3ae372006-04-12 21:21:57 +0000812 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000813 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
814 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
815 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
816 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000817
Owen Anderson825b72b2009-08-11 20:47:22 +0000818 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
819 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Eli Friedman23ef1052009-06-06 03:57:58 +0000820 if (!DisableMMX && Subtarget->hasMMX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000821 setOperationAction(ISD::FP_TO_SINT, MVT::v2i32, Custom);
822 setOperationAction(ISD::SINT_TO_FP, MVT::v2i32, Custom);
Eli Friedman23ef1052009-06-06 03:57:58 +0000823 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000824 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000825
Nate Begeman14d12ca2008-02-11 04:19:36 +0000826 if (Subtarget->hasSSE41()) {
Dale Johannesen54feef22010-05-27 20:12:41 +0000827 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
828 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
829 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
830 setOperationAction(ISD::FRINT, MVT::f32, Legal);
831 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
832 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
833 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
834 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
835 setOperationAction(ISD::FRINT, MVT::f64, Legal);
836 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
837
Nate Begeman14d12ca2008-02-11 04:19:36 +0000838 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000839 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000840
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000841 // Can turn SHL into an integer multiply.
842 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
Nate Begeman51409212010-07-28 00:21:48 +0000843 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000844
Nate Begeman14d12ca2008-02-11 04:19:36 +0000845 // i8 and i16 vectors are custom , because the source register and source
846 // source memory operand types are not the same width. f32 vectors are
847 // custom since the immediate controlling the insert encodes additional
848 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000849 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
850 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
851 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
852 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000853
Owen Anderson825b72b2009-08-11 20:47:22 +0000854 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
855 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
856 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
857 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000858
859 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000860 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
861 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000862 }
863 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000864
Nate Begeman30a0de92008-07-17 16:51:19 +0000865 if (Subtarget->hasSSE42()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000866 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
Nate Begeman30a0de92008-07-17 16:51:19 +0000867 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000868
David Greene9b9838d2009-06-29 16:47:10 +0000869 if (!UseSoftFloat && Subtarget->hasAVX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000870 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
871 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
872 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
873 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000874
Owen Anderson825b72b2009-08-11 20:47:22 +0000875 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
876 setOperationAction(ISD::LOAD, MVT::v8i32, Legal);
877 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
878 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
879 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
880 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
881 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
882 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
883 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
884 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
885 //setOperationAction(ISD::BUILD_VECTOR, MVT::v8f32, Custom);
886 //setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Custom);
887 //setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8f32, Custom);
888 //setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
889 //setOperationAction(ISD::VSETCC, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000890
891 // Operations to consider commented out -v16i16 v32i8
Owen Anderson825b72b2009-08-11 20:47:22 +0000892 //setOperationAction(ISD::ADD, MVT::v16i16, Legal);
893 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
894 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
895 //setOperationAction(ISD::SUB, MVT::v32i8, Legal);
896 //setOperationAction(ISD::SUB, MVT::v16i16, Legal);
897 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
898 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
899 //setOperationAction(ISD::MUL, MVT::v16i16, Legal);
900 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
901 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
902 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
903 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
904 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
905 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000906
Owen Anderson825b72b2009-08-11 20:47:22 +0000907 setOperationAction(ISD::VSETCC, MVT::v4f64, Custom);
908 // setOperationAction(ISD::VSETCC, MVT::v32i8, Custom);
909 // setOperationAction(ISD::VSETCC, MVT::v16i16, Custom);
910 setOperationAction(ISD::VSETCC, MVT::v8i32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000911
Owen Anderson825b72b2009-08-11 20:47:22 +0000912 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v32i8, Custom);
913 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i16, Custom);
914 // setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i16, Custom);
915 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i32, Custom);
916 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000917
Owen Anderson825b72b2009-08-11 20:47:22 +0000918 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f64, Custom);
919 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i64, Custom);
920 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f64, Custom);
921 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i64, Custom);
922 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f64, Custom);
923 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000924
925#if 0
926 // Not sure we want to do this since there are no 256-bit integer
927 // operations in AVX
928
929 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
930 // This includes 256-bit vectors
Owen Anderson825b72b2009-08-11 20:47:22 +0000931 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; ++i) {
932 EVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000933
934 // Do not attempt to custom lower non-power-of-2 vectors
935 if (!isPowerOf2_32(VT.getVectorNumElements()))
936 continue;
937
938 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
939 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
940 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
941 }
942
943 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000944 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i64, Custom);
945 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i64, Custom);
Eric Christopherfd179292009-08-27 18:07:15 +0000946 }
David Greene9b9838d2009-06-29 16:47:10 +0000947#endif
948
949#if 0
950 // Not sure we want to do this since there are no 256-bit integer
951 // operations in AVX
952
953 // Promote v32i8, v16i16, v8i32 load, select, and, or, xor to v4i64.
954 // Including 256-bit vectors
Owen Anderson825b72b2009-08-11 20:47:22 +0000955 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; i++) {
956 EVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000957
958 if (!VT.is256BitVector()) {
959 continue;
960 }
961 setOperationAction(ISD::AND, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000962 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000963 setOperationAction(ISD::OR, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000964 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000965 setOperationAction(ISD::XOR, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000966 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000967 setOperationAction(ISD::LOAD, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000968 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000969 setOperationAction(ISD::SELECT, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000970 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000971 }
972
Owen Anderson825b72b2009-08-11 20:47:22 +0000973 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
David Greene9b9838d2009-06-29 16:47:10 +0000974#endif
975 }
976
Evan Cheng6be2c582006-04-05 23:38:46 +0000977 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000978 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +0000979
Bill Wendling74c37652008-12-09 22:08:41 +0000980 // Add/Sub/Mul with overflow operations are custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000981 setOperationAction(ISD::SADDO, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000982 setOperationAction(ISD::UADDO, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000983 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000984 setOperationAction(ISD::USUBO, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000985 setOperationAction(ISD::SMULO, MVT::i32, Custom);
Dan Gohman71c62a22010-06-02 19:13:40 +0000986
Eli Friedman962f5492010-06-02 19:35:46 +0000987 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
988 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +0000989 //
Eli Friedman962f5492010-06-02 19:35:46 +0000990 // FIXME: We really should do custom legalization for addition and
991 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
992 // than generic legalization for 64-bit multiplication-with-overflow, though.
Eli Friedmana993f0a2010-06-02 00:27:18 +0000993 if (Subtarget->is64Bit()) {
994 setOperationAction(ISD::SADDO, MVT::i64, Custom);
995 setOperationAction(ISD::UADDO, MVT::i64, Custom);
996 setOperationAction(ISD::SSUBO, MVT::i64, Custom);
997 setOperationAction(ISD::USUBO, MVT::i64, Custom);
998 setOperationAction(ISD::SMULO, MVT::i64, Custom);
999 }
Bill Wendling41ea7e72008-11-24 19:21:46 +00001000
Evan Chengd54f2d52009-03-31 19:38:51 +00001001 if (!Subtarget->is64Bit()) {
1002 // These libcalls are not available in 32-bit.
1003 setLibcallName(RTLIB::SHL_I128, 0);
1004 setLibcallName(RTLIB::SRL_I128, 0);
1005 setLibcallName(RTLIB::SRA_I128, 0);
1006 }
1007
Evan Cheng206ee9d2006-07-07 08:33:52 +00001008 // We have target-specific dag combine patterns for the following nodes:
1009 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001010 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +00001011 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +00001012 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001013 setTargetDAGCombine(ISD::SHL);
1014 setTargetDAGCombine(ISD::SRA);
1015 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001016 setTargetDAGCombine(ISD::OR);
Chris Lattner149a4e52008-02-22 02:09:43 +00001017 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001018 setTargetDAGCombine(ISD::ZERO_EXTEND);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001019 if (Subtarget->is64Bit())
1020 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001021
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001022 computeRegisterProperties();
1023
Evan Cheng87ed7162006-02-14 08:25:08 +00001024 // FIXME: These should be based on subtarget info. Plus, the values should
1025 // be smaller when we are in optimizing for size mode.
Dan Gohman87060f52008-06-30 21:00:56 +00001026 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng255f20f2010-04-01 06:04:33 +00001027 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Dan Gohman87060f52008-06-30 21:00:56 +00001028 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Evan Chengfb8075d2008-02-28 00:43:03 +00001029 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001030 benefitFromCodePlacementOpt = true;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001031}
1032
Scott Michel5b8f82e2008-03-10 15:42:14 +00001033
Owen Anderson825b72b2009-08-11 20:47:22 +00001034MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
1035 return MVT::i8;
Scott Michel5b8f82e2008-03-10 15:42:14 +00001036}
1037
1038
Evan Cheng29286502008-01-23 23:17:41 +00001039/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1040/// the desired ByVal argument alignment.
1041static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
1042 if (MaxAlign == 16)
1043 return;
1044 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
1045 if (VTy->getBitWidth() == 128)
1046 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +00001047 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1048 unsigned EltAlign = 0;
1049 getMaxByValAlign(ATy->getElementType(), EltAlign);
1050 if (EltAlign > MaxAlign)
1051 MaxAlign = EltAlign;
1052 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1053 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1054 unsigned EltAlign = 0;
1055 getMaxByValAlign(STy->getElementType(i), EltAlign);
1056 if (EltAlign > MaxAlign)
1057 MaxAlign = EltAlign;
1058 if (MaxAlign == 16)
1059 break;
1060 }
1061 }
1062 return;
1063}
1064
1065/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1066/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001067/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1068/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +00001069unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001070 if (Subtarget->is64Bit()) {
1071 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001072 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001073 if (TyAlign > 8)
1074 return TyAlign;
1075 return 8;
1076 }
1077
Evan Cheng29286502008-01-23 23:17:41 +00001078 unsigned Align = 4;
Dale Johannesen0c191872008-02-08 19:48:20 +00001079 if (Subtarget->hasSSE1())
1080 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001081 return Align;
1082}
Chris Lattner2b02a442007-02-25 08:29:00 +00001083
Evan Chengf0df0312008-05-15 08:39:06 +00001084/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001085/// and store operations as a result of memset, memcpy, and memmove
1086/// lowering. If DstAlign is zero that means it's safe to destination
1087/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1088/// means there isn't a need to check it against alignment requirement,
1089/// probably because the source does not need to be loaded. If
1090/// 'NonScalarIntSafe' is true, that means it's safe to return a
1091/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1092/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1093/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001094/// It returns EVT::Other if the type should be determined using generic
1095/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001096EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001097X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1098 unsigned DstAlign, unsigned SrcAlign,
Evan Chengf28f8bc2010-04-02 19:36:14 +00001099 bool NonScalarIntSafe,
Evan Chengc3b0c342010-04-08 07:37:57 +00001100 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001101 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001102 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1103 // linux. This is because the stack realignment code can't handle certain
1104 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001105 const Function *F = MF.getFunction();
Evan Chengf28f8bc2010-04-02 19:36:14 +00001106 if (NonScalarIntSafe &&
1107 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001108 if (Size >= 16 &&
1109 (Subtarget->isUnalignedMemAccessFast() ||
Chandler Carruthae1d41c2010-04-02 01:31:24 +00001110 ((DstAlign == 0 || DstAlign >= 16) &&
1111 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001112 Subtarget->getStackAlignment() >= 16) {
1113 if (Subtarget->hasSSE2())
1114 return MVT::v4i32;
Evan Chengf28f8bc2010-04-02 19:36:14 +00001115 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001116 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001117 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001118 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001119 Subtarget->getStackAlignment() >= 8 &&
Evan Chengc3b0c342010-04-08 07:37:57 +00001120 Subtarget->hasSSE2()) {
1121 // Do not use f64 to lower memcpy if source is string constant. It's
1122 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001123 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001124 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001125 }
Evan Chengf0df0312008-05-15 08:39:06 +00001126 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001127 return MVT::i64;
1128 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001129}
1130
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001131/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1132/// current function. The returned value is a member of the
1133/// MachineJumpTableInfo::JTEntryKind enum.
1134unsigned X86TargetLowering::getJumpTableEncoding() const {
1135 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1136 // symbol.
1137 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1138 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001139 return MachineJumpTableInfo::EK_Custom32;
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001140
1141 // Otherwise, use the normal jump table encoding heuristics.
1142 return TargetLowering::getJumpTableEncoding();
1143}
1144
Chris Lattner589c6f62010-01-26 06:28:43 +00001145/// getPICBaseSymbol - Return the X86-32 PIC base.
1146MCSymbol *
1147X86TargetLowering::getPICBaseSymbol(const MachineFunction *MF,
1148 MCContext &Ctx) const {
1149 const MCAsmInfo &MAI = *getTargetMachine().getMCAsmInfo();
Chris Lattner9b97a732010-03-30 18:10:53 +00001150 return Ctx.GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix())+
1151 Twine(MF->getFunctionNumber())+"$pb");
Chris Lattner589c6f62010-01-26 06:28:43 +00001152}
1153
1154
Chris Lattnerc64daab2010-01-26 05:02:42 +00001155const MCExpr *
1156X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1157 const MachineBasicBlock *MBB,
1158 unsigned uid,MCContext &Ctx) const{
1159 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1160 Subtarget->isPICStyleGOT());
1161 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1162 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001163 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1164 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001165}
1166
Evan Chengcc415862007-11-09 01:32:10 +00001167/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1168/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001169SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001170 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001171 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001172 // This doesn't have DebugLoc associated with it, but is not really the
1173 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001174 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001175 return Table;
1176}
1177
Chris Lattner589c6f62010-01-26 06:28:43 +00001178/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1179/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1180/// MCExpr.
1181const MCExpr *X86TargetLowering::
1182getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1183 MCContext &Ctx) const {
1184 // X86-64 uses RIP relative addressing based on the jump table label.
1185 if (Subtarget->isPICStyleRIPRel())
1186 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1187
1188 // Otherwise, the reference is relative to the PIC base.
1189 return MCSymbolRefExpr::Create(getPICBaseSymbol(MF, Ctx), Ctx);
1190}
1191
Bill Wendlingb4202b82009-07-01 18:50:55 +00001192/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +00001193unsigned X86TargetLowering::getFunctionAlignment(const Function *F) const {
Dan Gohman25103a22009-08-18 00:20:06 +00001194 return F->hasFnAttr(Attribute::OptimizeForSize) ? 0 : 4;
Bill Wendling20c568f2009-06-30 22:38:32 +00001195}
1196
Evan Chengdee81012010-07-26 21:50:05 +00001197std::pair<const TargetRegisterClass*, uint8_t>
1198X86TargetLowering::findRepresentativeClass(EVT VT) const{
1199 const TargetRegisterClass *RRC = 0;
1200 uint8_t Cost = 1;
1201 switch (VT.getSimpleVT().SimpleTy) {
1202 default:
1203 return TargetLowering::findRepresentativeClass(VT);
1204 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1205 RRC = (Subtarget->is64Bit()
1206 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1207 break;
1208 case MVT::v8i8: case MVT::v4i16:
1209 case MVT::v2i32: case MVT::v1i64:
1210 RRC = X86::VR64RegisterClass;
1211 break;
1212 case MVT::f32: case MVT::f64:
1213 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1214 case MVT::v4f32: case MVT::v2f64:
1215 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1216 case MVT::v4f64:
1217 RRC = X86::VR128RegisterClass;
1218 break;
1219 }
1220 return std::make_pair(RRC, Cost);
1221}
1222
Evan Cheng70017e42010-07-24 00:39:05 +00001223unsigned
1224X86TargetLowering::getRegPressureLimit(const TargetRegisterClass *RC,
1225 MachineFunction &MF) const {
1226 unsigned FPDiff = RegInfo->hasFP(MF) ? 1 : 0;
1227 switch (RC->getID()) {
1228 default:
1229 return 0;
1230 case X86::GR32RegClassID:
1231 return 4 - FPDiff;
1232 case X86::GR64RegClassID:
1233 return 8 - FPDiff;
1234 case X86::VR128RegClassID:
1235 return Subtarget->is64Bit() ? 10 : 4;
1236 case X86::VR64RegClassID:
1237 return 4;
1238 }
1239}
1240
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001241bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1242 unsigned &Offset) const {
1243 if (!Subtarget->isTargetLinux())
1244 return false;
1245
1246 if (Subtarget->is64Bit()) {
1247 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1248 Offset = 0x28;
1249 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1250 AddressSpace = 256;
1251 else
1252 AddressSpace = 257;
1253 } else {
1254 // %gs:0x14 on i386
1255 Offset = 0x14;
1256 AddressSpace = 256;
1257 }
1258 return true;
1259}
1260
1261
Chris Lattner2b02a442007-02-25 08:29:00 +00001262//===----------------------------------------------------------------------===//
1263// Return Value Calling Convention Implementation
1264//===----------------------------------------------------------------------===//
1265
Chris Lattner59ed56b2007-02-28 04:55:35 +00001266#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001267
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001268bool
1269X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001270 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001271 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001272 SmallVector<CCValAssign, 16> RVLocs;
1273 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001274 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001275 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001276}
1277
Dan Gohman98ca4f22009-08-05 01:29:28 +00001278SDValue
1279X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001280 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001281 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001282 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001283 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001284 MachineFunction &MF = DAG.getMachineFunction();
1285 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001286
Chris Lattner9774c912007-02-27 05:28:59 +00001287 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001288 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1289 RVLocs, *DAG.getContext());
1290 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001291
Evan Chengdcea1632010-02-04 02:40:39 +00001292 // Add the regs to the liveout set for the function.
1293 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1294 for (unsigned i = 0; i != RVLocs.size(); ++i)
1295 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1296 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001297
Dan Gohman475871a2008-07-27 21:46:04 +00001298 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001299
Dan Gohman475871a2008-07-27 21:46:04 +00001300 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001301 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1302 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001303 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1304 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001305
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001306 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001307 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1308 CCValAssign &VA = RVLocs[i];
1309 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001310 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001311 EVT ValVT = ValToCopy.getValueType();
1312
1313 // If this is x86-64, and we disabled SSE, we can't return FP values
1314 if ((ValVT == MVT::f32 || ValVT == MVT::f64) &&
1315 (Subtarget->is64Bit() && !Subtarget->hasSSE1())) {
1316 report_fatal_error("SSE register return with SSE disabled");
1317 }
1318 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1319 // llvm-gcc has never done it right and no one has noticed, so this
1320 // should be OK for now.
1321 if (ValVT == MVT::f64 &&
1322 (Subtarget->is64Bit() && !Subtarget->hasSSE2())) {
1323 report_fatal_error("SSE2 register return with SSE2 disabled");
1324 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001325
Chris Lattner447ff682008-03-11 03:23:40 +00001326 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1327 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001328 if (VA.getLocReg() == X86::ST0 ||
1329 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001330 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1331 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001332 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001333 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001334 RetOps.push_back(ValToCopy);
1335 // Don't emit a copytoreg.
1336 continue;
1337 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001338
Evan Cheng242b38b2009-02-23 09:03:22 +00001339 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1340 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001341 if (Subtarget->is64Bit()) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001342 if (ValVT.isVector() && ValVT.getSizeInBits() == 64) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001343 ValToCopy = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, ValToCopy);
Evan Cheng242b38b2009-02-23 09:03:22 +00001344 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1)
Eric Christopher90eb4022010-07-22 00:26:08 +00001345 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1346 ValToCopy);
Evan Cheng242b38b2009-02-23 09:03:22 +00001347 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001348 }
1349
Dale Johannesendd64c412009-02-04 00:33:20 +00001350 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001351 Flag = Chain.getValue(1);
1352 }
Dan Gohman61a92132008-04-21 23:59:07 +00001353
1354 // The x86-64 ABI for returning structs by value requires that we copy
1355 // the sret argument into %rax for the return. We saved the argument into
1356 // a virtual register in the entry block, so now we copy the value out
1357 // and into %rax.
1358 if (Subtarget->is64Bit() &&
1359 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1360 MachineFunction &MF = DAG.getMachineFunction();
1361 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1362 unsigned Reg = FuncInfo->getSRetReturnReg();
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001363 assert(Reg &&
1364 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001365 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001366
Dale Johannesendd64c412009-02-04 00:33:20 +00001367 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001368 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001369
1370 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001371 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001372 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001373
Chris Lattner447ff682008-03-11 03:23:40 +00001374 RetOps[0] = Chain; // Update chain.
1375
1376 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001377 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001378 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001379
1380 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001381 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001382}
1383
Dan Gohman98ca4f22009-08-05 01:29:28 +00001384/// LowerCallResult - Lower the result values of a call into the
1385/// appropriate copies out of appropriate physical registers.
1386///
1387SDValue
1388X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001389 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001390 const SmallVectorImpl<ISD::InputArg> &Ins,
1391 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001392 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001393
Chris Lattnere32bbf62007-02-28 07:09:55 +00001394 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001395 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001396 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001397 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001398 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001399 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001400
Chris Lattner3085e152007-02-25 08:59:22 +00001401 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001402 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001403 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001404 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001405
Torok Edwin3f142c32009-02-01 18:15:56 +00001406 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001407 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Dan Gohman98ca4f22009-08-05 01:29:28 +00001408 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001409 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001410 }
1411
Evan Cheng79fb3b42009-02-20 20:43:02 +00001412 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001413
1414 // If this is a call to a function that returns an fp value on the floating
1415 // point stack, we must guarantee the the value is popped from the stack, so
1416 // a CopyFromReg is not good enough - the copy instruction may be eliminated
1417 // if the return value is not used. We use the FpGET_ST0 instructions
1418 // instead.
1419 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1420 // If we prefer to use the value in xmm registers, copy it out as f80 and
1421 // use a truncate to move it from fp stack reg to xmm reg.
1422 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
1423 bool isST0 = VA.getLocReg() == X86::ST0;
1424 unsigned Opc = 0;
1425 if (CopyVT == MVT::f32) Opc = isST0 ? X86::FpGET_ST0_32:X86::FpGET_ST1_32;
1426 if (CopyVT == MVT::f64) Opc = isST0 ? X86::FpGET_ST0_64:X86::FpGET_ST1_64;
1427 if (CopyVT == MVT::f80) Opc = isST0 ? X86::FpGET_ST0_80:X86::FpGET_ST1_80;
1428 SDValue Ops[] = { Chain, InFlag };
1429 Chain = SDValue(DAG.getMachineNode(Opc, dl, CopyVT, MVT::Other, MVT::Flag,
1430 Ops, 2), 1);
1431 Val = Chain.getValue(0);
1432
1433 // Round the f80 to the right size, which also moves it to the appropriate
1434 // xmm register.
1435 if (CopyVT != VA.getValVT())
1436 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1437 // This truncation won't change the value.
1438 DAG.getIntPtrConstant(1));
1439 } else if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001440 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1441 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1442 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001443 MVT::v2i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001444 Val = Chain.getValue(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001445 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1446 Val, DAG.getConstant(0, MVT::i64));
Evan Cheng242b38b2009-02-23 09:03:22 +00001447 } else {
1448 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001449 MVT::i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001450 Val = Chain.getValue(0);
1451 }
Evan Cheng79fb3b42009-02-20 20:43:02 +00001452 Val = DAG.getNode(ISD::BIT_CONVERT, dl, CopyVT, Val);
1453 } else {
1454 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1455 CopyVT, InFlag).getValue(1);
1456 Val = Chain.getValue(0);
1457 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001458 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001459 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001460 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001461
Dan Gohman98ca4f22009-08-05 01:29:28 +00001462 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001463}
1464
1465
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001466//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001467// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001468//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001469// StdCall calling convention seems to be standard for many Windows' API
1470// routines and around. It differs from C calling convention just a little:
1471// callee should clean up the stack, not caller. Symbols should be also
1472// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001473// For info on fast calling convention see Fast Calling Convention (tail call)
1474// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001475
Dan Gohman98ca4f22009-08-05 01:29:28 +00001476/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001477/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001478static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1479 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001480 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001481
Dan Gohman98ca4f22009-08-05 01:29:28 +00001482 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001483}
1484
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001485/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001486/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001487static bool
1488ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1489 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001490 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001491
Dan Gohman98ca4f22009-08-05 01:29:28 +00001492 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001493}
1494
Dan Gohman095cc292008-09-13 01:54:27 +00001495/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1496/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001497CCAssignFn *X86TargetLowering::CCAssignFnForNode(CallingConv::ID CC) const {
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001498 if (Subtarget->is64Bit()) {
Chris Lattner29689432010-03-11 00:22:57 +00001499 if (CC == CallingConv::GHC)
1500 return CC_X86_64_GHC;
1501 else if (Subtarget->isTargetWin64())
Anton Korobeynikov8f88cb02008-03-22 20:37:30 +00001502 return CC_X86_Win64_C;
Evan Chenge9ac9e62008-09-07 09:07:23 +00001503 else
1504 return CC_X86_64_C;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001505 }
1506
Gordon Henriksen86737662008-01-05 16:56:59 +00001507 if (CC == CallingConv::X86_FastCall)
1508 return CC_X86_32_FastCall;
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001509 else if (CC == CallingConv::X86_ThisCall)
1510 return CC_X86_32_ThisCall;
Evan Chengb188dd92008-09-10 18:25:29 +00001511 else if (CC == CallingConv::Fast)
1512 return CC_X86_32_FastCC;
Chris Lattner29689432010-03-11 00:22:57 +00001513 else if (CC == CallingConv::GHC)
1514 return CC_X86_32_GHC;
Gordon Henriksen86737662008-01-05 16:56:59 +00001515 else
1516 return CC_X86_32_C;
1517}
1518
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001519/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1520/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001521/// the specific parameter attribute. The copy will be passed as a byval
1522/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001523static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001524CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001525 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1526 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001527 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesendd64c412009-02-04 00:33:20 +00001528 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001529 /*isVolatile*/false, /*AlwaysInline=*/true,
1530 NULL, 0, NULL, 0);
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001531}
1532
Chris Lattner29689432010-03-11 00:22:57 +00001533/// IsTailCallConvention - Return true if the calling convention is one that
1534/// supports tail call optimization.
1535static bool IsTailCallConvention(CallingConv::ID CC) {
1536 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1537}
1538
Evan Cheng0c439eb2010-01-27 00:07:07 +00001539/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1540/// a tailcall target by changing its ABI.
1541static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001542 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001543}
1544
Dan Gohman98ca4f22009-08-05 01:29:28 +00001545SDValue
1546X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001547 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001548 const SmallVectorImpl<ISD::InputArg> &Ins,
1549 DebugLoc dl, SelectionDAG &DAG,
1550 const CCValAssign &VA,
1551 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001552 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001553 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001554 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001555 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001556 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001557 EVT ValVT;
1558
1559 // If value is passed by pointer we have address passed instead of the value
1560 // itself.
1561 if (VA.getLocInfo() == CCValAssign::Indirect)
1562 ValVT = VA.getLocVT();
1563 else
1564 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001565
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001566 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001567 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001568 // In case of tail call optimization mark all arguments mutable. Since they
1569 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001570 if (Flags.isByVal()) {
1571 int FI = MFI->CreateFixedObject(Flags.getByValSize(),
Evan Chenged2ae132010-07-03 00:40:23 +00001572 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001573 return DAG.getFrameIndex(FI, getPointerTy());
1574 } else {
1575 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001576 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001577 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1578 return DAG.getLoad(ValVT, dl, Chain, FIN,
David Greene67c9d422010-02-15 16:53:33 +00001579 PseudoSourceValue::getFixedStack(FI), 0,
1580 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001581 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001582}
1583
Dan Gohman475871a2008-07-27 21:46:04 +00001584SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001585X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001586 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001587 bool isVarArg,
1588 const SmallVectorImpl<ISD::InputArg> &Ins,
1589 DebugLoc dl,
1590 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001591 SmallVectorImpl<SDValue> &InVals)
1592 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001593 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001594 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001595
Gordon Henriksen86737662008-01-05 16:56:59 +00001596 const Function* Fn = MF.getFunction();
1597 if (Fn->hasExternalLinkage() &&
1598 Subtarget->isTargetCygMing() &&
1599 Fn->getName() == "main")
1600 FuncInfo->setForceFramePointer(true);
1601
Evan Cheng1bc78042006-04-26 01:20:17 +00001602 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001603 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001604 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001605
Chris Lattner29689432010-03-11 00:22:57 +00001606 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1607 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001608
Chris Lattner638402b2007-02-28 07:00:42 +00001609 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001610 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001611 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1612 ArgLocs, *DAG.getContext());
1613 CCInfo.AnalyzeFormalArguments(Ins, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001614
Chris Lattnerf39f7712007-02-28 05:46:49 +00001615 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001616 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001617 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1618 CCValAssign &VA = ArgLocs[i];
1619 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1620 // places.
1621 assert(VA.getValNo() != LastVal &&
1622 "Don't support value assigned to multiple locs yet");
1623 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001624
Chris Lattnerf39f7712007-02-28 05:46:49 +00001625 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001626 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001627 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001628 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001629 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001630 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001631 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001632 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001633 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001634 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001635 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001636 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1637 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001638 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001639 RC = X86::VR128RegisterClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001640 else if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
1641 RC = X86::VR64RegisterClass;
1642 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001643 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001644
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001645 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001646 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001647
Chris Lattnerf39f7712007-02-28 05:46:49 +00001648 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1649 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1650 // right size.
1651 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001652 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001653 DAG.getValueType(VA.getValVT()));
1654 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001655 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001656 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001657 else if (VA.getLocInfo() == CCValAssign::BCvt)
Anton Korobeynikov6dde14b2009-08-03 08:14:14 +00001658 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001659
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001660 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001661 // Handle MMX values passed in XMM regs.
1662 if (RegVT.isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001663 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1664 ArgValue, DAG.getConstant(0, MVT::i64));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001665 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1666 } else
1667 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001668 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001669 } else {
1670 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001671 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001672 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001673
1674 // If value is passed via pointer - do a load.
1675 if (VA.getLocInfo() == CCValAssign::Indirect)
David Greene67c9d422010-02-15 16:53:33 +00001676 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue, NULL, 0,
1677 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001678
Dan Gohman98ca4f22009-08-05 01:29:28 +00001679 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001680 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001681
Dan Gohman61a92132008-04-21 23:59:07 +00001682 // The x86-64 ABI for returning structs by value requires that we copy
1683 // the sret argument into %rax for the return. Save the argument into
1684 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001685 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001686 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1687 unsigned Reg = FuncInfo->getSRetReturnReg();
1688 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001689 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001690 FuncInfo->setSRetReturnReg(Reg);
1691 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001692 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001693 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001694 }
1695
Chris Lattnerf39f7712007-02-28 05:46:49 +00001696 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001697 // Align stack specially for tail calls.
1698 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001699 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001700
Evan Cheng1bc78042006-04-26 01:20:17 +00001701 // If the function takes variable number of arguments, make a frame index for
1702 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001703 if (isVarArg) {
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001704 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1705 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001706 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001707 }
1708 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001709 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1710
1711 // FIXME: We should really autogenerate these arrays
1712 static const unsigned GPR64ArgRegsWin64[] = {
1713 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001714 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001715 static const unsigned XMMArgRegsWin64[] = {
1716 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1717 };
1718 static const unsigned GPR64ArgRegs64Bit[] = {
1719 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1720 };
1721 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001722 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1723 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1724 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001725 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1726
1727 if (IsWin64) {
1728 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1729 GPR64ArgRegs = GPR64ArgRegsWin64;
1730 XMMArgRegs = XMMArgRegsWin64;
1731 } else {
1732 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1733 GPR64ArgRegs = GPR64ArgRegs64Bit;
1734 XMMArgRegs = XMMArgRegs64Bit;
1735 }
1736 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1737 TotalNumIntRegs);
1738 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1739 TotalNumXMMRegs);
1740
Devang Patel578efa92009-06-05 21:57:13 +00001741 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Evan Chengc7ce29b2009-02-13 22:36:38 +00001742 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001743 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001744 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001745 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001746 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00001747 // Kernel mode asks for SSE to be disabled, so don't push them
1748 // on the stack.
1749 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001750
Gordon Henriksen86737662008-01-05 16:56:59 +00001751 // For X86-64, if there are vararg parameters that are passed via
1752 // registers, then we must store them to their spots on the stack so they
1753 // may be loaded by deferencing the result of va_next.
Dan Gohman1e93df62010-04-17 14:41:14 +00001754 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1755 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1756 FuncInfo->setRegSaveFrameIndex(
1757 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
1758 false));
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001759
Gordon Henriksen86737662008-01-05 16:56:59 +00001760 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001761 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001762 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1763 getPointerTy());
1764 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001765 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001766 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1767 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001768 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
1769 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001770 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001771 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001772 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Dan Gohman1e93df62010-04-17 14:41:14 +00001773 PseudoSourceValue::getFixedStack(
1774 FuncInfo->getRegSaveFrameIndex()),
David Greene67c9d422010-02-15 16:53:33 +00001775 Offset, false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001776 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001777 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001778 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001779
Dan Gohmanface41a2009-08-16 21:24:25 +00001780 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1781 // Now store the XMM (fp + vector) parameter registers.
1782 SmallVector<SDValue, 11> SaveXMMOps;
1783 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001784
Dan Gohmanface41a2009-08-16 21:24:25 +00001785 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
1786 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1787 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001788
Dan Gohman1e93df62010-04-17 14:41:14 +00001789 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1790 FuncInfo->getRegSaveFrameIndex()));
1791 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1792 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001793
Dan Gohmanface41a2009-08-16 21:24:25 +00001794 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
1795 unsigned VReg = MF.addLiveIn(XMMArgRegs[NumXMMRegs],
1796 X86::VR128RegisterClass);
1797 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1798 SaveXMMOps.push_back(Val);
1799 }
1800 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1801 MVT::Other,
1802 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001803 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001804
1805 if (!MemOps.empty())
1806 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1807 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001808 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001809 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001810
Gordon Henriksen86737662008-01-05 16:56:59 +00001811 // Some CCs need callee pop.
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001812 if (Subtarget->IsCalleePop(isVarArg, CallConv)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001813 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001814 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001815 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001816 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001817 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001818 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001819 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001820
Gordon Henriksen86737662008-01-05 16:56:59 +00001821 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001822 // RegSaveFrameIndex is X86-64 only.
1823 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001824 if (CallConv == CallingConv::X86_FastCall ||
1825 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001826 // fastcc functions can't have varargs.
1827 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001828 }
Evan Cheng25caf632006-05-23 21:06:34 +00001829
Dan Gohman98ca4f22009-08-05 01:29:28 +00001830 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001831}
1832
Dan Gohman475871a2008-07-27 21:46:04 +00001833SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001834X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1835 SDValue StackPtr, SDValue Arg,
1836 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001837 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001838 ISD::ArgFlagsTy Flags) const {
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +00001839 const unsigned FirstStackArgOffset = (Subtarget->isTargetWin64() ? 32 : 0);
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +00001840 unsigned LocMemOffset = FirstStackArgOffset + VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001841 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001842 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001843 if (Flags.isByVal()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001844 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Evan Chengdffbd832008-01-10 00:09:10 +00001845 }
Dale Johannesenace16102009-02-03 19:33:06 +00001846 return DAG.getStore(Chain, dl, Arg, PtrOff,
David Greene67c9d422010-02-15 16:53:33 +00001847 PseudoSourceValue::getStack(), LocMemOffset,
1848 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001849}
1850
Bill Wendling64e87322009-01-16 19:25:27 +00001851/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001852/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001853SDValue
1854X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001855 SDValue &OutRetAddr, SDValue Chain,
1856 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001857 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001858 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001859 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001860 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001861
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001862 // Load the "old" Return address.
David Greene67c9d422010-02-15 16:53:33 +00001863 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, NULL, 0, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001864 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001865}
1866
1867/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1868/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001869static SDValue
1870EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001871 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001872 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001873 // Store the return address to the appropriate stack slot.
1874 if (!FPDiff) return Chain;
1875 // Calculate the new stack slot for the return address.
1876 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001877 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00001878 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001879 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001880 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001881 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
David Greene67c9d422010-02-15 16:53:33 +00001882 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0,
1883 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001884 return Chain;
1885}
1886
Dan Gohman98ca4f22009-08-05 01:29:28 +00001887SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001888X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001889 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001890 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001891 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001892 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001893 const SmallVectorImpl<ISD::InputArg> &Ins,
1894 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001895 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001896 MachineFunction &MF = DAG.getMachineFunction();
1897 bool Is64Bit = Subtarget->is64Bit();
1898 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00001899 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001900
Evan Cheng5f941932010-02-05 02:21:12 +00001901 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001902 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00001903 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1904 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001905 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00001906
1907 // Sibcalls are automatically detected tailcalls which do not require
1908 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00001909 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00001910 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00001911
1912 if (isTailCall)
1913 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00001914 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00001915
Chris Lattner29689432010-03-11 00:22:57 +00001916 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1917 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001918
Chris Lattner638402b2007-02-28 07:00:42 +00001919 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001920 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001921 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1922 ArgLocs, *DAG.getContext());
1923 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001924
Chris Lattner423c5f42007-02-28 05:31:48 +00001925 // Get a count of how many bytes are to be pushed on the stack.
1926 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00001927 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00001928 // This is a sibcall. The memory operands are available in caller's
1929 // own caller's stack.
1930 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00001931 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00001932 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001933
Gordon Henriksen86737662008-01-05 16:56:59 +00001934 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00001935 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001936 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00001937 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00001938 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1939 FPDiff = NumBytesCallerPushed - NumBytes;
1940
1941 // Set the delta of movement of the returnaddr stackslot.
1942 // But only set if delta is greater than previous delta.
1943 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1944 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1945 }
1946
Evan Chengf22f9b32010-02-06 03:28:46 +00001947 if (!IsSibcall)
1948 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001949
Dan Gohman475871a2008-07-27 21:46:04 +00001950 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001951 // Load return adress for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00001952 if (isTailCall && FPDiff)
1953 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
1954 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001955
Dan Gohman475871a2008-07-27 21:46:04 +00001956 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1957 SmallVector<SDValue, 8> MemOpChains;
1958 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001959
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001960 // Walk the register/memloc assignments, inserting copies/loads. In the case
1961 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00001962 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1963 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001964 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001965 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001966 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00001967 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00001968
Chris Lattner423c5f42007-02-28 05:31:48 +00001969 // Promote the value if needed.
1970 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001971 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00001972 case CCValAssign::Full: break;
1973 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001974 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001975 break;
1976 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001977 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001978 break;
1979 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001980 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
1981 // Special case: passing MMX values in XMM registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001982 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
1983 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
1984 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001985 } else
1986 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
1987 break;
1988 case CCValAssign::BCvt:
1989 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001990 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001991 case CCValAssign::Indirect: {
1992 // Store the argument.
1993 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00001994 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001995 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
David Greene67c9d422010-02-15 16:53:33 +00001996 PseudoSourceValue::getFixedStack(FI), 0,
1997 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001998 Arg = SpillSlot;
1999 break;
2000 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002001 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002002
Chris Lattner423c5f42007-02-28 05:31:48 +00002003 if (VA.isRegLoc()) {
2004 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Evan Chengf22f9b32010-02-06 03:28:46 +00002005 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002006 assert(VA.isMemLoc());
2007 if (StackPtr.getNode() == 0)
2008 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2009 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2010 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002011 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002012 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002013
Evan Cheng32fe1032006-05-25 00:59:30 +00002014 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002015 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002016 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002017
Evan Cheng347d5f72006-04-28 21:29:37 +00002018 // Build a sequence of copy-to-reg nodes chained together with token chain
2019 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002020 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002021 // Tail call byval lowering might overwrite argument registers so in case of
2022 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002023 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002024 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002025 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002026 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002027 InFlag = Chain.getValue(1);
2028 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002029
Chris Lattner88e1fd52009-07-09 04:24:46 +00002030 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002031 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2032 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002033 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002034 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2035 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002036 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002037 InFlag);
2038 InFlag = Chain.getValue(1);
2039 } else {
2040 // If we are tail calling and generating PIC/GOT style code load the
2041 // address of the callee into ECX. The value in ecx is used as target of
2042 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2043 // for tail calls on PIC/GOT architectures. Normally we would just put the
2044 // address of GOT into ebx and then call target@PLT. But for tail calls
2045 // ebx would be restored (since ebx is callee saved) before jumping to the
2046 // target@PLT.
2047
2048 // Note: The actual moving to ECX is done further down.
2049 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2050 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2051 !G->getGlobal()->hasProtectedVisibility())
2052 Callee = LowerGlobalAddress(Callee, DAG);
2053 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002054 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002055 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002056 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002057
Nate Begemanc8ea6732010-07-21 20:49:52 +00002058 if (Is64Bit && isVarArg && !Subtarget->isTargetWin64()) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002059 // From AMD64 ABI document:
2060 // For calls that may call functions that use varargs or stdargs
2061 // (prototype-less calls or calls to functions containing ellipsis (...) in
2062 // the declaration) %al is used as hidden argument to specify the number
2063 // of SSE registers used. The contents of %al do not need to match exactly
2064 // the number of registers, but must be an ubound on the number of SSE
2065 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002066
Gordon Henriksen86737662008-01-05 16:56:59 +00002067 // Count the number of XMM registers allocated.
2068 static const unsigned XMMArgRegs[] = {
2069 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2070 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2071 };
2072 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Scott Michelfdc40a02009-02-17 22:15:04 +00002073 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002074 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002075
Dale Johannesendd64c412009-02-04 00:33:20 +00002076 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002077 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002078 InFlag = Chain.getValue(1);
2079 }
2080
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002081
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002082 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002083 if (isTailCall) {
2084 // Force all the incoming stack arguments to be loaded from the stack
2085 // before any new outgoing arguments are stored to the stack, because the
2086 // outgoing stack slots may alias the incoming argument stack slots, and
2087 // the alias isn't otherwise explicit. This is slightly more conservative
2088 // than necessary, because it means that each store effectively depends
2089 // on every argument instead of just those arguments it would clobber.
2090 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2091
Dan Gohman475871a2008-07-27 21:46:04 +00002092 SmallVector<SDValue, 8> MemOpChains2;
2093 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002094 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002095 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002096 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002097 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002098 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2099 CCValAssign &VA = ArgLocs[i];
2100 if (VA.isRegLoc())
2101 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002102 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002103 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002104 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002105 // Create frame index.
2106 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002107 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002108 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002109 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002110
Duncan Sands276dcbd2008-03-21 09:14:45 +00002111 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002112 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002113 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002114 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002115 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002116 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002117 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002118
Dan Gohman98ca4f22009-08-05 01:29:28 +00002119 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2120 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002121 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002122 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002123 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002124 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002125 DAG.getStore(ArgChain, dl, Arg, FIN,
David Greene67c9d422010-02-15 16:53:33 +00002126 PseudoSourceValue::getFixedStack(FI), 0,
2127 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002128 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002129 }
2130 }
2131
2132 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002133 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002134 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002135
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002136 // Copy arguments to their registers.
2137 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002138 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002139 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002140 InFlag = Chain.getValue(1);
2141 }
Dan Gohman475871a2008-07-27 21:46:04 +00002142 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002143
Gordon Henriksen86737662008-01-05 16:56:59 +00002144 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002145 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002146 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002147 }
2148
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002149 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2150 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2151 // In the 64-bit large code model, we have to make all calls
2152 // through a register, since the call instruction's 32-bit
2153 // pc-relative offset may not be large enough to hold the whole
2154 // address.
2155 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002156 // If the callee is a GlobalAddress node (quite common, every direct call
2157 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2158 // it.
2159
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002160 // We should use extra load for direct calls to dllimported functions in
2161 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002162 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002163 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002164 unsigned char OpFlags = 0;
Eric Christopherfd179292009-08-27 18:07:15 +00002165
Chris Lattner48a7d022009-07-09 05:02:21 +00002166 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2167 // external symbols most go through the PLT in PIC mode. If the symbol
2168 // has hidden or protected visibility, or if it is static or local, then
2169 // we don't need to use the PLT - we can directly call it.
2170 if (Subtarget->isTargetELF() &&
2171 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002172 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002173 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002174 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002175 (GV->isDeclaration() || GV->isWeakForLinker()) &&
2176 Subtarget->getDarwinVers() < 9) {
2177 // PC-relative references to external symbols should go through $stub,
2178 // unless we're building with the leopard linker or later, which
2179 // automatically synthesizes these stubs.
2180 OpFlags = X86II::MO_DARWIN_STUB;
2181 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002182
Devang Patel0d881da2010-07-06 22:08:15 +00002183 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002184 G->getOffset(), OpFlags);
2185 }
Bill Wendling056292f2008-09-16 21:48:12 +00002186 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002187 unsigned char OpFlags = 0;
2188
2189 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to external
2190 // symbols should go through the PLT.
2191 if (Subtarget->isTargetELF() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002192 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002193 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002194 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002195 Subtarget->getDarwinVers() < 9) {
2196 // PC-relative references to external symbols should go through $stub,
2197 // unless we're building with the leopard linker or later, which
2198 // automatically synthesizes these stubs.
2199 OpFlags = X86II::MO_DARWIN_STUB;
2200 }
Eric Christopherfd179292009-08-27 18:07:15 +00002201
Chris Lattner48a7d022009-07-09 05:02:21 +00002202 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2203 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002204 }
2205
Chris Lattnerd96d0722007-02-25 06:40:16 +00002206 // Returns a chain & a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +00002207 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00002208 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002209
Evan Chengf22f9b32010-02-06 03:28:46 +00002210 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002211 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2212 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002213 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002214 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002215
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002216 Ops.push_back(Chain);
2217 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002218
Dan Gohman98ca4f22009-08-05 01:29:28 +00002219 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002220 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002221
Gordon Henriksen86737662008-01-05 16:56:59 +00002222 // Add argument registers to the end of the list so that they are known live
2223 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002224 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2225 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2226 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002227
Evan Cheng586ccac2008-03-18 23:36:35 +00002228 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002229 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002230 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2231
2232 // Add an implicit use of AL for x86 vararg functions.
2233 if (Is64Bit && isVarArg)
Owen Anderson825b72b2009-08-11 20:47:22 +00002234 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002235
Gabor Greifba36cb52008-08-28 21:40:38 +00002236 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002237 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002238
Dan Gohman98ca4f22009-08-05 01:29:28 +00002239 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002240 // We used to do:
2241 //// If this is the first return lowered for this function, add the regs
2242 //// to the liveout set for the function.
2243 // This isn't right, although it's probably harmless on x86; liveouts
2244 // should be computed from returns not tail calls. Consider a void
2245 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002246 return DAG.getNode(X86ISD::TC_RETURN, dl,
2247 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002248 }
2249
Dale Johannesenace16102009-02-03 19:33:06 +00002250 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002251 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002252
Chris Lattner2d297092006-05-23 18:50:38 +00002253 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002254 unsigned NumBytesForCalleeToPush;
Dan Gohman4d3d6e12010-05-27 18:43:40 +00002255 if (Subtarget->IsCalleePop(isVarArg, CallConv))
Gordon Henriksen86737662008-01-05 16:56:59 +00002256 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002257 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002258 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002259 // pops the hidden struct pointer, so we have to push it back.
2260 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002261 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002262 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002263 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002264
Gordon Henriksenae636f82008-01-03 16:47:34 +00002265 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002266 if (!IsSibcall) {
2267 Chain = DAG.getCALLSEQ_END(Chain,
2268 DAG.getIntPtrConstant(NumBytes, true),
2269 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2270 true),
2271 InFlag);
2272 InFlag = Chain.getValue(1);
2273 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002274
Chris Lattner3085e152007-02-25 08:59:22 +00002275 // Handle result values, copying them out of physregs into vregs that we
2276 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002277 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2278 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002279}
2280
Evan Cheng25ab6902006-09-08 06:48:29 +00002281
2282//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002283// Fast Calling Convention (tail call) implementation
2284//===----------------------------------------------------------------------===//
2285
2286// Like std call, callee cleans arguments, convention except that ECX is
2287// reserved for storing the tail called function address. Only 2 registers are
2288// free for argument passing (inreg). Tail call optimization is performed
2289// provided:
2290// * tailcallopt is enabled
2291// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002292// On X86_64 architecture with GOT-style position independent code only local
2293// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002294// To keep the stack aligned according to platform abi the function
2295// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2296// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002297// If a tail called function callee has more arguments than the caller the
2298// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002299// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002300// original REtADDR, but before the saved framepointer or the spilled registers
2301// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2302// stack layout:
2303// arg1
2304// arg2
2305// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002306// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002307// move area ]
2308// (possible EBP)
2309// ESI
2310// EDI
2311// local1 ..
2312
2313/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2314/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002315unsigned
2316X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2317 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002318 MachineFunction &MF = DAG.getMachineFunction();
2319 const TargetMachine &TM = MF.getTarget();
2320 const TargetFrameInfo &TFI = *TM.getFrameInfo();
2321 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002322 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002323 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002324 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002325 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2326 // Number smaller than 12 so just add the difference.
2327 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2328 } else {
2329 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002330 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002331 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002332 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002333 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002334}
2335
Evan Cheng5f941932010-02-05 02:21:12 +00002336/// MatchingStackOffset - Return true if the given stack call argument is
2337/// already available in the same position (relatively) of the caller's
2338/// incoming argument stack.
2339static
2340bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2341 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2342 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002343 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2344 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002345 if (Arg.getOpcode() == ISD::CopyFromReg) {
2346 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
2347 if (!VR || TargetRegisterInfo::isPhysicalRegister(VR))
2348 return false;
2349 MachineInstr *Def = MRI->getVRegDef(VR);
2350 if (!Def)
2351 return false;
2352 if (!Flags.isByVal()) {
2353 if (!TII->isLoadFromStackSlot(Def, FI))
2354 return false;
2355 } else {
2356 unsigned Opcode = Def->getOpcode();
2357 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2358 Def->getOperand(1).isFI()) {
2359 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002360 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002361 } else
2362 return false;
2363 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002364 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2365 if (Flags.isByVal())
2366 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002367 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002368 // define @foo(%struct.X* %A) {
2369 // tail call @bar(%struct.X* byval %A)
2370 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002371 return false;
2372 SDValue Ptr = Ld->getBasePtr();
2373 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2374 if (!FINode)
2375 return false;
2376 FI = FINode->getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002377 } else
2378 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002379
Evan Cheng4cae1332010-03-05 08:38:04 +00002380 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002381 if (!MFI->isFixedObjectIndex(FI))
2382 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002383 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002384}
2385
Dan Gohman98ca4f22009-08-05 01:29:28 +00002386/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2387/// for tail call optimization. Targets which want to do tail call
2388/// optimization should implement this function.
2389bool
2390X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002391 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002392 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002393 bool isCalleeStructRet,
2394 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002395 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002396 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002397 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002398 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002399 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002400 CalleeCC != CallingConv::C)
2401 return false;
2402
Evan Cheng7096ae42010-01-29 06:45:59 +00002403 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002404 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002405 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002406 CallingConv::ID CallerCC = CallerF->getCallingConv();
2407 bool CCMatch = CallerCC == CalleeCC;
2408
Dan Gohman1797ed52010-02-08 20:27:50 +00002409 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002410 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002411 return true;
2412 return false;
2413 }
2414
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002415 // Look for obvious safe cases to perform tail call optimization that do not
2416 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002417
Evan Cheng2c12cb42010-03-26 16:26:03 +00002418 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2419 // emit a special epilogue.
2420 if (RegInfo->needsStackRealignment(MF))
2421 return false;
2422
Eric Christopher90eb4022010-07-22 00:26:08 +00002423 // Do not sibcall optimize vararg calls unless the call site is not passing
2424 // any arguments.
Evan Cheng3c262ee2010-03-26 02:13:13 +00002425 if (isVarArg && !Outs.empty())
Evan Cheng843bd692010-01-31 06:44:49 +00002426 return false;
2427
Evan Chenga375d472010-03-15 18:54:48 +00002428 // Also avoid sibcall optimization if either caller or callee uses struct
2429 // return semantics.
2430 if (isCalleeStructRet || isCallerStructRet)
2431 return false;
2432
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002433 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2434 // Therefore if it's not used by the call it is not safe to optimize this into
2435 // a sibcall.
2436 bool Unused = false;
2437 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2438 if (!Ins[i].Used) {
2439 Unused = true;
2440 break;
2441 }
2442 }
2443 if (Unused) {
2444 SmallVector<CCValAssign, 16> RVLocs;
2445 CCState CCInfo(CalleeCC, false, getTargetMachine(),
2446 RVLocs, *DAG.getContext());
2447 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002448 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002449 CCValAssign &VA = RVLocs[i];
2450 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2451 return false;
2452 }
2453 }
2454
Evan Cheng13617962010-04-30 01:12:32 +00002455 // If the calling conventions do not match, then we'd better make sure the
2456 // results are returned in the same way as what the caller expects.
2457 if (!CCMatch) {
2458 SmallVector<CCValAssign, 16> RVLocs1;
2459 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
2460 RVLocs1, *DAG.getContext());
2461 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2462
2463 SmallVector<CCValAssign, 16> RVLocs2;
2464 CCState CCInfo2(CallerCC, false, getTargetMachine(),
2465 RVLocs2, *DAG.getContext());
2466 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2467
2468 if (RVLocs1.size() != RVLocs2.size())
2469 return false;
2470 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2471 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2472 return false;
2473 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2474 return false;
2475 if (RVLocs1[i].isRegLoc()) {
2476 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2477 return false;
2478 } else {
2479 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2480 return false;
2481 }
2482 }
2483 }
2484
Evan Chenga6bff982010-01-30 01:22:00 +00002485 // If the callee takes no arguments then go on to check the results of the
2486 // call.
2487 if (!Outs.empty()) {
2488 // Check if stack adjustment is needed. For now, do not do this if any
2489 // argument is passed on the stack.
2490 SmallVector<CCValAssign, 16> ArgLocs;
2491 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
2492 ArgLocs, *DAG.getContext());
2493 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CalleeCC));
Evan Chengb2c92902010-02-02 02:22:50 +00002494 if (CCInfo.getNextStackOffset()) {
2495 MachineFunction &MF = DAG.getMachineFunction();
2496 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2497 return false;
2498 if (Subtarget->isTargetWin64())
2499 // Win64 ABI has additional complications.
2500 return false;
2501
2502 // Check if the arguments are already laid out in the right way as
2503 // the caller's fixed stack objects.
2504 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002505 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2506 const X86InstrInfo *TII =
2507 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002508 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2509 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002510 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002511 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002512 if (VA.getLocInfo() == CCValAssign::Indirect)
2513 return false;
2514 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002515 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2516 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002517 return false;
2518 }
2519 }
2520 }
Evan Cheng9c044672010-05-29 01:35:22 +00002521
2522 // If the tailcall address may be in a register, then make sure it's
2523 // possible to register allocate for it. In 32-bit, the call address can
2524 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002525 // callee-saved registers are restored. These happen to be the same
2526 // registers used to pass 'inreg' arguments so watch out for those.
2527 if (!Subtarget->is64Bit() &&
2528 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002529 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002530 unsigned NumInRegs = 0;
2531 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2532 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002533 if (!VA.isRegLoc())
2534 continue;
2535 unsigned Reg = VA.getLocReg();
2536 switch (Reg) {
2537 default: break;
2538 case X86::EAX: case X86::EDX: case X86::ECX:
2539 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002540 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002541 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002542 }
2543 }
2544 }
Evan Chenga6bff982010-01-30 01:22:00 +00002545 }
Evan Chengb1712452010-01-27 06:25:16 +00002546
Evan Cheng86809cc2010-02-03 03:28:02 +00002547 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002548}
2549
Dan Gohman3df24e62008-09-03 23:12:08 +00002550FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002551X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2552 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002553}
2554
2555
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002556//===----------------------------------------------------------------------===//
2557// Other Lowering Hooks
2558//===----------------------------------------------------------------------===//
2559
2560
Dan Gohmand858e902010-04-17 15:26:15 +00002561SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002562 MachineFunction &MF = DAG.getMachineFunction();
2563 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2564 int ReturnAddrIndex = FuncInfo->getRAIndex();
2565
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002566 if (ReturnAddrIndex == 0) {
2567 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002568 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002569 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002570 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002571 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002572 }
2573
Evan Cheng25ab6902006-09-08 06:48:29 +00002574 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002575}
2576
2577
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002578bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2579 bool hasSymbolicDisplacement) {
2580 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002581 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002582 return false;
2583
2584 // If we don't have a symbolic displacement - we don't have any extra
2585 // restrictions.
2586 if (!hasSymbolicDisplacement)
2587 return true;
2588
2589 // FIXME: Some tweaks might be needed for medium code model.
2590 if (M != CodeModel::Small && M != CodeModel::Kernel)
2591 return false;
2592
2593 // For small code model we assume that latest object is 16MB before end of 31
2594 // bits boundary. We may also accept pretty large negative constants knowing
2595 // that all objects are in the positive half of address space.
2596 if (M == CodeModel::Small && Offset < 16*1024*1024)
2597 return true;
2598
2599 // For kernel code model we know that all object resist in the negative half
2600 // of 32bits address space. We may not accept negative offsets, since they may
2601 // be just off and we may accept pretty large positive ones.
2602 if (M == CodeModel::Kernel && Offset > 0)
2603 return true;
2604
2605 return false;
2606}
2607
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002608/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2609/// specific condition code, returning the condition code and the LHS/RHS of the
2610/// comparison to make.
2611static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2612 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002613 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002614 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2615 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2616 // X > -1 -> X == 0, jump !sign.
2617 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002618 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002619 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2620 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002621 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002622 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002623 // X < 1 -> X <= 0
2624 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002625 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002626 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002627 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002628
Evan Chengd9558e02006-01-06 00:43:03 +00002629 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002630 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002631 case ISD::SETEQ: return X86::COND_E;
2632 case ISD::SETGT: return X86::COND_G;
2633 case ISD::SETGE: return X86::COND_GE;
2634 case ISD::SETLT: return X86::COND_L;
2635 case ISD::SETLE: return X86::COND_LE;
2636 case ISD::SETNE: return X86::COND_NE;
2637 case ISD::SETULT: return X86::COND_B;
2638 case ISD::SETUGT: return X86::COND_A;
2639 case ISD::SETULE: return X86::COND_BE;
2640 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002641 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002642 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002643
Chris Lattner4c78e022008-12-23 23:42:27 +00002644 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002645
Chris Lattner4c78e022008-12-23 23:42:27 +00002646 // If LHS is a foldable load, but RHS is not, flip the condition.
2647 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2648 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2649 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2650 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002651 }
2652
Chris Lattner4c78e022008-12-23 23:42:27 +00002653 switch (SetCCOpcode) {
2654 default: break;
2655 case ISD::SETOLT:
2656 case ISD::SETOLE:
2657 case ISD::SETUGT:
2658 case ISD::SETUGE:
2659 std::swap(LHS, RHS);
2660 break;
2661 }
2662
2663 // On a floating point condition, the flags are set as follows:
2664 // ZF PF CF op
2665 // 0 | 0 | 0 | X > Y
2666 // 0 | 0 | 1 | X < Y
2667 // 1 | 0 | 0 | X == Y
2668 // 1 | 1 | 1 | unordered
2669 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002670 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002671 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002672 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002673 case ISD::SETOLT: // flipped
2674 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002675 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002676 case ISD::SETOLE: // flipped
2677 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002678 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002679 case ISD::SETUGT: // flipped
2680 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002681 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002682 case ISD::SETUGE: // flipped
2683 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002684 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002685 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002686 case ISD::SETNE: return X86::COND_NE;
2687 case ISD::SETUO: return X86::COND_P;
2688 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002689 case ISD::SETOEQ:
2690 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002691 }
Evan Chengd9558e02006-01-06 00:43:03 +00002692}
2693
Evan Cheng4a460802006-01-11 00:33:36 +00002694/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2695/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002696/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002697static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002698 switch (X86CC) {
2699 default:
2700 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002701 case X86::COND_B:
2702 case X86::COND_BE:
2703 case X86::COND_E:
2704 case X86::COND_P:
2705 case X86::COND_A:
2706 case X86::COND_AE:
2707 case X86::COND_NE:
2708 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002709 return true;
2710 }
2711}
2712
Evan Chengeb2f9692009-10-27 19:56:55 +00002713/// isFPImmLegal - Returns true if the target can instruction select the
2714/// specified FP immediate natively. If false, the legalizer will
2715/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002716bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00002717 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
2718 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
2719 return true;
2720 }
2721 return false;
2722}
2723
Nate Begeman9008ca62009-04-27 18:41:29 +00002724/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2725/// the specified range (L, H].
2726static bool isUndefOrInRange(int Val, int Low, int Hi) {
2727 return (Val < 0) || (Val >= Low && Val < Hi);
2728}
2729
2730/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2731/// specified value.
2732static bool isUndefOrEqual(int Val, int CmpVal) {
2733 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002734 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002735 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002736}
2737
Nate Begeman9008ca62009-04-27 18:41:29 +00002738/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2739/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2740/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00002741static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002742 if (VT == MVT::v4f32 || VT == MVT::v4i32 || VT == MVT::v4i16)
Nate Begeman9008ca62009-04-27 18:41:29 +00002743 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002744 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00002745 return (Mask[0] < 2 && Mask[1] < 2);
2746 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002747}
2748
Nate Begeman9008ca62009-04-27 18:41:29 +00002749bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002750 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002751 N->getMask(M);
2752 return ::isPSHUFDMask(M, N->getValueType(0));
2753}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002754
Nate Begeman9008ca62009-04-27 18:41:29 +00002755/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2756/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00002757static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002758 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002759 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002760
Nate Begeman9008ca62009-04-27 18:41:29 +00002761 // Lower quadword copied in order or undef.
2762 for (int i = 0; i != 4; ++i)
2763 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002764 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002765
Evan Cheng506d3df2006-03-29 23:07:14 +00002766 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002767 for (int i = 4; i != 8; ++i)
2768 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002769 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002770
Evan Cheng506d3df2006-03-29 23:07:14 +00002771 return true;
2772}
2773
Nate Begeman9008ca62009-04-27 18:41:29 +00002774bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002775 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002776 N->getMask(M);
2777 return ::isPSHUFHWMask(M, N->getValueType(0));
2778}
Evan Cheng506d3df2006-03-29 23:07:14 +00002779
Nate Begeman9008ca62009-04-27 18:41:29 +00002780/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2781/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00002782static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002783 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002784 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002785
Rafael Espindola15684b22009-04-24 12:40:33 +00002786 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00002787 for (int i = 4; i != 8; ++i)
2788 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00002789 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002790
Rafael Espindola15684b22009-04-24 12:40:33 +00002791 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002792 for (int i = 0; i != 4; ++i)
2793 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00002794 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002795
Rafael Espindola15684b22009-04-24 12:40:33 +00002796 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002797}
2798
Nate Begeman9008ca62009-04-27 18:41:29 +00002799bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002800 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002801 N->getMask(M);
2802 return ::isPSHUFLWMask(M, N->getValueType(0));
2803}
2804
Nate Begemana09008b2009-10-19 02:17:23 +00002805/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
2806/// is suitable for input to PALIGNR.
2807static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
2808 bool hasSSSE3) {
2809 int i, e = VT.getVectorNumElements();
2810
2811 // Do not handle v2i64 / v2f64 shuffles with palignr.
2812 if (e < 4 || !hasSSSE3)
2813 return false;
2814
2815 for (i = 0; i != e; ++i)
2816 if (Mask[i] >= 0)
2817 break;
2818
2819 // All undef, not a palignr.
2820 if (i == e)
2821 return false;
2822
2823 // Determine if it's ok to perform a palignr with only the LHS, since we
2824 // don't have access to the actual shuffle elements to see if RHS is undef.
2825 bool Unary = Mask[i] < (int)e;
2826 bool NeedsUnary = false;
2827
2828 int s = Mask[i] - i;
2829
2830 // Check the rest of the elements to see if they are consecutive.
2831 for (++i; i != e; ++i) {
2832 int m = Mask[i];
2833 if (m < 0)
2834 continue;
2835
2836 Unary = Unary && (m < (int)e);
2837 NeedsUnary = NeedsUnary || (m < s);
2838
2839 if (NeedsUnary && !Unary)
2840 return false;
2841 if (Unary && m != ((s+i) & (e-1)))
2842 return false;
2843 if (!Unary && m != (s+i))
2844 return false;
2845 }
2846 return true;
2847}
2848
2849bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
2850 SmallVector<int, 8> M;
2851 N->getMask(M);
2852 return ::isPALIGNRMask(M, N->getValueType(0), true);
2853}
2854
Evan Cheng14aed5e2006-03-24 01:18:28 +00002855/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2856/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00002857static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002858 int NumElems = VT.getVectorNumElements();
2859 if (NumElems != 2 && NumElems != 4)
2860 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002861
Nate Begeman9008ca62009-04-27 18:41:29 +00002862 int Half = NumElems / 2;
2863 for (int i = 0; i < Half; ++i)
2864 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002865 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002866 for (int i = Half; i < NumElems; ++i)
2867 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002868 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002869
Evan Cheng14aed5e2006-03-24 01:18:28 +00002870 return true;
2871}
2872
Nate Begeman9008ca62009-04-27 18:41:29 +00002873bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
2874 SmallVector<int, 8> M;
2875 N->getMask(M);
2876 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002877}
2878
Evan Cheng213d2cf2007-05-17 18:45:50 +00002879/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002880/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2881/// half elements to come from vector 1 (which would equal the dest.) and
2882/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00002883static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002884 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002885
2886 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00002887 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002888
Nate Begeman9008ca62009-04-27 18:41:29 +00002889 int Half = NumElems / 2;
2890 for (int i = 0; i < Half; ++i)
2891 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002892 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002893 for (int i = Half; i < NumElems; ++i)
2894 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002895 return false;
2896 return true;
2897}
2898
Nate Begeman9008ca62009-04-27 18:41:29 +00002899static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
2900 SmallVector<int, 8> M;
2901 N->getMask(M);
2902 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002903}
2904
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002905/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2906/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002907bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
2908 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002909 return false;
2910
Evan Cheng2064a2b2006-03-28 06:50:32 +00002911 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00002912 return isUndefOrEqual(N->getMaskElt(0), 6) &&
2913 isUndefOrEqual(N->getMaskElt(1), 7) &&
2914 isUndefOrEqual(N->getMaskElt(2), 2) &&
2915 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002916}
2917
Nate Begeman0b10b912009-11-07 23:17:15 +00002918/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2919/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2920/// <2, 3, 2, 3>
2921bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
2922 unsigned NumElems = N->getValueType(0).getVectorNumElements();
2923
2924 if (NumElems != 4)
2925 return false;
2926
2927 return isUndefOrEqual(N->getMaskElt(0), 2) &&
2928 isUndefOrEqual(N->getMaskElt(1), 3) &&
2929 isUndefOrEqual(N->getMaskElt(2), 2) &&
2930 isUndefOrEqual(N->getMaskElt(3), 3);
2931}
2932
Evan Cheng5ced1d82006-04-06 23:23:56 +00002933/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2934/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00002935bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
2936 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002937
Evan Cheng5ced1d82006-04-06 23:23:56 +00002938 if (NumElems != 2 && NumElems != 4)
2939 return false;
2940
Evan Chengc5cdff22006-04-07 21:53:05 +00002941 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002942 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002943 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002944
Evan Chengc5cdff22006-04-07 21:53:05 +00002945 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002946 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002947 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002948
2949 return true;
2950}
2951
Nate Begeman0b10b912009-11-07 23:17:15 +00002952/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
2953/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
2954bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002955 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002956
Evan Cheng5ced1d82006-04-06 23:23:56 +00002957 if (NumElems != 2 && NumElems != 4)
2958 return false;
2959
Evan Chengc5cdff22006-04-07 21:53:05 +00002960 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002961 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002962 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002963
Nate Begeman9008ca62009-04-27 18:41:29 +00002964 for (unsigned i = 0; i < NumElems/2; ++i)
2965 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002966 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002967
2968 return true;
2969}
2970
Evan Cheng0038e592006-03-28 00:39:58 +00002971/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2972/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00002973static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002974 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002975 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002976 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00002977 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002978
Nate Begeman9008ca62009-04-27 18:41:29 +00002979 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2980 int BitI = Mask[i];
2981 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002982 if (!isUndefOrEqual(BitI, j))
2983 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002984 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002985 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002986 return false;
2987 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002988 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002989 return false;
2990 }
Evan Cheng0038e592006-03-28 00:39:58 +00002991 }
Evan Cheng0038e592006-03-28 00:39:58 +00002992 return true;
2993}
2994
Nate Begeman9008ca62009-04-27 18:41:29 +00002995bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2996 SmallVector<int, 8> M;
2997 N->getMask(M);
2998 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002999}
3000
Evan Cheng4fcb9222006-03-28 02:43:26 +00003001/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3002/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003003static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003004 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003005 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003006 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003007 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003008
Nate Begeman9008ca62009-04-27 18:41:29 +00003009 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3010 int BitI = Mask[i];
3011 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00003012 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00003013 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003014 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00003015 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003016 return false;
3017 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003018 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003019 return false;
3020 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003021 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003022 return true;
3023}
3024
Nate Begeman9008ca62009-04-27 18:41:29 +00003025bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3026 SmallVector<int, 8> M;
3027 N->getMask(M);
3028 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003029}
3030
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003031/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3032/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3033/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003034static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003035 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003036 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003037 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003038
Nate Begeman9008ca62009-04-27 18:41:29 +00003039 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
3040 int BitI = Mask[i];
3041 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00003042 if (!isUndefOrEqual(BitI, j))
3043 return false;
3044 if (!isUndefOrEqual(BitI1, j))
3045 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003046 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003047 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003048}
3049
Nate Begeman9008ca62009-04-27 18:41:29 +00003050bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3051 SmallVector<int, 8> M;
3052 N->getMask(M);
3053 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3054}
3055
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003056/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3057/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3058/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003059static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003060 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003061 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3062 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003063
Nate Begeman9008ca62009-04-27 18:41:29 +00003064 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3065 int BitI = Mask[i];
3066 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003067 if (!isUndefOrEqual(BitI, j))
3068 return false;
3069 if (!isUndefOrEqual(BitI1, j))
3070 return false;
3071 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003072 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003073}
3074
Nate Begeman9008ca62009-04-27 18:41:29 +00003075bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3076 SmallVector<int, 8> M;
3077 N->getMask(M);
3078 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3079}
3080
Evan Cheng017dcc62006-04-21 01:05:10 +00003081/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3082/// specifies a shuffle of elements that is suitable for input to MOVSS,
3083/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003084static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003085 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003086 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003087
3088 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003089
Nate Begeman9008ca62009-04-27 18:41:29 +00003090 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003091 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003092
Nate Begeman9008ca62009-04-27 18:41:29 +00003093 for (int i = 1; i < NumElts; ++i)
3094 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003095 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003096
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003097 return true;
3098}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003099
Nate Begeman9008ca62009-04-27 18:41:29 +00003100bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3101 SmallVector<int, 8> M;
3102 N->getMask(M);
3103 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003104}
3105
Evan Cheng017dcc62006-04-21 01:05:10 +00003106/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3107/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003108/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003109static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003110 bool V2IsSplat = false, bool V2IsUndef = false) {
3111 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003112 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003113 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003114
Nate Begeman9008ca62009-04-27 18:41:29 +00003115 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003116 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003117
Nate Begeman9008ca62009-04-27 18:41:29 +00003118 for (int i = 1; i < NumOps; ++i)
3119 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3120 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3121 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003122 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003123
Evan Cheng39623da2006-04-20 08:58:49 +00003124 return true;
3125}
3126
Nate Begeman9008ca62009-04-27 18:41:29 +00003127static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003128 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003129 SmallVector<int, 8> M;
3130 N->getMask(M);
3131 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003132}
3133
Evan Chengd9539472006-04-14 21:59:03 +00003134/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3135/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003136bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
3137 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003138 return false;
3139
3140 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00003141 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003142 int Elt = N->getMaskElt(i);
3143 if (Elt >= 0 && Elt != 1)
3144 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00003145 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003146
3147 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003148 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003149 int Elt = N->getMaskElt(i);
3150 if (Elt >= 0 && Elt != 3)
3151 return false;
3152 if (Elt == 3)
3153 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003154 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003155 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00003156 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003157 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003158}
3159
3160/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3161/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003162bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
3163 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003164 return false;
3165
3166 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00003167 for (unsigned i = 0; i < 2; ++i)
3168 if (N->getMaskElt(i) > 0)
3169 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003170
3171 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003172 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003173 int Elt = N->getMaskElt(i);
3174 if (Elt >= 0 && Elt != 2)
3175 return false;
3176 if (Elt == 2)
3177 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003178 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003179 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003180 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003181}
3182
Evan Cheng0b457f02008-09-25 20:50:48 +00003183/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3184/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003185bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3186 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003187
Nate Begeman9008ca62009-04-27 18:41:29 +00003188 for (int i = 0; i < e; ++i)
3189 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003190 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003191 for (int i = 0; i < e; ++i)
3192 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003193 return false;
3194 return true;
3195}
3196
Evan Cheng63d33002006-03-22 08:01:21 +00003197/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003198/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003199unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003200 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3201 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3202
Evan Chengb9df0ca2006-03-22 02:53:00 +00003203 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3204 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003205 for (int i = 0; i < NumOperands; ++i) {
3206 int Val = SVOp->getMaskElt(NumOperands-i-1);
3207 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003208 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003209 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003210 if (i != NumOperands - 1)
3211 Mask <<= Shift;
3212 }
Evan Cheng63d33002006-03-22 08:01:21 +00003213 return Mask;
3214}
3215
Evan Cheng506d3df2006-03-29 23:07:14 +00003216/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003217/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003218unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003219 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003220 unsigned Mask = 0;
3221 // 8 nodes, but we only care about the last 4.
3222 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003223 int Val = SVOp->getMaskElt(i);
3224 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003225 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003226 if (i != 4)
3227 Mask <<= 2;
3228 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003229 return Mask;
3230}
3231
3232/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003233/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003234unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003235 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003236 unsigned Mask = 0;
3237 // 8 nodes, but we only care about the first 4.
3238 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003239 int Val = SVOp->getMaskElt(i);
3240 if (Val >= 0)
3241 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003242 if (i != 0)
3243 Mask <<= 2;
3244 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003245 return Mask;
3246}
3247
Nate Begemana09008b2009-10-19 02:17:23 +00003248/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3249/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3250unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3251 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3252 EVT VVT = N->getValueType(0);
3253 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3254 int Val = 0;
3255
3256 unsigned i, e;
3257 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3258 Val = SVOp->getMaskElt(i);
3259 if (Val >= 0)
3260 break;
3261 }
3262 return (Val - i) * EltSize;
3263}
3264
Evan Cheng37b73872009-07-30 08:33:02 +00003265/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3266/// constant +0.0.
3267bool X86::isZeroNode(SDValue Elt) {
3268 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00003269 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00003270 (isa<ConstantFPSDNode>(Elt) &&
3271 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3272}
3273
Nate Begeman9008ca62009-04-27 18:41:29 +00003274/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3275/// their permute mask.
3276static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3277 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003278 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003279 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003280 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003281
Nate Begeman5a5ca152009-04-29 05:20:52 +00003282 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003283 int idx = SVOp->getMaskElt(i);
3284 if (idx < 0)
3285 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003286 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003287 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003288 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003289 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003290 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003291 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3292 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003293}
3294
Evan Cheng779ccea2007-12-07 21:30:01 +00003295/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3296/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003297static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003298 unsigned NumElems = VT.getVectorNumElements();
3299 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003300 int idx = Mask[i];
3301 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003302 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003303 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003304 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003305 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003306 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003307 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003308}
3309
Evan Cheng533a0aa2006-04-19 20:35:22 +00003310/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3311/// match movhlps. The lower half elements should come from upper half of
3312/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003313/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003314static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3315 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003316 return false;
3317 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003318 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003319 return false;
3320 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003321 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003322 return false;
3323 return true;
3324}
3325
Evan Cheng5ced1d82006-04-06 23:23:56 +00003326/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003327/// is promoted to a vector. It also returns the LoadSDNode by reference if
3328/// required.
3329static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003330 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3331 return false;
3332 N = N->getOperand(0).getNode();
3333 if (!ISD::isNON_EXTLoad(N))
3334 return false;
3335 if (LD)
3336 *LD = cast<LoadSDNode>(N);
3337 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003338}
3339
Evan Cheng533a0aa2006-04-19 20:35:22 +00003340/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3341/// match movlp{s|d}. The lower half elements should come from lower half of
3342/// V1 (and in order), and the upper half elements should come from the upper
3343/// half of V2 (and in order). And since V1 will become the source of the
3344/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003345static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3346 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003347 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003348 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003349 // Is V2 is a vector load, don't do this transformation. We will try to use
3350 // load folding shufps op.
3351 if (ISD::isNON_EXTLoad(V2))
3352 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003353
Nate Begeman5a5ca152009-04-29 05:20:52 +00003354 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003355
Evan Cheng533a0aa2006-04-19 20:35:22 +00003356 if (NumElems != 2 && NumElems != 4)
3357 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003358 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003359 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003360 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003361 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003362 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003363 return false;
3364 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003365}
3366
Evan Cheng39623da2006-04-20 08:58:49 +00003367/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3368/// all the same.
3369static bool isSplatVector(SDNode *N) {
3370 if (N->getOpcode() != ISD::BUILD_VECTOR)
3371 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003372
Dan Gohman475871a2008-07-27 21:46:04 +00003373 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003374 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3375 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003376 return false;
3377 return true;
3378}
3379
Evan Cheng213d2cf2007-05-17 18:45:50 +00003380/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003381/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003382/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003383static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003384 SDValue V1 = N->getOperand(0);
3385 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003386 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3387 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003388 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003389 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003390 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003391 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3392 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003393 if (Opc != ISD::BUILD_VECTOR ||
3394 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003395 return false;
3396 } else if (Idx >= 0) {
3397 unsigned Opc = V1.getOpcode();
3398 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3399 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003400 if (Opc != ISD::BUILD_VECTOR ||
3401 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003402 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003403 }
3404 }
3405 return true;
3406}
3407
3408/// getZeroVector - Returns a vector of specified type with all zero elements.
3409///
Owen Andersone50ed302009-08-10 22:56:29 +00003410static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003411 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003412 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003413
Chris Lattner8a594482007-11-25 00:24:49 +00003414 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3415 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003416 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003417 if (VT.getSizeInBits() == 64) { // MMX
Owen Anderson825b72b2009-08-11 20:47:22 +00003418 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3419 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003420 } else if (HasSSE2) { // SSE2
Owen Anderson825b72b2009-08-11 20:47:22 +00003421 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3422 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003423 } else { // SSE1
Owen Anderson825b72b2009-08-11 20:47:22 +00003424 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3425 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003426 }
Dale Johannesenace16102009-02-03 19:33:06 +00003427 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003428}
3429
Chris Lattner8a594482007-11-25 00:24:49 +00003430/// getOnesVector - Returns a vector of specified type with all bits set.
3431///
Owen Andersone50ed302009-08-10 22:56:29 +00003432static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003433 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003434
Chris Lattner8a594482007-11-25 00:24:49 +00003435 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3436 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003437 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003438 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003439 if (VT.getSizeInBits() == 64) // MMX
Owen Anderson825b72b2009-08-11 20:47:22 +00003440 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Chris Lattner8a594482007-11-25 00:24:49 +00003441 else // SSE
Owen Anderson825b72b2009-08-11 20:47:22 +00003442 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Dale Johannesenace16102009-02-03 19:33:06 +00003443 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003444}
3445
3446
Evan Cheng39623da2006-04-20 08:58:49 +00003447/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3448/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003449static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003450 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003451 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003452
Evan Cheng39623da2006-04-20 08:58:49 +00003453 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003454 SmallVector<int, 8> MaskVec;
3455 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003456
Nate Begeman5a5ca152009-04-29 05:20:52 +00003457 for (unsigned i = 0; i != NumElems; ++i) {
3458 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003459 MaskVec[i] = NumElems;
3460 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003461 }
Evan Cheng39623da2006-04-20 08:58:49 +00003462 }
Evan Cheng39623da2006-04-20 08:58:49 +00003463 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003464 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3465 SVOp->getOperand(1), &MaskVec[0]);
3466 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003467}
3468
Evan Cheng017dcc62006-04-21 01:05:10 +00003469/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3470/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003471static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003472 SDValue V2) {
3473 unsigned NumElems = VT.getVectorNumElements();
3474 SmallVector<int, 8> Mask;
3475 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003476 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003477 Mask.push_back(i);
3478 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003479}
3480
Nate Begeman9008ca62009-04-27 18:41:29 +00003481/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003482static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003483 SDValue V2) {
3484 unsigned NumElems = VT.getVectorNumElements();
3485 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003486 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003487 Mask.push_back(i);
3488 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003489 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003490 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003491}
3492
Nate Begeman9008ca62009-04-27 18:41:29 +00003493/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003494static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003495 SDValue V2) {
3496 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003497 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003498 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003499 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003500 Mask.push_back(i + Half);
3501 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003502 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003503 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003504}
3505
Evan Cheng0c0f83f2008-04-05 00:30:36 +00003506/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Eric Christopherfd179292009-08-27 18:07:15 +00003507static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00003508 bool HasSSE2) {
3509 if (SV->getValueType(0).getVectorNumElements() <= 4)
3510 return SDValue(SV, 0);
Eric Christopherfd179292009-08-27 18:07:15 +00003511
Owen Anderson825b72b2009-08-11 20:47:22 +00003512 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003513 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003514 DebugLoc dl = SV->getDebugLoc();
3515 SDValue V1 = SV->getOperand(0);
3516 int NumElems = VT.getVectorNumElements();
3517 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003518
Nate Begeman9008ca62009-04-27 18:41:29 +00003519 // unpack elements to the correct location
3520 while (NumElems > 4) {
3521 if (EltNo < NumElems/2) {
3522 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3523 } else {
3524 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3525 EltNo -= NumElems/2;
3526 }
3527 NumElems >>= 1;
3528 }
Eric Christopherfd179292009-08-27 18:07:15 +00003529
Nate Begeman9008ca62009-04-27 18:41:29 +00003530 // Perform the splat.
3531 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Dale Johannesenace16102009-02-03 19:33:06 +00003532 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003533 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
3534 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003535}
3536
Evan Chengba05f722006-04-21 23:03:30 +00003537/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003538/// vector of zero or undef vector. This produces a shuffle where the low
3539/// element of V2 is swizzled into the zero/undef vector, landing at element
3540/// 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 +00003541static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003542 bool isZero, bool HasSSE2,
3543 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003544 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003545 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003546 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3547 unsigned NumElems = VT.getVectorNumElements();
3548 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003549 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003550 // If this is the insertion idx, put the low elt of V2 here.
3551 MaskVec.push_back(i == Idx ? NumElems : i);
3552 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003553}
3554
Evan Chengf26ffe92008-05-29 08:22:04 +00003555/// getNumOfConsecutiveZeros - Return the number of elements in a result of
3556/// a shuffle that is zero.
3557static
Nate Begeman9008ca62009-04-27 18:41:29 +00003558unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, int NumElems,
3559 bool Low, SelectionDAG &DAG) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003560 unsigned NumZeros = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003561 for (int i = 0; i < NumElems; ++i) {
Evan Chengab262272008-06-25 20:52:59 +00003562 unsigned Index = Low ? i : NumElems-i-1;
Nate Begeman9008ca62009-04-27 18:41:29 +00003563 int Idx = SVOp->getMaskElt(Index);
3564 if (Idx < 0) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003565 ++NumZeros;
3566 continue;
3567 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003568 SDValue Elt = DAG.getShuffleScalarElt(SVOp, Index);
Evan Cheng37b73872009-07-30 08:33:02 +00003569 if (Elt.getNode() && X86::isZeroNode(Elt))
Evan Chengf26ffe92008-05-29 08:22:04 +00003570 ++NumZeros;
3571 else
3572 break;
3573 }
3574 return NumZeros;
3575}
3576
3577/// isVectorShift - Returns true if the shuffle can be implemented as a
3578/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003579/// FIXME: split into pslldqi, psrldqi, palignr variants.
3580static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00003581 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
John McCallb1fb4492010-04-07 01:49:15 +00003582 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
Evan Chengf26ffe92008-05-29 08:22:04 +00003583
3584 isLeft = true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003585 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, true, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003586 if (!NumZeros) {
3587 isLeft = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003588 NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, false, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003589 if (!NumZeros)
3590 return false;
3591 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003592 bool SeenV1 = false;
3593 bool SeenV2 = false;
John McCallb1fb4492010-04-07 01:49:15 +00003594 for (unsigned i = NumZeros; i < NumElems; ++i) {
3595 unsigned Val = isLeft ? (i - NumZeros) : i;
3596 int Idx_ = SVOp->getMaskElt(isLeft ? i : (i - NumZeros));
3597 if (Idx_ < 0)
Evan Chengf26ffe92008-05-29 08:22:04 +00003598 continue;
John McCallb1fb4492010-04-07 01:49:15 +00003599 unsigned Idx = (unsigned) Idx_;
Nate Begeman9008ca62009-04-27 18:41:29 +00003600 if (Idx < NumElems)
Evan Chengf26ffe92008-05-29 08:22:04 +00003601 SeenV1 = true;
3602 else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003603 Idx -= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00003604 SeenV2 = true;
3605 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003606 if (Idx != Val)
Evan Chengf26ffe92008-05-29 08:22:04 +00003607 return false;
3608 }
3609 if (SeenV1 && SeenV2)
3610 return false;
3611
Nate Begeman9008ca62009-04-27 18:41:29 +00003612 ShVal = SeenV1 ? SVOp->getOperand(0) : SVOp->getOperand(1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003613 ShAmt = NumZeros;
3614 return true;
3615}
3616
3617
Evan Chengc78d3b42006-04-24 18:01:45 +00003618/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3619///
Dan Gohman475871a2008-07-27 21:46:04 +00003620static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003621 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00003622 SelectionDAG &DAG,
3623 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003624 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00003625 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003626
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003627 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003628 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003629 bool First = true;
3630 for (unsigned i = 0; i < 16; ++i) {
3631 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3632 if (ThisIsNonZero && First) {
3633 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003634 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003635 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003636 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003637 First = false;
3638 }
3639
3640 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00003641 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003642 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3643 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003644 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003645 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00003646 }
3647 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003648 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
3649 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
3650 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00003651 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003652 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00003653 } else
3654 ThisElt = LastElt;
3655
Gabor Greifba36cb52008-08-28 21:40:38 +00003656 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00003657 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00003658 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00003659 }
3660 }
3661
Owen Anderson825b72b2009-08-11 20:47:22 +00003662 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00003663}
3664
Bill Wendlinga348c562007-03-22 18:42:45 +00003665/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003666///
Dan Gohman475871a2008-07-27 21:46:04 +00003667static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00003668 unsigned NumNonZero, unsigned NumZero,
3669 SelectionDAG &DAG,
3670 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003671 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00003672 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003673
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003674 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003675 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003676 bool First = true;
3677 for (unsigned i = 0; i < 8; ++i) {
3678 bool isNonZero = (NonZeros & (1 << i)) != 0;
3679 if (isNonZero) {
3680 if (First) {
3681 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003682 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003683 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003684 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003685 First = false;
3686 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003687 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003688 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00003689 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00003690 }
3691 }
3692
3693 return V;
3694}
3695
Evan Chengf26ffe92008-05-29 08:22:04 +00003696/// getVShift - Return a vector logical shift node.
3697///
Owen Andersone50ed302009-08-10 22:56:29 +00003698static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00003699 unsigned NumBits, SelectionDAG &DAG,
3700 const TargetLowering &TLI, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003701 bool isMMX = VT.getSizeInBits() == 64;
Owen Anderson825b72b2009-08-11 20:47:22 +00003702 EVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00003703 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Dale Johannesenace16102009-02-03 19:33:06 +00003704 SrcOp = DAG.getNode(ISD::BIT_CONVERT, dl, ShVT, SrcOp);
3705 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3706 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00003707 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00003708}
3709
Dan Gohman475871a2008-07-27 21:46:04 +00003710SDValue
Evan Chengc3630942009-12-09 21:00:30 +00003711X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00003712 SelectionDAG &DAG) const {
Evan Chengc3630942009-12-09 21:00:30 +00003713
3714 // Check if the scalar load can be widened into a vector load. And if
3715 // the address is "base + cst" see if the cst can be "absorbed" into
3716 // the shuffle mask.
3717 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
3718 SDValue Ptr = LD->getBasePtr();
3719 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
3720 return SDValue();
3721 EVT PVT = LD->getValueType(0);
3722 if (PVT != MVT::i32 && PVT != MVT::f32)
3723 return SDValue();
3724
3725 int FI = -1;
3726 int64_t Offset = 0;
3727 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
3728 FI = FINode->getIndex();
3729 Offset = 0;
3730 } else if (Ptr.getOpcode() == ISD::ADD &&
3731 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
3732 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
3733 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
3734 Offset = Ptr.getConstantOperandVal(1);
3735 Ptr = Ptr.getOperand(0);
3736 } else {
3737 return SDValue();
3738 }
3739
3740 SDValue Chain = LD->getChain();
3741 // Make sure the stack object alignment is at least 16.
3742 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3743 if (DAG.InferPtrAlignment(Ptr) < 16) {
3744 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00003745 // Can't change the alignment. FIXME: It's possible to compute
3746 // the exact stack offset and reference FI + adjust offset instead.
3747 // If someone *really* cares about this. That's the way to implement it.
3748 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00003749 } else {
3750 MFI->setObjectAlignment(FI, 16);
3751 }
3752 }
3753
3754 // (Offset % 16) must be multiple of 4. Then address is then
3755 // Ptr + (Offset & ~15).
3756 if (Offset < 0)
3757 return SDValue();
3758 if ((Offset % 16) & 3)
3759 return SDValue();
3760 int64_t StartOffset = Offset & ~15;
3761 if (StartOffset)
3762 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
3763 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
3764
3765 int EltNo = (Offset - StartOffset) >> 2;
3766 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
3767 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
David Greene67c9d422010-02-15 16:53:33 +00003768 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,LD->getSrcValue(),0,
3769 false, false, 0);
Evan Chengc3630942009-12-09 21:00:30 +00003770 // Canonicalize it to a v4i32 shuffle.
3771 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32, V1);
3772 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3773 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
3774 DAG.getUNDEF(MVT::v4i32), &Mask[0]));
3775 }
3776
3777 return SDValue();
3778}
3779
Nate Begeman1449f292010-03-24 22:19:06 +00003780/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
3781/// vector of type 'VT', see if the elements can be replaced by a single large
3782/// load which has the same value as a build_vector whose operands are 'elts'.
3783///
3784/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
3785///
3786/// FIXME: we'd also like to handle the case where the last elements are zero
3787/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
3788/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00003789static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
3790 DebugLoc &dl, SelectionDAG &DAG) {
3791 EVT EltVT = VT.getVectorElementType();
3792 unsigned NumElems = Elts.size();
3793
Nate Begemanfdea31a2010-03-24 20:49:50 +00003794 LoadSDNode *LDBase = NULL;
3795 unsigned LastLoadedElt = -1U;
Nate Begeman1449f292010-03-24 22:19:06 +00003796
3797 // For each element in the initializer, see if we've found a load or an undef.
3798 // If we don't find an initial load element, or later load elements are
3799 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00003800 for (unsigned i = 0; i < NumElems; ++i) {
3801 SDValue Elt = Elts[i];
3802
3803 if (!Elt.getNode() ||
3804 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
3805 return SDValue();
3806 if (!LDBase) {
3807 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
3808 return SDValue();
3809 LDBase = cast<LoadSDNode>(Elt.getNode());
3810 LastLoadedElt = i;
3811 continue;
3812 }
3813 if (Elt.getOpcode() == ISD::UNDEF)
3814 continue;
3815
3816 LoadSDNode *LD = cast<LoadSDNode>(Elt);
3817 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
3818 return SDValue();
3819 LastLoadedElt = i;
3820 }
Nate Begeman1449f292010-03-24 22:19:06 +00003821
3822 // If we have found an entire vector of loads and undefs, then return a large
3823 // load of the entire vector width starting at the base pointer. If we found
3824 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00003825 if (LastLoadedElt == NumElems - 1) {
3826 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
3827 return DAG.getLoad(VT, dl, LDBase->getChain(), LDBase->getBasePtr(),
3828 LDBase->getSrcValue(), LDBase->getSrcValueOffset(),
3829 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
3830 return DAG.getLoad(VT, dl, LDBase->getChain(), LDBase->getBasePtr(),
3831 LDBase->getSrcValue(), LDBase->getSrcValueOffset(),
3832 LDBase->isVolatile(), LDBase->isNonTemporal(),
3833 LDBase->getAlignment());
3834 } else if (NumElems == 4 && LastLoadedElt == 1) {
3835 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
3836 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
3837 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2);
3838 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, ResNode);
3839 }
3840 return SDValue();
3841}
3842
Evan Chengc3630942009-12-09 21:00:30 +00003843SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00003844X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003845 DebugLoc dl = Op.getDebugLoc();
Chris Lattner8a594482007-11-25 00:24:49 +00003846 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif327ef032008-08-28 23:19:51 +00003847 if (ISD::isBuildVectorAllZeros(Op.getNode())
3848 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattner8a594482007-11-25 00:24:49 +00003849 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3850 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3851 // eliminated on x86-32 hosts.
Owen Anderson825b72b2009-08-11 20:47:22 +00003852 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
Chris Lattner8a594482007-11-25 00:24:49 +00003853 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003854
Gabor Greifba36cb52008-08-28 21:40:38 +00003855 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00003856 return getOnesVector(Op.getValueType(), DAG, dl);
3857 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00003858 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003859
Owen Andersone50ed302009-08-10 22:56:29 +00003860 EVT VT = Op.getValueType();
3861 EVT ExtVT = VT.getVectorElementType();
3862 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003863
3864 unsigned NumElems = Op.getNumOperands();
3865 unsigned NumZero = 0;
3866 unsigned NumNonZero = 0;
3867 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003868 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00003869 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003870 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003871 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00003872 if (Elt.getOpcode() == ISD::UNDEF)
3873 continue;
3874 Values.insert(Elt);
3875 if (Elt.getOpcode() != ISD::Constant &&
3876 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003877 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00003878 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00003879 NumZero++;
3880 else {
3881 NonZeros |= (1 << i);
3882 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003883 }
3884 }
3885
Dan Gohman7f321562007-06-25 16:23:39 +00003886 if (NumNonZero == 0) {
Chris Lattner8a594482007-11-25 00:24:49 +00003887 // All undef vector. Return an UNDEF. All zero vectors were handled above.
Dale Johannesene8d72302009-02-06 23:05:02 +00003888 return DAG.getUNDEF(VT);
Dan Gohman7f321562007-06-25 16:23:39 +00003889 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003890
Chris Lattner67f453a2008-03-09 05:42:06 +00003891 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00003892 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003893 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00003894 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00003895
Chris Lattner62098042008-03-09 01:05:04 +00003896 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3897 // the value are obviously zero, truncate the value to i32 and do the
3898 // insertion that way. Only do this if the value is non-constant or if the
3899 // value is a constant being inserted into element 0. It is cheaper to do
3900 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00003901 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00003902 (!IsAllConstants || Idx == 0)) {
3903 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3904 // Handle MMX and SSE both.
Owen Anderson825b72b2009-08-11 20:47:22 +00003905 EVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3906 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Scott Michelfdc40a02009-02-17 22:15:04 +00003907
Chris Lattner62098042008-03-09 01:05:04 +00003908 // Truncate the value (which may itself be a constant) to i32, and
3909 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00003910 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00003911 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00003912 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3913 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00003914
Chris Lattner62098042008-03-09 01:05:04 +00003915 // Now we have our 32-bit value zero extended in the low element of
3916 // a vector. If Idx != 0, swizzle it into place.
3917 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003918 SmallVector<int, 4> Mask;
3919 Mask.push_back(Idx);
3920 for (unsigned i = 1; i != VecElts; ++i)
3921 Mask.push_back(i);
3922 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00003923 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00003924 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003925 }
Dale Johannesenace16102009-02-03 19:33:06 +00003926 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00003927 }
3928 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003929
Chris Lattner19f79692008-03-08 22:59:52 +00003930 // If we have a constant or non-constant insertion into the low element of
3931 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3932 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00003933 // depending on what the source datatype is.
3934 if (Idx == 0) {
3935 if (NumZero == 0) {
3936 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00003937 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
3938 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00003939 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
3940 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
3941 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
3942 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00003943 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
3944 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
3945 EVT MiddleVT = VT.getSizeInBits() == 64 ? MVT::v2i32 : MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00003946 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
3947 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3948 Subtarget->hasSSE2(), DAG);
3949 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Item);
3950 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003951 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003952
3953 // Is it a vector logical left shift?
3954 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00003955 X86::isZeroNode(Op.getOperand(0)) &&
3956 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003957 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00003958 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00003959 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00003960 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00003961 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00003962 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003963
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003964 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00003965 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003966
Chris Lattner19f79692008-03-08 22:59:52 +00003967 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3968 // is a non-constant being inserted into an element other than the low one,
3969 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3970 // movd/movss) to move this into the low element, then shuffle it into
3971 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003972 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00003973 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00003974
Evan Cheng0db9fe62006-04-25 20:13:52 +00003975 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003976 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3977 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00003978 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003979 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00003980 MaskVec.push_back(i == Idx ? 0 : 1);
3981 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003982 }
3983 }
3984
Chris Lattner67f453a2008-03-09 05:42:06 +00003985 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00003986 if (Values.size() == 1) {
3987 if (EVTBits == 32) {
3988 // Instead of a shuffle like this:
3989 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
3990 // Check if it's possible to issue this instead.
3991 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
3992 unsigned Idx = CountTrailingZeros_32(NonZeros);
3993 SDValue Item = Op.getOperand(Idx);
3994 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
3995 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
3996 }
Dan Gohman475871a2008-07-27 21:46:04 +00003997 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00003998 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003999
Dan Gohmana3941172007-07-24 22:55:08 +00004000 // A vector full of immediates; various special cases are already
4001 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004002 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00004003 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00004004
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00004005 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004006 if (EVTBits == 64) {
4007 if (NumNonZero == 1) {
4008 // One half is zero or undef.
4009 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00004010 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00004011 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00004012 return getShuffleVectorZeroOrUndef(V2, Idx, true,
4013 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00004014 }
Dan Gohman475871a2008-07-27 21:46:04 +00004015 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00004016 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004017
4018 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00004019 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004020 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00004021 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004022 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004023 }
4024
Bill Wendling826f36f2007-03-28 00:57:11 +00004025 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00004026 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00004027 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004028 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004029 }
4030
4031 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004032 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00004033 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004034 if (NumElems == 4 && NumZero > 0) {
4035 for (unsigned i = 0; i < 4; ++i) {
4036 bool isZero = !(NonZeros & (1 << i));
4037 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00004038 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004039 else
Dale Johannesenace16102009-02-03 19:33:06 +00004040 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004041 }
4042
4043 for (unsigned i = 0; i < 2; ++i) {
4044 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
4045 default: break;
4046 case 0:
4047 V[i] = V[i*2]; // Must be a zero vector.
4048 break;
4049 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00004050 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004051 break;
4052 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00004053 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004054 break;
4055 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00004056 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004057 break;
4058 }
4059 }
4060
Nate Begeman9008ca62009-04-27 18:41:29 +00004061 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004062 bool Reverse = (NonZeros & 0x3) == 2;
4063 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004064 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004065 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
4066 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004067 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
4068 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004069 }
4070
Nate Begemanfdea31a2010-03-24 20:49:50 +00004071 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
4072 // Check for a build vector of consecutive loads.
4073 for (unsigned i = 0; i < NumElems; ++i)
4074 V[i] = Op.getOperand(i);
4075
4076 // Check for elements which are consecutive loads.
4077 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
4078 if (LD.getNode())
4079 return LD;
4080
4081 // For SSE 4.1, use inserts into undef.
4082 if (getSubtarget()->hasSSE41()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004083 V[0] = DAG.getUNDEF(VT);
4084 for (unsigned i = 0; i < NumElems; ++i)
4085 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
4086 V[0] = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, V[0],
4087 Op.getOperand(i), DAG.getIntPtrConstant(i));
4088 return V[0];
4089 }
Nate Begemanfdea31a2010-03-24 20:49:50 +00004090
4091 // Otherwise, expand into a number of unpckl*
Evan Cheng0db9fe62006-04-25 20:13:52 +00004092 // e.g. for v4f32
4093 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
4094 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
4095 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Evan Cheng0db9fe62006-04-25 20:13:52 +00004096 for (unsigned i = 0; i < NumElems; ++i)
Dale Johannesenace16102009-02-03 19:33:06 +00004097 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004098 NumElems >>= 1;
4099 while (NumElems != 0) {
4100 for (unsigned i = 0; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004101 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + NumElems]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004102 NumElems >>= 1;
4103 }
4104 return V[0];
4105 }
Dan Gohman475871a2008-07-27 21:46:04 +00004106 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004107}
4108
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004109SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004110X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004111 // We support concatenate two MMX registers and place them in a MMX
4112 // register. This is better than doing a stack convert.
4113 DebugLoc dl = Op.getDebugLoc();
4114 EVT ResVT = Op.getValueType();
4115 assert(Op.getNumOperands() == 2);
4116 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
4117 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
4118 int Mask[2];
4119 SDValue InVec = DAG.getNode(ISD::BIT_CONVERT,dl, MVT::v1i64, Op.getOperand(0));
4120 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4121 InVec = Op.getOperand(1);
4122 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4123 unsigned NumElts = ResVT.getVectorNumElements();
4124 VecOp = DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
4125 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
4126 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
4127 } else {
4128 InVec = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v1i64, InVec);
4129 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4130 Mask[0] = 0; Mask[1] = 2;
4131 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
4132 }
4133 return DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
4134}
4135
Nate Begemanb9a47b82009-02-23 08:49:38 +00004136// v8i16 shuffles - Prefer shuffles in the following order:
4137// 1. [all] pshuflw, pshufhw, optional move
4138// 2. [ssse3] 1 x pshufb
4139// 3. [ssse3] 2 x pshufb + 1 x por
4140// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004141static
Nate Begeman9008ca62009-04-27 18:41:29 +00004142SDValue LowerVECTOR_SHUFFLEv8i16(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00004143 SelectionDAG &DAG,
4144 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004145 SDValue V1 = SVOp->getOperand(0);
4146 SDValue V2 = SVOp->getOperand(1);
4147 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004148 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00004149
Nate Begemanb9a47b82009-02-23 08:49:38 +00004150 // Determine if more than 1 of the words in each of the low and high quadwords
4151 // of the result come from the same quadword of one of the two inputs. Undef
4152 // mask values count as coming from any quadword, for better codegen.
4153 SmallVector<unsigned, 4> LoQuad(4);
4154 SmallVector<unsigned, 4> HiQuad(4);
4155 BitVector InputQuads(4);
4156 for (unsigned i = 0; i < 8; ++i) {
4157 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00004158 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004159 MaskVals.push_back(EltIdx);
4160 if (EltIdx < 0) {
4161 ++Quad[0];
4162 ++Quad[1];
4163 ++Quad[2];
4164 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00004165 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004166 }
4167 ++Quad[EltIdx / 4];
4168 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00004169 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004170
Nate Begemanb9a47b82009-02-23 08:49:38 +00004171 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004172 unsigned MaxQuad = 1;
4173 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004174 if (LoQuad[i] > MaxQuad) {
4175 BestLoQuad = i;
4176 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004177 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004178 }
4179
Nate Begemanb9a47b82009-02-23 08:49:38 +00004180 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004181 MaxQuad = 1;
4182 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004183 if (HiQuad[i] > MaxQuad) {
4184 BestHiQuad = i;
4185 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004186 }
4187 }
4188
Nate Begemanb9a47b82009-02-23 08:49:38 +00004189 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00004190 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00004191 // single pshufb instruction is necessary. If There are more than 2 input
4192 // quads, disable the next transformation since it does not help SSSE3.
4193 bool V1Used = InputQuads[0] || InputQuads[1];
4194 bool V2Used = InputQuads[2] || InputQuads[3];
4195 if (TLI.getSubtarget()->hasSSSE3()) {
4196 if (InputQuads.count() == 2 && V1Used && V2Used) {
4197 BestLoQuad = InputQuads.find_first();
4198 BestHiQuad = InputQuads.find_next(BestLoQuad);
4199 }
4200 if (InputQuads.count() > 2) {
4201 BestLoQuad = -1;
4202 BestHiQuad = -1;
4203 }
4204 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004205
Nate Begemanb9a47b82009-02-23 08:49:38 +00004206 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
4207 // the shuffle mask. If a quad is scored as -1, that means that it contains
4208 // words from all 4 input quadwords.
4209 SDValue NewV;
4210 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004211 SmallVector<int, 8> MaskV;
4212 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
4213 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00004214 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004215 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V1),
4216 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V2), &MaskV[0]);
4217 NewV = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004218
Nate Begemanb9a47b82009-02-23 08:49:38 +00004219 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
4220 // source words for the shuffle, to aid later transformations.
4221 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00004222 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00004223 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004224 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00004225 if (idx != (int)i)
4226 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004227 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00004228 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004229 AllWordsInNewV = false;
4230 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00004231 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004232
Nate Begemanb9a47b82009-02-23 08:49:38 +00004233 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
4234 if (AllWordsInNewV) {
4235 for (int i = 0; i != 8; ++i) {
4236 int idx = MaskVals[i];
4237 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004238 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004239 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004240 if ((idx != i) && idx < 4)
4241 pshufhw = false;
4242 if ((idx != i) && idx > 3)
4243 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00004244 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00004245 V1 = NewV;
4246 V2Used = false;
4247 BestLoQuad = 0;
4248 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004249 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004250
Nate Begemanb9a47b82009-02-23 08:49:38 +00004251 // If we've eliminated the use of V2, and the new mask is a pshuflw or
4252 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00004253 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Eric Christopherfd179292009-08-27 18:07:15 +00004254 return DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00004255 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Evan Cheng14b32e12007-12-11 01:46:18 +00004256 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004257 }
Eric Christopherfd179292009-08-27 18:07:15 +00004258
Nate Begemanb9a47b82009-02-23 08:49:38 +00004259 // If we have SSSE3, and all words of the result are from 1 input vector,
4260 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
4261 // is present, fall back to case 4.
4262 if (TLI.getSubtarget()->hasSSSE3()) {
4263 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004264
Nate Begemanb9a47b82009-02-23 08:49:38 +00004265 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00004266 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00004267 // mask, and elements that come from V1 in the V2 mask, so that the two
4268 // results can be OR'd together.
4269 bool TwoInputs = V1Used && V2Used;
4270 for (unsigned i = 0; i != 8; ++i) {
4271 int EltIdx = MaskVals[i] * 2;
4272 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004273 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4274 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004275 continue;
4276 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004277 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
4278 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004279 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004280 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004281 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004282 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004283 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004284 if (!TwoInputs)
Owen Anderson825b72b2009-08-11 20:47:22 +00004285 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004286
Nate Begemanb9a47b82009-02-23 08:49:38 +00004287 // Calculate the shuffle mask for the second input, shuffle it, and
4288 // OR it with the first shuffled input.
4289 pshufbMask.clear();
4290 for (unsigned i = 0; i != 8; ++i) {
4291 int EltIdx = MaskVals[i] * 2;
4292 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004293 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4294 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004295 continue;
4296 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004297 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
4298 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004299 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004300 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00004301 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004302 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004303 MVT::v16i8, &pshufbMask[0], 16));
4304 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
4305 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004306 }
4307
4308 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
4309 // and update MaskVals with new element order.
4310 BitVector InOrder(8);
4311 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004312 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004313 for (int i = 0; i != 4; ++i) {
4314 int idx = MaskVals[i];
4315 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004316 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004317 InOrder.set(i);
4318 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004319 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004320 InOrder.set(i);
4321 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004322 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004323 }
4324 }
4325 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004326 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00004327 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004328 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004329 }
Eric Christopherfd179292009-08-27 18:07:15 +00004330
Nate Begemanb9a47b82009-02-23 08:49:38 +00004331 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
4332 // and update MaskVals with the new element order.
4333 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004334 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004335 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004336 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004337 for (unsigned i = 4; i != 8; ++i) {
4338 int idx = MaskVals[i];
4339 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004340 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004341 InOrder.set(i);
4342 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004343 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004344 InOrder.set(i);
4345 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004346 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004347 }
4348 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004349 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004350 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004351 }
Eric Christopherfd179292009-08-27 18:07:15 +00004352
Nate Begemanb9a47b82009-02-23 08:49:38 +00004353 // In case BestHi & BestLo were both -1, which means each quadword has a word
4354 // from each of the four input quadwords, calculate the InOrder bitvector now
4355 // before falling through to the insert/extract cleanup.
4356 if (BestLoQuad == -1 && BestHiQuad == -1) {
4357 NewV = V1;
4358 for (int i = 0; i != 8; ++i)
4359 if (MaskVals[i] < 0 || MaskVals[i] == i)
4360 InOrder.set(i);
4361 }
Eric Christopherfd179292009-08-27 18:07:15 +00004362
Nate Begemanb9a47b82009-02-23 08:49:38 +00004363 // The other elements are put in the right place using pextrw and pinsrw.
4364 for (unsigned i = 0; i != 8; ++i) {
4365 if (InOrder[i])
4366 continue;
4367 int EltIdx = MaskVals[i];
4368 if (EltIdx < 0)
4369 continue;
4370 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004371 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004372 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00004373 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004374 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004375 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004376 DAG.getIntPtrConstant(i));
4377 }
4378 return NewV;
4379}
4380
4381// v16i8 shuffles - Prefer shuffles in the following order:
4382// 1. [ssse3] 1 x pshufb
4383// 2. [ssse3] 2 x pshufb + 1 x por
4384// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
4385static
Nate Begeman9008ca62009-04-27 18:41:29 +00004386SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00004387 SelectionDAG &DAG,
4388 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004389 SDValue V1 = SVOp->getOperand(0);
4390 SDValue V2 = SVOp->getOperand(1);
4391 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004392 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00004393 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00004394
Nate Begemanb9a47b82009-02-23 08:49:38 +00004395 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00004396 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00004397 // present, fall back to case 3.
4398 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
4399 bool V1Only = true;
4400 bool V2Only = true;
4401 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004402 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00004403 if (EltIdx < 0)
4404 continue;
4405 if (EltIdx < 16)
4406 V2Only = false;
4407 else
4408 V1Only = false;
4409 }
Eric Christopherfd179292009-08-27 18:07:15 +00004410
Nate Begemanb9a47b82009-02-23 08:49:38 +00004411 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
4412 if (TLI.getSubtarget()->hasSSSE3()) {
4413 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004414
Nate Begemanb9a47b82009-02-23 08:49:38 +00004415 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00004416 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004417 //
4418 // Otherwise, we have elements from both input vectors, and must zero out
4419 // elements that come from V2 in the first mask, and V1 in the second mask
4420 // so that we can OR them together.
4421 bool TwoInputs = !(V1Only || V2Only);
4422 for (unsigned i = 0; i != 16; ++i) {
4423 int EltIdx = MaskVals[i];
4424 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004425 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004426 continue;
4427 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004428 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004429 }
4430 // If all the elements are from V2, assign it to V1 and return after
4431 // building the first pshufb.
4432 if (V2Only)
4433 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00004434 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004435 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004436 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004437 if (!TwoInputs)
4438 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00004439
Nate Begemanb9a47b82009-02-23 08:49:38 +00004440 // Calculate the shuffle mask for the second input, shuffle it, and
4441 // OR it with the first shuffled input.
4442 pshufbMask.clear();
4443 for (unsigned i = 0; i != 16; ++i) {
4444 int EltIdx = MaskVals[i];
4445 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004446 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004447 continue;
4448 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004449 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004450 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004451 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004452 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004453 MVT::v16i8, &pshufbMask[0], 16));
4454 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004455 }
Eric Christopherfd179292009-08-27 18:07:15 +00004456
Nate Begemanb9a47b82009-02-23 08:49:38 +00004457 // No SSSE3 - Calculate in place words and then fix all out of place words
4458 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
4459 // the 16 different words that comprise the two doublequadword input vectors.
Owen Anderson825b72b2009-08-11 20:47:22 +00004460 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
4461 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004462 SDValue NewV = V2Only ? V2 : V1;
4463 for (int i = 0; i != 8; ++i) {
4464 int Elt0 = MaskVals[i*2];
4465 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00004466
Nate Begemanb9a47b82009-02-23 08:49:38 +00004467 // This word of the result is all undef, skip it.
4468 if (Elt0 < 0 && Elt1 < 0)
4469 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004470
Nate Begemanb9a47b82009-02-23 08:49:38 +00004471 // This word of the result is already in the correct place, skip it.
4472 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
4473 continue;
4474 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
4475 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004476
Nate Begemanb9a47b82009-02-23 08:49:38 +00004477 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
4478 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
4479 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00004480
4481 // If Elt0 and Elt1 are defined, are consecutive, and can be load
4482 // using a single extract together, load it and store it.
4483 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004484 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004485 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00004486 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004487 DAG.getIntPtrConstant(i));
4488 continue;
4489 }
4490
Nate Begemanb9a47b82009-02-23 08:49:38 +00004491 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00004492 // source byte is not also odd, shift the extracted word left 8 bits
4493 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004494 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004495 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004496 DAG.getIntPtrConstant(Elt1 / 2));
4497 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004498 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004499 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004500 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004501 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
4502 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004503 }
4504 // If Elt0 is defined, extract it from the appropriate source. If the
4505 // source byte is not also even, shift the extracted word right 8 bits. If
4506 // Elt1 was also defined, OR the extracted values together before
4507 // inserting them in the result.
4508 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004509 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004510 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
4511 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004512 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004513 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004514 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004515 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
4516 DAG.getConstant(0x00FF, MVT::i16));
4517 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00004518 : InsElt0;
4519 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004520 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004521 DAG.getIntPtrConstant(i));
4522 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004523 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004524}
4525
Evan Cheng7a831ce2007-12-15 03:00:47 +00004526/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Chris Lattnerf172ecd2010-07-04 23:07:25 +00004527/// ones, or rewriting v4i32 / v2i32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00004528/// done when every pair / quad of shuffle mask elements point to elements in
4529/// the right sequence. e.g.
Evan Cheng14b32e12007-12-11 01:46:18 +00004530/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
4531static
Nate Begeman9008ca62009-04-27 18:41:29 +00004532SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
4533 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00004534 const TargetLowering &TLI, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00004535 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00004536 SDValue V1 = SVOp->getOperand(0);
4537 SDValue V2 = SVOp->getOperand(1);
4538 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00004539 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Owen Anderson825b72b2009-08-11 20:47:22 +00004540 EVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Owen Andersone50ed302009-08-10 22:56:29 +00004541 EVT NewVT = MaskVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00004542 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004543 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004544 case MVT::v4f32: NewVT = MVT::v2f64; break;
4545 case MVT::v4i32: NewVT = MVT::v2i64; break;
4546 case MVT::v8i16: NewVT = MVT::v4i32; break;
4547 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004548 }
4549
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004550 if (NewWidth == 2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004551 if (VT.isInteger())
Owen Anderson825b72b2009-08-11 20:47:22 +00004552 NewVT = MVT::v2i64;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004553 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004554 NewVT = MVT::v2f64;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004555 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004556 int Scale = NumElems / NewWidth;
4557 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00004558 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004559 int StartIdx = -1;
4560 for (int j = 0; j < Scale; ++j) {
4561 int EltIdx = SVOp->getMaskElt(i+j);
4562 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004563 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00004564 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00004565 StartIdx = EltIdx - (EltIdx % Scale);
4566 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00004567 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004568 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004569 if (StartIdx == -1)
4570 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00004571 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004572 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004573 }
4574
Dale Johannesenace16102009-02-03 19:33:06 +00004575 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V1);
4576 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00004577 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004578}
4579
Evan Chengd880b972008-05-09 21:53:03 +00004580/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004581///
Owen Andersone50ed302009-08-10 22:56:29 +00004582static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00004583 SDValue SrcOp, SelectionDAG &DAG,
4584 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004585 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004586 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00004587 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00004588 LD = dyn_cast<LoadSDNode>(SrcOp);
4589 if (!LD) {
4590 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
4591 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00004592 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
4593 if ((ExtVT.SimpleTy != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00004594 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4595 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00004596 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004597 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00004598 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Dale Johannesenace16102009-02-03 19:33:06 +00004599 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4600 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
4601 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
4602 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00004603 SrcOp.getOperand(0)
4604 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004605 }
4606 }
4607 }
4608
Dale Johannesenace16102009-02-03 19:33:06 +00004609 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4610 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004611 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00004612 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004613}
4614
Evan Chengace3c172008-07-22 21:13:36 +00004615/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
4616/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004617static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00004618LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
4619 SDValue V1 = SVOp->getOperand(0);
4620 SDValue V2 = SVOp->getOperand(1);
4621 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004622 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00004623
Evan Chengace3c172008-07-22 21:13:36 +00004624 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00004625 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00004626 SmallVector<int, 8> Mask1(4U, -1);
4627 SmallVector<int, 8> PermMask;
4628 SVOp->getMask(PermMask);
4629
Evan Chengace3c172008-07-22 21:13:36 +00004630 unsigned NumHi = 0;
4631 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00004632 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004633 int Idx = PermMask[i];
4634 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004635 Locs[i] = std::make_pair(-1, -1);
4636 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004637 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
4638 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004639 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00004640 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004641 NumLo++;
4642 } else {
4643 Locs[i] = std::make_pair(1, NumHi);
4644 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004645 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004646 NumHi++;
4647 }
4648 }
4649 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004650
Evan Chengace3c172008-07-22 21:13:36 +00004651 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004652 // If no more than two elements come from either vector. This can be
4653 // implemented with two shuffles. First shuffle gather the elements.
4654 // The second shuffle, which takes the first shuffle as both of its
4655 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00004656 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004657
Nate Begeman9008ca62009-04-27 18:41:29 +00004658 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00004659
Evan Chengace3c172008-07-22 21:13:36 +00004660 for (unsigned i = 0; i != 4; ++i) {
4661 if (Locs[i].first == -1)
4662 continue;
4663 else {
4664 unsigned Idx = (i < 2) ? 0 : 4;
4665 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004666 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004667 }
4668 }
4669
Nate Begeman9008ca62009-04-27 18:41:29 +00004670 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004671 } else if (NumLo == 3 || NumHi == 3) {
4672 // Otherwise, we must have three elements from one vector, call it X, and
4673 // one element from the other, call it Y. First, use a shufps to build an
4674 // intermediate vector with the one element from Y and the element from X
4675 // that will be in the same half in the final destination (the indexes don't
4676 // matter). Then, use a shufps to build the final vector, taking the half
4677 // containing the element from Y from the intermediate, and the other half
4678 // from X.
4679 if (NumHi == 3) {
4680 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00004681 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004682 std::swap(V1, V2);
4683 }
4684
4685 // Find the element from V2.
4686 unsigned HiIndex;
4687 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004688 int Val = PermMask[HiIndex];
4689 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004690 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004691 if (Val >= 4)
4692 break;
4693 }
4694
Nate Begeman9008ca62009-04-27 18:41:29 +00004695 Mask1[0] = PermMask[HiIndex];
4696 Mask1[1] = -1;
4697 Mask1[2] = PermMask[HiIndex^1];
4698 Mask1[3] = -1;
4699 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004700
4701 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004702 Mask1[0] = PermMask[0];
4703 Mask1[1] = PermMask[1];
4704 Mask1[2] = HiIndex & 1 ? 6 : 4;
4705 Mask1[3] = HiIndex & 1 ? 4 : 6;
4706 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004707 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004708 Mask1[0] = HiIndex & 1 ? 2 : 0;
4709 Mask1[1] = HiIndex & 1 ? 0 : 2;
4710 Mask1[2] = PermMask[2];
4711 Mask1[3] = PermMask[3];
4712 if (Mask1[2] >= 0)
4713 Mask1[2] += 4;
4714 if (Mask1[3] >= 0)
4715 Mask1[3] += 4;
4716 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004717 }
Evan Chengace3c172008-07-22 21:13:36 +00004718 }
4719
4720 // Break it into (shuffle shuffle_hi, shuffle_lo).
4721 Locs.clear();
Nate Begeman9008ca62009-04-27 18:41:29 +00004722 SmallVector<int,8> LoMask(4U, -1);
4723 SmallVector<int,8> HiMask(4U, -1);
4724
4725 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00004726 unsigned MaskIdx = 0;
4727 unsigned LoIdx = 0;
4728 unsigned HiIdx = 2;
4729 for (unsigned i = 0; i != 4; ++i) {
4730 if (i == 2) {
4731 MaskPtr = &HiMask;
4732 MaskIdx = 1;
4733 LoIdx = 0;
4734 HiIdx = 2;
4735 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004736 int Idx = PermMask[i];
4737 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004738 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00004739 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004740 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004741 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004742 LoIdx++;
4743 } else {
4744 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004745 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004746 HiIdx++;
4747 }
4748 }
4749
Nate Begeman9008ca62009-04-27 18:41:29 +00004750 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
4751 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
4752 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00004753 for (unsigned i = 0; i != 4; ++i) {
4754 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004755 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00004756 } else {
4757 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004758 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00004759 }
4760 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004761 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00004762}
4763
Dan Gohman475871a2008-07-27 21:46:04 +00004764SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004765X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00004766 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00004767 SDValue V1 = Op.getOperand(0);
4768 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00004769 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004770 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00004771 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004772 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004773 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
4774 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00004775 bool V1IsSplat = false;
4776 bool V2IsSplat = false;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004777
Nate Begeman9008ca62009-04-27 18:41:29 +00004778 if (isZeroShuffle(SVOp))
Dale Johannesenace16102009-02-03 19:33:06 +00004779 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004780
Nate Begeman9008ca62009-04-27 18:41:29 +00004781 // Promote splats to v4f32.
4782 if (SVOp->isSplat()) {
Eric Christopherfd179292009-08-27 18:07:15 +00004783 if (isMMX || NumElems < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004784 return Op;
4785 return PromoteSplat(SVOp, DAG, Subtarget->hasSSE2());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004786 }
4787
Evan Cheng7a831ce2007-12-15 03:00:47 +00004788 // If the shuffle can be profitably rewritten as a narrower shuffle, then
4789 // do it!
Owen Anderson825b72b2009-08-11 20:47:22 +00004790 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004791 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004792 if (NewOp.getNode())
Scott Michelfdc40a02009-02-17 22:15:04 +00004793 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004794 LowerVECTOR_SHUFFLE(NewOp, DAG));
Owen Anderson825b72b2009-08-11 20:47:22 +00004795 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Evan Cheng7a831ce2007-12-15 03:00:47 +00004796 // FIXME: Figure out a cleaner way to do this.
4797 // Try to make use of movq to zero out the top part.
Gabor Greifba36cb52008-08-28 21:40:38 +00004798 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004799 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004800 if (NewOp.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004801 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
4802 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
4803 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004804 }
Gabor Greifba36cb52008-08-28 21:40:38 +00004805 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004806 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
4807 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
Evan Chengd880b972008-05-09 21:53:03 +00004808 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Nate Begeman9008ca62009-04-27 18:41:29 +00004809 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004810 }
4811 }
Eric Christopherfd179292009-08-27 18:07:15 +00004812
Nate Begeman9008ca62009-04-27 18:41:29 +00004813 if (X86::isPSHUFDMask(SVOp))
4814 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004815
Evan Chengf26ffe92008-05-29 08:22:04 +00004816 // Check if this can be converted into a logical shift.
4817 bool isLeft = false;
4818 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00004819 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00004820 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00004821 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00004822 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004823 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00004824 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00004825 EVT EltVT = VT.getVectorElementType();
4826 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004827 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004828 }
Eric Christopherfd179292009-08-27 18:07:15 +00004829
Nate Begeman9008ca62009-04-27 18:41:29 +00004830 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004831 if (V1IsUndef)
4832 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00004833 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004834 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Nate Begemanfb8ead02008-07-25 19:05:58 +00004835 if (!isMMX)
4836 return Op;
Evan Cheng7e2ff772008-05-08 00:57:18 +00004837 }
Eric Christopherfd179292009-08-27 18:07:15 +00004838
Nate Begeman9008ca62009-04-27 18:41:29 +00004839 // FIXME: fold these into legal mask.
4840 if (!isMMX && (X86::isMOVSHDUPMask(SVOp) ||
4841 X86::isMOVSLDUPMask(SVOp) ||
4842 X86::isMOVHLPSMask(SVOp) ||
Nate Begeman0b10b912009-11-07 23:17:15 +00004843 X86::isMOVLHPSMask(SVOp) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00004844 X86::isMOVLPMask(SVOp)))
Evan Cheng9bbbb982006-10-25 20:48:19 +00004845 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004846
Nate Begeman9008ca62009-04-27 18:41:29 +00004847 if (ShouldXformToMOVHLPS(SVOp) ||
4848 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
4849 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004850
Evan Chengf26ffe92008-05-29 08:22:04 +00004851 if (isShift) {
4852 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00004853 EVT EltVT = VT.getVectorElementType();
4854 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004855 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004856 }
Eric Christopherfd179292009-08-27 18:07:15 +00004857
Evan Cheng9eca5e82006-10-25 21:49:50 +00004858 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00004859 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4860 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00004861 V1IsSplat = isSplatVector(V1.getNode());
4862 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00004863
Chris Lattner8a594482007-11-25 00:24:49 +00004864 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00004865 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004866 Op = CommuteVectorShuffle(SVOp, DAG);
4867 SVOp = cast<ShuffleVectorSDNode>(Op);
4868 V1 = SVOp->getOperand(0);
4869 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00004870 std::swap(V1IsSplat, V2IsSplat);
4871 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00004872 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00004873 }
4874
Nate Begeman9008ca62009-04-27 18:41:29 +00004875 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
4876 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00004877 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00004878 return V1;
4879 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
4880 // the instruction selector will not match, so get a canonical MOVL with
4881 // swapped operands to undo the commute.
4882 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00004883 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004884
Nate Begeman9008ca62009-04-27 18:41:29 +00004885 if (X86::isUNPCKL_v_undef_Mask(SVOp) ||
4886 X86::isUNPCKH_v_undef_Mask(SVOp) ||
4887 X86::isUNPCKLMask(SVOp) ||
4888 X86::isUNPCKHMask(SVOp))
Evan Chengd9b8e402006-10-16 06:36:00 +00004889 return Op;
Evan Chenge1113032006-10-04 18:33:38 +00004890
Evan Cheng9bbbb982006-10-25 20:48:19 +00004891 if (V2IsSplat) {
4892 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004893 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00004894 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00004895 SDValue NewMask = NormalizeMask(SVOp, DAG);
4896 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
4897 if (NSVOp != SVOp) {
4898 if (X86::isUNPCKLMask(NSVOp, true)) {
4899 return NewMask;
4900 } else if (X86::isUNPCKHMask(NSVOp, true)) {
4901 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004902 }
4903 }
4904 }
4905
Evan Cheng9eca5e82006-10-25 21:49:50 +00004906 if (Commuted) {
4907 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00004908 // FIXME: this seems wrong.
4909 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
4910 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
4911 if (X86::isUNPCKL_v_undef_Mask(NewSVOp) ||
4912 X86::isUNPCKH_v_undef_Mask(NewSVOp) ||
4913 X86::isUNPCKLMask(NewSVOp) ||
4914 X86::isUNPCKHMask(NewSVOp))
4915 return NewOp;
Evan Cheng9eca5e82006-10-25 21:49:50 +00004916 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004917
Nate Begemanb9a47b82009-02-23 08:49:38 +00004918 // FIXME: for mmx, bitcast v2i32 to v4i16 for shuffle.
Nate Begeman9008ca62009-04-27 18:41:29 +00004919
4920 // Normalize the node to match x86 shuffle ops if needed
4921 if (!isMMX && V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
4922 return CommuteVectorShuffle(SVOp, DAG);
4923
4924 // Check for legal shuffle and return?
4925 SmallVector<int, 16> PermMask;
4926 SVOp->getMask(PermMask);
4927 if (isShuffleMaskLegal(PermMask, VT))
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004928 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004929
Evan Cheng14b32e12007-12-11 01:46:18 +00004930 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00004931 if (VT == MVT::v8i16) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004932 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(SVOp, DAG, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004933 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00004934 return NewOp;
4935 }
4936
Owen Anderson825b72b2009-08-11 20:47:22 +00004937 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004938 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004939 if (NewOp.getNode())
4940 return NewOp;
4941 }
Eric Christopherfd179292009-08-27 18:07:15 +00004942
Evan Chengace3c172008-07-22 21:13:36 +00004943 // Handle all 4 wide cases with a number of shuffles except for MMX.
4944 if (NumElems == 4 && !isMMX)
Nate Begeman9008ca62009-04-27 18:41:29 +00004945 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004946
Dan Gohman475871a2008-07-27 21:46:04 +00004947 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004948}
4949
Dan Gohman475871a2008-07-27 21:46:04 +00004950SDValue
4951X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00004952 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00004953 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004954 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004955 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004956 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004957 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004958 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004959 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004960 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004961 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00004962 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
4963 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
4964 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004965 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4966 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004967 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004968 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00004969 Op.getOperand(0)),
4970 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00004971 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004972 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004973 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004974 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004975 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00004976 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00004977 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4978 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004979 // result has a single use which is a store or a bitcast to i32. And in
4980 // the case of a store, it's not worth it if the index is a constant 0,
4981 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00004982 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00004983 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004984 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004985 if ((User->getOpcode() != ISD::STORE ||
4986 (isa<ConstantSDNode>(Op.getOperand(1)) &&
4987 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman171c11e2008-04-16 02:32:24 +00004988 (User->getOpcode() != ISD::BIT_CONVERT ||
Owen Anderson825b72b2009-08-11 20:47:22 +00004989 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00004990 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00004991 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
4992 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004993 Op.getOperand(0)),
4994 Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004995 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Extract);
4996 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00004997 // ExtractPS works with constant index.
4998 if (isa<ConstantSDNode>(Op.getOperand(1)))
4999 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00005000 }
Dan Gohman475871a2008-07-27 21:46:04 +00005001 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005002}
5003
5004
Dan Gohman475871a2008-07-27 21:46:04 +00005005SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005006X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
5007 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005008 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00005009 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005010
Evan Cheng62a3f152008-03-24 21:52:23 +00005011 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005012 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005013 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00005014 return Res;
5015 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00005016
Owen Andersone50ed302009-08-10 22:56:29 +00005017 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005018 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005019 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005020 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005021 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005022 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005023 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005024 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
5025 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Scott Michelfdc40a02009-02-17 22:15:04 +00005026 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005027 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00005028 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005029 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00005030 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00005031 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00005032 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00005033 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00005034 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005035 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005036 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005037 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005038 if (Idx == 0)
5039 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00005040
Evan Cheng0db9fe62006-04-25 20:13:52 +00005041 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00005042 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00005043 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00005044 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00005045 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00005046 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00005047 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00005048 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00005049 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
5050 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
5051 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005052 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005053 if (Idx == 0)
5054 return Op;
5055
5056 // UNPCKHPD the element to the lowest double word, then movsd.
5057 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
5058 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00005059 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00005060 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00005061 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00005062 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00005063 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00005064 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005065 }
5066
Dan Gohman475871a2008-07-27 21:46:04 +00005067 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005068}
5069
Dan Gohman475871a2008-07-27 21:46:04 +00005070SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005071X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
5072 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005073 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00005074 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005075 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005076
Dan Gohman475871a2008-07-27 21:46:04 +00005077 SDValue N0 = Op.getOperand(0);
5078 SDValue N1 = Op.getOperand(1);
5079 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00005080
Dan Gohman8a55ce42009-09-23 21:02:20 +00005081 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00005082 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00005083 unsigned Opc;
5084 if (VT == MVT::v8i16)
5085 Opc = X86ISD::PINSRW;
5086 else if (VT == MVT::v4i16)
5087 Opc = X86ISD::MMX_PINSRW;
5088 else if (VT == MVT::v16i8)
5089 Opc = X86ISD::PINSRB;
5090 else
5091 Opc = X86ISD::PINSRB;
5092
Nate Begeman14d12ca2008-02-11 04:19:36 +00005093 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
5094 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00005095 if (N1.getValueType() != MVT::i32)
5096 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
5097 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005098 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00005099 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00005100 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00005101 // Bits [7:6] of the constant are the source select. This will always be
5102 // zero here. The DAG Combiner may combine an extract_elt index into these
5103 // bits. For example (insert (extract, 3), 2) could be matched by putting
5104 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00005105 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00005106 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00005107 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00005108 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005109 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00005110 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00005111 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00005112 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00005113 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00005114 // PINSR* works with constant index.
5115 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00005116 }
Dan Gohman475871a2008-07-27 21:46:04 +00005117 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005118}
5119
Dan Gohman475871a2008-07-27 21:46:04 +00005120SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005121X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005122 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00005123 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005124
5125 if (Subtarget->hasSSE41())
5126 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
5127
Dan Gohman8a55ce42009-09-23 21:02:20 +00005128 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00005129 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00005130
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005131 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005132 SDValue N0 = Op.getOperand(0);
5133 SDValue N1 = Op.getOperand(1);
5134 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00005135
Dan Gohman8a55ce42009-09-23 21:02:20 +00005136 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00005137 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
5138 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00005139 if (N1.getValueType() != MVT::i32)
5140 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
5141 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005142 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00005143 return DAG.getNode(VT == MVT::v8i16 ? X86ISD::PINSRW : X86ISD::MMX_PINSRW,
5144 dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005145 }
Dan Gohman475871a2008-07-27 21:46:04 +00005146 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005147}
5148
Dan Gohman475871a2008-07-27 21:46:04 +00005149SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005150X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005151 DebugLoc dl = Op.getDebugLoc();
Chris Lattnerf172ecd2010-07-04 23:07:25 +00005152
5153 if (Op.getValueType() == MVT::v1i64 &&
5154 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00005155 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00005156
Owen Anderson825b72b2009-08-11 20:47:22 +00005157 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
5158 EVT VT = MVT::v2i32;
5159 switch (Op.getValueType().getSimpleVT().SimpleTy) {
Evan Chengefec7512008-02-18 23:04:32 +00005160 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00005161 case MVT::v16i8:
5162 case MVT::v8i16:
5163 VT = MVT::v4i32;
Evan Chengefec7512008-02-18 23:04:32 +00005164 break;
5165 }
Dale Johannesenace16102009-02-03 19:33:06 +00005166 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(),
5167 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005168}
5169
Bill Wendling056292f2008-09-16 21:48:12 +00005170// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
5171// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
5172// one of the above mentioned nodes. It has to be wrapped because otherwise
5173// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
5174// be used to form addressing mode. These wrapped nodes will be selected
5175// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00005176SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005177X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005178 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00005179
Chris Lattner41621a22009-06-26 19:22:52 +00005180 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5181 // global base reg.
5182 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005183 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005184 CodeModel::Model M = getTargetMachine().getCodeModel();
5185
Chris Lattner4f066492009-07-11 20:29:19 +00005186 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005187 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005188 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005189 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005190 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005191 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005192 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005193
Evan Cheng1606e8e2009-03-13 07:51:59 +00005194 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00005195 CP->getAlignment(),
5196 CP->getOffset(), OpFlag);
5197 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00005198 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005199 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00005200 if (OpFlag) {
5201 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005202 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005203 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005204 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005205 }
5206
5207 return Result;
5208}
5209
Dan Gohmand858e902010-04-17 15:26:15 +00005210SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00005211 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00005212
Chris Lattner18c59872009-06-27 04:16:01 +00005213 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5214 // global base reg.
5215 unsigned char OpFlag = 0;
5216 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005217 CodeModel::Model M = getTargetMachine().getCodeModel();
5218
Chris Lattner4f066492009-07-11 20:29:19 +00005219 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005220 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005221 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005222 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005223 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005224 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005225 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005226
Chris Lattner18c59872009-06-27 04:16:01 +00005227 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
5228 OpFlag);
5229 DebugLoc DL = JT->getDebugLoc();
5230 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005231
Chris Lattner18c59872009-06-27 04:16:01 +00005232 // With PIC, the address is actually $g + Offset.
5233 if (OpFlag) {
5234 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5235 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005236 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00005237 Result);
5238 }
Eric Christopherfd179292009-08-27 18:07:15 +00005239
Chris Lattner18c59872009-06-27 04:16:01 +00005240 return Result;
5241}
5242
5243SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005244X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00005245 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00005246
Chris Lattner18c59872009-06-27 04:16:01 +00005247 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5248 // global base reg.
5249 unsigned char OpFlag = 0;
5250 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005251 CodeModel::Model M = getTargetMachine().getCodeModel();
5252
Chris Lattner4f066492009-07-11 20:29:19 +00005253 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005254 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005255 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005256 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005257 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005258 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005259 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005260
Chris Lattner18c59872009-06-27 04:16:01 +00005261 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00005262
Chris Lattner18c59872009-06-27 04:16:01 +00005263 DebugLoc DL = Op.getDebugLoc();
5264 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005265
5266
Chris Lattner18c59872009-06-27 04:16:01 +00005267 // With PIC, the address is actually $g + Offset.
5268 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00005269 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00005270 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5271 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005272 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00005273 Result);
5274 }
Eric Christopherfd179292009-08-27 18:07:15 +00005275
Chris Lattner18c59872009-06-27 04:16:01 +00005276 return Result;
5277}
5278
Dan Gohman475871a2008-07-27 21:46:04 +00005279SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005280X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00005281 // Create the TargetBlockAddressAddress node.
5282 unsigned char OpFlags =
5283 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00005284 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00005285 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00005286 DebugLoc dl = Op.getDebugLoc();
5287 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
5288 /*isTarget=*/true, OpFlags);
5289
Dan Gohmanf705adb2009-10-30 01:28:02 +00005290 if (Subtarget->isPICStyleRIPRel() &&
5291 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00005292 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
5293 else
5294 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00005295
Dan Gohman29cbade2009-11-20 23:18:13 +00005296 // With PIC, the address is actually $g + Offset.
5297 if (isGlobalRelativeToPICBase(OpFlags)) {
5298 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5299 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
5300 Result);
5301 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00005302
5303 return Result;
5304}
5305
5306SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00005307X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00005308 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00005309 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00005310 // Create the TargetGlobalAddress node, folding in the constant
5311 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00005312 unsigned char OpFlags =
5313 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005314 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00005315 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005316 if (OpFlags == X86II::MO_NO_FLAG &&
5317 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00005318 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00005319 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00005320 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005321 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00005322 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00005323 }
Eric Christopherfd179292009-08-27 18:07:15 +00005324
Chris Lattner4f066492009-07-11 20:29:19 +00005325 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005326 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00005327 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
5328 else
5329 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00005330
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005331 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00005332 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00005333 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5334 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005335 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005336 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005337
Chris Lattner36c25012009-07-10 07:34:39 +00005338 // For globals that require a load from a stub to get the address, emit the
5339 // load.
5340 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00005341 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
David Greene67c9d422010-02-15 16:53:33 +00005342 PseudoSourceValue::getGOT(), 0, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005343
Dan Gohman6520e202008-10-18 02:06:02 +00005344 // If there was a non-zero offset that we didn't fold, create an explicit
5345 // addition for it.
5346 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00005347 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00005348 DAG.getConstant(Offset, getPointerTy()));
5349
Evan Cheng0db9fe62006-04-25 20:13:52 +00005350 return Result;
5351}
5352
Evan Chengda43bcf2008-09-24 00:05:32 +00005353SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005354X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00005355 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00005356 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005357 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00005358}
5359
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005360static SDValue
5361GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00005362 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00005363 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00005364 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Owen Anderson825b72b2009-08-11 20:47:22 +00005365 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005366 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00005367 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005368 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00005369 GA->getOffset(),
5370 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005371 if (InFlag) {
5372 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00005373 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005374 } else {
5375 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00005376 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005377 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00005378
5379 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00005380 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00005381
Rafael Espindola15f1b662009-04-24 12:59:40 +00005382 SDValue Flag = Chain.getValue(1);
5383 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005384}
5385
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005386// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00005387static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005388LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005389 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005390 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00005391 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
5392 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005393 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005394 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005395 InFlag = Chain.getValue(1);
5396
Chris Lattnerb903bed2009-06-26 21:20:29 +00005397 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005398}
5399
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005400// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00005401static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005402LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005403 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00005404 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
5405 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005406}
5407
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005408// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
5409// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00005410static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005411 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00005412 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00005413 DebugLoc dl = GA->getDebugLoc();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005414 // Get the Thread Pointer
Rafael Espindola094fad32009-04-08 21:14:34 +00005415 SDValue Base = DAG.getNode(X86ISD::SegmentBaseAddress,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005416 DebugLoc(), PtrVT,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00005417 DAG.getRegister(is64Bit? X86::FS : X86::GS,
Owen Anderson825b72b2009-08-11 20:47:22 +00005418 MVT::i32));
Rafael Espindola094fad32009-04-08 21:14:34 +00005419
5420 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Base,
David Greene67c9d422010-02-15 16:53:33 +00005421 NULL, 0, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00005422
Chris Lattnerb903bed2009-06-26 21:20:29 +00005423 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005424 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
5425 // initialexec.
5426 unsigned WrapperKind = X86ISD::Wrapper;
5427 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00005428 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00005429 } else if (is64Bit) {
5430 assert(model == TLSModel::InitialExec);
5431 OperandFlags = X86II::MO_GOTTPOFF;
5432 WrapperKind = X86ISD::WrapperRIP;
5433 } else {
5434 assert(model == TLSModel::InitialExec);
5435 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00005436 }
Eric Christopherfd179292009-08-27 18:07:15 +00005437
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005438 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
5439 // exec)
Devang Patel0d881da2010-07-06 22:08:15 +00005440 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
5441 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00005442 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00005443 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00005444
Rafael Espindola9a580232009-02-27 13:37:18 +00005445 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00005446 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
David Greene67c9d422010-02-15 16:53:33 +00005447 PseudoSourceValue::getGOT(), 0, false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00005448
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005449 // The address of the thread local variable is the add of the thread
5450 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00005451 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005452}
5453
Dan Gohman475871a2008-07-27 21:46:04 +00005454SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005455X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Eric Christopher30ef0e52010-06-03 04:07:48 +00005456
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005457 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00005458 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00005459
Eric Christopher30ef0e52010-06-03 04:07:48 +00005460 if (Subtarget->isTargetELF()) {
5461 // TODO: implement the "local dynamic" model
5462 // TODO: implement the "initial exec"model for pic executables
5463
5464 // If GV is an alias then use the aliasee for determining
5465 // thread-localness.
5466 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
5467 GV = GA->resolveAliasedGlobal(false);
5468
5469 TLSModel::Model model
5470 = getTLSModel(GV, getTargetMachine().getRelocationModel());
5471
5472 switch (model) {
5473 case TLSModel::GeneralDynamic:
5474 case TLSModel::LocalDynamic: // not implemented
5475 if (Subtarget->is64Bit())
5476 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
5477 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
5478
5479 case TLSModel::InitialExec:
5480 case TLSModel::LocalExec:
5481 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
5482 Subtarget->is64Bit());
5483 }
5484 } else if (Subtarget->isTargetDarwin()) {
5485 // Darwin only has one model of TLS. Lower to that.
5486 unsigned char OpFlag = 0;
5487 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
5488 X86ISD::WrapperRIP : X86ISD::Wrapper;
5489
5490 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5491 // global base reg.
5492 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
5493 !Subtarget->is64Bit();
5494 if (PIC32)
5495 OpFlag = X86II::MO_TLVP_PIC_BASE;
5496 else
5497 OpFlag = X86II::MO_TLVP;
Devang Patel0d881da2010-07-06 22:08:15 +00005498 DebugLoc DL = Op.getDebugLoc();
5499 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopher30ef0e52010-06-03 04:07:48 +00005500 getPointerTy(),
5501 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00005502 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
5503
5504 // With PIC32, the address is actually $g + Offset.
5505 if (PIC32)
5506 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5507 DAG.getNode(X86ISD::GlobalBaseReg,
5508 DebugLoc(), getPointerTy()),
5509 Offset);
5510
5511 // Lowering the machine isd will make sure everything is in the right
5512 // location.
5513 SDValue Args[] = { Offset };
5514 SDValue Chain = DAG.getNode(X86ISD::TLSCALL, DL, MVT::Other, Args, 1);
5515
5516 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
5517 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
5518 MFI->setAdjustsStack(true);
Eric Christopherfd179292009-08-27 18:07:15 +00005519
Eric Christopher30ef0e52010-06-03 04:07:48 +00005520 // And our return value (tls address) is in the standard call return value
5521 // location.
5522 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
5523 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005524 }
Eric Christopher30ef0e52010-06-03 04:07:48 +00005525
5526 assert(false &&
5527 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00005528
Torok Edwinc23197a2009-07-14 16:55:14 +00005529 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00005530 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005531}
5532
Evan Cheng0db9fe62006-04-25 20:13:52 +00005533
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005534/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00005535/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00005536SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00005537 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00005538 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005539 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005540 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005541 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00005542 SDValue ShOpLo = Op.getOperand(0);
5543 SDValue ShOpHi = Op.getOperand(1);
5544 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00005545 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00005546 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00005547 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00005548
Dan Gohman475871a2008-07-27 21:46:04 +00005549 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005550 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00005551 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
5552 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005553 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005554 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
5555 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005556 }
Evan Chenge3413162006-01-09 18:33:28 +00005557
Owen Anderson825b72b2009-08-11 20:47:22 +00005558 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
5559 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00005560 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00005561 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00005562
Dan Gohman475871a2008-07-27 21:46:04 +00005563 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00005564 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00005565 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
5566 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00005567
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005568 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00005569 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
5570 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005571 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005572 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
5573 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005574 }
5575
Dan Gohman475871a2008-07-27 21:46:04 +00005576 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00005577 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005578}
Evan Chenga3195e82006-01-12 22:54:21 +00005579
Dan Gohmand858e902010-04-17 15:26:15 +00005580SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
5581 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005582 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00005583
5584 if (SrcVT.isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005585 if (SrcVT == MVT::v2i32 && Op.getValueType() == MVT::v2f64) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005586 return Op;
5587 }
5588 return SDValue();
5589 }
5590
Owen Anderson825b72b2009-08-11 20:47:22 +00005591 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00005592 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00005593
Eli Friedman36df4992009-05-27 00:47:34 +00005594 // These are really Legal; return the operand so the caller accepts it as
5595 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00005596 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00005597 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00005598 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00005599 Subtarget->is64Bit()) {
5600 return Op;
5601 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005602
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005603 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005604 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005605 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005606 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005607 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00005608 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00005609 StackSlot,
David Greene67c9d422010-02-15 16:53:33 +00005610 PseudoSourceValue::getFixedStack(SSFI), 0,
5611 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00005612 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
5613}
Evan Cheng0db9fe62006-04-25 20:13:52 +00005614
Owen Andersone50ed302009-08-10 22:56:29 +00005615SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Dale Johannesen8d908eb2010-05-15 18:51:12 +00005616 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00005617 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005618 // Build the FILD
Eli Friedman948e95a2009-05-23 09:59:16 +00005619 DebugLoc dl = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00005620 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00005621 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005622 if (useSSE)
Owen Anderson825b72b2009-08-11 20:47:22 +00005623 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00005624 else
Owen Anderson825b72b2009-08-11 20:47:22 +00005625 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005626 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Dale Johannesenace16102009-02-03 19:33:06 +00005627 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD, dl,
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005628 Tys, Ops, array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005629
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005630 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005631 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00005632 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005633
5634 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
5635 // shouldn't be necessary except that RFP cannot be live across
5636 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005637 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005638 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005639 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00005640 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005641 SDValue Ops[] = {
5642 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
5643 };
5644 Chain = DAG.getNode(X86ISD::FST, dl, Tys, Ops, array_lengthof(Ops));
Dale Johannesenace16102009-02-03 19:33:06 +00005645 Result = DAG.getLoad(Op.getValueType(), dl, Chain, StackSlot,
David Greene67c9d422010-02-15 16:53:33 +00005646 PseudoSourceValue::getFixedStack(SSFI), 0,
5647 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005648 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005649
Evan Cheng0db9fe62006-04-25 20:13:52 +00005650 return Result;
5651}
5652
Bill Wendling8b8a6362009-01-17 03:56:04 +00005653// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00005654SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
5655 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00005656 // This algorithm is not obvious. Here it is in C code, more or less:
5657 /*
5658 double uint64_to_double( uint32_t hi, uint32_t lo ) {
5659 static const __m128i exp = { 0x4330000045300000ULL, 0 };
5660 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00005661
Bill Wendling8b8a6362009-01-17 03:56:04 +00005662 // Copy ints to xmm registers.
5663 __m128i xh = _mm_cvtsi32_si128( hi );
5664 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00005665
Bill Wendling8b8a6362009-01-17 03:56:04 +00005666 // Combine into low half of a single xmm register.
5667 __m128i x = _mm_unpacklo_epi32( xh, xl );
5668 __m128d d;
5669 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00005670
Bill Wendling8b8a6362009-01-17 03:56:04 +00005671 // Merge in appropriate exponents to give the integer bits the right
5672 // magnitude.
5673 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00005674
Bill Wendling8b8a6362009-01-17 03:56:04 +00005675 // Subtract away the biases to deal with the IEEE-754 double precision
5676 // implicit 1.
5677 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00005678
Bill Wendling8b8a6362009-01-17 03:56:04 +00005679 // All conversions up to here are exact. The correctly rounded result is
5680 // calculated using the current rounding mode using the following
5681 // horizontal add.
5682 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
5683 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
5684 // store doesn't really need to be here (except
5685 // maybe to zero the other double)
5686 return sd;
5687 }
5688 */
Dale Johannesen040225f2008-10-21 23:07:49 +00005689
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005690 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00005691 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00005692
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005693 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00005694 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00005695 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
5696 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
5697 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
5698 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00005699 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005700 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005701
Bill Wendling8b8a6362009-01-17 03:56:04 +00005702 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00005703 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005704 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00005705 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005706 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00005707 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005708 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005709
Owen Anderson825b72b2009-08-11 20:47:22 +00005710 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5711 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00005712 Op.getOperand(0),
5713 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005714 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5715 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00005716 Op.getOperand(0),
5717 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005718 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
5719 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005720 PseudoSourceValue::getConstantPool(), 0,
David Greene67c9d422010-02-15 16:53:33 +00005721 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00005722 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
5723 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Unpck2);
5724 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005725 PseudoSourceValue::getConstantPool(), 0,
David Greene67c9d422010-02-15 16:53:33 +00005726 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00005727 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005728
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005729 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00005730 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00005731 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
5732 DAG.getUNDEF(MVT::v2f64), ShufMask);
5733 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
5734 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005735 DAG.getIntPtrConstant(0));
5736}
5737
Bill Wendling8b8a6362009-01-17 03:56:04 +00005738// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00005739SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
5740 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005741 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00005742 // FP constant to bias correct the final result.
5743 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00005744 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005745
5746 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00005747 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5748 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005749 Op.getOperand(0),
5750 DAG.getIntPtrConstant(0)));
5751
Owen Anderson825b72b2009-08-11 20:47:22 +00005752 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5753 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00005754 DAG.getIntPtrConstant(0));
5755
5756 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00005757 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
5758 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005759 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005760 MVT::v2f64, Load)),
5761 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005762 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005763 MVT::v2f64, Bias)));
5764 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5765 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00005766 DAG.getIntPtrConstant(0));
5767
5768 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00005769 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005770
5771 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00005772 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00005773
Owen Anderson825b72b2009-08-11 20:47:22 +00005774 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005775 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00005776 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00005777 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005778 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00005779 }
5780
5781 // Handle final rounding.
5782 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00005783}
5784
Dan Gohmand858e902010-04-17 15:26:15 +00005785SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
5786 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00005787 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005788 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00005789
Dale Johannesen8d908eb2010-05-15 18:51:12 +00005790 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00005791 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
5792 // the optimization here.
5793 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00005794 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00005795
Owen Andersone50ed302009-08-10 22:56:29 +00005796 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00005797 EVT DstVT = Op.getValueType();
5798 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00005799 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00005800 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00005801 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00005802
5803 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00005804 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00005805 if (SrcVT == MVT::i32) {
5806 SDValue WordOff = DAG.getConstant(4, getPointerTy());
5807 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
5808 getPointerTy(), StackSlot, WordOff);
5809 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
5810 StackSlot, NULL, 0, false, false, 0);
5811 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
5812 OffsetSlot, NULL, 0, false, false, 0);
5813 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
5814 return Fild;
5815 }
5816
5817 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
5818 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
David Greene67c9d422010-02-15 16:53:33 +00005819 StackSlot, NULL, 0, false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00005820 // For i64 source, we need to add the appropriate power of 2 if the input
5821 // was negative. This is the same as the optimization in
5822 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
5823 // we must be careful to do the computation in x87 extended precision, not
5824 // in SSE. (The generic code can't know it's OK to do this, or how to.)
5825 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
5826 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
5827 SDValue Fild = DAG.getNode(X86ISD::FILD, dl, Tys, Ops, 3);
5828
5829 APInt FF(32, 0x5F800000ULL);
5830
5831 // Check whether the sign bit is set.
5832 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
5833 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
5834 ISD::SETLT);
5835
5836 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
5837 SDValue FudgePtr = DAG.getConstantPool(
5838 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
5839 getPointerTy());
5840
5841 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
5842 SDValue Zero = DAG.getIntPtrConstant(0);
5843 SDValue Four = DAG.getIntPtrConstant(4);
5844 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
5845 Zero, Four);
5846 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
5847
5848 // Load the value out, extending it from f32 to f80.
5849 // FIXME: Avoid the extend by constructing the right constant pool?
Evan Chengbcc80172010-07-07 22:15:37 +00005850 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, MVT::f80, dl, DAG.getEntryNode(),
Dale Johannesen8d908eb2010-05-15 18:51:12 +00005851 FudgePtr, PseudoSourceValue::getConstantPool(),
5852 0, MVT::f32, false, false, 4);
5853 // Extend everything to 80 bits to force it to be done on x87.
5854 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
5855 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00005856}
5857
Dan Gohman475871a2008-07-27 21:46:04 +00005858std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00005859FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005860 DebugLoc dl = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00005861
Owen Andersone50ed302009-08-10 22:56:29 +00005862 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00005863
5864 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005865 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
5866 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00005867 }
5868
Owen Anderson825b72b2009-08-11 20:47:22 +00005869 assert(DstTy.getSimpleVT() <= MVT::i64 &&
5870 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00005871 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00005872
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005873 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00005874 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00005875 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005876 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00005877 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00005878 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00005879 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005880 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005881
Evan Cheng87c89352007-10-15 20:11:21 +00005882 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
5883 // stack slot.
5884 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00005885 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00005886 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005887 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00005888
Evan Cheng0db9fe62006-04-25 20:13:52 +00005889 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00005890 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005891 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005892 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
5893 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
5894 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005895 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005896
Dan Gohman475871a2008-07-27 21:46:04 +00005897 SDValue Chain = DAG.getEntryNode();
5898 SDValue Value = Op.getOperand(0);
Chris Lattner78631162008-01-16 06:24:21 +00005899 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005900 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dale Johannesenace16102009-02-03 19:33:06 +00005901 Chain = DAG.getStore(Chain, dl, Value, StackSlot,
David Greene67c9d422010-02-15 16:53:33 +00005902 PseudoSourceValue::getFixedStack(SSFI), 0,
5903 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005904 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00005905 SDValue Ops[] = {
Chris Lattner5a88b832007-02-25 07:10:00 +00005906 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
5907 };
Dale Johannesenace16102009-02-03 19:33:06 +00005908 Value = DAG.getNode(X86ISD::FLD, dl, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005909 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00005910 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005911 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5912 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005913
Evan Cheng0db9fe62006-04-25 20:13:52 +00005914 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00005915 SDValue Ops[] = { Chain, Value, StackSlot };
Owen Anderson825b72b2009-08-11 20:47:22 +00005916 SDValue FIST = DAG.getNode(Opc, dl, MVT::Other, Ops, 3);
Evan Chengd9558e02006-01-06 00:43:03 +00005917
Chris Lattner27a6c732007-11-24 07:07:01 +00005918 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005919}
5920
Dan Gohmand858e902010-04-17 15:26:15 +00005921SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
5922 SelectionDAG &DAG) const {
Eli Friedman23ef1052009-06-06 03:57:58 +00005923 if (Op.getValueType().isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005924 if (Op.getValueType() == MVT::v2i32 &&
5925 Op.getOperand(0).getValueType() == MVT::v2f64) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005926 return Op;
5927 }
5928 return SDValue();
5929 }
5930
Eli Friedman948e95a2009-05-23 09:59:16 +00005931 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00005932 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00005933 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
5934 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00005935
Chris Lattner27a6c732007-11-24 07:07:01 +00005936 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005937 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
David Greene67c9d422010-02-15 16:53:33 +00005938 FIST, StackSlot, NULL, 0, false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00005939}
5940
Dan Gohmand858e902010-04-17 15:26:15 +00005941SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
5942 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00005943 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
5944 SDValue FIST = Vals.first, StackSlot = Vals.second;
5945 assert(FIST.getNode() && "Unexpected failure");
5946
5947 // Load the result.
5948 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
David Greene67c9d422010-02-15 16:53:33 +00005949 FIST, StackSlot, NULL, 0, false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00005950}
5951
Dan Gohmand858e902010-04-17 15:26:15 +00005952SDValue X86TargetLowering::LowerFABS(SDValue Op,
5953 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005954 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005955 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005956 EVT VT = Op.getValueType();
5957 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005958 if (VT.isVector())
5959 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005960 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005961 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005962 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00005963 CV.push_back(C);
5964 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005965 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005966 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00005967 CV.push_back(C);
5968 CV.push_back(C);
5969 CV.push_back(C);
5970 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005971 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005972 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005973 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005974 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005975 PseudoSourceValue::getConstantPool(), 0,
5976 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005977 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005978}
5979
Dan Gohmand858e902010-04-17 15:26:15 +00005980SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005981 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005982 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005983 EVT VT = Op.getValueType();
5984 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00005985 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00005986 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005987 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005988 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005989 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00005990 CV.push_back(C);
5991 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005992 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005993 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00005994 CV.push_back(C);
5995 CV.push_back(C);
5996 CV.push_back(C);
5997 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005998 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005999 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006000 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006001 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00006002 PseudoSourceValue::getConstantPool(), 0,
6003 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006004 if (VT.isVector()) {
Dale Johannesenace16102009-02-03 19:33:06 +00006005 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00006006 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
6007 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006008 Op.getOperand(0)),
Owen Anderson825b72b2009-08-11 20:47:22 +00006009 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00006010 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006011 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00006012 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006013}
6014
Dan Gohmand858e902010-04-17 15:26:15 +00006015SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00006016 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00006017 SDValue Op0 = Op.getOperand(0);
6018 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006019 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006020 EVT VT = Op.getValueType();
6021 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00006022
6023 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00006024 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006025 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00006026 SrcVT = VT;
6027 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00006028 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00006029 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006030 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00006031 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00006032 }
6033
6034 // At this point the operands and the result should have the same
6035 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00006036
Evan Cheng68c47cb2007-01-05 07:55:56 +00006037 // First get the sign bit of second operand.
6038 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00006039 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006040 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
6041 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00006042 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006043 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
6044 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6045 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6046 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00006047 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006048 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006049 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006050 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00006051 PseudoSourceValue::getConstantPool(), 0,
6052 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006053 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00006054
6055 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00006056 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006057 // Op0 is MVT::f32, Op1 is MVT::f64.
6058 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
6059 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
6060 DAG.getConstant(32, MVT::i32));
6061 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, SignBit);
6062 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00006063 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00006064 }
6065
Evan Cheng73d6cf12007-01-05 21:37:56 +00006066 // Clear first operand sign bit.
6067 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00006068 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006069 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
6070 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00006071 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006072 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
6073 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6074 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6075 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00006076 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006077 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006078 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006079 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00006080 PseudoSourceValue::getConstantPool(), 0,
6081 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006082 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00006083
6084 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00006085 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00006086}
6087
Dan Gohman076aee32009-03-04 19:44:21 +00006088/// Emit nodes that will be selected as "test Op0,Op0", or something
6089/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00006090SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00006091 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00006092 DebugLoc dl = Op.getDebugLoc();
6093
Dan Gohman31125812009-03-07 01:58:32 +00006094 // CF and OF aren't always set the way we want. Determine which
6095 // of these we need.
6096 bool NeedCF = false;
6097 bool NeedOF = false;
6098 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006099 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00006100 case X86::COND_A: case X86::COND_AE:
6101 case X86::COND_B: case X86::COND_BE:
6102 NeedCF = true;
6103 break;
6104 case X86::COND_G: case X86::COND_GE:
6105 case X86::COND_L: case X86::COND_LE:
6106 case X86::COND_O: case X86::COND_NO:
6107 NeedOF = true;
6108 break;
Dan Gohman31125812009-03-07 01:58:32 +00006109 }
6110
Dan Gohman076aee32009-03-04 19:44:21 +00006111 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00006112 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
6113 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006114 if (Op.getResNo() != 0 || NeedOF || NeedCF)
6115 // Emit a CMP with 0, which is the TEST pattern.
6116 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
6117 DAG.getConstant(0, Op.getValueType()));
6118
6119 unsigned Opcode = 0;
6120 unsigned NumOperands = 0;
6121 switch (Op.getNode()->getOpcode()) {
6122 case ISD::ADD:
6123 // Due to an isel shortcoming, be conservative if this add is likely to be
6124 // selected as part of a load-modify-store instruction. When the root node
6125 // in a match is a store, isel doesn't know how to remap non-chain non-flag
6126 // uses of other nodes in the match, such as the ADD in this case. This
6127 // leads to the ADD being left around and reselected, with the result being
6128 // two adds in the output. Alas, even if none our users are stores, that
6129 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
6130 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
6131 // climbing the DAG back to the root, and it doesn't seem to be worth the
6132 // effort.
6133 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00006134 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006135 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
6136 goto default_case;
6137
6138 if (ConstantSDNode *C =
6139 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
6140 // An add of one will be selected as an INC.
6141 if (C->getAPIntValue() == 1) {
6142 Opcode = X86ISD::INC;
6143 NumOperands = 1;
6144 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00006145 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006146
6147 // An add of negative one (subtract of one) will be selected as a DEC.
6148 if (C->getAPIntValue().isAllOnesValue()) {
6149 Opcode = X86ISD::DEC;
6150 NumOperands = 1;
6151 break;
6152 }
Dan Gohman076aee32009-03-04 19:44:21 +00006153 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006154
6155 // Otherwise use a regular EFLAGS-setting add.
6156 Opcode = X86ISD::ADD;
6157 NumOperands = 2;
6158 break;
6159 case ISD::AND: {
6160 // If the primary and result isn't used, don't bother using X86ISD::AND,
6161 // because a TEST instruction will be better.
6162 bool NonFlagUse = false;
6163 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
6164 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
6165 SDNode *User = *UI;
6166 unsigned UOpNo = UI.getOperandNo();
6167 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
6168 // Look pass truncate.
6169 UOpNo = User->use_begin().getOperandNo();
6170 User = *User->use_begin();
6171 }
6172
6173 if (User->getOpcode() != ISD::BRCOND &&
6174 User->getOpcode() != ISD::SETCC &&
6175 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
6176 NonFlagUse = true;
6177 break;
6178 }
Dan Gohman076aee32009-03-04 19:44:21 +00006179 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006180
6181 if (!NonFlagUse)
6182 break;
6183 }
6184 // FALL THROUGH
6185 case ISD::SUB:
6186 case ISD::OR:
6187 case ISD::XOR:
6188 // Due to the ISEL shortcoming noted above, be conservative if this op is
6189 // likely to be selected as part of a load-modify-store instruction.
6190 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
6191 UE = Op.getNode()->use_end(); UI != UE; ++UI)
6192 if (UI->getOpcode() == ISD::STORE)
6193 goto default_case;
6194
6195 // Otherwise use a regular EFLAGS-setting instruction.
6196 switch (Op.getNode()->getOpcode()) {
6197 default: llvm_unreachable("unexpected operator!");
6198 case ISD::SUB: Opcode = X86ISD::SUB; break;
6199 case ISD::OR: Opcode = X86ISD::OR; break;
6200 case ISD::XOR: Opcode = X86ISD::XOR; break;
6201 case ISD::AND: Opcode = X86ISD::AND; break;
6202 }
6203
6204 NumOperands = 2;
6205 break;
6206 case X86ISD::ADD:
6207 case X86ISD::SUB:
6208 case X86ISD::INC:
6209 case X86ISD::DEC:
6210 case X86ISD::OR:
6211 case X86ISD::XOR:
6212 case X86ISD::AND:
6213 return SDValue(Op.getNode(), 1);
6214 default:
6215 default_case:
6216 break;
Dan Gohman076aee32009-03-04 19:44:21 +00006217 }
6218
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006219 if (Opcode == 0)
6220 // Emit a CMP with 0, which is the TEST pattern.
6221 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
6222 DAG.getConstant(0, Op.getValueType()));
6223
6224 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
6225 SmallVector<SDValue, 4> Ops;
6226 for (unsigned i = 0; i != NumOperands; ++i)
6227 Ops.push_back(Op.getOperand(i));
6228
6229 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
6230 DAG.ReplaceAllUsesWith(Op, New);
6231 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00006232}
6233
6234/// Emit nodes that will be selected as "cmp Op0,Op1", or something
6235/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00006236SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00006237 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00006238 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
6239 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00006240 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00006241
6242 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00006243 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00006244}
6245
Evan Chengd40d03e2010-01-06 19:38:29 +00006246/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
6247/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00006248SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
6249 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00006250 SDValue Op0 = And.getOperand(0);
6251 SDValue Op1 = And.getOperand(1);
6252 if (Op0.getOpcode() == ISD::TRUNCATE)
6253 Op0 = Op0.getOperand(0);
6254 if (Op1.getOpcode() == ISD::TRUNCATE)
6255 Op1 = Op1.getOperand(0);
6256
Evan Chengd40d03e2010-01-06 19:38:29 +00006257 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00006258 if (Op1.getOpcode() == ISD::SHL)
6259 std::swap(Op0, Op1);
6260 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00006261 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
6262 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00006263 // If we looked past a truncate, check that it's only truncating away
6264 // known zeros.
6265 unsigned BitWidth = Op0.getValueSizeInBits();
6266 unsigned AndBitWidth = And.getValueSizeInBits();
6267 if (BitWidth > AndBitWidth) {
6268 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
6269 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
6270 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
6271 return SDValue();
6272 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00006273 LHS = Op1;
6274 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00006275 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00006276 } else if (Op1.getOpcode() == ISD::Constant) {
6277 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
6278 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00006279 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
6280 LHS = AndLHS.getOperand(0);
6281 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00006282 }
Evan Chengd40d03e2010-01-06 19:38:29 +00006283 }
Evan Cheng0488db92007-09-25 01:57:46 +00006284
Evan Chengd40d03e2010-01-06 19:38:29 +00006285 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00006286 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00006287 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00006288 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00006289 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00006290 // Also promote i16 to i32 for performance / code size reason.
6291 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00006292 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00006293 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00006294
Evan Chengd40d03e2010-01-06 19:38:29 +00006295 // If the operand types disagree, extend the shift amount to match. Since
6296 // BT ignores high bits (like shifts) we can use anyextend.
6297 if (LHS.getValueType() != RHS.getValueType())
6298 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00006299
Evan Chengd40d03e2010-01-06 19:38:29 +00006300 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
6301 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
6302 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6303 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00006304 }
6305
Evan Cheng54de3ea2010-01-05 06:52:31 +00006306 return SDValue();
6307}
6308
Dan Gohmand858e902010-04-17 15:26:15 +00006309SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng54de3ea2010-01-05 06:52:31 +00006310 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
6311 SDValue Op0 = Op.getOperand(0);
6312 SDValue Op1 = Op.getOperand(1);
6313 DebugLoc dl = Op.getDebugLoc();
6314 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
6315
6316 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00006317 // Lower (X & (1 << N)) == 0 to BT(X, N).
6318 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
6319 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
6320 if (Op0.getOpcode() == ISD::AND &&
6321 Op0.hasOneUse() &&
6322 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00006323 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00006324 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
6325 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
6326 if (NewSetCC.getNode())
6327 return NewSetCC;
6328 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00006329
Evan Cheng2c755ba2010-02-27 07:36:59 +00006330 // Look for "(setcc) == / != 1" to avoid unncessary setcc.
6331 if (Op0.getOpcode() == X86ISD::SETCC &&
6332 Op1.getOpcode() == ISD::Constant &&
6333 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
6334 cast<ConstantSDNode>(Op1)->isNullValue()) &&
6335 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
6336 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
6337 bool Invert = (CC == ISD::SETNE) ^
6338 cast<ConstantSDNode>(Op1)->isNullValue();
6339 if (Invert)
6340 CCode = X86::GetOppositeBranchCondition(CCode);
6341 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6342 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
6343 }
6344
Evan Chenge5b51ac2010-04-17 06:13:15 +00006345 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00006346 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00006347 if (X86CC == X86::COND_INVALID)
6348 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00006349
Evan Cheng552f09a2010-04-26 19:06:11 +00006350 SDValue Cond = EmitCmp(Op0, Op1, X86CC, DAG);
Evan Chengad9c0a32009-12-15 00:53:42 +00006351
6352 // Use sbb x, x to materialize carry bit into a GPR.
Evan Cheng2e489c42009-12-16 00:53:11 +00006353 if (X86CC == X86::COND_B)
Evan Chengad9c0a32009-12-15 00:53:42 +00006354 return DAG.getNode(ISD::AND, dl, MVT::i8,
6355 DAG.getNode(X86ISD::SETCC_CARRY, dl, MVT::i8,
6356 DAG.getConstant(X86CC, MVT::i8), Cond),
6357 DAG.getConstant(1, MVT::i8));
Evan Chengad9c0a32009-12-15 00:53:42 +00006358
Owen Anderson825b72b2009-08-11 20:47:22 +00006359 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6360 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00006361}
6362
Dan Gohmand858e902010-04-17 15:26:15 +00006363SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00006364 SDValue Cond;
6365 SDValue Op0 = Op.getOperand(0);
6366 SDValue Op1 = Op.getOperand(1);
6367 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00006368 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00006369 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
6370 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006371 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00006372
6373 if (isFP) {
6374 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00006375 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00006376 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
6377 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00006378 bool Swap = false;
6379
6380 switch (SetCCOpcode) {
6381 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00006382 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00006383 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00006384 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00006385 case ISD::SETGT: Swap = true; // Fallthrough
6386 case ISD::SETLT:
6387 case ISD::SETOLT: SSECC = 1; break;
6388 case ISD::SETOGE:
6389 case ISD::SETGE: Swap = true; // Fallthrough
6390 case ISD::SETLE:
6391 case ISD::SETOLE: SSECC = 2; break;
6392 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00006393 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00006394 case ISD::SETNE: SSECC = 4; break;
6395 case ISD::SETULE: Swap = true;
6396 case ISD::SETUGE: SSECC = 5; break;
6397 case ISD::SETULT: Swap = true;
6398 case ISD::SETUGT: SSECC = 6; break;
6399 case ISD::SETO: SSECC = 7; break;
6400 }
6401 if (Swap)
6402 std::swap(Op0, Op1);
6403
Nate Begemanfb8ead02008-07-25 19:05:58 +00006404 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00006405 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00006406 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00006407 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00006408 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
6409 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00006410 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00006411 }
6412 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00006413 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00006414 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
6415 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00006416 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00006417 }
Torok Edwinc23197a2009-07-14 16:55:14 +00006418 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00006419 }
6420 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00006421 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00006422 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006423
Nate Begeman30a0de92008-07-17 16:51:19 +00006424 // We are handling one of the integer comparisons here. Since SSE only has
6425 // GT and EQ comparisons for integer, swapping operands and multiple
6426 // operations may be required for some comparisons.
6427 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
6428 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00006429
Owen Anderson825b72b2009-08-11 20:47:22 +00006430 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00006431 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00006432 case MVT::v8i8:
6433 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
6434 case MVT::v4i16:
6435 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
6436 case MVT::v2i32:
6437 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
6438 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00006439 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006440
Nate Begeman30a0de92008-07-17 16:51:19 +00006441 switch (SetCCOpcode) {
6442 default: break;
6443 case ISD::SETNE: Invert = true;
6444 case ISD::SETEQ: Opc = EQOpc; break;
6445 case ISD::SETLT: Swap = true;
6446 case ISD::SETGT: Opc = GTOpc; break;
6447 case ISD::SETGE: Swap = true;
6448 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
6449 case ISD::SETULT: Swap = true;
6450 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
6451 case ISD::SETUGE: Swap = true;
6452 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
6453 }
6454 if (Swap)
6455 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006456
Nate Begeman30a0de92008-07-17 16:51:19 +00006457 // Since SSE has no unsigned integer comparisons, we need to flip the sign
6458 // bits of the inputs before performing those operations.
6459 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00006460 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00006461 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
6462 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00006463 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00006464 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
6465 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00006466 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
6467 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00006468 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006469
Dale Johannesenace16102009-02-03 19:33:06 +00006470 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00006471
6472 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00006473 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00006474 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00006475
Nate Begeman30a0de92008-07-17 16:51:19 +00006476 return Result;
6477}
Evan Cheng0488db92007-09-25 01:57:46 +00006478
Evan Cheng370e5342008-12-03 08:38:43 +00006479// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00006480static bool isX86LogicalCmp(SDValue Op) {
6481 unsigned Opc = Op.getNode()->getOpcode();
6482 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
6483 return true;
6484 if (Op.getResNo() == 1 &&
6485 (Opc == X86ISD::ADD ||
6486 Opc == X86ISD::SUB ||
6487 Opc == X86ISD::SMUL ||
6488 Opc == X86ISD::UMUL ||
6489 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00006490 Opc == X86ISD::DEC ||
6491 Opc == X86ISD::OR ||
6492 Opc == X86ISD::XOR ||
6493 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00006494 return true;
6495
6496 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00006497}
6498
Dan Gohmand858e902010-04-17 15:26:15 +00006499SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00006500 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00006501 SDValue Cond = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006502 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00006503 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00006504
Dan Gohman1a492952009-10-20 16:22:37 +00006505 if (Cond.getOpcode() == ISD::SETCC) {
6506 SDValue NewCond = LowerSETCC(Cond, DAG);
6507 if (NewCond.getNode())
6508 Cond = NewCond;
6509 }
Evan Cheng734503b2006-09-11 02:19:56 +00006510
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006511 // (select (x == 0), -1, 0) -> (sign_bit (x - 1))
6512 SDValue Op1 = Op.getOperand(1);
6513 SDValue Op2 = Op.getOperand(2);
6514 if (Cond.getOpcode() == X86ISD::SETCC &&
6515 cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue() == X86::COND_E) {
6516 SDValue Cmp = Cond.getOperand(1);
6517 if (Cmp.getOpcode() == X86ISD::CMP) {
6518 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op1);
6519 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
6520 ConstantSDNode *RHSC =
6521 dyn_cast<ConstantSDNode>(Cmp.getOperand(1).getNode());
6522 if (N1C && N1C->isAllOnesValue() &&
6523 N2C && N2C->isNullValue() &&
6524 RHSC && RHSC->isNullValue()) {
6525 SDValue CmpOp0 = Cmp.getOperand(0);
Chris Lattnerda0688e2010-03-14 18:44:35 +00006526 Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006527 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
6528 return DAG.getNode(X86ISD::SETCC_CARRY, dl, Op.getValueType(),
6529 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
6530 }
6531 }
6532 }
6533
Evan Chengad9c0a32009-12-15 00:53:42 +00006534 // Look pass (and (setcc_carry (cmp ...)), 1).
6535 if (Cond.getOpcode() == ISD::AND &&
6536 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
6537 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
6538 if (C && C->getAPIntValue() == 1)
6539 Cond = Cond.getOperand(0);
6540 }
6541
Evan Cheng3f41d662007-10-08 22:16:29 +00006542 // If condition flag is set by a X86ISD::CMP, then use it as the condition
6543 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00006544 if (Cond.getOpcode() == X86ISD::SETCC ||
6545 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00006546 CC = Cond.getOperand(0);
6547
Dan Gohman475871a2008-07-27 21:46:04 +00006548 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00006549 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00006550 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00006551
Evan Cheng3f41d662007-10-08 22:16:29 +00006552 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006553 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00006554 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00006555 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00006556
Chris Lattnerd1980a52009-03-12 06:52:53 +00006557 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
6558 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00006559 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00006560 addTest = false;
6561 }
6562 }
6563
6564 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00006565 // Look pass the truncate.
6566 if (Cond.getOpcode() == ISD::TRUNCATE)
6567 Cond = Cond.getOperand(0);
6568
6569 // We know the result of AND is compared against zero. Try to match
6570 // it to BT.
6571 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
6572 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
6573 if (NewSetCC.getNode()) {
6574 CC = NewSetCC.getOperand(0);
6575 Cond = NewSetCC.getOperand(1);
6576 addTest = false;
6577 }
6578 }
6579 }
6580
6581 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006582 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00006583 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00006584 }
6585
Evan Cheng0488db92007-09-25 01:57:46 +00006586 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
6587 // condition is true.
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006588 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
6589 SDValue Ops[] = { Op2, Op1, CC, Cond };
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006590 return DAG.getNode(X86ISD::CMOV, dl, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00006591}
6592
Evan Cheng370e5342008-12-03 08:38:43 +00006593// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
6594// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
6595// from the AND / OR.
6596static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
6597 Opc = Op.getOpcode();
6598 if (Opc != ISD::OR && Opc != ISD::AND)
6599 return false;
6600 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
6601 Op.getOperand(0).hasOneUse() &&
6602 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
6603 Op.getOperand(1).hasOneUse());
6604}
6605
Evan Cheng961d6d42009-02-02 08:19:07 +00006606// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
6607// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00006608static bool isXor1OfSetCC(SDValue Op) {
6609 if (Op.getOpcode() != ISD::XOR)
6610 return false;
6611 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6612 if (N1C && N1C->getAPIntValue() == 1) {
6613 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
6614 Op.getOperand(0).hasOneUse();
6615 }
6616 return false;
6617}
6618
Dan Gohmand858e902010-04-17 15:26:15 +00006619SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00006620 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00006621 SDValue Chain = Op.getOperand(0);
6622 SDValue Cond = Op.getOperand(1);
6623 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006624 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00006625 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00006626
Dan Gohman1a492952009-10-20 16:22:37 +00006627 if (Cond.getOpcode() == ISD::SETCC) {
6628 SDValue NewCond = LowerSETCC(Cond, DAG);
6629 if (NewCond.getNode())
6630 Cond = NewCond;
6631 }
Chris Lattnere55484e2008-12-25 05:34:37 +00006632#if 0
6633 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00006634 else if (Cond.getOpcode() == X86ISD::ADD ||
6635 Cond.getOpcode() == X86ISD::SUB ||
6636 Cond.getOpcode() == X86ISD::SMUL ||
6637 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00006638 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00006639#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00006640
Evan Chengad9c0a32009-12-15 00:53:42 +00006641 // Look pass (and (setcc_carry (cmp ...)), 1).
6642 if (Cond.getOpcode() == ISD::AND &&
6643 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
6644 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
6645 if (C && C->getAPIntValue() == 1)
6646 Cond = Cond.getOperand(0);
6647 }
6648
Evan Cheng3f41d662007-10-08 22:16:29 +00006649 // If condition flag is set by a X86ISD::CMP, then use it as the condition
6650 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00006651 if (Cond.getOpcode() == X86ISD::SETCC ||
6652 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00006653 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006654
Dan Gohman475871a2008-07-27 21:46:04 +00006655 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00006656 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00006657 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00006658 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00006659 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00006660 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00006661 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00006662 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00006663 default: break;
6664 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00006665 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00006666 // These can only come from an arithmetic instruction with overflow,
6667 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00006668 Cond = Cond.getNode()->getOperand(1);
6669 addTest = false;
6670 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00006671 }
Evan Cheng0488db92007-09-25 01:57:46 +00006672 }
Evan Cheng370e5342008-12-03 08:38:43 +00006673 } else {
6674 unsigned CondOpc;
6675 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
6676 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00006677 if (CondOpc == ISD::OR) {
6678 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
6679 // two branches instead of an explicit OR instruction with a
6680 // separate test.
6681 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00006682 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00006683 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006684 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00006685 Chain, Dest, CC, Cmp);
6686 CC = Cond.getOperand(1).getOperand(0);
6687 Cond = Cmp;
6688 addTest = false;
6689 }
6690 } else { // ISD::AND
6691 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
6692 // two branches instead of an explicit AND instruction with a
6693 // separate test. However, we only do this if this block doesn't
6694 // have a fall-through edge, because this requires an explicit
6695 // jmp when the condition is false.
6696 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00006697 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00006698 Op.getNode()->hasOneUse()) {
6699 X86::CondCode CCode =
6700 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
6701 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006702 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00006703 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00006704 // Look for an unconditional branch following this conditional branch.
6705 // We need this because we need to reverse the successors in order
6706 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00006707 if (User->getOpcode() == ISD::BR) {
6708 SDValue FalseBB = User->getOperand(1);
6709 SDNode *NewBR =
6710 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00006711 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00006712 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00006713 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00006714
Dale Johannesene4d209d2009-02-03 20:21:25 +00006715 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00006716 Chain, Dest, CC, Cmp);
6717 X86::CondCode CCode =
6718 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
6719 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006720 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00006721 Cond = Cmp;
6722 addTest = false;
6723 }
6724 }
Dan Gohman279c22e2008-10-21 03:29:32 +00006725 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00006726 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
6727 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
6728 // It should be transformed during dag combiner except when the condition
6729 // is set by a arithmetics with overflow node.
6730 X86::CondCode CCode =
6731 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
6732 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006733 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00006734 Cond = Cond.getOperand(0).getOperand(1);
6735 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00006736 }
Evan Cheng0488db92007-09-25 01:57:46 +00006737 }
6738
6739 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00006740 // Look pass the truncate.
6741 if (Cond.getOpcode() == ISD::TRUNCATE)
6742 Cond = Cond.getOperand(0);
6743
6744 // We know the result of AND is compared against zero. Try to match
6745 // it to BT.
6746 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
6747 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
6748 if (NewSetCC.getNode()) {
6749 CC = NewSetCC.getOperand(0);
6750 Cond = NewSetCC.getOperand(1);
6751 addTest = false;
6752 }
6753 }
6754 }
6755
6756 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006757 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00006758 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00006759 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00006760 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00006761 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00006762}
6763
Anton Korobeynikove060b532007-04-17 19:34:00 +00006764
6765// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
6766// Calls to _alloca is needed to probe the stack when allocating more than 4k
6767// bytes in one go. Touching the stack at 4K increments is necessary to ensure
6768// that the guard pages used by the OS virtual memory manager are allocated in
6769// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00006770SDValue
6771X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006772 SelectionDAG &DAG) const {
Anton Korobeynikove060b532007-04-17 19:34:00 +00006773 assert(Subtarget->isTargetCygMing() &&
6774 "This should be used only on Cygwin/Mingw targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006775 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006776
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006777 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00006778 SDValue Chain = Op.getOperand(0);
6779 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006780 // FIXME: Ensure alignment here
6781
Dan Gohman475871a2008-07-27 21:46:04 +00006782 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006783
Owen Anderson825b72b2009-08-11 20:47:22 +00006784 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006785
Dale Johannesendd64c412009-02-04 00:33:20 +00006786 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006787 Flag = Chain.getValue(1);
6788
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00006789 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006790
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00006791 Chain = DAG.getNode(X86ISD::MINGW_ALLOCA, dl, NodeTys, Chain, Flag);
6792 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006793
Dale Johannesendd64c412009-02-04 00:33:20 +00006794 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006795
Dan Gohman475871a2008-07-27 21:46:04 +00006796 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006797 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006798}
6799
Dan Gohmand858e902010-04-17 15:26:15 +00006800SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00006801 MachineFunction &MF = DAG.getMachineFunction();
6802 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
6803
Dan Gohman69de1932008-02-06 22:27:42 +00006804 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006805 DebugLoc dl = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00006806
Evan Cheng25ab6902006-09-08 06:48:29 +00006807 if (!Subtarget->is64Bit()) {
6808 // vastart just stores the address of the VarArgsFrameIndex slot into the
6809 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00006810 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
6811 getPointerTy());
David Greene67c9d422010-02-15 16:53:33 +00006812 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0,
6813 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006814 }
6815
6816 // __va_list_tag:
6817 // gp_offset (0 - 6 * 8)
6818 // fp_offset (48 - 48 + 8 * 16)
6819 // overflow_arg_area (point to parameters coming in memory).
6820 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00006821 SmallVector<SDValue, 8> MemOps;
6822 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00006823 // Store gp_offset
Dale Johannesene4d209d2009-02-03 20:21:25 +00006824 SDValue Store = DAG.getStore(Op.getOperand(0), dl,
Dan Gohman1e93df62010-04-17 14:41:14 +00006825 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
6826 MVT::i32),
David Greene67c9d422010-02-15 16:53:33 +00006827 FIN, SV, 0, false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006828 MemOps.push_back(Store);
6829
6830 // Store fp_offset
Scott Michelfdc40a02009-02-17 22:15:04 +00006831 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006832 FIN, DAG.getIntPtrConstant(4));
6833 Store = DAG.getStore(Op.getOperand(0), dl,
Dan Gohman1e93df62010-04-17 14:41:14 +00006834 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
6835 MVT::i32),
Dan Gohman01dcb182010-07-09 01:06:48 +00006836 FIN, SV, 4, false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006837 MemOps.push_back(Store);
6838
6839 // Store ptr to overflow_arg_area
Scott Michelfdc40a02009-02-17 22:15:04 +00006840 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006841 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00006842 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
6843 getPointerTy());
Dan Gohman01dcb182010-07-09 01:06:48 +00006844 Store = DAG.getStore(Op.getOperand(0), dl, OVFIN, FIN, SV, 8,
David Greene67c9d422010-02-15 16:53:33 +00006845 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006846 MemOps.push_back(Store);
6847
6848 // Store ptr to reg_save_area.
Scott Michelfdc40a02009-02-17 22:15:04 +00006849 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006850 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00006851 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
6852 getPointerTy());
Dan Gohman01dcb182010-07-09 01:06:48 +00006853 Store = DAG.getStore(Op.getOperand(0), dl, RSFIN, FIN, SV, 16,
David Greene67c9d422010-02-15 16:53:33 +00006854 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006855 MemOps.push_back(Store);
Owen Anderson825b72b2009-08-11 20:47:22 +00006856 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006857 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00006858}
6859
Dan Gohmand858e902010-04-17 15:26:15 +00006860SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman9018e832008-05-10 01:26:14 +00006861 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
6862 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman9018e832008-05-10 01:26:14 +00006863
Chris Lattner75361b62010-04-07 22:58:41 +00006864 report_fatal_error("VAArgInst is not yet implemented for x86-64!");
Dan Gohman475871a2008-07-27 21:46:04 +00006865 return SDValue();
Dan Gohman9018e832008-05-10 01:26:14 +00006866}
6867
Dan Gohmand858e902010-04-17 15:26:15 +00006868SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00006869 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00006870 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00006871 SDValue Chain = Op.getOperand(0);
6872 SDValue DstPtr = Op.getOperand(1);
6873 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00006874 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
6875 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006876 DebugLoc dl = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00006877
Dale Johannesendd64c412009-02-04 00:33:20 +00006878 return DAG.getMemcpy(Chain, dl, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00006879 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
6880 false, DstSV, 0, SrcSV, 0);
Evan Chengae642192007-03-02 23:16:35 +00006881}
6882
Dan Gohman475871a2008-07-27 21:46:04 +00006883SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006884X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006885 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006886 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006887 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00006888 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00006889 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00006890 case Intrinsic::x86_sse_comieq_ss:
6891 case Intrinsic::x86_sse_comilt_ss:
6892 case Intrinsic::x86_sse_comile_ss:
6893 case Intrinsic::x86_sse_comigt_ss:
6894 case Intrinsic::x86_sse_comige_ss:
6895 case Intrinsic::x86_sse_comineq_ss:
6896 case Intrinsic::x86_sse_ucomieq_ss:
6897 case Intrinsic::x86_sse_ucomilt_ss:
6898 case Intrinsic::x86_sse_ucomile_ss:
6899 case Intrinsic::x86_sse_ucomigt_ss:
6900 case Intrinsic::x86_sse_ucomige_ss:
6901 case Intrinsic::x86_sse_ucomineq_ss:
6902 case Intrinsic::x86_sse2_comieq_sd:
6903 case Intrinsic::x86_sse2_comilt_sd:
6904 case Intrinsic::x86_sse2_comile_sd:
6905 case Intrinsic::x86_sse2_comigt_sd:
6906 case Intrinsic::x86_sse2_comige_sd:
6907 case Intrinsic::x86_sse2_comineq_sd:
6908 case Intrinsic::x86_sse2_ucomieq_sd:
6909 case Intrinsic::x86_sse2_ucomilt_sd:
6910 case Intrinsic::x86_sse2_ucomile_sd:
6911 case Intrinsic::x86_sse2_ucomigt_sd:
6912 case Intrinsic::x86_sse2_ucomige_sd:
6913 case Intrinsic::x86_sse2_ucomineq_sd: {
6914 unsigned Opc = 0;
6915 ISD::CondCode CC = ISD::SETCC_INVALID;
6916 switch (IntNo) {
6917 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006918 case Intrinsic::x86_sse_comieq_ss:
6919 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006920 Opc = X86ISD::COMI;
6921 CC = ISD::SETEQ;
6922 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006923 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006924 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006925 Opc = X86ISD::COMI;
6926 CC = ISD::SETLT;
6927 break;
6928 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006929 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006930 Opc = X86ISD::COMI;
6931 CC = ISD::SETLE;
6932 break;
6933 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006934 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006935 Opc = X86ISD::COMI;
6936 CC = ISD::SETGT;
6937 break;
6938 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006939 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006940 Opc = X86ISD::COMI;
6941 CC = ISD::SETGE;
6942 break;
6943 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006944 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006945 Opc = X86ISD::COMI;
6946 CC = ISD::SETNE;
6947 break;
6948 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006949 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006950 Opc = X86ISD::UCOMI;
6951 CC = ISD::SETEQ;
6952 break;
6953 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006954 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006955 Opc = X86ISD::UCOMI;
6956 CC = ISD::SETLT;
6957 break;
6958 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006959 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006960 Opc = X86ISD::UCOMI;
6961 CC = ISD::SETLE;
6962 break;
6963 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006964 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006965 Opc = X86ISD::UCOMI;
6966 CC = ISD::SETGT;
6967 break;
6968 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006969 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006970 Opc = X86ISD::UCOMI;
6971 CC = ISD::SETGE;
6972 break;
6973 case Intrinsic::x86_sse_ucomineq_ss:
6974 case Intrinsic::x86_sse2_ucomineq_sd:
6975 Opc = X86ISD::UCOMI;
6976 CC = ISD::SETNE;
6977 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006978 }
Evan Cheng734503b2006-09-11 02:19:56 +00006979
Dan Gohman475871a2008-07-27 21:46:04 +00006980 SDValue LHS = Op.getOperand(1);
6981 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00006982 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00006983 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00006984 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
6985 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6986 DAG.getConstant(X86CC, MVT::i8), Cond);
6987 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00006988 }
Eric Christopher71c67532009-07-29 00:28:05 +00006989 // ptest intrinsics. The intrinsic these come from are designed to return
Eric Christopher794bfed2009-07-29 01:01:19 +00006990 // an integer value, not just an instruction so lower it to the ptest
6991 // pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00006992 case Intrinsic::x86_sse41_ptestz:
6993 case Intrinsic::x86_sse41_ptestc:
6994 case Intrinsic::x86_sse41_ptestnzc:{
6995 unsigned X86CC = 0;
6996 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00006997 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Eric Christopher71c67532009-07-29 00:28:05 +00006998 case Intrinsic::x86_sse41_ptestz:
6999 // ZF = 1
7000 X86CC = X86::COND_E;
7001 break;
7002 case Intrinsic::x86_sse41_ptestc:
7003 // CF = 1
7004 X86CC = X86::COND_B;
7005 break;
Eric Christopherfd179292009-08-27 18:07:15 +00007006 case Intrinsic::x86_sse41_ptestnzc:
Eric Christopher71c67532009-07-29 00:28:05 +00007007 // ZF and CF = 0
7008 X86CC = X86::COND_A;
7009 break;
7010 }
Eric Christopherfd179292009-08-27 18:07:15 +00007011
Eric Christopher71c67532009-07-29 00:28:05 +00007012 SDValue LHS = Op.getOperand(1);
7013 SDValue RHS = Op.getOperand(2);
Owen Anderson825b72b2009-08-11 20:47:22 +00007014 SDValue Test = DAG.getNode(X86ISD::PTEST, dl, MVT::i32, LHS, RHS);
7015 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
7016 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
7017 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00007018 }
Evan Cheng5759f972008-05-04 09:15:50 +00007019
7020 // Fix vector shift instructions where the last operand is a non-immediate
7021 // i32 value.
7022 case Intrinsic::x86_sse2_pslli_w:
7023 case Intrinsic::x86_sse2_pslli_d:
7024 case Intrinsic::x86_sse2_pslli_q:
7025 case Intrinsic::x86_sse2_psrli_w:
7026 case Intrinsic::x86_sse2_psrli_d:
7027 case Intrinsic::x86_sse2_psrli_q:
7028 case Intrinsic::x86_sse2_psrai_w:
7029 case Intrinsic::x86_sse2_psrai_d:
7030 case Intrinsic::x86_mmx_pslli_w:
7031 case Intrinsic::x86_mmx_pslli_d:
7032 case Intrinsic::x86_mmx_pslli_q:
7033 case Intrinsic::x86_mmx_psrli_w:
7034 case Intrinsic::x86_mmx_psrli_d:
7035 case Intrinsic::x86_mmx_psrli_q:
7036 case Intrinsic::x86_mmx_psrai_w:
7037 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00007038 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00007039 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00007040 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00007041
7042 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00007043 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00007044 switch (IntNo) {
7045 case Intrinsic::x86_sse2_pslli_w:
7046 NewIntNo = Intrinsic::x86_sse2_psll_w;
7047 break;
7048 case Intrinsic::x86_sse2_pslli_d:
7049 NewIntNo = Intrinsic::x86_sse2_psll_d;
7050 break;
7051 case Intrinsic::x86_sse2_pslli_q:
7052 NewIntNo = Intrinsic::x86_sse2_psll_q;
7053 break;
7054 case Intrinsic::x86_sse2_psrli_w:
7055 NewIntNo = Intrinsic::x86_sse2_psrl_w;
7056 break;
7057 case Intrinsic::x86_sse2_psrli_d:
7058 NewIntNo = Intrinsic::x86_sse2_psrl_d;
7059 break;
7060 case Intrinsic::x86_sse2_psrli_q:
7061 NewIntNo = Intrinsic::x86_sse2_psrl_q;
7062 break;
7063 case Intrinsic::x86_sse2_psrai_w:
7064 NewIntNo = Intrinsic::x86_sse2_psra_w;
7065 break;
7066 case Intrinsic::x86_sse2_psrai_d:
7067 NewIntNo = Intrinsic::x86_sse2_psra_d;
7068 break;
7069 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00007070 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00007071 switch (IntNo) {
7072 case Intrinsic::x86_mmx_pslli_w:
7073 NewIntNo = Intrinsic::x86_mmx_psll_w;
7074 break;
7075 case Intrinsic::x86_mmx_pslli_d:
7076 NewIntNo = Intrinsic::x86_mmx_psll_d;
7077 break;
7078 case Intrinsic::x86_mmx_pslli_q:
7079 NewIntNo = Intrinsic::x86_mmx_psll_q;
7080 break;
7081 case Intrinsic::x86_mmx_psrli_w:
7082 NewIntNo = Intrinsic::x86_mmx_psrl_w;
7083 break;
7084 case Intrinsic::x86_mmx_psrli_d:
7085 NewIntNo = Intrinsic::x86_mmx_psrl_d;
7086 break;
7087 case Intrinsic::x86_mmx_psrli_q:
7088 NewIntNo = Intrinsic::x86_mmx_psrl_q;
7089 break;
7090 case Intrinsic::x86_mmx_psrai_w:
7091 NewIntNo = Intrinsic::x86_mmx_psra_w;
7092 break;
7093 case Intrinsic::x86_mmx_psrai_d:
7094 NewIntNo = Intrinsic::x86_mmx_psra_d;
7095 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00007096 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00007097 }
7098 break;
7099 }
7100 }
Mon P Wangefa42202009-09-03 19:56:25 +00007101
7102 // The vector shift intrinsics with scalars uses 32b shift amounts but
7103 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
7104 // to be zero.
7105 SDValue ShOps[4];
7106 ShOps[0] = ShAmt;
7107 ShOps[1] = DAG.getConstant(0, MVT::i32);
7108 if (ShAmtVT == MVT::v4i32) {
7109 ShOps[2] = DAG.getUNDEF(MVT::i32);
7110 ShOps[3] = DAG.getUNDEF(MVT::i32);
7111 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
7112 } else {
7113 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
7114 }
7115
Owen Andersone50ed302009-08-10 22:56:29 +00007116 EVT VT = Op.getValueType();
Mon P Wangefa42202009-09-03 19:56:25 +00007117 ShAmt = DAG.getNode(ISD::BIT_CONVERT, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007118 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007119 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00007120 Op.getOperand(1), ShAmt);
7121 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00007122 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007123}
Evan Cheng72261582005-12-20 06:22:03 +00007124
Dan Gohmand858e902010-04-17 15:26:15 +00007125SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
7126 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00007127 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7128 MFI->setReturnAddressIsTaken(true);
7129
Bill Wendling64e87322009-01-16 19:25:27 +00007130 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007131 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00007132
7133 if (Depth > 0) {
7134 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
7135 SDValue Offset =
7136 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00007137 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007138 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00007139 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007140 FrameAddr, Offset),
David Greene67c9d422010-02-15 16:53:33 +00007141 NULL, 0, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00007142 }
7143
7144 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00007145 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00007146 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
David Greene67c9d422010-02-15 16:53:33 +00007147 RetAddrFI, NULL, 0, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00007148}
7149
Dan Gohmand858e902010-04-17 15:26:15 +00007150SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00007151 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7152 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00007153
Owen Andersone50ed302009-08-10 22:56:29 +00007154 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007155 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00007156 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
7157 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00007158 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00007159 while (Depth--)
David Greene67c9d422010-02-15 16:53:33 +00007160 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0,
7161 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00007162 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00007163}
7164
Dan Gohman475871a2008-07-27 21:46:04 +00007165SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007166 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00007167 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007168}
7169
Dan Gohmand858e902010-04-17 15:26:15 +00007170SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007171 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00007172 SDValue Chain = Op.getOperand(0);
7173 SDValue Offset = Op.getOperand(1);
7174 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007175 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007176
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007177 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
7178 getPointerTy());
7179 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007180
Dale Johannesene4d209d2009-02-03 20:21:25 +00007181 SDValue StoreAddr = DAG.getNode(ISD::SUB, dl, getPointerTy(), Frame,
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00007182 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007183 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
David Greene67c9d422010-02-15 16:53:33 +00007184 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, NULL, 0, false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00007185 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007186 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007187
Dale Johannesene4d209d2009-02-03 20:21:25 +00007188 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007189 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007190 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007191}
7192
Dan Gohman475871a2008-07-27 21:46:04 +00007193SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007194 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007195 SDValue Root = Op.getOperand(0);
7196 SDValue Trmp = Op.getOperand(1); // trampoline
7197 SDValue FPtr = Op.getOperand(2); // nested function
7198 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007199 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00007200
Dan Gohman69de1932008-02-06 22:27:42 +00007201 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00007202
7203 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00007204 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00007205
7206 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00007207 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
7208 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00007209
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00007210 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
7211 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00007212
7213 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
7214
7215 // Load the pointer to the nested function into R11.
7216 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00007217 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00007218 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
David Greene67c9d422010-02-15 16:53:33 +00007219 Addr, TrmpAddr, 0, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007220
Owen Anderson825b72b2009-08-11 20:47:22 +00007221 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7222 DAG.getConstant(2, MVT::i64));
David Greene67c9d422010-02-15 16:53:33 +00007223 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr, TrmpAddr, 2,
7224 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00007225
7226 // Load the 'nest' parameter value into R10.
7227 // R10 is specified in X86CallingConv.td
7228 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00007229 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7230 DAG.getConstant(10, MVT::i64));
7231 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
David Greene67c9d422010-02-15 16:53:33 +00007232 Addr, TrmpAddr, 10, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007233
Owen Anderson825b72b2009-08-11 20:47:22 +00007234 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7235 DAG.getConstant(12, MVT::i64));
David Greene67c9d422010-02-15 16:53:33 +00007236 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 12,
7237 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00007238
7239 // Jump to the nested function.
7240 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00007241 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7242 DAG.getConstant(20, MVT::i64));
7243 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
David Greene67c9d422010-02-15 16:53:33 +00007244 Addr, TrmpAddr, 20, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007245
7246 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00007247 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7248 DAG.getConstant(22, MVT::i64));
7249 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
David Greene67c9d422010-02-15 16:53:33 +00007250 TrmpAddr, 22, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007251
Dan Gohman475871a2008-07-27 21:46:04 +00007252 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00007253 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007254 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007255 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00007256 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00007257 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00007258 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00007259 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007260
7261 switch (CC) {
7262 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00007263 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00007264 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00007265 case CallingConv::X86_StdCall: {
7266 // Pass 'nest' parameter in ECX.
7267 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00007268 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007269
7270 // Check that ECX wasn't needed by an 'inreg' parameter.
7271 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00007272 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00007273
Chris Lattner58d74912008-03-12 17:45:29 +00007274 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00007275 unsigned InRegCount = 0;
7276 unsigned Idx = 1;
7277
7278 for (FunctionType::param_iterator I = FTy->param_begin(),
7279 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00007280 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00007281 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00007282 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007283
7284 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00007285 report_fatal_error("Nest register in use - reduce number of inreg"
7286 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00007287 }
7288 }
7289 break;
7290 }
7291 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00007292 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00007293 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00007294 // Pass 'nest' parameter in EAX.
7295 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00007296 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007297 break;
7298 }
7299
Dan Gohman475871a2008-07-27 21:46:04 +00007300 SDValue OutChains[4];
7301 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007302
Owen Anderson825b72b2009-08-11 20:47:22 +00007303 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7304 DAG.getConstant(10, MVT::i32));
7305 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007306
Chris Lattnera62fe662010-02-05 19:20:30 +00007307 // This is storing the opcode for MOV32ri.
7308 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00007309 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00007310 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007311 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
David Greene67c9d422010-02-15 16:53:33 +00007312 Trmp, TrmpAddr, 0, false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007313
Owen Anderson825b72b2009-08-11 20:47:22 +00007314 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7315 DAG.getConstant(1, MVT::i32));
David Greene67c9d422010-02-15 16:53:33 +00007316 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 1,
7317 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007318
Chris Lattnera62fe662010-02-05 19:20:30 +00007319 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00007320 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7321 DAG.getConstant(5, MVT::i32));
7322 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
David Greene67c9d422010-02-15 16:53:33 +00007323 TrmpAddr, 5, false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007324
Owen Anderson825b72b2009-08-11 20:47:22 +00007325 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7326 DAG.getConstant(6, MVT::i32));
David Greene67c9d422010-02-15 16:53:33 +00007327 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr, TrmpAddr, 6,
7328 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007329
Dan Gohman475871a2008-07-27 21:46:04 +00007330 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00007331 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007332 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007333 }
7334}
7335
Dan Gohmand858e902010-04-17 15:26:15 +00007336SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
7337 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007338 /*
7339 The rounding mode is in bits 11:10 of FPSR, and has the following
7340 settings:
7341 00 Round to nearest
7342 01 Round to -inf
7343 10 Round to +inf
7344 11 Round to 0
7345
7346 FLT_ROUNDS, on the other hand, expects the following:
7347 -1 Undefined
7348 0 Round to 0
7349 1 Round to nearest
7350 2 Round to +inf
7351 3 Round to -inf
7352
7353 To perform the conversion, we do:
7354 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
7355 */
7356
7357 MachineFunction &MF = DAG.getMachineFunction();
7358 const TargetMachine &TM = MF.getTarget();
7359 const TargetFrameInfo &TFI = *TM.getFrameInfo();
7360 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00007361 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007362 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007363
7364 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00007365 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007366 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007367
Owen Anderson825b72b2009-08-11 20:47:22 +00007368 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, dl, MVT::Other,
Evan Cheng8a186ae2008-09-24 23:26:36 +00007369 DAG.getEntryNode(), StackSlot);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007370
7371 // Load FP Control Word from stack slot
David Greene67c9d422010-02-15 16:53:33 +00007372 SDValue CWD = DAG.getLoad(MVT::i16, dl, Chain, StackSlot, NULL, 0,
7373 false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007374
7375 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00007376 SDValue CWD1 =
Owen Anderson825b72b2009-08-11 20:47:22 +00007377 DAG.getNode(ISD::SRL, dl, MVT::i16,
7378 DAG.getNode(ISD::AND, dl, MVT::i16,
7379 CWD, DAG.getConstant(0x800, MVT::i16)),
7380 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00007381 SDValue CWD2 =
Owen Anderson825b72b2009-08-11 20:47:22 +00007382 DAG.getNode(ISD::SRL, dl, MVT::i16,
7383 DAG.getNode(ISD::AND, dl, MVT::i16,
7384 CWD, DAG.getConstant(0x400, MVT::i16)),
7385 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007386
Dan Gohman475871a2008-07-27 21:46:04 +00007387 SDValue RetVal =
Owen Anderson825b72b2009-08-11 20:47:22 +00007388 DAG.getNode(ISD::AND, dl, MVT::i16,
7389 DAG.getNode(ISD::ADD, dl, MVT::i16,
7390 DAG.getNode(ISD::OR, dl, MVT::i16, CWD1, CWD2),
7391 DAG.getConstant(1, MVT::i16)),
7392 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007393
7394
Duncan Sands83ec4b62008-06-06 12:08:01 +00007395 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007396 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007397}
7398
Dan Gohmand858e902010-04-17 15:26:15 +00007399SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007400 EVT VT = Op.getValueType();
7401 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007402 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007403 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00007404
7405 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007406 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00007407 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00007408 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007409 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007410 }
Evan Cheng18efe262007-12-14 02:13:44 +00007411
Evan Cheng152804e2007-12-14 08:30:15 +00007412 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007413 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007414 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00007415
7416 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007417 SDValue Ops[] = {
7418 Op,
7419 DAG.getConstant(NumBits+NumBits-1, OpVT),
7420 DAG.getConstant(X86::COND_E, MVT::i8),
7421 Op.getValue(1)
7422 };
7423 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00007424
7425 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00007426 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00007427
Owen Anderson825b72b2009-08-11 20:47:22 +00007428 if (VT == MVT::i8)
7429 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007430 return Op;
7431}
7432
Dan Gohmand858e902010-04-17 15:26:15 +00007433SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007434 EVT VT = Op.getValueType();
7435 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007436 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007437 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00007438
7439 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007440 if (VT == MVT::i8) {
7441 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007442 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007443 }
Evan Cheng152804e2007-12-14 08:30:15 +00007444
7445 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007446 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007447 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00007448
7449 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007450 SDValue Ops[] = {
7451 Op,
7452 DAG.getConstant(NumBits, OpVT),
7453 DAG.getConstant(X86::COND_E, MVT::i8),
7454 Op.getValue(1)
7455 };
7456 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00007457
Owen Anderson825b72b2009-08-11 20:47:22 +00007458 if (VT == MVT::i8)
7459 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007460 return Op;
7461}
7462
Dan Gohmand858e902010-04-17 15:26:15 +00007463SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007464 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007465 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007466 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00007467
Mon P Wangaf9b9522008-12-18 21:42:19 +00007468 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
7469 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
7470 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
7471 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
7472 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
7473 //
7474 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
7475 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
7476 // return AloBlo + AloBhi + AhiBlo;
7477
7478 SDValue A = Op.getOperand(0);
7479 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007480
Dale Johannesene4d209d2009-02-03 20:21:25 +00007481 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007482 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7483 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007484 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007485 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7486 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007487 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007488 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007489 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007490 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007491 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007492 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007493 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007494 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007495 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007496 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007497 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7498 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007499 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007500 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7501 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007502 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
7503 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00007504 return Res;
7505}
7506
Nate Begemanbdcb5af2010-07-27 22:37:06 +00007507SDValue X86TargetLowering::LowerSHL(SDValue Op, SelectionDAG &DAG) const {
7508 EVT VT = Op.getValueType();
7509 DebugLoc dl = Op.getDebugLoc();
7510 SDValue R = Op.getOperand(0);
7511
Nate Begemanbdcb5af2010-07-27 22:37:06 +00007512 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00007513
Nate Begeman51409212010-07-28 00:21:48 +00007514 assert(Subtarget->hasSSE41() && "Cannot lower SHL without SSE4.1 or later");
7515
7516 if (VT == MVT::v4i32) {
7517 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
7518 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
7519 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
7520
7521 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
7522
7523 std::vector<Constant*> CV(4, CI);
7524 Constant *C = ConstantVector::get(CV);
7525 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
7526 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
7527 PseudoSourceValue::getConstantPool(), 0,
7528 false, false, 16);
7529
7530 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
7531 Op = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, Op);
7532 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
7533 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
7534 }
7535 if (VT == MVT::v16i8) {
7536 // a = a << 5;
7537 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
7538 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
7539 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
7540
7541 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
7542 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
7543
7544 std::vector<Constant*> CVM1(16, CM1);
7545 std::vector<Constant*> CVM2(16, CM2);
7546 Constant *C = ConstantVector::get(CVM1);
7547 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
7548 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
7549 PseudoSourceValue::getConstantPool(), 0,
7550 false, false, 16);
7551
7552 // r = pblendv(r, psllw(r & (char16)15, 4), a);
7553 M = DAG.getNode(ISD::AND, dl, VT, R, M);
7554 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
7555 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
7556 DAG.getConstant(4, MVT::i32));
7557 R = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
7558 DAG.getConstant(Intrinsic::x86_sse41_pblendvb, MVT::i32),
7559 R, M, Op);
7560 // a += a
7561 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
7562
7563 C = ConstantVector::get(CVM2);
7564 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
7565 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
7566 PseudoSourceValue::getConstantPool(), 0, false, false, 16);
7567
7568 // r = pblendv(r, psllw(r & (char16)63, 2), a);
7569 M = DAG.getNode(ISD::AND, dl, VT, R, M);
7570 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
7571 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
7572 DAG.getConstant(2, MVT::i32));
7573 R = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
7574 DAG.getConstant(Intrinsic::x86_sse41_pblendvb, MVT::i32),
7575 R, M, Op);
7576 // a += a
7577 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
7578
7579 // return pblendv(r, r+r, a);
7580 R = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
7581 DAG.getConstant(Intrinsic::x86_sse41_pblendvb, MVT::i32),
7582 R, DAG.getNode(ISD::ADD, dl, VT, R, R), Op);
7583 return R;
7584 }
7585 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00007586}
Mon P Wangaf9b9522008-12-18 21:42:19 +00007587
Dan Gohmand858e902010-04-17 15:26:15 +00007588SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +00007589 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
7590 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00007591 // looks for this combo and may remove the "setcc" instruction if the "setcc"
7592 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00007593 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00007594 SDValue LHS = N->getOperand(0);
7595 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00007596 unsigned BaseOp = 0;
7597 unsigned Cond = 0;
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007598 DebugLoc dl = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00007599
7600 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007601 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00007602 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00007603 // A subtract of one will be selected as a INC. Note that INC doesn't
7604 // set CF, so we can't do this for UADDO.
7605 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
7606 if (C->getAPIntValue() == 1) {
7607 BaseOp = X86ISD::INC;
7608 Cond = X86::COND_O;
7609 break;
7610 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007611 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00007612 Cond = X86::COND_O;
7613 break;
7614 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007615 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00007616 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007617 break;
7618 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00007619 // A subtract of one will be selected as a DEC. Note that DEC doesn't
7620 // set CF, so we can't do this for USUBO.
7621 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
7622 if (C->getAPIntValue() == 1) {
7623 BaseOp = X86ISD::DEC;
7624 Cond = X86::COND_O;
7625 break;
7626 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007627 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00007628 Cond = X86::COND_O;
7629 break;
7630 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007631 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00007632 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007633 break;
7634 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00007635 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00007636 Cond = X86::COND_O;
7637 break;
7638 case ISD::UMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00007639 BaseOp = X86ISD::UMUL;
Dan Gohman653456c2009-01-07 00:15:08 +00007640 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007641 break;
7642 }
Bill Wendling3fafd932008-11-26 22:37:40 +00007643
Bill Wendling61edeb52008-12-02 01:06:39 +00007644 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007645 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007646 SDValue Sum = DAG.getNode(BaseOp, dl, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00007647
Bill Wendling61edeb52008-12-02 01:06:39 +00007648 SDValue SetCC =
Dale Johannesene4d209d2009-02-03 20:21:25 +00007649 DAG.getNode(X86ISD::SETCC, dl, N->getValueType(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00007650 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00007651
Bill Wendling61edeb52008-12-02 01:06:39 +00007652 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
7653 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00007654}
7655
Eric Christopher9a9d2752010-07-22 02:48:34 +00007656SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
7657 DebugLoc dl = Op.getDebugLoc();
7658
Eric Christopherb6729dc2010-08-04 23:03:04 +00007659 if (!Subtarget->hasSSE2()) {
7660 SDValue Zero = DAG.getConstant(0,
7661 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Eric Christopher9a9d2752010-07-22 02:48:34 +00007662 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
Eric Christopherb6729dc2010-08-04 23:03:04 +00007663 Zero);
7664 }
Eric Christopher9a9d2752010-07-22 02:48:34 +00007665
7666 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
7667 if(!isDev)
7668 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
7669 else {
7670 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
7671 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
7672 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
7673 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
7674
7675 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
7676 if (!Op1 && !Op2 && !Op3 && Op4)
7677 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
7678
7679 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
7680 if (Op1 && !Op2 && !Op3 && !Op4)
7681 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
7682
7683 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
7684 // (MFENCE)>;
7685 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
7686 }
7687}
7688
Dan Gohmand858e902010-04-17 15:26:15 +00007689SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007690 EVT T = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007691 DebugLoc dl = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00007692 unsigned Reg = 0;
7693 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00007694 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007695 default:
7696 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007697 case MVT::i8: Reg = X86::AL; size = 1; break;
7698 case MVT::i16: Reg = X86::AX; size = 2; break;
7699 case MVT::i32: Reg = X86::EAX; size = 4; break;
7700 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00007701 assert(Subtarget->is64Bit() && "Node not type legal!");
7702 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00007703 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007704 }
Dale Johannesendd64c412009-02-04 00:33:20 +00007705 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), dl, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00007706 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00007707 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00007708 Op.getOperand(1),
7709 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00007710 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00007711 cpIn.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00007712 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007713 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, dl, Tys, Ops, 5);
Scott Michelfdc40a02009-02-17 22:15:04 +00007714 SDValue cpOut =
Dale Johannesendd64c412009-02-04 00:33:20 +00007715 DAG.getCopyFromReg(Result.getValue(0), dl, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00007716 return cpOut;
7717}
7718
Duncan Sands1607f052008-12-01 11:39:25 +00007719SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007720 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +00007721 assert(Subtarget->is64Bit() && "Result not type legalized?");
Owen Anderson825b72b2009-08-11 20:47:22 +00007722 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00007723 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007724 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007725 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007726 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
7727 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00007728 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00007729 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
7730 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00007731 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00007732 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00007733 rdx.getValue(1)
7734 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007735 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007736}
7737
Dale Johannesen7d07b482010-05-21 00:52:33 +00007738SDValue X86TargetLowering::LowerBIT_CONVERT(SDValue Op,
7739 SelectionDAG &DAG) const {
7740 EVT SrcVT = Op.getOperand(0).getValueType();
7741 EVT DstVT = Op.getValueType();
7742 assert((Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
7743 Subtarget->hasMMX() && !DisableMMX) &&
7744 "Unexpected custom BIT_CONVERT");
7745 assert((DstVT == MVT::i64 ||
7746 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
7747 "Unexpected custom BIT_CONVERT");
7748 // i64 <=> MMX conversions are Legal.
7749 if (SrcVT==MVT::i64 && DstVT.isVector())
7750 return Op;
7751 if (DstVT==MVT::i64 && SrcVT.isVector())
7752 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +00007753 // MMX <=> MMX conversions are Legal.
7754 if (SrcVT.isVector() && DstVT.isVector())
7755 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +00007756 // All other conversions need to be expanded.
7757 return SDValue();
7758}
Dan Gohmand858e902010-04-17 15:26:15 +00007759SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +00007760 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007761 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007762 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007763 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00007764 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007765 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007766 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00007767 Node->getOperand(0),
7768 Node->getOperand(1), negOp,
7769 cast<AtomicSDNode>(Node)->getSrcValue(),
7770 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00007771}
7772
Evan Cheng0db9fe62006-04-25 20:13:52 +00007773/// LowerOperation - Provide custom lowering hooks for some operations.
7774///
Dan Gohmand858e902010-04-17 15:26:15 +00007775SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007776 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007777 default: llvm_unreachable("Should not custom lower this!");
Eric Christopher9a9d2752010-07-22 02:48:34 +00007778 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007779 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
7780 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007781 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00007782 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007783 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
7784 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
7785 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
7786 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
7787 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
7788 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007789 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00007790 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007791 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007792 case ISD::SHL_PARTS:
7793 case ISD::SRA_PARTS:
7794 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
7795 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007796 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007797 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00007798 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007799 case ISD::FABS: return LowerFABS(Op, DAG);
7800 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007801 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00007802 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00007803 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00007804 case ISD::SELECT: return LowerSELECT(Op, DAG);
7805 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007806 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007807 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00007808 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00007809 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007810 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00007811 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
7812 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007813 case ISD::FRAME_TO_ARGS_OFFSET:
7814 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007815 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007816 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007817 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00007818 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00007819 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
7820 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00007821 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00007822 case ISD::SHL: return LowerSHL(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00007823 case ISD::SADDO:
7824 case ISD::UADDO:
7825 case ISD::SSUBO:
7826 case ISD::USUBO:
7827 case ISD::SMULO:
7828 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00007829 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Dale Johannesen7d07b482010-05-21 00:52:33 +00007830 case ISD::BIT_CONVERT: return LowerBIT_CONVERT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007831 }
Chris Lattner27a6c732007-11-24 07:07:01 +00007832}
7833
Duncan Sands1607f052008-12-01 11:39:25 +00007834void X86TargetLowering::
7835ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00007836 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007837 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007838 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00007839 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00007840
7841 SDValue Chain = Node->getOperand(0);
7842 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007843 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007844 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007845 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007846 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00007847 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00007848 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00007849 SDValue Result =
7850 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
7851 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00007852 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00007853 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007854 Results.push_back(Result.getValue(2));
7855}
7856
Duncan Sands126d9072008-07-04 11:47:58 +00007857/// ReplaceNodeResults - Replace a node with an illegal result type
7858/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00007859void X86TargetLowering::ReplaceNodeResults(SDNode *N,
7860 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00007861 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007862 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00007863 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00007864 default:
Duncan Sands1607f052008-12-01 11:39:25 +00007865 assert(false && "Do not know how to custom type legalize this operation!");
7866 return;
7867 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00007868 std::pair<SDValue,SDValue> Vals =
7869 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00007870 SDValue FIST = Vals.first, StackSlot = Vals.second;
7871 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00007872 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00007873 // Return a load from the stack slot.
David Greene67c9d422010-02-15 16:53:33 +00007874 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot, NULL, 0,
7875 false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00007876 }
7877 return;
7878 }
7879 case ISD::READCYCLECOUNTER: {
Owen Anderson825b72b2009-08-11 20:47:22 +00007880 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00007881 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007882 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007883 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00007884 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007885 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007886 eax.getValue(2));
7887 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
7888 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00007889 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007890 Results.push_back(edx.getValue(1));
7891 return;
7892 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007893 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00007894 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007895 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00007896 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00007897 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
7898 DAG.getConstant(0, MVT::i32));
7899 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
7900 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00007901 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
7902 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00007903 cpInL.getValue(1));
7904 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00007905 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
7906 DAG.getConstant(0, MVT::i32));
7907 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
7908 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00007909 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00007910 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00007911 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00007912 swapInL.getValue(1));
7913 SDValue Ops[] = { swapInH.getValue(0),
7914 N->getOperand(1),
7915 swapInH.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00007916 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007917 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, dl, Tys, Ops, 3);
Dale Johannesendd64c412009-02-04 00:33:20 +00007918 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00007919 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00007920 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00007921 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00007922 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00007923 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007924 Results.push_back(cpOutH.getValue(1));
7925 return;
7926 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007927 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00007928 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
7929 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007930 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00007931 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
7932 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007933 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00007934 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
7935 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007936 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00007937 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
7938 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007939 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00007940 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
7941 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007942 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00007943 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
7944 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007945 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00007946 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
7947 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00007948 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007949}
7950
Evan Cheng72261582005-12-20 06:22:03 +00007951const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
7952 switch (Opcode) {
7953 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00007954 case X86ISD::BSF: return "X86ISD::BSF";
7955 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00007956 case X86ISD::SHLD: return "X86ISD::SHLD";
7957 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00007958 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00007959 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00007960 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00007961 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00007962 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00007963 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00007964 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
7965 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
7966 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00007967 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00007968 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00007969 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00007970 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00007971 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00007972 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00007973 case X86ISD::COMI: return "X86ISD::COMI";
7974 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00007975 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00007976 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Evan Cheng72261582005-12-20 06:22:03 +00007977 case X86ISD::CMOV: return "X86ISD::CMOV";
7978 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00007979 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00007980 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
7981 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00007982 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00007983 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00007984 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00007985 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00007986 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00007987 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
7988 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00007989 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007990 case X86ISD::MMX_PINSRW: return "X86ISD::MMX_PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00007991 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Evan Cheng8ca29322006-11-10 21:43:37 +00007992 case X86ISD::FMAX: return "X86ISD::FMAX";
7993 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00007994 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
7995 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007996 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +00007997 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Rafael Espindola094fad32009-04-08 21:14:34 +00007998 case X86ISD::SegmentBaseAddress: return "X86ISD::SegmentBaseAddress";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007999 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00008000 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008001 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00008002 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
8003 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008004 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
8005 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
8006 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
8007 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
8008 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
8009 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00008010 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
8011 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00008012 case X86ISD::VSHL: return "X86ISD::VSHL";
8013 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00008014 case X86ISD::CMPPD: return "X86ISD::CMPPD";
8015 case X86ISD::CMPPS: return "X86ISD::CMPPS";
8016 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
8017 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
8018 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
8019 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
8020 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
8021 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
8022 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
8023 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008024 case X86ISD::ADD: return "X86ISD::ADD";
8025 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingd350e022008-12-12 21:15:41 +00008026 case X86ISD::SMUL: return "X86ISD::SMUL";
8027 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00008028 case X86ISD::INC: return "X86ISD::INC";
8029 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00008030 case X86ISD::OR: return "X86ISD::OR";
8031 case X86ISD::XOR: return "X86ISD::XOR";
8032 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00008033 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00008034 case X86ISD::PTEST: return "X86ISD::PTEST";
Dan Gohmand6708ea2009-08-15 01:38:56 +00008035 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008036 case X86ISD::MINGW_ALLOCA: return "X86ISD::MINGW_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00008037 }
8038}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008039
Chris Lattnerc9addb72007-03-30 23:15:24 +00008040// isLegalAddressingMode - Return true if the addressing mode represented
8041// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00008042bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00008043 const Type *Ty) const {
8044 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008045 CodeModel::Model M = getTargetMachine().getCodeModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00008046
Chris Lattnerc9addb72007-03-30 23:15:24 +00008047 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008048 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00008049 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00008050
Chris Lattnerc9addb72007-03-30 23:15:24 +00008051 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00008052 unsigned GVFlags =
8053 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008054
Chris Lattnerdfed4132009-07-10 07:38:24 +00008055 // If a reference to this global requires an extra load, we can't fold it.
8056 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00008057 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008058
Chris Lattnerdfed4132009-07-10 07:38:24 +00008059 // If BaseGV requires a register for the PIC base, we cannot also have a
8060 // BaseReg specified.
8061 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00008062 return false;
Evan Cheng52787842007-08-01 23:46:47 +00008063
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008064 // If lower 4G is not available, then we must use rip-relative addressing.
8065 if (Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
8066 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00008067 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008068
Chris Lattnerc9addb72007-03-30 23:15:24 +00008069 switch (AM.Scale) {
8070 case 0:
8071 case 1:
8072 case 2:
8073 case 4:
8074 case 8:
8075 // These scales always work.
8076 break;
8077 case 3:
8078 case 5:
8079 case 9:
8080 // These scales are formed with basereg+scalereg. Only accept if there is
8081 // no basereg yet.
8082 if (AM.HasBaseReg)
8083 return false;
8084 break;
8085 default: // Other stuff never works.
8086 return false;
8087 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008088
Chris Lattnerc9addb72007-03-30 23:15:24 +00008089 return true;
8090}
8091
8092
Evan Cheng2bd122c2007-10-26 01:56:11 +00008093bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00008094 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00008095 return false;
Evan Chenge127a732007-10-29 07:57:50 +00008096 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
8097 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00008098 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00008099 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00008100 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00008101}
8102
Owen Andersone50ed302009-08-10 22:56:29 +00008103bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008104 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00008105 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008106 unsigned NumBits1 = VT1.getSizeInBits();
8107 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00008108 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00008109 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00008110 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00008111}
Evan Cheng2bd122c2007-10-26 01:56:11 +00008112
Dan Gohman97121ba2009-04-08 00:15:30 +00008113bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00008114 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00008115 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00008116}
8117
Owen Andersone50ed302009-08-10 22:56:29 +00008118bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00008119 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00008120 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00008121}
8122
Owen Andersone50ed302009-08-10 22:56:29 +00008123bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00008124 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00008125 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00008126}
8127
Evan Cheng60c07e12006-07-05 22:17:51 +00008128/// isShuffleMaskLegal - Targets can use this to indicate that they only
8129/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
8130/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
8131/// are assumed to be legal.
8132bool
Eric Christopherfd179292009-08-27 18:07:15 +00008133X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00008134 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +00008135 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +00008136 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +00008137 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +00008138
Nate Begemana09008b2009-10-19 02:17:23 +00008139 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00008140 return (VT.getVectorNumElements() == 2 ||
8141 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
8142 isMOVLMask(M, VT) ||
8143 isSHUFPMask(M, VT) ||
8144 isPSHUFDMask(M, VT) ||
8145 isPSHUFHWMask(M, VT) ||
8146 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00008147 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00008148 isUNPCKLMask(M, VT) ||
8149 isUNPCKHMask(M, VT) ||
8150 isUNPCKL_v_undef_Mask(M, VT) ||
8151 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00008152}
8153
Dan Gohman7d8143f2008-04-09 20:09:42 +00008154bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00008155X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00008156 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00008157 unsigned NumElts = VT.getVectorNumElements();
8158 // FIXME: This collection of masks seems suspect.
8159 if (NumElts == 2)
8160 return true;
8161 if (NumElts == 4 && VT.getSizeInBits() == 128) {
8162 return (isMOVLMask(Mask, VT) ||
8163 isCommutedMOVLMask(Mask, VT, true) ||
8164 isSHUFPMask(Mask, VT) ||
8165 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00008166 }
8167 return false;
8168}
8169
8170//===----------------------------------------------------------------------===//
8171// X86 Scheduler Hooks
8172//===----------------------------------------------------------------------===//
8173
Mon P Wang63307c32008-05-05 19:05:59 +00008174// private utility function
8175MachineBasicBlock *
8176X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
8177 MachineBasicBlock *MBB,
8178 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008179 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008180 unsigned LoadOpc,
8181 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008182 unsigned notOpc,
8183 unsigned EAXreg,
8184 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00008185 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00008186 // For the atomic bitwise operator, we generate
8187 // thisMBB:
8188 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00008189 // ld t1 = [bitinstr.addr]
8190 // op t2 = t1, [bitinstr.val]
8191 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00008192 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
8193 // bz newMBB
8194 // fallthrough -->nextMBB
8195 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8196 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008197 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00008198 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00008199
Mon P Wang63307c32008-05-05 19:05:59 +00008200 /// First build the CFG
8201 MachineFunction *F = MBB->getParent();
8202 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008203 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
8204 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
8205 F->insert(MBBIter, newMBB);
8206 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008207
Dan Gohman14152b42010-07-06 20:24:04 +00008208 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
8209 nextMBB->splice(nextMBB->begin(), thisMBB,
8210 llvm::next(MachineBasicBlock::iterator(bInstr)),
8211 thisMBB->end());
8212 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008213
Mon P Wang63307c32008-05-05 19:05:59 +00008214 // Update thisMBB to fall through to newMBB
8215 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008216
Mon P Wang63307c32008-05-05 19:05:59 +00008217 // newMBB jumps to itself and fall through to nextMBB
8218 newMBB->addSuccessor(nextMBB);
8219 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008220
Mon P Wang63307c32008-05-05 19:05:59 +00008221 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00008222 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00008223 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00008224 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00008225 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00008226 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00008227 int numArgs = bInstr->getNumOperands() - 1;
8228 for (int i=0; i < numArgs; ++i)
8229 argOpers[i] = &bInstr->getOperand(i+1);
8230
8231 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00008232 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008233 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00008234
Dale Johannesen140be2d2008-08-19 18:47:28 +00008235 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008236 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00008237 for (int i=0; i <= lastAddrIndx; ++i)
8238 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008239
Dale Johannesen140be2d2008-08-19 18:47:28 +00008240 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008241 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00008242 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008243 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008244 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008245 tt = t1;
8246
Dale Johannesen140be2d2008-08-19 18:47:28 +00008247 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00008248 assert((argOpers[valArgIndx]->isReg() ||
8249 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00008250 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00008251 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008252 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00008253 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008254 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008255 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00008256 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008257
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00008258 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00008259 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008260
Dale Johannesene4d209d2009-02-03 20:21:25 +00008261 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00008262 for (int i=0; i <= lastAddrIndx; ++i)
8263 (*MIB).addOperand(*argOpers[i]);
8264 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00008265 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00008266 (*MIB).setMemRefs(bInstr->memoperands_begin(),
8267 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00008268
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00008269 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00008270 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00008271
Mon P Wang63307c32008-05-05 19:05:59 +00008272 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008273 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00008274
Dan Gohman14152b42010-07-06 20:24:04 +00008275 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00008276 return nextMBB;
8277}
8278
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00008279// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00008280MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008281X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
8282 MachineBasicBlock *MBB,
8283 unsigned regOpcL,
8284 unsigned regOpcH,
8285 unsigned immOpcL,
8286 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00008287 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008288 // For the atomic bitwise operator, we generate
8289 // thisMBB (instructions are in pairs, except cmpxchg8b)
8290 // ld t1,t2 = [bitinstr.addr]
8291 // newMBB:
8292 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
8293 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00008294 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008295 // mov ECX, EBX <- t5, t6
8296 // mov EAX, EDX <- t1, t2
8297 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
8298 // mov t3, t4 <- EAX, EDX
8299 // bz newMBB
8300 // result in out1, out2
8301 // fallthrough -->nextMBB
8302
8303 const TargetRegisterClass *RC = X86::GR32RegisterClass;
8304 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008305 const unsigned NotOpc = X86::NOT32r;
8306 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8307 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
8308 MachineFunction::iterator MBBIter = MBB;
8309 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00008310
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008311 /// First build the CFG
8312 MachineFunction *F = MBB->getParent();
8313 MachineBasicBlock *thisMBB = MBB;
8314 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
8315 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
8316 F->insert(MBBIter, newMBB);
8317 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008318
Dan Gohman14152b42010-07-06 20:24:04 +00008319 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
8320 nextMBB->splice(nextMBB->begin(), thisMBB,
8321 llvm::next(MachineBasicBlock::iterator(bInstr)),
8322 thisMBB->end());
8323 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008324
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008325 // Update thisMBB to fall through to newMBB
8326 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008327
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008328 // newMBB jumps to itself and fall through to nextMBB
8329 newMBB->addSuccessor(nextMBB);
8330 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008331
Dale Johannesene4d209d2009-02-03 20:21:25 +00008332 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008333 // Insert instructions into newMBB based on incoming instruction
8334 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00008335 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00008336 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008337 MachineOperand& dest1Oper = bInstr->getOperand(0);
8338 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00008339 MachineOperand* argOpers[2 + X86::AddrNumOperands];
8340 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008341 argOpers[i] = &bInstr->getOperand(i+2);
8342
Dan Gohman71ea4e52010-05-14 21:01:44 +00008343 // We use some of the operands multiple times, so conservatively just
8344 // clear any kill flags that might be present.
8345 if (argOpers[i]->isReg() && argOpers[i]->isUse())
8346 argOpers[i]->setIsKill(false);
8347 }
8348
Evan Chengad5b52f2010-01-08 19:14:57 +00008349 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00008350 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00008351
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008352 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008353 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008354 for (int i=0; i <= lastAddrIndx; ++i)
8355 (*MIB).addOperand(*argOpers[i]);
8356 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008357 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00008358 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00008359 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008360 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00008361 MachineOperand newOp3 = *(argOpers[3]);
8362 if (newOp3.isImm())
8363 newOp3.setImm(newOp3.getImm()+4);
8364 else
8365 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008366 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00008367 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008368
8369 // t3/4 are defined later, at the bottom of the loop
8370 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
8371 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008372 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008373 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008374 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008375 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
8376
Evan Cheng306b4ca2010-01-08 23:41:50 +00008377 // The subsequent operations should be using the destination registers of
8378 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00008379 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00008380 t1 = F->getRegInfo().createVirtualRegister(RC);
8381 t2 = F->getRegInfo().createVirtualRegister(RC);
8382 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
8383 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008384 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00008385 t1 = dest1Oper.getReg();
8386 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008387 }
8388
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008389 int valArgIndx = lastAddrIndx + 1;
8390 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00008391 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008392 "invalid operand");
8393 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
8394 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008395 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008396 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008397 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008398 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00008399 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00008400 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008401 (*MIB).addOperand(*argOpers[valArgIndx]);
8402 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00008403 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008404 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00008405 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008406 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008407 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008408 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008409 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00008410 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00008411 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008412 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008413
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00008414 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008415 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00008416 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008417 MIB.addReg(t2);
8418
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00008419 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008420 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00008421 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008422 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00008423
Dale Johannesene4d209d2009-02-03 20:21:25 +00008424 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008425 for (int i=0; i <= lastAddrIndx; ++i)
8426 (*MIB).addOperand(*argOpers[i]);
8427
8428 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00008429 (*MIB).setMemRefs(bInstr->memoperands_begin(),
8430 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008431
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00008432 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008433 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00008434 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008435 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00008436
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008437 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008438 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008439
Dan Gohman14152b42010-07-06 20:24:04 +00008440 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008441 return nextMBB;
8442}
8443
8444// private utility function
8445MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00008446X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
8447 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00008448 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00008449 // For the atomic min/max operator, we generate
8450 // thisMBB:
8451 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00008452 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00008453 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00008454 // cmp t1, t2
8455 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00008456 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00008457 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
8458 // bz newMBB
8459 // fallthrough -->nextMBB
8460 //
8461 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8462 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008463 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00008464 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00008465
Mon P Wang63307c32008-05-05 19:05:59 +00008466 /// First build the CFG
8467 MachineFunction *F = MBB->getParent();
8468 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008469 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
8470 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
8471 F->insert(MBBIter, newMBB);
8472 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008473
Dan Gohman14152b42010-07-06 20:24:04 +00008474 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
8475 nextMBB->splice(nextMBB->begin(), thisMBB,
8476 llvm::next(MachineBasicBlock::iterator(mInstr)),
8477 thisMBB->end());
8478 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008479
Mon P Wang63307c32008-05-05 19:05:59 +00008480 // Update thisMBB to fall through to newMBB
8481 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008482
Mon P Wang63307c32008-05-05 19:05:59 +00008483 // newMBB jumps to newMBB and fall through to nextMBB
8484 newMBB->addSuccessor(nextMBB);
8485 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008486
Dale Johannesene4d209d2009-02-03 20:21:25 +00008487 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00008488 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00008489 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00008490 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00008491 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00008492 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00008493 int numArgs = mInstr->getNumOperands() - 1;
8494 for (int i=0; i < numArgs; ++i)
8495 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008496
Mon P Wang63307c32008-05-05 19:05:59 +00008497 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00008498 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008499 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00008500
Mon P Wangab3e7472008-05-05 22:56:23 +00008501 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008502 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00008503 for (int i=0; i <= lastAddrIndx; ++i)
8504 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00008505
Mon P Wang63307c32008-05-05 19:05:59 +00008506 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00008507 assert((argOpers[valArgIndx]->isReg() ||
8508 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00008509 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00008510
8511 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00008512 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00008513 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00008514 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008515 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00008516 (*MIB).addOperand(*argOpers[valArgIndx]);
8517
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00008518 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00008519 MIB.addReg(t1);
8520
Dale Johannesene4d209d2009-02-03 20:21:25 +00008521 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00008522 MIB.addReg(t1);
8523 MIB.addReg(t2);
8524
8525 // Generate movc
8526 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008527 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00008528 MIB.addReg(t2);
8529 MIB.addReg(t1);
8530
8531 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00008532 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00008533 for (int i=0; i <= lastAddrIndx; ++i)
8534 (*MIB).addOperand(*argOpers[i]);
8535 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00008536 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00008537 (*MIB).setMemRefs(mInstr->memoperands_begin(),
8538 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +00008539
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00008540 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00008541 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00008542
Mon P Wang63307c32008-05-05 19:05:59 +00008543 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008544 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00008545
Dan Gohman14152b42010-07-06 20:24:04 +00008546 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00008547 return nextMBB;
8548}
8549
Eric Christopherf83a5de2009-08-27 18:08:16 +00008550// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00008551// or XMM0_V32I8 in AVX all of this code can be replaced with that
8552// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +00008553MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +00008554X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +00008555 unsigned numArgs, bool memArg) const {
Eric Christopherb120ab42009-08-18 22:50:32 +00008556
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00008557 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
8558 "Target must have SSE4.2 or AVX features enabled");
8559
Eric Christopherb120ab42009-08-18 22:50:32 +00008560 DebugLoc dl = MI->getDebugLoc();
8561 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8562
8563 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00008564
8565 if (!Subtarget->hasAVX()) {
8566 if (memArg)
8567 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
8568 else
8569 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
8570 } else {
8571 if (memArg)
8572 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
8573 else
8574 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
8575 }
Eric Christopherb120ab42009-08-18 22:50:32 +00008576
8577 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(Opc));
8578
8579 for (unsigned i = 0; i < numArgs; ++i) {
8580 MachineOperand &Op = MI->getOperand(i+1);
8581
8582 if (!(Op.isReg() && Op.isImplicit()))
8583 MIB.addOperand(Op);
8584 }
8585
8586 BuildMI(BB, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
8587 .addReg(X86::XMM0);
8588
Dan Gohman14152b42010-07-06 20:24:04 +00008589 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +00008590
8591 return BB;
8592}
8593
8594MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +00008595X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
8596 MachineInstr *MI,
8597 MachineBasicBlock *MBB) const {
8598 // Emit code to save XMM registers to the stack. The ABI says that the
8599 // number of registers to save is given in %al, so it's theoretically
8600 // possible to do an indirect jump trick to avoid saving all of them,
8601 // however this code takes a simpler approach and just executes all
8602 // of the stores if %al is non-zero. It's less code, and it's probably
8603 // easier on the hardware branch predictor, and stores aren't all that
8604 // expensive anyway.
8605
8606 // Create the new basic blocks. One block contains all the XMM stores,
8607 // and one block is the final destination regardless of whether any
8608 // stores were performed.
8609 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
8610 MachineFunction *F = MBB->getParent();
8611 MachineFunction::iterator MBBIter = MBB;
8612 ++MBBIter;
8613 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
8614 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
8615 F->insert(MBBIter, XMMSaveMBB);
8616 F->insert(MBBIter, EndMBB);
8617
Dan Gohman14152b42010-07-06 20:24:04 +00008618 // Transfer the remainder of MBB and its successor edges to EndMBB.
8619 EndMBB->splice(EndMBB->begin(), MBB,
8620 llvm::next(MachineBasicBlock::iterator(MI)),
8621 MBB->end());
8622 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
8623
Dan Gohmand6708ea2009-08-15 01:38:56 +00008624 // The original block will now fall through to the XMM save block.
8625 MBB->addSuccessor(XMMSaveMBB);
8626 // The XMMSaveMBB will fall through to the end block.
8627 XMMSaveMBB->addSuccessor(EndMBB);
8628
8629 // Now add the instructions.
8630 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8631 DebugLoc DL = MI->getDebugLoc();
8632
8633 unsigned CountReg = MI->getOperand(0).getReg();
8634 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
8635 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
8636
8637 if (!Subtarget->isTargetWin64()) {
8638 // If %al is 0, branch around the XMM save block.
8639 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008640 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008641 MBB->addSuccessor(EndMBB);
8642 }
8643
8644 // In the XMM save block, save all the XMM argument registers.
8645 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
8646 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +00008647 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +00008648 F->getMachineMemOperand(
8649 PseudoSourceValue::getFixedStack(RegSaveFrameIndex),
8650 MachineMemOperand::MOStore, Offset,
8651 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008652 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
8653 .addFrameIndex(RegSaveFrameIndex)
8654 .addImm(/*Scale=*/1)
8655 .addReg(/*IndexReg=*/0)
8656 .addImm(/*Disp=*/Offset)
8657 .addReg(/*Segment=*/0)
8658 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +00008659 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008660 }
8661
Dan Gohman14152b42010-07-06 20:24:04 +00008662 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +00008663
8664 return EndMBB;
8665}
Mon P Wang63307c32008-05-05 19:05:59 +00008666
Evan Cheng60c07e12006-07-05 22:17:51 +00008667MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +00008668X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00008669 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +00008670 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8671 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +00008672
Chris Lattner52600972009-09-02 05:57:00 +00008673 // To "insert" a SELECT_CC instruction, we actually have to insert the
8674 // diamond control-flow pattern. The incoming instruction knows the
8675 // destination vreg to set, the condition code register to branch on, the
8676 // true/false values to select between, and a branch opcode to use.
8677 const BasicBlock *LLVM_BB = BB->getBasicBlock();
8678 MachineFunction::iterator It = BB;
8679 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +00008680
Chris Lattner52600972009-09-02 05:57:00 +00008681 // thisMBB:
8682 // ...
8683 // TrueVal = ...
8684 // cmpTY ccX, r1, r2
8685 // bCC copy1MBB
8686 // fallthrough --> copy0MBB
8687 MachineBasicBlock *thisMBB = BB;
8688 MachineFunction *F = BB->getParent();
8689 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
8690 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +00008691 F->insert(It, copy0MBB);
8692 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +00008693
Bill Wendling730c07e2010-06-25 20:48:10 +00008694 // If the EFLAGS register isn't dead in the terminator, then claim that it's
8695 // live into the sink and copy blocks.
8696 const MachineFunction *MF = BB->getParent();
8697 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
8698 BitVector ReservedRegs = TRI->getReservedRegs(*MF);
Bill Wendling730c07e2010-06-25 20:48:10 +00008699
Dan Gohman14152b42010-07-06 20:24:04 +00008700 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
8701 const MachineOperand &MO = MI->getOperand(I);
8702 if (!MO.isReg() || !MO.isUse() || MO.isKill()) continue;
Bill Wendling730c07e2010-06-25 20:48:10 +00008703 unsigned Reg = MO.getReg();
8704 if (Reg != X86::EFLAGS) continue;
8705 copy0MBB->addLiveIn(Reg);
8706 sinkMBB->addLiveIn(Reg);
8707 }
8708
Dan Gohman14152b42010-07-06 20:24:04 +00008709 // Transfer the remainder of BB and its successor edges to sinkMBB.
8710 sinkMBB->splice(sinkMBB->begin(), BB,
8711 llvm::next(MachineBasicBlock::iterator(MI)),
8712 BB->end());
8713 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
8714
8715 // Add the true and fallthrough blocks as its successors.
8716 BB->addSuccessor(copy0MBB);
8717 BB->addSuccessor(sinkMBB);
8718
8719 // Create the conditional branch instruction.
8720 unsigned Opc =
8721 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
8722 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
8723
Chris Lattner52600972009-09-02 05:57:00 +00008724 // copy0MBB:
8725 // %FalseValue = ...
8726 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +00008727 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00008728
Chris Lattner52600972009-09-02 05:57:00 +00008729 // sinkMBB:
8730 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
8731 // ...
Dan Gohman14152b42010-07-06 20:24:04 +00008732 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
8733 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +00008734 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
8735 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
8736
Dan Gohman14152b42010-07-06 20:24:04 +00008737 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +00008738 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +00008739}
8740
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008741MachineBasicBlock *
8742X86TargetLowering::EmitLoweredMingwAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00008743 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008744 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8745 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008746
8747 // The lowering is pretty easy: we're just emitting the call to _alloca. The
8748 // non-trivial part is impdef of ESP.
8749 // FIXME: The code should be tweaked as soon as we'll try to do codegen for
8750 // mingw-w64.
8751
Dan Gohman14152b42010-07-06 20:24:04 +00008752 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008753 .addExternalSymbol("_alloca")
8754 .addReg(X86::EAX, RegState::Implicit)
8755 .addReg(X86::ESP, RegState::Implicit)
8756 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
8757 .addReg(X86::ESP, RegState::Define | RegState::Implicit);
8758
Dan Gohman14152b42010-07-06 20:24:04 +00008759 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008760 return BB;
8761}
Chris Lattner52600972009-09-02 05:57:00 +00008762
8763MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +00008764X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
8765 MachineBasicBlock *BB) const {
8766 // This is pretty easy. We're taking the value that we received from
8767 // our load from the relocation, sticking it in either RDI (x86-64)
8768 // or EAX and doing an indirect call. The return value will then
8769 // be in the normal return register.
Eric Christopher54415362010-06-08 22:04:25 +00008770 const X86InstrInfo *TII
8771 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +00008772 DebugLoc DL = MI->getDebugLoc();
8773 MachineFunction *F = BB->getParent();
8774
Eric Christopher54415362010-06-08 22:04:25 +00008775 assert(MI->getOperand(3).isGlobal() && "This should be a global");
8776
Eric Christopher30ef0e52010-06-03 04:07:48 +00008777 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +00008778 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
8779 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +00008780 .addReg(X86::RIP)
8781 .addImm(0).addReg(0)
8782 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
8783 MI->getOperand(3).getTargetFlags())
8784 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +00008785 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +00008786 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +00008787 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +00008788 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
8789 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +00008790 .addReg(0)
8791 .addImm(0).addReg(0)
8792 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
8793 MI->getOperand(3).getTargetFlags())
8794 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +00008795 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +00008796 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +00008797 } else {
Dan Gohman14152b42010-07-06 20:24:04 +00008798 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
8799 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +00008800 .addReg(TII->getGlobalBaseReg(F))
8801 .addImm(0).addReg(0)
8802 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
8803 MI->getOperand(3).getTargetFlags())
8804 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +00008805 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +00008806 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +00008807 }
8808
Dan Gohman14152b42010-07-06 20:24:04 +00008809 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +00008810 return BB;
8811}
8812
8813MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00008814X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00008815 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00008816 switch (MI->getOpcode()) {
8817 default: assert(false && "Unexpected instr type to insert");
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008818 case X86::MINGW_ALLOCA:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00008819 return EmitLoweredMingwAlloca(MI, BB);
Eric Christopher30ef0e52010-06-03 04:07:48 +00008820 case X86::TLSCall_32:
8821 case X86::TLSCall_64:
8822 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +00008823 case X86::CMOV_GR8:
Mon P Wang9e5ecb82008-12-12 01:25:51 +00008824 case X86::CMOV_V1I64:
Evan Cheng60c07e12006-07-05 22:17:51 +00008825 case X86::CMOV_FR32:
8826 case X86::CMOV_FR64:
8827 case X86::CMOV_V4F32:
8828 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +00008829 case X86::CMOV_V2I64:
Chris Lattner314a1132010-03-14 18:31:44 +00008830 case X86::CMOV_GR16:
8831 case X86::CMOV_GR32:
8832 case X86::CMOV_RFP32:
8833 case X86::CMOV_RFP64:
8834 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00008835 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +00008836
Dale Johannesen849f2142007-07-03 00:53:03 +00008837 case X86::FP32_TO_INT16_IN_MEM:
8838 case X86::FP32_TO_INT32_IN_MEM:
8839 case X86::FP32_TO_INT64_IN_MEM:
8840 case X86::FP64_TO_INT16_IN_MEM:
8841 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00008842 case X86::FP64_TO_INT64_IN_MEM:
8843 case X86::FP80_TO_INT16_IN_MEM:
8844 case X86::FP80_TO_INT32_IN_MEM:
8845 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +00008846 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8847 DebugLoc DL = MI->getDebugLoc();
8848
Evan Cheng60c07e12006-07-05 22:17:51 +00008849 // Change the floating point control register to use "round towards zero"
8850 // mode when truncating to an integer value.
8851 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +00008852 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +00008853 addFrameReference(BuildMI(*BB, MI, DL,
8854 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008855
8856 // Load the old value of the high byte of the control word...
8857 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +00008858 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +00008859 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008860 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008861
8862 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +00008863 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00008864 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00008865
8866 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +00008867 addFrameReference(BuildMI(*BB, MI, DL,
8868 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008869
8870 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +00008871 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00008872 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00008873
8874 // Get the X86 opcode to use.
8875 unsigned Opc;
8876 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008877 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00008878 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
8879 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
8880 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
8881 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
8882 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
8883 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00008884 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
8885 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
8886 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00008887 }
8888
8889 X86AddressMode AM;
8890 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +00008891 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00008892 AM.BaseType = X86AddressMode::RegBase;
8893 AM.Base.Reg = Op.getReg();
8894 } else {
8895 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +00008896 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +00008897 }
8898 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +00008899 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00008900 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008901 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +00008902 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00008903 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008904 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +00008905 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00008906 AM.GV = Op.getGlobal();
8907 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00008908 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008909 }
Dan Gohman14152b42010-07-06 20:24:04 +00008910 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00008911 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00008912
8913 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +00008914 addFrameReference(BuildMI(*BB, MI, DL,
8915 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008916
Dan Gohman14152b42010-07-06 20:24:04 +00008917 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00008918 return BB;
8919 }
Eric Christopherb120ab42009-08-18 22:50:32 +00008920 // String/text processing lowering.
8921 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00008922 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +00008923 return EmitPCMP(MI, BB, 3, false /* in-mem */);
8924 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00008925 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +00008926 return EmitPCMP(MI, BB, 3, true /* in-mem */);
8927 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00008928 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +00008929 return EmitPCMP(MI, BB, 5, false /* in mem */);
8930 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00008931 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +00008932 return EmitPCMP(MI, BB, 5, true /* in mem */);
8933
8934 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +00008935 case X86::ATOMAND32:
8936 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008937 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00008938 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008939 X86::NOT32r, X86::EAX,
8940 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00008941 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +00008942 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
8943 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00008944 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008945 X86::NOT32r, X86::EAX,
8946 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00008947 case X86::ATOMXOR32:
8948 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008949 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00008950 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008951 X86::NOT32r, X86::EAX,
8952 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008953 case X86::ATOMNAND32:
8954 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008955 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00008956 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008957 X86::NOT32r, X86::EAX,
8958 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +00008959 case X86::ATOMMIN32:
8960 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
8961 case X86::ATOMMAX32:
8962 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
8963 case X86::ATOMUMIN32:
8964 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
8965 case X86::ATOMUMAX32:
8966 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +00008967
8968 case X86::ATOMAND16:
8969 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
8970 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00008971 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008972 X86::NOT16r, X86::AX,
8973 X86::GR16RegisterClass);
8974 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +00008975 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008976 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00008977 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008978 X86::NOT16r, X86::AX,
8979 X86::GR16RegisterClass);
8980 case X86::ATOMXOR16:
8981 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
8982 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00008983 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008984 X86::NOT16r, X86::AX,
8985 X86::GR16RegisterClass);
8986 case X86::ATOMNAND16:
8987 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
8988 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00008989 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008990 X86::NOT16r, X86::AX,
8991 X86::GR16RegisterClass, true);
8992 case X86::ATOMMIN16:
8993 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
8994 case X86::ATOMMAX16:
8995 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
8996 case X86::ATOMUMIN16:
8997 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
8998 case X86::ATOMUMAX16:
8999 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
9000
9001 case X86::ATOMAND8:
9002 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
9003 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009004 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009005 X86::NOT8r, X86::AL,
9006 X86::GR8RegisterClass);
9007 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +00009008 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009009 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009010 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009011 X86::NOT8r, X86::AL,
9012 X86::GR8RegisterClass);
9013 case X86::ATOMXOR8:
9014 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
9015 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009016 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009017 X86::NOT8r, X86::AL,
9018 X86::GR8RegisterClass);
9019 case X86::ATOMNAND8:
9020 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
9021 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009022 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009023 X86::NOT8r, X86::AL,
9024 X86::GR8RegisterClass, true);
9025 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009026 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +00009027 case X86::ATOMAND64:
9028 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00009029 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009030 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +00009031 X86::NOT64r, X86::RAX,
9032 X86::GR64RegisterClass);
9033 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +00009034 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
9035 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009036 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +00009037 X86::NOT64r, X86::RAX,
9038 X86::GR64RegisterClass);
9039 case X86::ATOMXOR64:
9040 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00009041 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009042 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +00009043 X86::NOT64r, X86::RAX,
9044 X86::GR64RegisterClass);
9045 case X86::ATOMNAND64:
9046 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
9047 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009048 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +00009049 X86::NOT64r, X86::RAX,
9050 X86::GR64RegisterClass, true);
9051 case X86::ATOMMIN64:
9052 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
9053 case X86::ATOMMAX64:
9054 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
9055 case X86::ATOMUMIN64:
9056 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
9057 case X86::ATOMUMAX64:
9058 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009059
9060 // This group does 64-bit operations on a 32-bit host.
9061 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00009062 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009063 X86::AND32rr, X86::AND32rr,
9064 X86::AND32ri, X86::AND32ri,
9065 false);
9066 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00009067 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009068 X86::OR32rr, X86::OR32rr,
9069 X86::OR32ri, X86::OR32ri,
9070 false);
9071 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00009072 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009073 X86::XOR32rr, X86::XOR32rr,
9074 X86::XOR32ri, X86::XOR32ri,
9075 false);
9076 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00009077 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009078 X86::AND32rr, X86::AND32rr,
9079 X86::AND32ri, X86::AND32ri,
9080 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009081 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00009082 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009083 X86::ADD32rr, X86::ADC32rr,
9084 X86::ADD32ri, X86::ADC32ri,
9085 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009086 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00009087 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009088 X86::SUB32rr, X86::SBB32rr,
9089 X86::SUB32ri, X86::SBB32ri,
9090 false);
Dale Johannesen880ae362008-10-03 22:25:52 +00009091 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00009092 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +00009093 X86::MOV32rr, X86::MOV32rr,
9094 X86::MOV32ri, X86::MOV32ri,
9095 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +00009096 case X86::VASTART_SAVE_XMM_REGS:
9097 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +00009098 }
9099}
9100
9101//===----------------------------------------------------------------------===//
9102// X86 Optimization Hooks
9103//===----------------------------------------------------------------------===//
9104
Dan Gohman475871a2008-07-27 21:46:04 +00009105void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00009106 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00009107 APInt &KnownZero,
9108 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00009109 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00009110 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009111 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00009112 assert((Opc >= ISD::BUILTIN_OP_END ||
9113 Opc == ISD::INTRINSIC_WO_CHAIN ||
9114 Opc == ISD::INTRINSIC_W_CHAIN ||
9115 Opc == ISD::INTRINSIC_VOID) &&
9116 "Should use MaskedValueIsZero if you don't know whether Op"
9117 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009118
Dan Gohmanf4f92f52008-02-13 23:07:24 +00009119 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009120 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00009121 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +00009122 case X86ISD::ADD:
9123 case X86ISD::SUB:
9124 case X86ISD::SMUL:
9125 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +00009126 case X86ISD::INC:
9127 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +00009128 case X86ISD::OR:
9129 case X86ISD::XOR:
9130 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +00009131 // These nodes' second result is a boolean.
9132 if (Op.getResNo() == 0)
9133 break;
9134 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009135 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00009136 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
9137 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +00009138 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009139 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009140}
Chris Lattner259e97c2006-01-31 19:43:35 +00009141
Evan Cheng206ee9d2006-07-07 08:33:52 +00009142/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +00009143/// node is a GlobalAddress + offset.
9144bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +00009145 const GlobalValue* &GA,
9146 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +00009147 if (N->getOpcode() == X86ISD::Wrapper) {
9148 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00009149 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00009150 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +00009151 return true;
9152 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00009153 }
Evan Chengad4196b2008-05-12 19:56:52 +00009154 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +00009155}
9156
Evan Cheng206ee9d2006-07-07 08:33:52 +00009157/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
9158/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
9159/// if the load addresses are consecutive, non-overlapping, and in the right
Nate Begemanfdea31a2010-03-24 20:49:50 +00009160/// order.
Dan Gohman475871a2008-07-27 21:46:04 +00009161static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00009162 const TargetLowering &TLI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009163 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00009164 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00009165 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
Mon P Wang1e955802009-04-03 02:43:30 +00009166
Eli Friedman7a5e5552009-06-07 06:52:44 +00009167 if (VT.getSizeInBits() != 128)
9168 return SDValue();
9169
Nate Begemanfdea31a2010-03-24 20:49:50 +00009170 SmallVector<SDValue, 16> Elts;
9171 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
9172 Elts.push_back(DAG.getShuffleScalarElt(SVN, i));
9173
9174 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00009175}
Evan Chengd880b972008-05-09 21:53:03 +00009176
Dan Gohman1bbf72b2010-03-15 23:23:03 +00009177/// PerformShuffleCombine - Detect vector gather/scatter index generation
9178/// and convert it from being a bunch of shuffles and extracts to a simple
9179/// store and scalar loads to extract the elements.
9180static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
9181 const TargetLowering &TLI) {
9182 SDValue InputVector = N->getOperand(0);
9183
9184 // Only operate on vectors of 4 elements, where the alternative shuffling
9185 // gets to be more expensive.
9186 if (InputVector.getValueType() != MVT::v4i32)
9187 return SDValue();
9188
9189 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
9190 // single use which is a sign-extend or zero-extend, and all elements are
9191 // used.
9192 SmallVector<SDNode *, 4> Uses;
9193 unsigned ExtractedElements = 0;
9194 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
9195 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
9196 if (UI.getUse().getResNo() != InputVector.getResNo())
9197 return SDValue();
9198
9199 SDNode *Extract = *UI;
9200 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
9201 return SDValue();
9202
9203 if (Extract->getValueType(0) != MVT::i32)
9204 return SDValue();
9205 if (!Extract->hasOneUse())
9206 return SDValue();
9207 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
9208 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
9209 return SDValue();
9210 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
9211 return SDValue();
9212
9213 // Record which element was extracted.
9214 ExtractedElements |=
9215 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
9216
9217 Uses.push_back(Extract);
9218 }
9219
9220 // If not all the elements were used, this may not be worthwhile.
9221 if (ExtractedElements != 15)
9222 return SDValue();
9223
9224 // Ok, we've now decided to do the transformation.
9225 DebugLoc dl = InputVector.getDebugLoc();
9226
9227 // Store the value to a temporary stack slot.
9228 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Eric Christopher90eb4022010-07-22 00:26:08 +00009229 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr, NULL,
9230 0, false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00009231
9232 // Replace each use (extract) with a load of the appropriate element.
9233 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
9234 UE = Uses.end(); UI != UE; ++UI) {
9235 SDNode *Extract = *UI;
9236
9237 // Compute the element's address.
9238 SDValue Idx = Extract->getOperand(1);
9239 unsigned EltSize =
9240 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
9241 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
9242 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
9243
Eric Christopher90eb4022010-07-22 00:26:08 +00009244 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(),
9245 OffsetVal, StackPtr);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00009246
9247 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +00009248 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
9249 ScalarAddr, NULL, 0, false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00009250
9251 // Replace the exact with the load.
9252 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
9253 }
9254
9255 // The replacement was made in place; don't return anything.
9256 return SDValue();
9257}
9258
Chris Lattner83e6c992006-10-04 06:57:07 +00009259/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009260static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +00009261 const X86Subtarget *Subtarget) {
9262 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009263 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +00009264 // Get the LHS/RHS of the select.
9265 SDValue LHS = N->getOperand(1);
9266 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +00009267
Dan Gohman670e5392009-09-21 18:03:22 +00009268 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +00009269 // instructions match the semantics of the common C idiom x<y?x:y but not
9270 // x<=y?x:y, because of how they handle negative zero (which can be
9271 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +00009272 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00009273 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +00009274 Cond.getOpcode() == ISD::SETCC) {
9275 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009276
Chris Lattner47b4ce82009-03-11 05:48:52 +00009277 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +00009278 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +00009279 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
9280 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +00009281 switch (CC) {
9282 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +00009283 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +00009284 // Converting this to a min would handle NaNs incorrectly, and swapping
9285 // the operands would cause it to handle comparisons between positive
9286 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +00009287 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +00009288 if (!UnsafeFPMath &&
9289 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9290 break;
9291 std::swap(LHS, RHS);
9292 }
Dan Gohman670e5392009-09-21 18:03:22 +00009293 Opcode = X86ISD::FMIN;
9294 break;
9295 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +00009296 // Converting this to a min would handle comparisons between positive
9297 // and negative zero incorrectly.
9298 if (!UnsafeFPMath &&
9299 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
9300 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009301 Opcode = X86ISD::FMIN;
9302 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +00009303 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +00009304 // Converting this to a min would handle both negative zeros and NaNs
9305 // incorrectly, but we can swap the operands to fix both.
9306 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00009307 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009308 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +00009309 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009310 Opcode = X86ISD::FMIN;
9311 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009312
Dan Gohman670e5392009-09-21 18:03:22 +00009313 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +00009314 // Converting this to a max would handle comparisons between positive
9315 // and negative zero incorrectly.
9316 if (!UnsafeFPMath &&
9317 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
9318 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009319 Opcode = X86ISD::FMAX;
9320 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +00009321 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +00009322 // Converting this to a max would handle NaNs incorrectly, and swapping
9323 // the operands would cause it to handle comparisons between positive
9324 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +00009325 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +00009326 if (!UnsafeFPMath &&
9327 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9328 break;
9329 std::swap(LHS, RHS);
9330 }
Dan Gohman670e5392009-09-21 18:03:22 +00009331 Opcode = X86ISD::FMAX;
9332 break;
9333 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +00009334 // Converting this to a max would handle both negative zeros and NaNs
9335 // incorrectly, but we can swap the operands to fix both.
9336 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00009337 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009338 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009339 case ISD::SETGE:
9340 Opcode = X86ISD::FMAX;
9341 break;
Chris Lattner83e6c992006-10-04 06:57:07 +00009342 }
Dan Gohman670e5392009-09-21 18:03:22 +00009343 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +00009344 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
9345 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +00009346 switch (CC) {
9347 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +00009348 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +00009349 // Converting this to a min would handle comparisons between positive
9350 // and negative zero incorrectly, and swapping the operands would
9351 // cause it to handle NaNs incorrectly.
9352 if (!UnsafeFPMath &&
9353 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +00009354 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +00009355 break;
9356 std::swap(LHS, RHS);
9357 }
Dan Gohman670e5392009-09-21 18:03:22 +00009358 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +00009359 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009360 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +00009361 // Converting this to a min would handle NaNs incorrectly.
9362 if (!UnsafeFPMath &&
9363 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
9364 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009365 Opcode = X86ISD::FMIN;
9366 break;
9367 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +00009368 // Converting this to a min would handle both negative zeros and NaNs
9369 // incorrectly, but we can swap the operands to fix both.
9370 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00009371 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009372 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009373 case ISD::SETGE:
9374 Opcode = X86ISD::FMIN;
9375 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009376
Dan Gohman670e5392009-09-21 18:03:22 +00009377 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +00009378 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +00009379 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +00009380 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009381 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +00009382 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009383 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +00009384 // Converting this to a max would handle comparisons between positive
9385 // and negative zero incorrectly, and swapping the operands would
9386 // cause it to handle NaNs incorrectly.
9387 if (!UnsafeFPMath &&
9388 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +00009389 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +00009390 break;
9391 std::swap(LHS, RHS);
9392 }
Dan Gohman670e5392009-09-21 18:03:22 +00009393 Opcode = X86ISD::FMAX;
9394 break;
9395 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +00009396 // Converting this to a max would handle both negative zeros and NaNs
9397 // incorrectly, but we can swap the operands to fix both.
9398 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00009399 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009400 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +00009401 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009402 Opcode = X86ISD::FMAX;
9403 break;
9404 }
Chris Lattner83e6c992006-10-04 06:57:07 +00009405 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009406
Chris Lattner47b4ce82009-03-11 05:48:52 +00009407 if (Opcode)
9408 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +00009409 }
Eric Christopherfd179292009-08-27 18:07:15 +00009410
Chris Lattnerd1980a52009-03-12 06:52:53 +00009411 // If this is a select between two integer constants, try to do some
9412 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +00009413 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
9414 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +00009415 // Don't do this for crazy integer types.
9416 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
9417 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +00009418 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +00009419 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +00009420
Chris Lattnercee56e72009-03-13 05:53:31 +00009421 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +00009422 // Efficiently invertible.
9423 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
9424 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
9425 isa<ConstantSDNode>(Cond.getOperand(1))))) {
9426 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +00009427 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +00009428 }
Eric Christopherfd179292009-08-27 18:07:15 +00009429
Chris Lattnerd1980a52009-03-12 06:52:53 +00009430 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00009431 if (FalseC->getAPIntValue() == 0 &&
9432 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00009433 if (NeedsCondInvert) // Invert the condition if needed.
9434 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
9435 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009436
Chris Lattnerd1980a52009-03-12 06:52:53 +00009437 // Zero extend the condition if needed.
9438 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009439
Chris Lattnercee56e72009-03-13 05:53:31 +00009440 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +00009441 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +00009442 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +00009443 }
Eric Christopherfd179292009-08-27 18:07:15 +00009444
Chris Lattner97a29a52009-03-13 05:22:11 +00009445 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +00009446 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +00009447 if (NeedsCondInvert) // Invert the condition if needed.
9448 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
9449 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009450
Chris Lattner97a29a52009-03-13 05:22:11 +00009451 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00009452 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
9453 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00009454 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +00009455 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +00009456 }
Eric Christopherfd179292009-08-27 18:07:15 +00009457
Chris Lattnercee56e72009-03-13 05:53:31 +00009458 // Optimize cases that will turn into an LEA instruction. This requires
9459 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +00009460 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +00009461 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00009462 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +00009463
Chris Lattnercee56e72009-03-13 05:53:31 +00009464 bool isFastMultiplier = false;
9465 if (Diff < 10) {
9466 switch ((unsigned char)Diff) {
9467 default: break;
9468 case 1: // result = add base, cond
9469 case 2: // result = lea base( , cond*2)
9470 case 3: // result = lea base(cond, cond*2)
9471 case 4: // result = lea base( , cond*4)
9472 case 5: // result = lea base(cond, cond*4)
9473 case 8: // result = lea base( , cond*8)
9474 case 9: // result = lea base(cond, cond*8)
9475 isFastMultiplier = true;
9476 break;
9477 }
9478 }
Eric Christopherfd179292009-08-27 18:07:15 +00009479
Chris Lattnercee56e72009-03-13 05:53:31 +00009480 if (isFastMultiplier) {
9481 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
9482 if (NeedsCondInvert) // Invert the condition if needed.
9483 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
9484 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009485
Chris Lattnercee56e72009-03-13 05:53:31 +00009486 // Zero extend the condition if needed.
9487 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
9488 Cond);
9489 // Scale the condition by the difference.
9490 if (Diff != 1)
9491 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
9492 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009493
Chris Lattnercee56e72009-03-13 05:53:31 +00009494 // Add the base if non-zero.
9495 if (FalseC->getAPIntValue() != 0)
9496 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9497 SDValue(FalseC, 0));
9498 return Cond;
9499 }
Eric Christopherfd179292009-08-27 18:07:15 +00009500 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00009501 }
9502 }
Eric Christopherfd179292009-08-27 18:07:15 +00009503
Dan Gohman475871a2008-07-27 21:46:04 +00009504 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +00009505}
9506
Chris Lattnerd1980a52009-03-12 06:52:53 +00009507/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
9508static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
9509 TargetLowering::DAGCombinerInfo &DCI) {
9510 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +00009511
Chris Lattnerd1980a52009-03-12 06:52:53 +00009512 // If the flag operand isn't dead, don't touch this CMOV.
9513 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
9514 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +00009515
Chris Lattnerd1980a52009-03-12 06:52:53 +00009516 // If this is a select between two integer constants, try to do some
9517 // optimizations. Note that the operands are ordered the opposite of SELECT
9518 // operands.
9519 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
9520 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
9521 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
9522 // larger than FalseC (the false value).
9523 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
Eric Christopherfd179292009-08-27 18:07:15 +00009524
Chris Lattnerd1980a52009-03-12 06:52:53 +00009525 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
9526 CC = X86::GetOppositeBranchCondition(CC);
9527 std::swap(TrueC, FalseC);
9528 }
Eric Christopherfd179292009-08-27 18:07:15 +00009529
Chris Lattnerd1980a52009-03-12 06:52:53 +00009530 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00009531 // This is efficient for any integer data type (including i8/i16) and
9532 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +00009533 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
9534 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00009535 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9536 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009537
Chris Lattnerd1980a52009-03-12 06:52:53 +00009538 // Zero extend the condition if needed.
9539 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009540
Chris Lattnerd1980a52009-03-12 06:52:53 +00009541 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
9542 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +00009543 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +00009544 if (N->getNumValues() == 2) // Dead flag value?
9545 return DCI.CombineTo(N, Cond, SDValue());
9546 return Cond;
9547 }
Eric Christopherfd179292009-08-27 18:07:15 +00009548
Chris Lattnercee56e72009-03-13 05:53:31 +00009549 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
9550 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +00009551 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
9552 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00009553 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9554 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009555
Chris Lattner97a29a52009-03-13 05:22:11 +00009556 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00009557 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
9558 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00009559 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9560 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +00009561
Chris Lattner97a29a52009-03-13 05:22:11 +00009562 if (N->getNumValues() == 2) // Dead flag value?
9563 return DCI.CombineTo(N, Cond, SDValue());
9564 return Cond;
9565 }
Eric Christopherfd179292009-08-27 18:07:15 +00009566
Chris Lattnercee56e72009-03-13 05:53:31 +00009567 // Optimize cases that will turn into an LEA instruction. This requires
9568 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +00009569 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +00009570 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00009571 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +00009572
Chris Lattnercee56e72009-03-13 05:53:31 +00009573 bool isFastMultiplier = false;
9574 if (Diff < 10) {
9575 switch ((unsigned char)Diff) {
9576 default: break;
9577 case 1: // result = add base, cond
9578 case 2: // result = lea base( , cond*2)
9579 case 3: // result = lea base(cond, cond*2)
9580 case 4: // result = lea base( , cond*4)
9581 case 5: // result = lea base(cond, cond*4)
9582 case 8: // result = lea base( , cond*8)
9583 case 9: // result = lea base(cond, cond*8)
9584 isFastMultiplier = true;
9585 break;
9586 }
9587 }
Eric Christopherfd179292009-08-27 18:07:15 +00009588
Chris Lattnercee56e72009-03-13 05:53:31 +00009589 if (isFastMultiplier) {
9590 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
9591 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00009592 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9593 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +00009594 // Zero extend the condition if needed.
9595 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
9596 Cond);
9597 // Scale the condition by the difference.
9598 if (Diff != 1)
9599 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
9600 DAG.getConstant(Diff, Cond.getValueType()));
9601
9602 // Add the base if non-zero.
9603 if (FalseC->getAPIntValue() != 0)
9604 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9605 SDValue(FalseC, 0));
9606 if (N->getNumValues() == 2) // Dead flag value?
9607 return DCI.CombineTo(N, Cond, SDValue());
9608 return Cond;
9609 }
Eric Christopherfd179292009-08-27 18:07:15 +00009610 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00009611 }
9612 }
9613 return SDValue();
9614}
9615
9616
Evan Cheng0b0cd912009-03-28 05:57:29 +00009617/// PerformMulCombine - Optimize a single multiply with constant into two
9618/// in order to implement it with two cheaper instructions, e.g.
9619/// LEA + SHL, LEA + LEA.
9620static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
9621 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +00009622 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
9623 return SDValue();
9624
Owen Andersone50ed302009-08-10 22:56:29 +00009625 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009626 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +00009627 return SDValue();
9628
9629 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
9630 if (!C)
9631 return SDValue();
9632 uint64_t MulAmt = C->getZExtValue();
9633 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
9634 return SDValue();
9635
9636 uint64_t MulAmt1 = 0;
9637 uint64_t MulAmt2 = 0;
9638 if ((MulAmt % 9) == 0) {
9639 MulAmt1 = 9;
9640 MulAmt2 = MulAmt / 9;
9641 } else if ((MulAmt % 5) == 0) {
9642 MulAmt1 = 5;
9643 MulAmt2 = MulAmt / 5;
9644 } else if ((MulAmt % 3) == 0) {
9645 MulAmt1 = 3;
9646 MulAmt2 = MulAmt / 3;
9647 }
9648 if (MulAmt2 &&
9649 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
9650 DebugLoc DL = N->getDebugLoc();
9651
9652 if (isPowerOf2_64(MulAmt2) &&
9653 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
9654 // If second multiplifer is pow2, issue it first. We want the multiply by
9655 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
9656 // is an add.
9657 std::swap(MulAmt1, MulAmt2);
9658
9659 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +00009660 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +00009661 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00009662 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +00009663 else
Evan Cheng73f24c92009-03-30 21:36:47 +00009664 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +00009665 DAG.getConstant(MulAmt1, VT));
9666
Eric Christopherfd179292009-08-27 18:07:15 +00009667 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +00009668 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +00009669 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +00009670 else
Evan Cheng73f24c92009-03-30 21:36:47 +00009671 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +00009672 DAG.getConstant(MulAmt2, VT));
9673
9674 // Do not add new nodes to DAG combiner worklist.
9675 DCI.CombineTo(N, NewMul, false);
9676 }
9677 return SDValue();
9678}
9679
Evan Chengad9c0a32009-12-15 00:53:42 +00009680static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
9681 SDValue N0 = N->getOperand(0);
9682 SDValue N1 = N->getOperand(1);
9683 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
9684 EVT VT = N0.getValueType();
9685
9686 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
9687 // since the result of setcc_c is all zero's or all ones.
9688 if (N1C && N0.getOpcode() == ISD::AND &&
9689 N0.getOperand(1).getOpcode() == ISD::Constant) {
9690 SDValue N00 = N0.getOperand(0);
9691 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
9692 ((N00.getOpcode() == ISD::ANY_EXTEND ||
9693 N00.getOpcode() == ISD::ZERO_EXTEND) &&
9694 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
9695 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
9696 APInt ShAmt = N1C->getAPIntValue();
9697 Mask = Mask.shl(ShAmt);
9698 if (Mask != 0)
9699 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
9700 N00, DAG.getConstant(Mask, VT));
9701 }
9702 }
9703
9704 return SDValue();
9705}
Evan Cheng0b0cd912009-03-28 05:57:29 +00009706
Nate Begeman740ab032009-01-26 00:52:55 +00009707/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
9708/// when possible.
9709static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
9710 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +00009711 EVT VT = N->getValueType(0);
9712 if (!VT.isVector() && VT.isInteger() &&
9713 N->getOpcode() == ISD::SHL)
9714 return PerformSHLCombine(N, DAG);
9715
Nate Begeman740ab032009-01-26 00:52:55 +00009716 // On X86 with SSE2 support, we can transform this to a vector shift if
9717 // all elements are shifted by the same amount. We can't do this in legalize
9718 // because the a constant vector is typically transformed to a constant pool
9719 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009720 if (!Subtarget->hasSSE2())
9721 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009722
Owen Anderson825b72b2009-08-11 20:47:22 +00009723 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009724 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009725
Mon P Wang3becd092009-01-28 08:12:05 +00009726 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00009727 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +00009728 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +00009729 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +00009730 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
9731 unsigned NumElts = VT.getVectorNumElements();
9732 unsigned i = 0;
9733 for (; i != NumElts; ++i) {
9734 SDValue Arg = ShAmtOp.getOperand(i);
9735 if (Arg.getOpcode() == ISD::UNDEF) continue;
9736 BaseShAmt = Arg;
9737 break;
9738 }
9739 for (; i != NumElts; ++i) {
9740 SDValue Arg = ShAmtOp.getOperand(i);
9741 if (Arg.getOpcode() == ISD::UNDEF) continue;
9742 if (Arg != BaseShAmt) {
9743 return SDValue();
9744 }
9745 }
9746 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +00009747 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +00009748 SDValue InVec = ShAmtOp.getOperand(0);
9749 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
9750 unsigned NumElts = InVec.getValueType().getVectorNumElements();
9751 unsigned i = 0;
9752 for (; i != NumElts; ++i) {
9753 SDValue Arg = InVec.getOperand(i);
9754 if (Arg.getOpcode() == ISD::UNDEF) continue;
9755 BaseShAmt = Arg;
9756 break;
9757 }
9758 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
9759 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +00009760 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +00009761 if (C->getZExtValue() == SplatIdx)
9762 BaseShAmt = InVec.getOperand(1);
9763 }
9764 }
9765 if (BaseShAmt.getNode() == 0)
9766 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
9767 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +00009768 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009769 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +00009770
Mon P Wangefa42202009-09-03 19:56:25 +00009771 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +00009772 if (EltVT.bitsGT(MVT::i32))
9773 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
9774 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +00009775 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +00009776
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009777 // The shift amount is identical so we can do a vector shift.
9778 SDValue ValOp = N->getOperand(0);
9779 switch (N->getOpcode()) {
9780 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009781 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009782 break;
9783 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +00009784 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009785 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009786 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009787 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009788 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009789 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009790 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009791 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009792 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009793 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009794 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009795 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009796 break;
9797 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +00009798 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009799 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009800 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009801 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009802 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009803 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009804 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009805 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009806 break;
9807 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +00009808 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009809 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009810 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009811 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009812 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009813 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009814 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009815 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009816 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009817 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009818 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009819 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009820 break;
Nate Begeman740ab032009-01-26 00:52:55 +00009821 }
9822 return SDValue();
9823}
9824
Evan Cheng760d1942010-01-04 21:22:48 +00009825static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +00009826 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +00009827 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +00009828 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +00009829 return SDValue();
9830
Evan Cheng760d1942010-01-04 21:22:48 +00009831 EVT VT = N->getValueType(0);
Evan Cheng8b1190a2010-04-28 01:18:01 +00009832 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
Evan Cheng760d1942010-01-04 21:22:48 +00009833 return SDValue();
9834
9835 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
9836 SDValue N0 = N->getOperand(0);
9837 SDValue N1 = N->getOperand(1);
9838 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
9839 std::swap(N0, N1);
9840 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
9841 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +00009842 if (!N0.hasOneUse() || !N1.hasOneUse())
9843 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +00009844
9845 SDValue ShAmt0 = N0.getOperand(1);
9846 if (ShAmt0.getValueType() != MVT::i8)
9847 return SDValue();
9848 SDValue ShAmt1 = N1.getOperand(1);
9849 if (ShAmt1.getValueType() != MVT::i8)
9850 return SDValue();
9851 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
9852 ShAmt0 = ShAmt0.getOperand(0);
9853 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
9854 ShAmt1 = ShAmt1.getOperand(0);
9855
9856 DebugLoc DL = N->getDebugLoc();
9857 unsigned Opc = X86ISD::SHLD;
9858 SDValue Op0 = N0.getOperand(0);
9859 SDValue Op1 = N1.getOperand(0);
9860 if (ShAmt0.getOpcode() == ISD::SUB) {
9861 Opc = X86ISD::SHRD;
9862 std::swap(Op0, Op1);
9863 std::swap(ShAmt0, ShAmt1);
9864 }
9865
Evan Cheng8b1190a2010-04-28 01:18:01 +00009866 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +00009867 if (ShAmt1.getOpcode() == ISD::SUB) {
9868 SDValue Sum = ShAmt1.getOperand(0);
9869 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +00009870 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
9871 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
9872 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
9873 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +00009874 return DAG.getNode(Opc, DL, VT,
9875 Op0, Op1,
9876 DAG.getNode(ISD::TRUNCATE, DL,
9877 MVT::i8, ShAmt0));
9878 }
9879 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
9880 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
9881 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +00009882 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +00009883 return DAG.getNode(Opc, DL, VT,
9884 N0.getOperand(0), N1.getOperand(0),
9885 DAG.getNode(ISD::TRUNCATE, DL,
9886 MVT::i8, ShAmt0));
9887 }
9888
9889 return SDValue();
9890}
9891
Chris Lattner149a4e52008-02-22 02:09:43 +00009892/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009893static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +00009894 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +00009895 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
9896 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +00009897 // A preferable solution to the general problem is to figure out the right
9898 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +00009899
9900 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +00009901 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +00009902 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +00009903 if (VT.getSizeInBits() != 64)
9904 return SDValue();
9905
Devang Patel578efa92009-06-05 21:57:13 +00009906 const Function *F = DAG.getMachineFunction().getFunction();
9907 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +00009908 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +00009909 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +00009910 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +00009911 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +00009912 isa<LoadSDNode>(St->getValue()) &&
9913 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
9914 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00009915 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009916 LoadSDNode *Ld = 0;
9917 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +00009918 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +00009919 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009920 // Must be a store of a load. We currently handle two cases: the load
9921 // is a direct child, and it's under an intervening TokenFactor. It is
9922 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +00009923 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +00009924 Ld = cast<LoadSDNode>(St->getChain());
9925 else if (St->getValue().hasOneUse() &&
9926 ChainVal->getOpcode() == ISD::TokenFactor) {
9927 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +00009928 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +00009929 TokenFactorIndex = i;
9930 Ld = cast<LoadSDNode>(St->getValue());
9931 } else
9932 Ops.push_back(ChainVal->getOperand(i));
9933 }
9934 }
Dale Johannesen079f2a62008-02-25 19:20:14 +00009935
Evan Cheng536e6672009-03-12 05:59:15 +00009936 if (!Ld || !ISD::isNormalLoad(Ld))
9937 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009938
Evan Cheng536e6672009-03-12 05:59:15 +00009939 // If this is not the MMX case, i.e. we are just turning i64 load/store
9940 // into f64 load/store, avoid the transformation if there are multiple
9941 // uses of the loaded value.
9942 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
9943 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009944
Evan Cheng536e6672009-03-12 05:59:15 +00009945 DebugLoc LdDL = Ld->getDebugLoc();
9946 DebugLoc StDL = N->getDebugLoc();
9947 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
9948 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
9949 // pair instead.
9950 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009951 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Evan Cheng536e6672009-03-12 05:59:15 +00009952 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(),
9953 Ld->getBasePtr(), Ld->getSrcValue(),
9954 Ld->getSrcValueOffset(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +00009955 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +00009956 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +00009957 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +00009958 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +00009959 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +00009960 Ops.size());
9961 }
Evan Cheng536e6672009-03-12 05:59:15 +00009962 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner149a4e52008-02-22 02:09:43 +00009963 St->getSrcValue(), St->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00009964 St->isVolatile(), St->isNonTemporal(),
9965 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +00009966 }
Evan Cheng536e6672009-03-12 05:59:15 +00009967
9968 // Otherwise, lower to two pairs of 32-bit loads / stores.
9969 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +00009970 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
9971 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +00009972
Owen Anderson825b72b2009-08-11 20:47:22 +00009973 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Evan Cheng536e6672009-03-12 05:59:15 +00009974 Ld->getSrcValue(), Ld->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00009975 Ld->isVolatile(), Ld->isNonTemporal(),
9976 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00009977 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Evan Cheng536e6672009-03-12 05:59:15 +00009978 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
David Greene67c9d422010-02-15 16:53:33 +00009979 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +00009980 MinAlign(Ld->getAlignment(), 4));
9981
9982 SDValue NewChain = LoLd.getValue(1);
9983 if (TokenFactorIndex != -1) {
9984 Ops.push_back(LoLd);
9985 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +00009986 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +00009987 Ops.size());
9988 }
9989
9990 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +00009991 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
9992 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +00009993
9994 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
9995 St->getSrcValue(), St->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00009996 St->isVolatile(), St->isNonTemporal(),
9997 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +00009998 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
9999 St->getSrcValue(),
10000 St->getSrcValueOffset() + 4,
10001 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000010002 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000010003 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000010004 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000010005 }
Dan Gohman475871a2008-07-27 21:46:04 +000010006 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000010007}
10008
Chris Lattner6cf73262008-01-25 06:14:17 +000010009/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
10010/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000010011static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000010012 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
10013 // F[X]OR(0.0, x) -> x
10014 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000010015 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
10016 if (C->getValueAPF().isPosZero())
10017 return N->getOperand(1);
10018 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
10019 if (C->getValueAPF().isPosZero())
10020 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000010021 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000010022}
10023
10024/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000010025static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000010026 // FAND(0.0, x) -> 0.0
10027 // FAND(x, 0.0) -> 0.0
10028 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
10029 if (C->getValueAPF().isPosZero())
10030 return N->getOperand(0);
10031 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
10032 if (C->getValueAPF().isPosZero())
10033 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000010034 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000010035}
10036
Dan Gohmane5af2d32009-01-29 01:59:02 +000010037static SDValue PerformBTCombine(SDNode *N,
10038 SelectionDAG &DAG,
10039 TargetLowering::DAGCombinerInfo &DCI) {
10040 // BT ignores high bits in the bit index operand.
10041 SDValue Op1 = N->getOperand(1);
10042 if (Op1.hasOneUse()) {
10043 unsigned BitWidth = Op1.getValueSizeInBits();
10044 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
10045 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000010046 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
10047 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000010048 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000010049 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
10050 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
10051 DCI.CommitTargetLoweringOpt(TLO);
10052 }
10053 return SDValue();
10054}
Chris Lattner83e6c992006-10-04 06:57:07 +000010055
Eli Friedman7a5e5552009-06-07 06:52:44 +000010056static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
10057 SDValue Op = N->getOperand(0);
10058 if (Op.getOpcode() == ISD::BIT_CONVERT)
10059 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000010060 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000010061 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000010062 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000010063 OpVT.getVectorElementType().getSizeInBits()) {
10064 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT, Op);
10065 }
10066 return SDValue();
10067}
10068
Evan Cheng2e489c42009-12-16 00:53:11 +000010069static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
10070 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
10071 // (and (i32 x86isd::setcc_carry), 1)
10072 // This eliminates the zext. This transformation is necessary because
10073 // ISD::SETCC is always legalized to i8.
10074 DebugLoc dl = N->getDebugLoc();
10075 SDValue N0 = N->getOperand(0);
10076 EVT VT = N->getValueType(0);
10077 if (N0.getOpcode() == ISD::AND &&
10078 N0.hasOneUse() &&
10079 N0.getOperand(0).hasOneUse()) {
10080 SDValue N00 = N0.getOperand(0);
10081 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
10082 return SDValue();
10083 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
10084 if (!C || C->getZExtValue() != 1)
10085 return SDValue();
10086 return DAG.getNode(ISD::AND, dl, VT,
10087 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
10088 N00.getOperand(0), N00.getOperand(1)),
10089 DAG.getConstant(1, VT));
10090 }
10091
10092 return SDValue();
10093}
10094
Dan Gohman475871a2008-07-27 21:46:04 +000010095SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000010096 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000010097 SelectionDAG &DAG = DCI.DAG;
10098 switch (N->getOpcode()) {
10099 default: break;
Evan Chengad4196b2008-05-12 19:56:52 +000010100 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010101 case ISD::EXTRACT_VECTOR_ELT:
10102 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +000010103 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000010104 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000010105 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000010106 case ISD::SHL:
10107 case ISD::SRA:
10108 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000010109 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000010110 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000010111 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000010112 case X86ISD::FOR: return PerformFORCombine(N, DAG);
10113 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000010114 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000010115 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000010116 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Evan Cheng206ee9d2006-07-07 08:33:52 +000010117 }
10118
Dan Gohman475871a2008-07-27 21:46:04 +000010119 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000010120}
10121
Evan Chenge5b51ac2010-04-17 06:13:15 +000010122/// isTypeDesirableForOp - Return true if the target has native support for
10123/// the specified value type and it is 'desirable' to use the type for the
10124/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
10125/// instruction encodings are longer and some i16 instructions are slow.
10126bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
10127 if (!isTypeLegal(VT))
10128 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000010129 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000010130 return true;
10131
10132 switch (Opc) {
10133 default:
10134 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000010135 case ISD::LOAD:
10136 case ISD::SIGN_EXTEND:
10137 case ISD::ZERO_EXTEND:
10138 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000010139 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000010140 case ISD::SRL:
10141 case ISD::SUB:
10142 case ISD::ADD:
10143 case ISD::MUL:
10144 case ISD::AND:
10145 case ISD::OR:
10146 case ISD::XOR:
10147 return false;
10148 }
10149}
10150
Evan Chengc82c20b2010-04-24 04:44:57 +000010151static bool MayFoldLoad(SDValue Op) {
10152 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
10153}
10154
10155static bool MayFoldIntoStore(SDValue Op) {
10156 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
10157}
10158
Evan Chenge5b51ac2010-04-17 06:13:15 +000010159/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000010160/// beneficial for dag combiner to promote the specified node. If true, it
10161/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000010162bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000010163 EVT VT = Op.getValueType();
10164 if (VT != MVT::i16)
10165 return false;
10166
Evan Cheng4c26e932010-04-19 19:29:22 +000010167 bool Promote = false;
10168 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000010169 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000010170 default: break;
10171 case ISD::LOAD: {
10172 LoadSDNode *LD = cast<LoadSDNode>(Op);
10173 // If the non-extending load has a single use and it's not live out, then it
10174 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000010175 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
10176 Op.hasOneUse()*/) {
10177 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
10178 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
10179 // The only case where we'd want to promote LOAD (rather then it being
10180 // promoted as an operand is when it's only use is liveout.
10181 if (UI->getOpcode() != ISD::CopyToReg)
10182 return false;
10183 }
10184 }
Evan Cheng4c26e932010-04-19 19:29:22 +000010185 Promote = true;
10186 break;
10187 }
10188 case ISD::SIGN_EXTEND:
10189 case ISD::ZERO_EXTEND:
10190 case ISD::ANY_EXTEND:
10191 Promote = true;
10192 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000010193 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000010194 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000010195 SDValue N0 = Op.getOperand(0);
10196 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000010197 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000010198 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000010199 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000010200 break;
10201 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000010202 case ISD::ADD:
10203 case ISD::MUL:
10204 case ISD::AND:
10205 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000010206 case ISD::XOR:
10207 Commute = true;
10208 // fallthrough
10209 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000010210 SDValue N0 = Op.getOperand(0);
10211 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000010212 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000010213 return false;
10214 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000010215 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000010216 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000010217 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000010218 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000010219 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000010220 }
10221 }
10222
10223 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000010224 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000010225}
10226
Evan Cheng60c07e12006-07-05 22:17:51 +000010227//===----------------------------------------------------------------------===//
10228// X86 Inline Assembly Support
10229//===----------------------------------------------------------------------===//
10230
Chris Lattnerb8105652009-07-20 17:51:36 +000010231static bool LowerToBSwap(CallInst *CI) {
10232 // FIXME: this should verify that we are targetting a 486 or better. If not,
10233 // we will turn this bswap into something that will be lowered to logical ops
10234 // instead of emitting the bswap asm. For now, we don't support 486 or lower
10235 // so don't worry about this.
Eric Christopherfd179292009-08-27 18:07:15 +000010236
Chris Lattnerb8105652009-07-20 17:51:36 +000010237 // Verify this is a simple bswap.
Gabor Greife1c2b9c2010-06-30 13:03:37 +000010238 if (CI->getNumArgOperands() != 1 ||
Gabor Greif1cfe44a2010-06-26 11:51:52 +000010239 CI->getType() != CI->getArgOperand(0)->getType() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000010240 !CI->getType()->isIntegerTy())
Chris Lattnerb8105652009-07-20 17:51:36 +000010241 return false;
Eric Christopherfd179292009-08-27 18:07:15 +000010242
Chris Lattnerb8105652009-07-20 17:51:36 +000010243 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
10244 if (!Ty || Ty->getBitWidth() % 16 != 0)
10245 return false;
Eric Christopherfd179292009-08-27 18:07:15 +000010246
Chris Lattnerb8105652009-07-20 17:51:36 +000010247 // Okay, we can do this xform, do so now.
10248 const Type *Tys[] = { Ty };
10249 Module *M = CI->getParent()->getParent()->getParent();
10250 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
Eric Christopherfd179292009-08-27 18:07:15 +000010251
Gabor Greif1cfe44a2010-06-26 11:51:52 +000010252 Value *Op = CI->getArgOperand(0);
Chris Lattnerb8105652009-07-20 17:51:36 +000010253 Op = CallInst::Create(Int, Op, CI->getName(), CI);
Eric Christopherfd179292009-08-27 18:07:15 +000010254
Chris Lattnerb8105652009-07-20 17:51:36 +000010255 CI->replaceAllUsesWith(Op);
10256 CI->eraseFromParent();
10257 return true;
10258}
10259
10260bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
10261 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
10262 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
10263
10264 std::string AsmStr = IA->getAsmString();
10265
10266 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000010267 SmallVector<StringRef, 4> AsmPieces;
Chris Lattnerb8105652009-07-20 17:51:36 +000010268 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
10269
10270 switch (AsmPieces.size()) {
10271 default: return false;
10272 case 1:
10273 AsmStr = AsmPieces[0];
10274 AsmPieces.clear();
10275 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
10276
10277 // bswap $0
10278 if (AsmPieces.size() == 2 &&
10279 (AsmPieces[0] == "bswap" ||
10280 AsmPieces[0] == "bswapq" ||
10281 AsmPieces[0] == "bswapl") &&
10282 (AsmPieces[1] == "$0" ||
10283 AsmPieces[1] == "${0:q}")) {
10284 // No need to check constraints, nothing other than the equivalent of
10285 // "=r,0" would be valid here.
10286 return LowerToBSwap(CI);
10287 }
10288 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000010289 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000010290 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000010291 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000010292 AsmPieces[1] == "$$8," &&
10293 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000010294 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
10295 AsmPieces.clear();
Benjamin Kramer018cbd52010-03-12 13:54:59 +000010296 const std::string &Constraints = IA->getConstraintString();
10297 SplitString(StringRef(Constraints).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000010298 std::sort(AsmPieces.begin(), AsmPieces.end());
10299 if (AsmPieces.size() == 4 &&
10300 AsmPieces[0] == "~{cc}" &&
10301 AsmPieces[1] == "~{dirflag}" &&
10302 AsmPieces[2] == "~{flags}" &&
10303 AsmPieces[3] == "~{fpsr}") {
10304 return LowerToBSwap(CI);
10305 }
Chris Lattnerb8105652009-07-20 17:51:36 +000010306 }
10307 break;
10308 case 3:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000010309 if (CI->getType()->isIntegerTy(64) &&
Owen Anderson1d0be152009-08-13 21:58:54 +000010310 Constraints.size() >= 2 &&
Chris Lattnerb8105652009-07-20 17:51:36 +000010311 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
10312 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
10313 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramerd4f19592010-01-11 18:03:24 +000010314 SmallVector<StringRef, 4> Words;
Chris Lattnerb8105652009-07-20 17:51:36 +000010315 SplitString(AsmPieces[0], Words, " \t");
10316 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
10317 Words.clear();
10318 SplitString(AsmPieces[1], Words, " \t");
10319 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
10320 Words.clear();
10321 SplitString(AsmPieces[2], Words, " \t,");
10322 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
10323 Words[2] == "%edx") {
10324 return LowerToBSwap(CI);
10325 }
10326 }
10327 }
10328 }
10329 break;
10330 }
10331 return false;
10332}
10333
10334
10335
Chris Lattnerf4dff842006-07-11 02:54:03 +000010336/// getConstraintType - Given a constraint letter, return the type of
10337/// constraint it is for this target.
10338X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000010339X86TargetLowering::getConstraintType(const std::string &Constraint) const {
10340 if (Constraint.size() == 1) {
10341 switch (Constraint[0]) {
10342 case 'A':
Dale Johannesen330169f2008-11-13 21:52:36 +000010343 return C_Register;
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010344 case 'f':
Chris Lattner4234f572007-03-25 02:14:49 +000010345 case 'r':
10346 case 'R':
10347 case 'l':
10348 case 'q':
10349 case 'Q':
10350 case 'x':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000010351 case 'y':
Chris Lattner4234f572007-03-25 02:14:49 +000010352 case 'Y':
10353 return C_RegisterClass;
Dale Johannesen78e3e522009-02-12 20:58:09 +000010354 case 'e':
10355 case 'Z':
10356 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000010357 default:
10358 break;
10359 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000010360 }
Chris Lattner4234f572007-03-25 02:14:49 +000010361 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000010362}
10363
Dale Johannesenba2a0b92008-01-29 02:21:21 +000010364/// LowerXConstraint - try to replace an X constraint, which matches anything,
10365/// with another that has more specific requirements based on the type of the
10366/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000010367const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000010368LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000010369 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
10370 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000010371 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesenba2a0b92008-01-29 02:21:21 +000010372 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +000010373 return "Y";
10374 if (Subtarget->hasSSE1())
10375 return "x";
10376 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010377
Chris Lattner5e764232008-04-26 23:02:14 +000010378 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000010379}
10380
Chris Lattner48884cd2007-08-25 00:47:38 +000010381/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
10382/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000010383void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +000010384 char Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000010385 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000010386 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010387 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000010388
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010389 switch (Constraint) {
10390 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000010391 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000010392 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000010393 if (C->getZExtValue() <= 31) {
10394 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000010395 break;
10396 }
Devang Patel84f7fd22007-03-17 00:13:28 +000010397 }
Chris Lattner48884cd2007-08-25 00:47:38 +000010398 return;
Evan Cheng364091e2008-09-22 23:57:37 +000010399 case 'J':
10400 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000010401 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000010402 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
10403 break;
10404 }
10405 }
10406 return;
10407 case 'K':
10408 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000010409 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000010410 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
10411 break;
10412 }
10413 }
10414 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000010415 case 'N':
10416 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000010417 if (C->getZExtValue() <= 255) {
10418 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000010419 break;
10420 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000010421 }
Chris Lattner48884cd2007-08-25 00:47:38 +000010422 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000010423 case 'e': {
10424 // 32-bit signed value
10425 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000010426 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
10427 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000010428 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000010429 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000010430 break;
10431 }
10432 // FIXME gcc accepts some relocatable values here too, but only in certain
10433 // memory models; it's complicated.
10434 }
10435 return;
10436 }
10437 case 'Z': {
10438 // 32-bit unsigned value
10439 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000010440 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
10441 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000010442 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
10443 break;
10444 }
10445 }
10446 // FIXME gcc accepts some relocatable values here too, but only in certain
10447 // memory models; it's complicated.
10448 return;
10449 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000010450 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010451 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000010452 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000010453 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000010454 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000010455 break;
10456 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010457
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000010458 // In any sort of PIC mode addresses need to be computed at runtime by
10459 // adding in a register or some sort of table lookup. These can't
10460 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000010461 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000010462 return;
10463
Chris Lattnerdc43a882007-05-03 16:52:29 +000010464 // If we are in non-pic codegen mode, we allow the address of a global (with
10465 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000010466 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000010467 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000010468
Chris Lattner49921962009-05-08 18:23:14 +000010469 // Match either (GA), (GA+C), (GA+C1+C2), etc.
10470 while (1) {
10471 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
10472 Offset += GA->getOffset();
10473 break;
10474 } else if (Op.getOpcode() == ISD::ADD) {
10475 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
10476 Offset += C->getZExtValue();
10477 Op = Op.getOperand(0);
10478 continue;
10479 }
10480 } else if (Op.getOpcode() == ISD::SUB) {
10481 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
10482 Offset += -C->getZExtValue();
10483 Op = Op.getOperand(0);
10484 continue;
10485 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000010486 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000010487
Chris Lattner49921962009-05-08 18:23:14 +000010488 // Otherwise, this isn't something we can handle, reject it.
10489 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000010490 }
Eric Christopherfd179292009-08-27 18:07:15 +000010491
Dan Gohman46510a72010-04-15 01:51:59 +000010492 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000010493 // If we require an extra load to get this address, as in PIC mode, we
10494 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000010495 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
10496 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000010497 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000010498
Devang Patel0d881da2010-07-06 22:08:15 +000010499 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
10500 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000010501 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010502 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000010503 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010504
Gabor Greifba36cb52008-08-28 21:40:38 +000010505 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000010506 Ops.push_back(Result);
10507 return;
10508 }
Dale Johannesen1784d162010-06-25 21:55:36 +000010509 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010510}
10511
Chris Lattner259e97c2006-01-31 19:43:35 +000010512std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +000010513getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000010514 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +000010515 if (Constraint.size() == 1) {
10516 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +000010517 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +000010518 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +000010519 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
10520 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000010521 if (VT == MVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010522 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
10523 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
10524 X86::R10D,X86::R11D,X86::R12D,
10525 X86::R13D,X86::R14D,X86::R15D,
10526 X86::EBP, X86::ESP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010527 else if (VT == MVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010528 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
10529 X86::SI, X86::DI, X86::R8W,X86::R9W,
10530 X86::R10W,X86::R11W,X86::R12W,
10531 X86::R13W,X86::R14W,X86::R15W,
10532 X86::BP, X86::SP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010533 else if (VT == MVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010534 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
10535 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
10536 X86::R10B,X86::R11B,X86::R12B,
10537 X86::R13B,X86::R14B,X86::R15B,
10538 X86::BPL, X86::SPL, 0);
10539
Owen Anderson825b72b2009-08-11 20:47:22 +000010540 else if (VT == MVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010541 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
10542 X86::RSI, X86::RDI, X86::R8, X86::R9,
10543 X86::R10, X86::R11, X86::R12,
10544 X86::R13, X86::R14, X86::R15,
10545 X86::RBP, X86::RSP, 0);
10546
10547 break;
10548 }
Eric Christopherfd179292009-08-27 18:07:15 +000010549 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +000010550 case 'Q': // Q_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000010551 if (VT == MVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000010552 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010553 else if (VT == MVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000010554 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010555 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +000010556 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010557 else if (VT == MVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +000010558 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
10559 break;
Chris Lattner259e97c2006-01-31 19:43:35 +000010560 }
10561 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010562
Chris Lattner1efa40f2006-02-22 00:56:39 +000010563 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +000010564}
Chris Lattnerf76d1802006-07-31 23:26:50 +000010565
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010566std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000010567X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000010568 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000010569 // First, see if this is a constraint that directly corresponds to an LLVM
10570 // register class.
10571 if (Constraint.size() == 1) {
10572 // GCC Constraint Letters
10573 switch (Constraint[0]) {
10574 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000010575 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000010576 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000010577 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +000010578 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010579 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000010580 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010581 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000010582 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000010583 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000010584 case 'R': // LEGACY_REGS
10585 if (VT == MVT::i8)
10586 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
10587 if (VT == MVT::i16)
10588 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
10589 if (VT == MVT::i32 || !Subtarget->is64Bit())
10590 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
10591 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010592 case 'f': // FP Stack registers.
10593 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
10594 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000010595 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010596 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010597 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010598 return std::make_pair(0U, X86::RFP64RegisterClass);
10599 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000010600 case 'y': // MMX_REGS if MMX allowed.
10601 if (!Subtarget->hasMMX()) break;
10602 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000010603 case 'Y': // SSE_REGS if SSE2 allowed
10604 if (!Subtarget->hasSSE2()) break;
10605 // FALL THROUGH.
10606 case 'x': // SSE_REGS if SSE1 allowed
10607 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010608
Owen Anderson825b72b2009-08-11 20:47:22 +000010609 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000010610 default: break;
10611 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000010612 case MVT::f32:
10613 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000010614 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010615 case MVT::f64:
10616 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000010617 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000010618 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000010619 case MVT::v16i8:
10620 case MVT::v8i16:
10621 case MVT::v4i32:
10622 case MVT::v2i64:
10623 case MVT::v4f32:
10624 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000010625 return std::make_pair(0U, X86::VR128RegisterClass);
10626 }
Chris Lattnerad043e82007-04-09 05:11:28 +000010627 break;
10628 }
10629 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010630
Chris Lattnerf76d1802006-07-31 23:26:50 +000010631 // Use the default implementation in TargetLowering to convert the register
10632 // constraint into a member of a register class.
10633 std::pair<unsigned, const TargetRegisterClass*> Res;
10634 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000010635
10636 // Not found as a standard register?
10637 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000010638 // Map st(0) -> st(7) -> ST0
10639 if (Constraint.size() == 7 && Constraint[0] == '{' &&
10640 tolower(Constraint[1]) == 's' &&
10641 tolower(Constraint[2]) == 't' &&
10642 Constraint[3] == '(' &&
10643 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
10644 Constraint[5] == ')' &&
10645 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000010646
Chris Lattner56d77c72009-09-13 22:41:48 +000010647 Res.first = X86::ST0+Constraint[4]-'0';
10648 Res.second = X86::RFP80RegisterClass;
10649 return Res;
10650 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000010651
Chris Lattner56d77c72009-09-13 22:41:48 +000010652 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000010653 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000010654 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000010655 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000010656 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000010657 }
Chris Lattner56d77c72009-09-13 22:41:48 +000010658
10659 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000010660 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000010661 Res.first = X86::EFLAGS;
10662 Res.second = X86::CCRRegisterClass;
10663 return Res;
10664 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000010665
Dale Johannesen330169f2008-11-13 21:52:36 +000010666 // 'A' means EAX + EDX.
10667 if (Constraint == "A") {
10668 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000010669 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000010670 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000010671 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000010672 return Res;
10673 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010674
Chris Lattnerf76d1802006-07-31 23:26:50 +000010675 // Otherwise, check to see if this is a register class of the wrong value
10676 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
10677 // turn into {ax},{dx}.
10678 if (Res.second->hasType(VT))
10679 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010680
Chris Lattnerf76d1802006-07-31 23:26:50 +000010681 // All of the single-register GCC register classes map their values onto
10682 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
10683 // really want an 8-bit or 32-bit register, map to the appropriate register
10684 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000010685 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000010686 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010687 unsigned DestReg = 0;
10688 switch (Res.first) {
10689 default: break;
10690 case X86::AX: DestReg = X86::AL; break;
10691 case X86::DX: DestReg = X86::DL; break;
10692 case X86::CX: DestReg = X86::CL; break;
10693 case X86::BX: DestReg = X86::BL; break;
10694 }
10695 if (DestReg) {
10696 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010697 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010698 }
Owen Anderson825b72b2009-08-11 20:47:22 +000010699 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010700 unsigned DestReg = 0;
10701 switch (Res.first) {
10702 default: break;
10703 case X86::AX: DestReg = X86::EAX; break;
10704 case X86::DX: DestReg = X86::EDX; break;
10705 case X86::CX: DestReg = X86::ECX; break;
10706 case X86::BX: DestReg = X86::EBX; break;
10707 case X86::SI: DestReg = X86::ESI; break;
10708 case X86::DI: DestReg = X86::EDI; break;
10709 case X86::BP: DestReg = X86::EBP; break;
10710 case X86::SP: DestReg = X86::ESP; break;
10711 }
10712 if (DestReg) {
10713 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010714 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010715 }
Owen Anderson825b72b2009-08-11 20:47:22 +000010716 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010717 unsigned DestReg = 0;
10718 switch (Res.first) {
10719 default: break;
10720 case X86::AX: DestReg = X86::RAX; break;
10721 case X86::DX: DestReg = X86::RDX; break;
10722 case X86::CX: DestReg = X86::RCX; break;
10723 case X86::BX: DestReg = X86::RBX; break;
10724 case X86::SI: DestReg = X86::RSI; break;
10725 case X86::DI: DestReg = X86::RDI; break;
10726 case X86::BP: DestReg = X86::RBP; break;
10727 case X86::SP: DestReg = X86::RSP; break;
10728 }
10729 if (DestReg) {
10730 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010731 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010732 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000010733 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000010734 } else if (Res.second == X86::FR32RegisterClass ||
10735 Res.second == X86::FR64RegisterClass ||
10736 Res.second == X86::VR128RegisterClass) {
10737 // Handle references to XMM physical registers that got mapped into the
10738 // wrong class. This can happen with constraints like {xmm0} where the
10739 // target independent register mapper will just pick the first match it can
10740 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000010741 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000010742 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000010743 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000010744 Res.second = X86::FR64RegisterClass;
10745 else if (X86::VR128RegisterClass->hasType(VT))
10746 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000010747 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010748
Chris Lattnerf76d1802006-07-31 23:26:50 +000010749 return Res;
10750}