blob: c0f5a403b9f7281cdd9f6cacfbc311f1ca8c871a [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;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001636 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001637 RC = X86::VR128RegisterClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001638 else if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
1639 RC = X86::VR64RegisterClass;
1640 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001641 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001642
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001643 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001644 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001645
Chris Lattnerf39f7712007-02-28 05:46:49 +00001646 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1647 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1648 // right size.
1649 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001650 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001651 DAG.getValueType(VA.getValVT()));
1652 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001653 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001654 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001655 else if (VA.getLocInfo() == CCValAssign::BCvt)
Anton Korobeynikov6dde14b2009-08-03 08:14:14 +00001656 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001657
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001658 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001659 // Handle MMX values passed in XMM regs.
1660 if (RegVT.isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001661 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1662 ArgValue, DAG.getConstant(0, MVT::i64));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001663 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1664 } else
1665 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001666 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001667 } else {
1668 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001669 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001670 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001671
1672 // If value is passed via pointer - do a load.
1673 if (VA.getLocInfo() == CCValAssign::Indirect)
David Greene67c9d422010-02-15 16:53:33 +00001674 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue, NULL, 0,
1675 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001676
Dan Gohman98ca4f22009-08-05 01:29:28 +00001677 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001678 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001679
Dan Gohman61a92132008-04-21 23:59:07 +00001680 // The x86-64 ABI for returning structs by value requires that we copy
1681 // the sret argument into %rax for the return. Save the argument into
1682 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001683 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001684 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1685 unsigned Reg = FuncInfo->getSRetReturnReg();
1686 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001687 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001688 FuncInfo->setSRetReturnReg(Reg);
1689 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001690 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001691 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001692 }
1693
Chris Lattnerf39f7712007-02-28 05:46:49 +00001694 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001695 // Align stack specially for tail calls.
1696 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001697 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001698
Evan Cheng1bc78042006-04-26 01:20:17 +00001699 // If the function takes variable number of arguments, make a frame index for
1700 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001701 if (isVarArg) {
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001702 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1703 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001704 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001705 }
1706 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001707 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1708
1709 // FIXME: We should really autogenerate these arrays
1710 static const unsigned GPR64ArgRegsWin64[] = {
1711 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001712 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001713 static const unsigned XMMArgRegsWin64[] = {
1714 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1715 };
1716 static const unsigned GPR64ArgRegs64Bit[] = {
1717 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1718 };
1719 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001720 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1721 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1722 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001723 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1724
1725 if (IsWin64) {
1726 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1727 GPR64ArgRegs = GPR64ArgRegsWin64;
1728 XMMArgRegs = XMMArgRegsWin64;
1729 } else {
1730 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1731 GPR64ArgRegs = GPR64ArgRegs64Bit;
1732 XMMArgRegs = XMMArgRegs64Bit;
1733 }
1734 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1735 TotalNumIntRegs);
1736 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1737 TotalNumXMMRegs);
1738
Devang Patel578efa92009-06-05 21:57:13 +00001739 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Evan Chengc7ce29b2009-02-13 22:36:38 +00001740 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001741 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001742 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001743 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001744 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00001745 // Kernel mode asks for SSE to be disabled, so don't push them
1746 // on the stack.
1747 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001748
Gordon Henriksen86737662008-01-05 16:56:59 +00001749 // For X86-64, if there are vararg parameters that are passed via
1750 // registers, then we must store them to their spots on the stack so they
1751 // may be loaded by deferencing the result of va_next.
Dan Gohman1e93df62010-04-17 14:41:14 +00001752 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1753 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1754 FuncInfo->setRegSaveFrameIndex(
1755 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
1756 false));
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001757
Gordon Henriksen86737662008-01-05 16:56:59 +00001758 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001759 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001760 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1761 getPointerTy());
1762 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001763 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001764 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1765 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001766 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
1767 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001768 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001769 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001770 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Dan Gohman1e93df62010-04-17 14:41:14 +00001771 PseudoSourceValue::getFixedStack(
1772 FuncInfo->getRegSaveFrameIndex()),
David Greene67c9d422010-02-15 16:53:33 +00001773 Offset, false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001774 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001775 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001776 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001777
Dan Gohmanface41a2009-08-16 21:24:25 +00001778 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1779 // Now store the XMM (fp + vector) parameter registers.
1780 SmallVector<SDValue, 11> SaveXMMOps;
1781 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001782
Dan Gohmanface41a2009-08-16 21:24:25 +00001783 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
1784 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1785 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001786
Dan Gohman1e93df62010-04-17 14:41:14 +00001787 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1788 FuncInfo->getRegSaveFrameIndex()));
1789 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1790 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001791
Dan Gohmanface41a2009-08-16 21:24:25 +00001792 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
1793 unsigned VReg = MF.addLiveIn(XMMArgRegs[NumXMMRegs],
1794 X86::VR128RegisterClass);
1795 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1796 SaveXMMOps.push_back(Val);
1797 }
1798 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1799 MVT::Other,
1800 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001801 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001802
1803 if (!MemOps.empty())
1804 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1805 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001806 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001807 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001808
Gordon Henriksen86737662008-01-05 16:56:59 +00001809 // Some CCs need callee pop.
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001810 if (Subtarget->IsCalleePop(isVarArg, CallConv)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001811 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001812 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001813 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001814 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001815 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001816 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001817 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001818
Gordon Henriksen86737662008-01-05 16:56:59 +00001819 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001820 // RegSaveFrameIndex is X86-64 only.
1821 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001822 if (CallConv == CallingConv::X86_FastCall ||
1823 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001824 // fastcc functions can't have varargs.
1825 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001826 }
Evan Cheng25caf632006-05-23 21:06:34 +00001827
Dan Gohman98ca4f22009-08-05 01:29:28 +00001828 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001829}
1830
Dan Gohman475871a2008-07-27 21:46:04 +00001831SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001832X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1833 SDValue StackPtr, SDValue Arg,
1834 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001835 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001836 ISD::ArgFlagsTy Flags) const {
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +00001837 const unsigned FirstStackArgOffset = (Subtarget->isTargetWin64() ? 32 : 0);
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +00001838 unsigned LocMemOffset = FirstStackArgOffset + VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001839 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001840 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001841 if (Flags.isByVal()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001842 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Evan Chengdffbd832008-01-10 00:09:10 +00001843 }
Dale Johannesenace16102009-02-03 19:33:06 +00001844 return DAG.getStore(Chain, dl, Arg, PtrOff,
David Greene67c9d422010-02-15 16:53:33 +00001845 PseudoSourceValue::getStack(), LocMemOffset,
1846 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001847}
1848
Bill Wendling64e87322009-01-16 19:25:27 +00001849/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001850/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001851SDValue
1852X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001853 SDValue &OutRetAddr, SDValue Chain,
1854 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001855 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001856 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001857 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001858 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001859
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001860 // Load the "old" Return address.
David Greene67c9d422010-02-15 16:53:33 +00001861 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, NULL, 0, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001862 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001863}
1864
1865/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1866/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001867static SDValue
1868EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001869 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001870 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001871 // Store the return address to the appropriate stack slot.
1872 if (!FPDiff) return Chain;
1873 // Calculate the new stack slot for the return address.
1874 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001875 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00001876 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001877 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001878 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001879 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
David Greene67c9d422010-02-15 16:53:33 +00001880 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0,
1881 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001882 return Chain;
1883}
1884
Dan Gohman98ca4f22009-08-05 01:29:28 +00001885SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001886X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001887 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001888 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001889 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001890 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001891 const SmallVectorImpl<ISD::InputArg> &Ins,
1892 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001893 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001894 MachineFunction &MF = DAG.getMachineFunction();
1895 bool Is64Bit = Subtarget->is64Bit();
1896 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00001897 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001898
Evan Cheng5f941932010-02-05 02:21:12 +00001899 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001900 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00001901 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1902 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001903 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00001904
1905 // Sibcalls are automatically detected tailcalls which do not require
1906 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00001907 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00001908 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00001909
1910 if (isTailCall)
1911 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00001912 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00001913
Chris Lattner29689432010-03-11 00:22:57 +00001914 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1915 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001916
Chris Lattner638402b2007-02-28 07:00:42 +00001917 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001918 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001919 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1920 ArgLocs, *DAG.getContext());
1921 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001922
Chris Lattner423c5f42007-02-28 05:31:48 +00001923 // Get a count of how many bytes are to be pushed on the stack.
1924 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00001925 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00001926 // This is a sibcall. The memory operands are available in caller's
1927 // own caller's stack.
1928 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00001929 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00001930 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001931
Gordon Henriksen86737662008-01-05 16:56:59 +00001932 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00001933 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001934 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00001935 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00001936 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1937 FPDiff = NumBytesCallerPushed - NumBytes;
1938
1939 // Set the delta of movement of the returnaddr stackslot.
1940 // But only set if delta is greater than previous delta.
1941 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1942 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1943 }
1944
Evan Chengf22f9b32010-02-06 03:28:46 +00001945 if (!IsSibcall)
1946 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001947
Dan Gohman475871a2008-07-27 21:46:04 +00001948 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001949 // Load return adress for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00001950 if (isTailCall && FPDiff)
1951 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
1952 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001953
Dan Gohman475871a2008-07-27 21:46:04 +00001954 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1955 SmallVector<SDValue, 8> MemOpChains;
1956 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001957
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001958 // Walk the register/memloc assignments, inserting copies/loads. In the case
1959 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00001960 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1961 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001962 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001963 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001964 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00001965 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00001966
Chris Lattner423c5f42007-02-28 05:31:48 +00001967 // Promote the value if needed.
1968 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001969 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00001970 case CCValAssign::Full: break;
1971 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001972 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001973 break;
1974 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001975 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001976 break;
1977 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001978 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
1979 // Special case: passing MMX values in XMM registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001980 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
1981 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
1982 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001983 } else
1984 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
1985 break;
1986 case CCValAssign::BCvt:
1987 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001988 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001989 case CCValAssign::Indirect: {
1990 // Store the argument.
1991 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00001992 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001993 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
David Greene67c9d422010-02-15 16:53:33 +00001994 PseudoSourceValue::getFixedStack(FI), 0,
1995 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001996 Arg = SpillSlot;
1997 break;
1998 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00001999 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002000
Chris Lattner423c5f42007-02-28 05:31:48 +00002001 if (VA.isRegLoc()) {
2002 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Evan Chengf22f9b32010-02-06 03:28:46 +00002003 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002004 assert(VA.isMemLoc());
2005 if (StackPtr.getNode() == 0)
2006 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2007 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2008 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002009 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002010 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002011
Evan Cheng32fe1032006-05-25 00:59:30 +00002012 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002013 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002014 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002015
Evan Cheng347d5f72006-04-28 21:29:37 +00002016 // Build a sequence of copy-to-reg nodes chained together with token chain
2017 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002018 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002019 // Tail call byval lowering might overwrite argument registers so in case of
2020 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002021 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002022 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002023 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002024 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002025 InFlag = Chain.getValue(1);
2026 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002027
Chris Lattner88e1fd52009-07-09 04:24:46 +00002028 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002029 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2030 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002031 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002032 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2033 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002034 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002035 InFlag);
2036 InFlag = Chain.getValue(1);
2037 } else {
2038 // If we are tail calling and generating PIC/GOT style code load the
2039 // address of the callee into ECX. The value in ecx is used as target of
2040 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2041 // for tail calls on PIC/GOT architectures. Normally we would just put the
2042 // address of GOT into ebx and then call target@PLT. But for tail calls
2043 // ebx would be restored (since ebx is callee saved) before jumping to the
2044 // target@PLT.
2045
2046 // Note: The actual moving to ECX is done further down.
2047 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2048 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2049 !G->getGlobal()->hasProtectedVisibility())
2050 Callee = LowerGlobalAddress(Callee, DAG);
2051 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002052 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002053 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002054 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002055
Nate Begemanc8ea6732010-07-21 20:49:52 +00002056 if (Is64Bit && isVarArg && !Subtarget->isTargetWin64()) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002057 // From AMD64 ABI document:
2058 // For calls that may call functions that use varargs or stdargs
2059 // (prototype-less calls or calls to functions containing ellipsis (...) in
2060 // the declaration) %al is used as hidden argument to specify the number
2061 // of SSE registers used. The contents of %al do not need to match exactly
2062 // the number of registers, but must be an ubound on the number of SSE
2063 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002064
Gordon Henriksen86737662008-01-05 16:56:59 +00002065 // Count the number of XMM registers allocated.
2066 static const unsigned XMMArgRegs[] = {
2067 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2068 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2069 };
2070 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Scott Michelfdc40a02009-02-17 22:15:04 +00002071 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002072 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002073
Dale Johannesendd64c412009-02-04 00:33:20 +00002074 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002075 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002076 InFlag = Chain.getValue(1);
2077 }
2078
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002079
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002080 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002081 if (isTailCall) {
2082 // Force all the incoming stack arguments to be loaded from the stack
2083 // before any new outgoing arguments are stored to the stack, because the
2084 // outgoing stack slots may alias the incoming argument stack slots, and
2085 // the alias isn't otherwise explicit. This is slightly more conservative
2086 // than necessary, because it means that each store effectively depends
2087 // on every argument instead of just those arguments it would clobber.
2088 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2089
Dan Gohman475871a2008-07-27 21:46:04 +00002090 SmallVector<SDValue, 8> MemOpChains2;
2091 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002092 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002093 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002094 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002095 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002096 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2097 CCValAssign &VA = ArgLocs[i];
2098 if (VA.isRegLoc())
2099 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002100 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002101 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002102 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002103 // Create frame index.
2104 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002105 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002106 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002107 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002108
Duncan Sands276dcbd2008-03-21 09:14:45 +00002109 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002110 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002111 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002112 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002113 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002114 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002115 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002116
Dan Gohman98ca4f22009-08-05 01:29:28 +00002117 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2118 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002119 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002120 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002121 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002122 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002123 DAG.getStore(ArgChain, dl, Arg, FIN,
David Greene67c9d422010-02-15 16:53:33 +00002124 PseudoSourceValue::getFixedStack(FI), 0,
2125 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002126 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002127 }
2128 }
2129
2130 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002131 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002132 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002133
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002134 // Copy arguments to their registers.
2135 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002136 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002137 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002138 InFlag = Chain.getValue(1);
2139 }
Dan Gohman475871a2008-07-27 21:46:04 +00002140 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002141
Gordon Henriksen86737662008-01-05 16:56:59 +00002142 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002143 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002144 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002145 }
2146
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002147 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2148 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2149 // In the 64-bit large code model, we have to make all calls
2150 // through a register, since the call instruction's 32-bit
2151 // pc-relative offset may not be large enough to hold the whole
2152 // address.
2153 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002154 // If the callee is a GlobalAddress node (quite common, every direct call
2155 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2156 // it.
2157
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002158 // We should use extra load for direct calls to dllimported functions in
2159 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002160 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002161 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002162 unsigned char OpFlags = 0;
Eric Christopherfd179292009-08-27 18:07:15 +00002163
Chris Lattner48a7d022009-07-09 05:02:21 +00002164 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2165 // external symbols most go through the PLT in PIC mode. If the symbol
2166 // has hidden or protected visibility, or if it is static or local, then
2167 // we don't need to use the PLT - we can directly call it.
2168 if (Subtarget->isTargetELF() &&
2169 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002170 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002171 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002172 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002173 (GV->isDeclaration() || GV->isWeakForLinker()) &&
2174 Subtarget->getDarwinVers() < 9) {
2175 // PC-relative references to external symbols should go through $stub,
2176 // unless we're building with the leopard linker or later, which
2177 // automatically synthesizes these stubs.
2178 OpFlags = X86II::MO_DARWIN_STUB;
2179 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002180
Devang Patel0d881da2010-07-06 22:08:15 +00002181 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002182 G->getOffset(), OpFlags);
2183 }
Bill Wendling056292f2008-09-16 21:48:12 +00002184 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002185 unsigned char OpFlags = 0;
2186
2187 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to external
2188 // symbols should go through the PLT.
2189 if (Subtarget->isTargetELF() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002190 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002191 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002192 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002193 Subtarget->getDarwinVers() < 9) {
2194 // PC-relative references to external symbols should go through $stub,
2195 // unless we're building with the leopard linker or later, which
2196 // automatically synthesizes these stubs.
2197 OpFlags = X86II::MO_DARWIN_STUB;
2198 }
Eric Christopherfd179292009-08-27 18:07:15 +00002199
Chris Lattner48a7d022009-07-09 05:02:21 +00002200 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2201 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002202 }
2203
Chris Lattnerd96d0722007-02-25 06:40:16 +00002204 // Returns a chain & a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +00002205 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00002206 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002207
Evan Chengf22f9b32010-02-06 03:28:46 +00002208 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002209 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2210 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002211 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002212 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002213
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002214 Ops.push_back(Chain);
2215 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002216
Dan Gohman98ca4f22009-08-05 01:29:28 +00002217 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002218 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002219
Gordon Henriksen86737662008-01-05 16:56:59 +00002220 // Add argument registers to the end of the list so that they are known live
2221 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002222 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2223 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2224 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002225
Evan Cheng586ccac2008-03-18 23:36:35 +00002226 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002227 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002228 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2229
2230 // Add an implicit use of AL for x86 vararg functions.
2231 if (Is64Bit && isVarArg)
Owen Anderson825b72b2009-08-11 20:47:22 +00002232 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002233
Gabor Greifba36cb52008-08-28 21:40:38 +00002234 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002235 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002236
Dan Gohman98ca4f22009-08-05 01:29:28 +00002237 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002238 // We used to do:
2239 //// If this is the first return lowered for this function, add the regs
2240 //// to the liveout set for the function.
2241 // This isn't right, although it's probably harmless on x86; liveouts
2242 // should be computed from returns not tail calls. Consider a void
2243 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002244 return DAG.getNode(X86ISD::TC_RETURN, dl,
2245 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002246 }
2247
Dale Johannesenace16102009-02-03 19:33:06 +00002248 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002249 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002250
Chris Lattner2d297092006-05-23 18:50:38 +00002251 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002252 unsigned NumBytesForCalleeToPush;
Dan Gohman4d3d6e12010-05-27 18:43:40 +00002253 if (Subtarget->IsCalleePop(isVarArg, CallConv))
Gordon Henriksen86737662008-01-05 16:56:59 +00002254 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002255 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002256 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002257 // pops the hidden struct pointer, so we have to push it back.
2258 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002259 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002260 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002261 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002262
Gordon Henriksenae636f82008-01-03 16:47:34 +00002263 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002264 if (!IsSibcall) {
2265 Chain = DAG.getCALLSEQ_END(Chain,
2266 DAG.getIntPtrConstant(NumBytes, true),
2267 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2268 true),
2269 InFlag);
2270 InFlag = Chain.getValue(1);
2271 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002272
Chris Lattner3085e152007-02-25 08:59:22 +00002273 // Handle result values, copying them out of physregs into vregs that we
2274 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002275 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2276 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002277}
2278
Evan Cheng25ab6902006-09-08 06:48:29 +00002279
2280//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002281// Fast Calling Convention (tail call) implementation
2282//===----------------------------------------------------------------------===//
2283
2284// Like std call, callee cleans arguments, convention except that ECX is
2285// reserved for storing the tail called function address. Only 2 registers are
2286// free for argument passing (inreg). Tail call optimization is performed
2287// provided:
2288// * tailcallopt is enabled
2289// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002290// On X86_64 architecture with GOT-style position independent code only local
2291// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002292// To keep the stack aligned according to platform abi the function
2293// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2294// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002295// If a tail called function callee has more arguments than the caller the
2296// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002297// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002298// original REtADDR, but before the saved framepointer or the spilled registers
2299// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2300// stack layout:
2301// arg1
2302// arg2
2303// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002304// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002305// move area ]
2306// (possible EBP)
2307// ESI
2308// EDI
2309// local1 ..
2310
2311/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2312/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002313unsigned
2314X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2315 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002316 MachineFunction &MF = DAG.getMachineFunction();
2317 const TargetMachine &TM = MF.getTarget();
2318 const TargetFrameInfo &TFI = *TM.getFrameInfo();
2319 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002320 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002321 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002322 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002323 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2324 // Number smaller than 12 so just add the difference.
2325 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2326 } else {
2327 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002328 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002329 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002330 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002331 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002332}
2333
Evan Cheng5f941932010-02-05 02:21:12 +00002334/// MatchingStackOffset - Return true if the given stack call argument is
2335/// already available in the same position (relatively) of the caller's
2336/// incoming argument stack.
2337static
2338bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2339 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2340 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002341 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2342 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002343 if (Arg.getOpcode() == ISD::CopyFromReg) {
2344 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
2345 if (!VR || TargetRegisterInfo::isPhysicalRegister(VR))
2346 return false;
2347 MachineInstr *Def = MRI->getVRegDef(VR);
2348 if (!Def)
2349 return false;
2350 if (!Flags.isByVal()) {
2351 if (!TII->isLoadFromStackSlot(Def, FI))
2352 return false;
2353 } else {
2354 unsigned Opcode = Def->getOpcode();
2355 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2356 Def->getOperand(1).isFI()) {
2357 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002358 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002359 } else
2360 return false;
2361 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002362 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2363 if (Flags.isByVal())
2364 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002365 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002366 // define @foo(%struct.X* %A) {
2367 // tail call @bar(%struct.X* byval %A)
2368 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002369 return false;
2370 SDValue Ptr = Ld->getBasePtr();
2371 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2372 if (!FINode)
2373 return false;
2374 FI = FINode->getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002375 } else
2376 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002377
Evan Cheng4cae1332010-03-05 08:38:04 +00002378 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002379 if (!MFI->isFixedObjectIndex(FI))
2380 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002381 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002382}
2383
Dan Gohman98ca4f22009-08-05 01:29:28 +00002384/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2385/// for tail call optimization. Targets which want to do tail call
2386/// optimization should implement this function.
2387bool
2388X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002389 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002390 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002391 bool isCalleeStructRet,
2392 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002393 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002394 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002395 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002396 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002397 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002398 CalleeCC != CallingConv::C)
2399 return false;
2400
Evan Cheng7096ae42010-01-29 06:45:59 +00002401 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002402 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002403 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002404 CallingConv::ID CallerCC = CallerF->getCallingConv();
2405 bool CCMatch = CallerCC == CalleeCC;
2406
Dan Gohman1797ed52010-02-08 20:27:50 +00002407 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002408 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002409 return true;
2410 return false;
2411 }
2412
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002413 // Look for obvious safe cases to perform tail call optimization that do not
2414 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002415
Evan Cheng2c12cb42010-03-26 16:26:03 +00002416 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2417 // emit a special epilogue.
2418 if (RegInfo->needsStackRealignment(MF))
2419 return false;
2420
Eric Christopher90eb4022010-07-22 00:26:08 +00002421 // Do not sibcall optimize vararg calls unless the call site is not passing
2422 // any arguments.
Evan Cheng3c262ee2010-03-26 02:13:13 +00002423 if (isVarArg && !Outs.empty())
Evan Cheng843bd692010-01-31 06:44:49 +00002424 return false;
2425
Evan Chenga375d472010-03-15 18:54:48 +00002426 // Also avoid sibcall optimization if either caller or callee uses struct
2427 // return semantics.
2428 if (isCalleeStructRet || isCallerStructRet)
2429 return false;
2430
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002431 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2432 // Therefore if it's not used by the call it is not safe to optimize this into
2433 // a sibcall.
2434 bool Unused = false;
2435 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2436 if (!Ins[i].Used) {
2437 Unused = true;
2438 break;
2439 }
2440 }
2441 if (Unused) {
2442 SmallVector<CCValAssign, 16> RVLocs;
2443 CCState CCInfo(CalleeCC, false, getTargetMachine(),
2444 RVLocs, *DAG.getContext());
2445 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002446 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002447 CCValAssign &VA = RVLocs[i];
2448 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2449 return false;
2450 }
2451 }
2452
Evan Cheng13617962010-04-30 01:12:32 +00002453 // If the calling conventions do not match, then we'd better make sure the
2454 // results are returned in the same way as what the caller expects.
2455 if (!CCMatch) {
2456 SmallVector<CCValAssign, 16> RVLocs1;
2457 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
2458 RVLocs1, *DAG.getContext());
2459 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2460
2461 SmallVector<CCValAssign, 16> RVLocs2;
2462 CCState CCInfo2(CallerCC, false, getTargetMachine(),
2463 RVLocs2, *DAG.getContext());
2464 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2465
2466 if (RVLocs1.size() != RVLocs2.size())
2467 return false;
2468 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2469 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2470 return false;
2471 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2472 return false;
2473 if (RVLocs1[i].isRegLoc()) {
2474 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2475 return false;
2476 } else {
2477 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2478 return false;
2479 }
2480 }
2481 }
2482
Evan Chenga6bff982010-01-30 01:22:00 +00002483 // If the callee takes no arguments then go on to check the results of the
2484 // call.
2485 if (!Outs.empty()) {
2486 // Check if stack adjustment is needed. For now, do not do this if any
2487 // argument is passed on the stack.
2488 SmallVector<CCValAssign, 16> ArgLocs;
2489 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
2490 ArgLocs, *DAG.getContext());
2491 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CalleeCC));
Evan Chengb2c92902010-02-02 02:22:50 +00002492 if (CCInfo.getNextStackOffset()) {
2493 MachineFunction &MF = DAG.getMachineFunction();
2494 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2495 return false;
2496 if (Subtarget->isTargetWin64())
2497 // Win64 ABI has additional complications.
2498 return false;
2499
2500 // Check if the arguments are already laid out in the right way as
2501 // the caller's fixed stack objects.
2502 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002503 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2504 const X86InstrInfo *TII =
2505 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002506 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2507 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002508 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002509 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002510 if (VA.getLocInfo() == CCValAssign::Indirect)
2511 return false;
2512 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002513 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2514 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002515 return false;
2516 }
2517 }
2518 }
Evan Cheng9c044672010-05-29 01:35:22 +00002519
2520 // If the tailcall address may be in a register, then make sure it's
2521 // possible to register allocate for it. In 32-bit, the call address can
2522 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002523 // callee-saved registers are restored. These happen to be the same
2524 // registers used to pass 'inreg' arguments so watch out for those.
2525 if (!Subtarget->is64Bit() &&
2526 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002527 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002528 unsigned NumInRegs = 0;
2529 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2530 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002531 if (!VA.isRegLoc())
2532 continue;
2533 unsigned Reg = VA.getLocReg();
2534 switch (Reg) {
2535 default: break;
2536 case X86::EAX: case X86::EDX: case X86::ECX:
2537 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002538 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002539 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002540 }
2541 }
2542 }
Evan Chenga6bff982010-01-30 01:22:00 +00002543 }
Evan Chengb1712452010-01-27 06:25:16 +00002544
Evan Cheng86809cc2010-02-03 03:28:02 +00002545 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002546}
2547
Dan Gohman3df24e62008-09-03 23:12:08 +00002548FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002549X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2550 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002551}
2552
2553
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002554//===----------------------------------------------------------------------===//
2555// Other Lowering Hooks
2556//===----------------------------------------------------------------------===//
2557
2558
Dan Gohmand858e902010-04-17 15:26:15 +00002559SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002560 MachineFunction &MF = DAG.getMachineFunction();
2561 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2562 int ReturnAddrIndex = FuncInfo->getRAIndex();
2563
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002564 if (ReturnAddrIndex == 0) {
2565 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002566 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002567 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002568 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002569 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002570 }
2571
Evan Cheng25ab6902006-09-08 06:48:29 +00002572 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002573}
2574
2575
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002576bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2577 bool hasSymbolicDisplacement) {
2578 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002579 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002580 return false;
2581
2582 // If we don't have a symbolic displacement - we don't have any extra
2583 // restrictions.
2584 if (!hasSymbolicDisplacement)
2585 return true;
2586
2587 // FIXME: Some tweaks might be needed for medium code model.
2588 if (M != CodeModel::Small && M != CodeModel::Kernel)
2589 return false;
2590
2591 // For small code model we assume that latest object is 16MB before end of 31
2592 // bits boundary. We may also accept pretty large negative constants knowing
2593 // that all objects are in the positive half of address space.
2594 if (M == CodeModel::Small && Offset < 16*1024*1024)
2595 return true;
2596
2597 // For kernel code model we know that all object resist in the negative half
2598 // of 32bits address space. We may not accept negative offsets, since they may
2599 // be just off and we may accept pretty large positive ones.
2600 if (M == CodeModel::Kernel && Offset > 0)
2601 return true;
2602
2603 return false;
2604}
2605
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002606/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2607/// specific condition code, returning the condition code and the LHS/RHS of the
2608/// comparison to make.
2609static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2610 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002611 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002612 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2613 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2614 // X > -1 -> X == 0, jump !sign.
2615 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002616 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002617 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2618 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002619 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002620 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002621 // X < 1 -> X <= 0
2622 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002623 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002624 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002625 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002626
Evan Chengd9558e02006-01-06 00:43:03 +00002627 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002628 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002629 case ISD::SETEQ: return X86::COND_E;
2630 case ISD::SETGT: return X86::COND_G;
2631 case ISD::SETGE: return X86::COND_GE;
2632 case ISD::SETLT: return X86::COND_L;
2633 case ISD::SETLE: return X86::COND_LE;
2634 case ISD::SETNE: return X86::COND_NE;
2635 case ISD::SETULT: return X86::COND_B;
2636 case ISD::SETUGT: return X86::COND_A;
2637 case ISD::SETULE: return X86::COND_BE;
2638 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002639 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002640 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002641
Chris Lattner4c78e022008-12-23 23:42:27 +00002642 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002643
Chris Lattner4c78e022008-12-23 23:42:27 +00002644 // If LHS is a foldable load, but RHS is not, flip the condition.
2645 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2646 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2647 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2648 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002649 }
2650
Chris Lattner4c78e022008-12-23 23:42:27 +00002651 switch (SetCCOpcode) {
2652 default: break;
2653 case ISD::SETOLT:
2654 case ISD::SETOLE:
2655 case ISD::SETUGT:
2656 case ISD::SETUGE:
2657 std::swap(LHS, RHS);
2658 break;
2659 }
2660
2661 // On a floating point condition, the flags are set as follows:
2662 // ZF PF CF op
2663 // 0 | 0 | 0 | X > Y
2664 // 0 | 0 | 1 | X < Y
2665 // 1 | 0 | 0 | X == Y
2666 // 1 | 1 | 1 | unordered
2667 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002668 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002669 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002670 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002671 case ISD::SETOLT: // flipped
2672 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002673 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002674 case ISD::SETOLE: // flipped
2675 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002676 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002677 case ISD::SETUGT: // flipped
2678 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002679 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002680 case ISD::SETUGE: // flipped
2681 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002682 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002683 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002684 case ISD::SETNE: return X86::COND_NE;
2685 case ISD::SETUO: return X86::COND_P;
2686 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002687 case ISD::SETOEQ:
2688 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002689 }
Evan Chengd9558e02006-01-06 00:43:03 +00002690}
2691
Evan Cheng4a460802006-01-11 00:33:36 +00002692/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2693/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002694/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002695static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002696 switch (X86CC) {
2697 default:
2698 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002699 case X86::COND_B:
2700 case X86::COND_BE:
2701 case X86::COND_E:
2702 case X86::COND_P:
2703 case X86::COND_A:
2704 case X86::COND_AE:
2705 case X86::COND_NE:
2706 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002707 return true;
2708 }
2709}
2710
Evan Chengeb2f9692009-10-27 19:56:55 +00002711/// isFPImmLegal - Returns true if the target can instruction select the
2712/// specified FP immediate natively. If false, the legalizer will
2713/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002714bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00002715 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
2716 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
2717 return true;
2718 }
2719 return false;
2720}
2721
Nate Begeman9008ca62009-04-27 18:41:29 +00002722/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2723/// the specified range (L, H].
2724static bool isUndefOrInRange(int Val, int Low, int Hi) {
2725 return (Val < 0) || (Val >= Low && Val < Hi);
2726}
2727
2728/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2729/// specified value.
2730static bool isUndefOrEqual(int Val, int CmpVal) {
2731 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002732 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002733 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002734}
2735
Nate Begeman9008ca62009-04-27 18:41:29 +00002736/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2737/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2738/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00002739static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002740 if (VT == MVT::v4f32 || VT == MVT::v4i32 || VT == MVT::v4i16)
Nate Begeman9008ca62009-04-27 18:41:29 +00002741 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002742 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00002743 return (Mask[0] < 2 && Mask[1] < 2);
2744 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002745}
2746
Nate Begeman9008ca62009-04-27 18:41:29 +00002747bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002748 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002749 N->getMask(M);
2750 return ::isPSHUFDMask(M, N->getValueType(0));
2751}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002752
Nate Begeman9008ca62009-04-27 18:41:29 +00002753/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2754/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00002755static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002756 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002757 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002758
Nate Begeman9008ca62009-04-27 18:41:29 +00002759 // Lower quadword copied in order or undef.
2760 for (int i = 0; i != 4; ++i)
2761 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002762 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002763
Evan Cheng506d3df2006-03-29 23:07:14 +00002764 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002765 for (int i = 4; i != 8; ++i)
2766 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002767 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002768
Evan Cheng506d3df2006-03-29 23:07:14 +00002769 return true;
2770}
2771
Nate Begeman9008ca62009-04-27 18:41:29 +00002772bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002773 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002774 N->getMask(M);
2775 return ::isPSHUFHWMask(M, N->getValueType(0));
2776}
Evan Cheng506d3df2006-03-29 23:07:14 +00002777
Nate Begeman9008ca62009-04-27 18:41:29 +00002778/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2779/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00002780static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002781 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002782 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002783
Rafael Espindola15684b22009-04-24 12:40:33 +00002784 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00002785 for (int i = 4; i != 8; ++i)
2786 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00002787 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002788
Rafael Espindola15684b22009-04-24 12:40:33 +00002789 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002790 for (int i = 0; i != 4; ++i)
2791 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00002792 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002793
Rafael Espindola15684b22009-04-24 12:40:33 +00002794 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002795}
2796
Nate Begeman9008ca62009-04-27 18:41:29 +00002797bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002798 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002799 N->getMask(M);
2800 return ::isPSHUFLWMask(M, N->getValueType(0));
2801}
2802
Nate Begemana09008b2009-10-19 02:17:23 +00002803/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
2804/// is suitable for input to PALIGNR.
2805static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
2806 bool hasSSSE3) {
2807 int i, e = VT.getVectorNumElements();
2808
2809 // Do not handle v2i64 / v2f64 shuffles with palignr.
2810 if (e < 4 || !hasSSSE3)
2811 return false;
2812
2813 for (i = 0; i != e; ++i)
2814 if (Mask[i] >= 0)
2815 break;
2816
2817 // All undef, not a palignr.
2818 if (i == e)
2819 return false;
2820
2821 // Determine if it's ok to perform a palignr with only the LHS, since we
2822 // don't have access to the actual shuffle elements to see if RHS is undef.
2823 bool Unary = Mask[i] < (int)e;
2824 bool NeedsUnary = false;
2825
2826 int s = Mask[i] - i;
2827
2828 // Check the rest of the elements to see if they are consecutive.
2829 for (++i; i != e; ++i) {
2830 int m = Mask[i];
2831 if (m < 0)
2832 continue;
2833
2834 Unary = Unary && (m < (int)e);
2835 NeedsUnary = NeedsUnary || (m < s);
2836
2837 if (NeedsUnary && !Unary)
2838 return false;
2839 if (Unary && m != ((s+i) & (e-1)))
2840 return false;
2841 if (!Unary && m != (s+i))
2842 return false;
2843 }
2844 return true;
2845}
2846
2847bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
2848 SmallVector<int, 8> M;
2849 N->getMask(M);
2850 return ::isPALIGNRMask(M, N->getValueType(0), true);
2851}
2852
Evan Cheng14aed5e2006-03-24 01:18:28 +00002853/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2854/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00002855static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002856 int NumElems = VT.getVectorNumElements();
2857 if (NumElems != 2 && NumElems != 4)
2858 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002859
Nate Begeman9008ca62009-04-27 18:41:29 +00002860 int Half = NumElems / 2;
2861 for (int i = 0; i < Half; ++i)
2862 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002863 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002864 for (int i = Half; i < NumElems; ++i)
2865 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002866 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002867
Evan Cheng14aed5e2006-03-24 01:18:28 +00002868 return true;
2869}
2870
Nate Begeman9008ca62009-04-27 18:41:29 +00002871bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
2872 SmallVector<int, 8> M;
2873 N->getMask(M);
2874 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002875}
2876
Evan Cheng213d2cf2007-05-17 18:45:50 +00002877/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002878/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2879/// half elements to come from vector 1 (which would equal the dest.) and
2880/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00002881static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002882 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002883
2884 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00002885 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002886
Nate Begeman9008ca62009-04-27 18:41:29 +00002887 int Half = NumElems / 2;
2888 for (int i = 0; i < Half; ++i)
2889 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002890 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002891 for (int i = Half; i < NumElems; ++i)
2892 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002893 return false;
2894 return true;
2895}
2896
Nate Begeman9008ca62009-04-27 18:41:29 +00002897static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
2898 SmallVector<int, 8> M;
2899 N->getMask(M);
2900 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002901}
2902
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002903/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2904/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002905bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
2906 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002907 return false;
2908
Evan Cheng2064a2b2006-03-28 06:50:32 +00002909 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00002910 return isUndefOrEqual(N->getMaskElt(0), 6) &&
2911 isUndefOrEqual(N->getMaskElt(1), 7) &&
2912 isUndefOrEqual(N->getMaskElt(2), 2) &&
2913 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002914}
2915
Nate Begeman0b10b912009-11-07 23:17:15 +00002916/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2917/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2918/// <2, 3, 2, 3>
2919bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
2920 unsigned NumElems = N->getValueType(0).getVectorNumElements();
2921
2922 if (NumElems != 4)
2923 return false;
2924
2925 return isUndefOrEqual(N->getMaskElt(0), 2) &&
2926 isUndefOrEqual(N->getMaskElt(1), 3) &&
2927 isUndefOrEqual(N->getMaskElt(2), 2) &&
2928 isUndefOrEqual(N->getMaskElt(3), 3);
2929}
2930
Evan Cheng5ced1d82006-04-06 23:23:56 +00002931/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2932/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00002933bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
2934 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002935
Evan Cheng5ced1d82006-04-06 23:23:56 +00002936 if (NumElems != 2 && NumElems != 4)
2937 return false;
2938
Evan Chengc5cdff22006-04-07 21:53:05 +00002939 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002940 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002941 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002942
Evan Chengc5cdff22006-04-07 21:53:05 +00002943 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002944 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002945 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002946
2947 return true;
2948}
2949
Nate Begeman0b10b912009-11-07 23:17:15 +00002950/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
2951/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
2952bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002953 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002954
Evan Cheng5ced1d82006-04-06 23:23:56 +00002955 if (NumElems != 2 && NumElems != 4)
2956 return false;
2957
Evan Chengc5cdff22006-04-07 21:53:05 +00002958 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002959 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002960 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002961
Nate Begeman9008ca62009-04-27 18:41:29 +00002962 for (unsigned i = 0; i < NumElems/2; ++i)
2963 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002964 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002965
2966 return true;
2967}
2968
Evan Cheng0038e592006-03-28 00:39:58 +00002969/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2970/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00002971static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002972 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002973 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002974 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00002975 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002976
Nate Begeman9008ca62009-04-27 18:41:29 +00002977 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2978 int BitI = Mask[i];
2979 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002980 if (!isUndefOrEqual(BitI, j))
2981 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002982 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002983 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002984 return false;
2985 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002986 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002987 return false;
2988 }
Evan Cheng0038e592006-03-28 00:39:58 +00002989 }
Evan Cheng0038e592006-03-28 00:39:58 +00002990 return true;
2991}
2992
Nate Begeman9008ca62009-04-27 18:41:29 +00002993bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2994 SmallVector<int, 8> M;
2995 N->getMask(M);
2996 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002997}
2998
Evan Cheng4fcb9222006-03-28 02:43:26 +00002999/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3000/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003001static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003002 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003003 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003004 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003005 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003006
Nate Begeman9008ca62009-04-27 18:41:29 +00003007 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3008 int BitI = Mask[i];
3009 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00003010 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00003011 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003012 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00003013 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003014 return false;
3015 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003016 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003017 return false;
3018 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003019 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003020 return true;
3021}
3022
Nate Begeman9008ca62009-04-27 18:41:29 +00003023bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3024 SmallVector<int, 8> M;
3025 N->getMask(M);
3026 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003027}
3028
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003029/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3030/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3031/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003032static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003033 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003034 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003035 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003036
Nate Begeman9008ca62009-04-27 18:41:29 +00003037 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
3038 int BitI = Mask[i];
3039 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00003040 if (!isUndefOrEqual(BitI, j))
3041 return false;
3042 if (!isUndefOrEqual(BitI1, j))
3043 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003044 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003045 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003046}
3047
Nate Begeman9008ca62009-04-27 18:41:29 +00003048bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3049 SmallVector<int, 8> M;
3050 N->getMask(M);
3051 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3052}
3053
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003054/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3055/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3056/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003057static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003058 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003059 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3060 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003061
Nate Begeman9008ca62009-04-27 18:41:29 +00003062 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3063 int BitI = Mask[i];
3064 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003065 if (!isUndefOrEqual(BitI, j))
3066 return false;
3067 if (!isUndefOrEqual(BitI1, j))
3068 return false;
3069 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003070 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003071}
3072
Nate Begeman9008ca62009-04-27 18:41:29 +00003073bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3074 SmallVector<int, 8> M;
3075 N->getMask(M);
3076 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3077}
3078
Evan Cheng017dcc62006-04-21 01:05:10 +00003079/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3080/// specifies a shuffle of elements that is suitable for input to MOVSS,
3081/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003082static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003083 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003084 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003085
3086 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003087
Nate Begeman9008ca62009-04-27 18:41:29 +00003088 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003089 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003090
Nate Begeman9008ca62009-04-27 18:41:29 +00003091 for (int i = 1; i < NumElts; ++i)
3092 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003093 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003094
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003095 return true;
3096}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003097
Nate Begeman9008ca62009-04-27 18:41:29 +00003098bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3099 SmallVector<int, 8> M;
3100 N->getMask(M);
3101 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003102}
3103
Evan Cheng017dcc62006-04-21 01:05:10 +00003104/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3105/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003106/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003107static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003108 bool V2IsSplat = false, bool V2IsUndef = false) {
3109 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003110 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003111 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003112
Nate Begeman9008ca62009-04-27 18:41:29 +00003113 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003114 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003115
Nate Begeman9008ca62009-04-27 18:41:29 +00003116 for (int i = 1; i < NumOps; ++i)
3117 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3118 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3119 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003120 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003121
Evan Cheng39623da2006-04-20 08:58:49 +00003122 return true;
3123}
3124
Nate Begeman9008ca62009-04-27 18:41:29 +00003125static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003126 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003127 SmallVector<int, 8> M;
3128 N->getMask(M);
3129 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003130}
3131
Evan Chengd9539472006-04-14 21:59:03 +00003132/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3133/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003134bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
3135 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003136 return false;
3137
3138 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00003139 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003140 int Elt = N->getMaskElt(i);
3141 if (Elt >= 0 && Elt != 1)
3142 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00003143 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003144
3145 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003146 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003147 int Elt = N->getMaskElt(i);
3148 if (Elt >= 0 && Elt != 3)
3149 return false;
3150 if (Elt == 3)
3151 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003152 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003153 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00003154 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003155 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003156}
3157
3158/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3159/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003160bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
3161 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003162 return false;
3163
3164 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00003165 for (unsigned i = 0; i < 2; ++i)
3166 if (N->getMaskElt(i) > 0)
3167 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003168
3169 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003170 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003171 int Elt = N->getMaskElt(i);
3172 if (Elt >= 0 && Elt != 2)
3173 return false;
3174 if (Elt == 2)
3175 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003176 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003177 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003178 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003179}
3180
Evan Cheng0b457f02008-09-25 20:50:48 +00003181/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3182/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003183bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3184 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003185
Nate Begeman9008ca62009-04-27 18:41:29 +00003186 for (int i = 0; i < e; ++i)
3187 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003188 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003189 for (int i = 0; i < e; ++i)
3190 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003191 return false;
3192 return true;
3193}
3194
Evan Cheng63d33002006-03-22 08:01:21 +00003195/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003196/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003197unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003198 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3199 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3200
Evan Chengb9df0ca2006-03-22 02:53:00 +00003201 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3202 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003203 for (int i = 0; i < NumOperands; ++i) {
3204 int Val = SVOp->getMaskElt(NumOperands-i-1);
3205 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003206 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003207 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003208 if (i != NumOperands - 1)
3209 Mask <<= Shift;
3210 }
Evan Cheng63d33002006-03-22 08:01:21 +00003211 return Mask;
3212}
3213
Evan Cheng506d3df2006-03-29 23:07:14 +00003214/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003215/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003216unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003217 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003218 unsigned Mask = 0;
3219 // 8 nodes, but we only care about the last 4.
3220 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003221 int Val = SVOp->getMaskElt(i);
3222 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003223 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003224 if (i != 4)
3225 Mask <<= 2;
3226 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003227 return Mask;
3228}
3229
3230/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003231/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003232unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003233 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003234 unsigned Mask = 0;
3235 // 8 nodes, but we only care about the first 4.
3236 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003237 int Val = SVOp->getMaskElt(i);
3238 if (Val >= 0)
3239 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003240 if (i != 0)
3241 Mask <<= 2;
3242 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003243 return Mask;
3244}
3245
Nate Begemana09008b2009-10-19 02:17:23 +00003246/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3247/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3248unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3249 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3250 EVT VVT = N->getValueType(0);
3251 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3252 int Val = 0;
3253
3254 unsigned i, e;
3255 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3256 Val = SVOp->getMaskElt(i);
3257 if (Val >= 0)
3258 break;
3259 }
3260 return (Val - i) * EltSize;
3261}
3262
Evan Cheng37b73872009-07-30 08:33:02 +00003263/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3264/// constant +0.0.
3265bool X86::isZeroNode(SDValue Elt) {
3266 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00003267 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00003268 (isa<ConstantFPSDNode>(Elt) &&
3269 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3270}
3271
Nate Begeman9008ca62009-04-27 18:41:29 +00003272/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3273/// their permute mask.
3274static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3275 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003276 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003277 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003278 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003279
Nate Begeman5a5ca152009-04-29 05:20:52 +00003280 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003281 int idx = SVOp->getMaskElt(i);
3282 if (idx < 0)
3283 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003284 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003285 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003286 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003287 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003288 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003289 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3290 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003291}
3292
Evan Cheng779ccea2007-12-07 21:30:01 +00003293/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3294/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003295static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003296 unsigned NumElems = VT.getVectorNumElements();
3297 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003298 int idx = Mask[i];
3299 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003300 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003301 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003302 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003303 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003304 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003305 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003306}
3307
Evan Cheng533a0aa2006-04-19 20:35:22 +00003308/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3309/// match movhlps. The lower half elements should come from upper half of
3310/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003311/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003312static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3313 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003314 return false;
3315 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003316 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003317 return false;
3318 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003319 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003320 return false;
3321 return true;
3322}
3323
Evan Cheng5ced1d82006-04-06 23:23:56 +00003324/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003325/// is promoted to a vector. It also returns the LoadSDNode by reference if
3326/// required.
3327static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003328 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3329 return false;
3330 N = N->getOperand(0).getNode();
3331 if (!ISD::isNON_EXTLoad(N))
3332 return false;
3333 if (LD)
3334 *LD = cast<LoadSDNode>(N);
3335 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003336}
3337
Evan Cheng533a0aa2006-04-19 20:35:22 +00003338/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3339/// match movlp{s|d}. The lower half elements should come from lower half of
3340/// V1 (and in order), and the upper half elements should come from the upper
3341/// half of V2 (and in order). And since V1 will become the source of the
3342/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003343static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3344 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003345 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003346 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003347 // Is V2 is a vector load, don't do this transformation. We will try to use
3348 // load folding shufps op.
3349 if (ISD::isNON_EXTLoad(V2))
3350 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003351
Nate Begeman5a5ca152009-04-29 05:20:52 +00003352 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003353
Evan Cheng533a0aa2006-04-19 20:35:22 +00003354 if (NumElems != 2 && NumElems != 4)
3355 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003356 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003357 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003358 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003359 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003360 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003361 return false;
3362 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003363}
3364
Evan Cheng39623da2006-04-20 08:58:49 +00003365/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3366/// all the same.
3367static bool isSplatVector(SDNode *N) {
3368 if (N->getOpcode() != ISD::BUILD_VECTOR)
3369 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003370
Dan Gohman475871a2008-07-27 21:46:04 +00003371 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003372 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3373 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003374 return false;
3375 return true;
3376}
3377
Evan Cheng213d2cf2007-05-17 18:45:50 +00003378/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003379/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003380/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003381static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003382 SDValue V1 = N->getOperand(0);
3383 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003384 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3385 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003386 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003387 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003388 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003389 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3390 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003391 if (Opc != ISD::BUILD_VECTOR ||
3392 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003393 return false;
3394 } else if (Idx >= 0) {
3395 unsigned Opc = V1.getOpcode();
3396 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3397 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003398 if (Opc != ISD::BUILD_VECTOR ||
3399 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003400 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003401 }
3402 }
3403 return true;
3404}
3405
3406/// getZeroVector - Returns a vector of specified type with all zero elements.
3407///
Owen Andersone50ed302009-08-10 22:56:29 +00003408static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003409 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003410 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003411
Chris Lattner8a594482007-11-25 00:24:49 +00003412 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3413 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003414 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003415 if (VT.getSizeInBits() == 64) { // MMX
Owen Anderson825b72b2009-08-11 20:47:22 +00003416 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3417 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003418 } else if (HasSSE2) { // SSE2
Owen Anderson825b72b2009-08-11 20:47:22 +00003419 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3420 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003421 } else { // SSE1
Owen Anderson825b72b2009-08-11 20:47:22 +00003422 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3423 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003424 }
Dale Johannesenace16102009-02-03 19:33:06 +00003425 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003426}
3427
Chris Lattner8a594482007-11-25 00:24:49 +00003428/// getOnesVector - Returns a vector of specified type with all bits set.
3429///
Owen Andersone50ed302009-08-10 22:56:29 +00003430static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003431 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003432
Chris Lattner8a594482007-11-25 00:24:49 +00003433 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3434 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003435 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003436 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003437 if (VT.getSizeInBits() == 64) // MMX
Owen Anderson825b72b2009-08-11 20:47:22 +00003438 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Chris Lattner8a594482007-11-25 00:24:49 +00003439 else // SSE
Owen Anderson825b72b2009-08-11 20:47:22 +00003440 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Dale Johannesenace16102009-02-03 19:33:06 +00003441 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003442}
3443
3444
Evan Cheng39623da2006-04-20 08:58:49 +00003445/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3446/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003447static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003448 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003449 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003450
Evan Cheng39623da2006-04-20 08:58:49 +00003451 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003452 SmallVector<int, 8> MaskVec;
3453 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003454
Nate Begeman5a5ca152009-04-29 05:20:52 +00003455 for (unsigned i = 0; i != NumElems; ++i) {
3456 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003457 MaskVec[i] = NumElems;
3458 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003459 }
Evan Cheng39623da2006-04-20 08:58:49 +00003460 }
Evan Cheng39623da2006-04-20 08:58:49 +00003461 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003462 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3463 SVOp->getOperand(1), &MaskVec[0]);
3464 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003465}
3466
Evan Cheng017dcc62006-04-21 01:05:10 +00003467/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3468/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003469static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003470 SDValue V2) {
3471 unsigned NumElems = VT.getVectorNumElements();
3472 SmallVector<int, 8> Mask;
3473 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003474 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003475 Mask.push_back(i);
3476 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003477}
3478
Nate Begeman9008ca62009-04-27 18:41:29 +00003479/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003480static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003481 SDValue V2) {
3482 unsigned NumElems = VT.getVectorNumElements();
3483 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003484 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003485 Mask.push_back(i);
3486 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003487 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003488 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003489}
3490
Nate Begeman9008ca62009-04-27 18:41:29 +00003491/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003492static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003493 SDValue V2) {
3494 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003495 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003496 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003497 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003498 Mask.push_back(i + Half);
3499 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003500 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003501 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003502}
3503
Evan Cheng0c0f83f2008-04-05 00:30:36 +00003504/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Eric Christopherfd179292009-08-27 18:07:15 +00003505static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00003506 bool HasSSE2) {
3507 if (SV->getValueType(0).getVectorNumElements() <= 4)
3508 return SDValue(SV, 0);
Eric Christopherfd179292009-08-27 18:07:15 +00003509
Owen Anderson825b72b2009-08-11 20:47:22 +00003510 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003511 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003512 DebugLoc dl = SV->getDebugLoc();
3513 SDValue V1 = SV->getOperand(0);
3514 int NumElems = VT.getVectorNumElements();
3515 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003516
Nate Begeman9008ca62009-04-27 18:41:29 +00003517 // unpack elements to the correct location
3518 while (NumElems > 4) {
3519 if (EltNo < NumElems/2) {
3520 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3521 } else {
3522 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3523 EltNo -= NumElems/2;
3524 }
3525 NumElems >>= 1;
3526 }
Eric Christopherfd179292009-08-27 18:07:15 +00003527
Nate Begeman9008ca62009-04-27 18:41:29 +00003528 // Perform the splat.
3529 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Dale Johannesenace16102009-02-03 19:33:06 +00003530 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003531 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
3532 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003533}
3534
Evan Chengba05f722006-04-21 23:03:30 +00003535/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003536/// vector of zero or undef vector. This produces a shuffle where the low
3537/// element of V2 is swizzled into the zero/undef vector, landing at element
3538/// 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 +00003539static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003540 bool isZero, bool HasSSE2,
3541 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003542 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003543 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003544 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3545 unsigned NumElems = VT.getVectorNumElements();
3546 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003547 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003548 // If this is the insertion idx, put the low elt of V2 here.
3549 MaskVec.push_back(i == Idx ? NumElems : i);
3550 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003551}
3552
Evan Chengf26ffe92008-05-29 08:22:04 +00003553/// getNumOfConsecutiveZeros - Return the number of elements in a result of
3554/// a shuffle that is zero.
3555static
Nate Begeman9008ca62009-04-27 18:41:29 +00003556unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, int NumElems,
3557 bool Low, SelectionDAG &DAG) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003558 unsigned NumZeros = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003559 for (int i = 0; i < NumElems; ++i) {
Evan Chengab262272008-06-25 20:52:59 +00003560 unsigned Index = Low ? i : NumElems-i-1;
Nate Begeman9008ca62009-04-27 18:41:29 +00003561 int Idx = SVOp->getMaskElt(Index);
3562 if (Idx < 0) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003563 ++NumZeros;
3564 continue;
3565 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003566 SDValue Elt = DAG.getShuffleScalarElt(SVOp, Index);
Evan Cheng37b73872009-07-30 08:33:02 +00003567 if (Elt.getNode() && X86::isZeroNode(Elt))
Evan Chengf26ffe92008-05-29 08:22:04 +00003568 ++NumZeros;
3569 else
3570 break;
3571 }
3572 return NumZeros;
3573}
3574
3575/// isVectorShift - Returns true if the shuffle can be implemented as a
3576/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003577/// FIXME: split into pslldqi, psrldqi, palignr variants.
3578static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00003579 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
John McCallb1fb4492010-04-07 01:49:15 +00003580 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
Evan Chengf26ffe92008-05-29 08:22:04 +00003581
3582 isLeft = true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003583 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, true, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003584 if (!NumZeros) {
3585 isLeft = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003586 NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, false, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003587 if (!NumZeros)
3588 return false;
3589 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003590 bool SeenV1 = false;
3591 bool SeenV2 = false;
John McCallb1fb4492010-04-07 01:49:15 +00003592 for (unsigned i = NumZeros; i < NumElems; ++i) {
3593 unsigned Val = isLeft ? (i - NumZeros) : i;
3594 int Idx_ = SVOp->getMaskElt(isLeft ? i : (i - NumZeros));
3595 if (Idx_ < 0)
Evan Chengf26ffe92008-05-29 08:22:04 +00003596 continue;
John McCallb1fb4492010-04-07 01:49:15 +00003597 unsigned Idx = (unsigned) Idx_;
Nate Begeman9008ca62009-04-27 18:41:29 +00003598 if (Idx < NumElems)
Evan Chengf26ffe92008-05-29 08:22:04 +00003599 SeenV1 = true;
3600 else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003601 Idx -= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00003602 SeenV2 = true;
3603 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003604 if (Idx != Val)
Evan Chengf26ffe92008-05-29 08:22:04 +00003605 return false;
3606 }
3607 if (SeenV1 && SeenV2)
3608 return false;
3609
Nate Begeman9008ca62009-04-27 18:41:29 +00003610 ShVal = SeenV1 ? SVOp->getOperand(0) : SVOp->getOperand(1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003611 ShAmt = NumZeros;
3612 return true;
3613}
3614
3615
Evan Chengc78d3b42006-04-24 18:01:45 +00003616/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3617///
Dan Gohman475871a2008-07-27 21:46:04 +00003618static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003619 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00003620 SelectionDAG &DAG,
3621 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003622 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00003623 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003624
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003625 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003626 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003627 bool First = true;
3628 for (unsigned i = 0; i < 16; ++i) {
3629 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3630 if (ThisIsNonZero && First) {
3631 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003632 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003633 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003634 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003635 First = false;
3636 }
3637
3638 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00003639 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003640 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3641 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003642 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003643 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00003644 }
3645 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003646 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
3647 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
3648 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00003649 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003650 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00003651 } else
3652 ThisElt = LastElt;
3653
Gabor Greifba36cb52008-08-28 21:40:38 +00003654 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00003655 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00003656 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00003657 }
3658 }
3659
Owen Anderson825b72b2009-08-11 20:47:22 +00003660 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00003661}
3662
Bill Wendlinga348c562007-03-22 18:42:45 +00003663/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003664///
Dan Gohman475871a2008-07-27 21:46:04 +00003665static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00003666 unsigned NumNonZero, unsigned NumZero,
3667 SelectionDAG &DAG,
3668 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003669 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00003670 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003671
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003672 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003673 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003674 bool First = true;
3675 for (unsigned i = 0; i < 8; ++i) {
3676 bool isNonZero = (NonZeros & (1 << i)) != 0;
3677 if (isNonZero) {
3678 if (First) {
3679 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003680 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003681 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003682 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003683 First = false;
3684 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003685 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003686 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00003687 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00003688 }
3689 }
3690
3691 return V;
3692}
3693
Evan Chengf26ffe92008-05-29 08:22:04 +00003694/// getVShift - Return a vector logical shift node.
3695///
Owen Andersone50ed302009-08-10 22:56:29 +00003696static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00003697 unsigned NumBits, SelectionDAG &DAG,
3698 const TargetLowering &TLI, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003699 bool isMMX = VT.getSizeInBits() == 64;
Owen Anderson825b72b2009-08-11 20:47:22 +00003700 EVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00003701 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Dale Johannesenace16102009-02-03 19:33:06 +00003702 SrcOp = DAG.getNode(ISD::BIT_CONVERT, dl, ShVT, SrcOp);
3703 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3704 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00003705 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00003706}
3707
Dan Gohman475871a2008-07-27 21:46:04 +00003708SDValue
Evan Chengc3630942009-12-09 21:00:30 +00003709X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00003710 SelectionDAG &DAG) const {
Evan Chengc3630942009-12-09 21:00:30 +00003711
3712 // Check if the scalar load can be widened into a vector load. And if
3713 // the address is "base + cst" see if the cst can be "absorbed" into
3714 // the shuffle mask.
3715 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
3716 SDValue Ptr = LD->getBasePtr();
3717 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
3718 return SDValue();
3719 EVT PVT = LD->getValueType(0);
3720 if (PVT != MVT::i32 && PVT != MVT::f32)
3721 return SDValue();
3722
3723 int FI = -1;
3724 int64_t Offset = 0;
3725 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
3726 FI = FINode->getIndex();
3727 Offset = 0;
3728 } else if (Ptr.getOpcode() == ISD::ADD &&
3729 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
3730 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
3731 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
3732 Offset = Ptr.getConstantOperandVal(1);
3733 Ptr = Ptr.getOperand(0);
3734 } else {
3735 return SDValue();
3736 }
3737
3738 SDValue Chain = LD->getChain();
3739 // Make sure the stack object alignment is at least 16.
3740 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3741 if (DAG.InferPtrAlignment(Ptr) < 16) {
3742 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00003743 // Can't change the alignment. FIXME: It's possible to compute
3744 // the exact stack offset and reference FI + adjust offset instead.
3745 // If someone *really* cares about this. That's the way to implement it.
3746 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00003747 } else {
3748 MFI->setObjectAlignment(FI, 16);
3749 }
3750 }
3751
3752 // (Offset % 16) must be multiple of 4. Then address is then
3753 // Ptr + (Offset & ~15).
3754 if (Offset < 0)
3755 return SDValue();
3756 if ((Offset % 16) & 3)
3757 return SDValue();
3758 int64_t StartOffset = Offset & ~15;
3759 if (StartOffset)
3760 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
3761 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
3762
3763 int EltNo = (Offset - StartOffset) >> 2;
3764 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
3765 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
David Greene67c9d422010-02-15 16:53:33 +00003766 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,LD->getSrcValue(),0,
3767 false, false, 0);
Evan Chengc3630942009-12-09 21:00:30 +00003768 // Canonicalize it to a v4i32 shuffle.
3769 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32, V1);
3770 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3771 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
3772 DAG.getUNDEF(MVT::v4i32), &Mask[0]));
3773 }
3774
3775 return SDValue();
3776}
3777
Nate Begeman1449f292010-03-24 22:19:06 +00003778/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
3779/// vector of type 'VT', see if the elements can be replaced by a single large
3780/// load which has the same value as a build_vector whose operands are 'elts'.
3781///
3782/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
3783///
3784/// FIXME: we'd also like to handle the case where the last elements are zero
3785/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
3786/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00003787static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
3788 DebugLoc &dl, SelectionDAG &DAG) {
3789 EVT EltVT = VT.getVectorElementType();
3790 unsigned NumElems = Elts.size();
3791
Nate Begemanfdea31a2010-03-24 20:49:50 +00003792 LoadSDNode *LDBase = NULL;
3793 unsigned LastLoadedElt = -1U;
Nate Begeman1449f292010-03-24 22:19:06 +00003794
3795 // For each element in the initializer, see if we've found a load or an undef.
3796 // If we don't find an initial load element, or later load elements are
3797 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00003798 for (unsigned i = 0; i < NumElems; ++i) {
3799 SDValue Elt = Elts[i];
3800
3801 if (!Elt.getNode() ||
3802 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
3803 return SDValue();
3804 if (!LDBase) {
3805 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
3806 return SDValue();
3807 LDBase = cast<LoadSDNode>(Elt.getNode());
3808 LastLoadedElt = i;
3809 continue;
3810 }
3811 if (Elt.getOpcode() == ISD::UNDEF)
3812 continue;
3813
3814 LoadSDNode *LD = cast<LoadSDNode>(Elt);
3815 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
3816 return SDValue();
3817 LastLoadedElt = i;
3818 }
Nate Begeman1449f292010-03-24 22:19:06 +00003819
3820 // If we have found an entire vector of loads and undefs, then return a large
3821 // load of the entire vector width starting at the base pointer. If we found
3822 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00003823 if (LastLoadedElt == NumElems - 1) {
3824 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
3825 return DAG.getLoad(VT, dl, LDBase->getChain(), LDBase->getBasePtr(),
3826 LDBase->getSrcValue(), LDBase->getSrcValueOffset(),
3827 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
3828 return DAG.getLoad(VT, dl, LDBase->getChain(), LDBase->getBasePtr(),
3829 LDBase->getSrcValue(), LDBase->getSrcValueOffset(),
3830 LDBase->isVolatile(), LDBase->isNonTemporal(),
3831 LDBase->getAlignment());
3832 } else if (NumElems == 4 && LastLoadedElt == 1) {
3833 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
3834 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
3835 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2);
3836 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, ResNode);
3837 }
3838 return SDValue();
3839}
3840
Evan Chengc3630942009-12-09 21:00:30 +00003841SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00003842X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003843 DebugLoc dl = Op.getDebugLoc();
Chris Lattner8a594482007-11-25 00:24:49 +00003844 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif327ef032008-08-28 23:19:51 +00003845 if (ISD::isBuildVectorAllZeros(Op.getNode())
3846 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattner8a594482007-11-25 00:24:49 +00003847 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3848 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3849 // eliminated on x86-32 hosts.
Owen Anderson825b72b2009-08-11 20:47:22 +00003850 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
Chris Lattner8a594482007-11-25 00:24:49 +00003851 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003852
Gabor Greifba36cb52008-08-28 21:40:38 +00003853 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00003854 return getOnesVector(Op.getValueType(), DAG, dl);
3855 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00003856 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003857
Owen Andersone50ed302009-08-10 22:56:29 +00003858 EVT VT = Op.getValueType();
3859 EVT ExtVT = VT.getVectorElementType();
3860 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003861
3862 unsigned NumElems = Op.getNumOperands();
3863 unsigned NumZero = 0;
3864 unsigned NumNonZero = 0;
3865 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003866 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00003867 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003868 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003869 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00003870 if (Elt.getOpcode() == ISD::UNDEF)
3871 continue;
3872 Values.insert(Elt);
3873 if (Elt.getOpcode() != ISD::Constant &&
3874 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003875 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00003876 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00003877 NumZero++;
3878 else {
3879 NonZeros |= (1 << i);
3880 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003881 }
3882 }
3883
Dan Gohman7f321562007-06-25 16:23:39 +00003884 if (NumNonZero == 0) {
Chris Lattner8a594482007-11-25 00:24:49 +00003885 // All undef vector. Return an UNDEF. All zero vectors were handled above.
Dale Johannesene8d72302009-02-06 23:05:02 +00003886 return DAG.getUNDEF(VT);
Dan Gohman7f321562007-06-25 16:23:39 +00003887 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003888
Chris Lattner67f453a2008-03-09 05:42:06 +00003889 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00003890 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003891 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00003892 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00003893
Chris Lattner62098042008-03-09 01:05:04 +00003894 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3895 // the value are obviously zero, truncate the value to i32 and do the
3896 // insertion that way. Only do this if the value is non-constant or if the
3897 // value is a constant being inserted into element 0. It is cheaper to do
3898 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00003899 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00003900 (!IsAllConstants || Idx == 0)) {
3901 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3902 // Handle MMX and SSE both.
Owen Anderson825b72b2009-08-11 20:47:22 +00003903 EVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3904 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Scott Michelfdc40a02009-02-17 22:15:04 +00003905
Chris Lattner62098042008-03-09 01:05:04 +00003906 // Truncate the value (which may itself be a constant) to i32, and
3907 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00003908 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00003909 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00003910 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3911 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00003912
Chris Lattner62098042008-03-09 01:05:04 +00003913 // Now we have our 32-bit value zero extended in the low element of
3914 // a vector. If Idx != 0, swizzle it into place.
3915 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003916 SmallVector<int, 4> Mask;
3917 Mask.push_back(Idx);
3918 for (unsigned i = 1; i != VecElts; ++i)
3919 Mask.push_back(i);
3920 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00003921 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00003922 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003923 }
Dale Johannesenace16102009-02-03 19:33:06 +00003924 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00003925 }
3926 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003927
Chris Lattner19f79692008-03-08 22:59:52 +00003928 // If we have a constant or non-constant insertion into the low element of
3929 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3930 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00003931 // depending on what the source datatype is.
3932 if (Idx == 0) {
3933 if (NumZero == 0) {
3934 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00003935 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
3936 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00003937 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
3938 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
3939 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
3940 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00003941 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
3942 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
3943 EVT MiddleVT = VT.getSizeInBits() == 64 ? MVT::v2i32 : MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00003944 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
3945 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3946 Subtarget->hasSSE2(), DAG);
3947 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Item);
3948 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003949 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003950
3951 // Is it a vector logical left shift?
3952 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00003953 X86::isZeroNode(Op.getOperand(0)) &&
3954 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003955 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00003956 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00003957 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00003958 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00003959 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00003960 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003961
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003962 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00003963 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003964
Chris Lattner19f79692008-03-08 22:59:52 +00003965 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3966 // is a non-constant being inserted into an element other than the low one,
3967 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3968 // movd/movss) to move this into the low element, then shuffle it into
3969 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003970 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00003971 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00003972
Evan Cheng0db9fe62006-04-25 20:13:52 +00003973 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003974 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3975 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00003976 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003977 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00003978 MaskVec.push_back(i == Idx ? 0 : 1);
3979 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003980 }
3981 }
3982
Chris Lattner67f453a2008-03-09 05:42:06 +00003983 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00003984 if (Values.size() == 1) {
3985 if (EVTBits == 32) {
3986 // Instead of a shuffle like this:
3987 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
3988 // Check if it's possible to issue this instead.
3989 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
3990 unsigned Idx = CountTrailingZeros_32(NonZeros);
3991 SDValue Item = Op.getOperand(Idx);
3992 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
3993 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
3994 }
Dan Gohman475871a2008-07-27 21:46:04 +00003995 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00003996 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003997
Dan Gohmana3941172007-07-24 22:55:08 +00003998 // A vector full of immediates; various special cases are already
3999 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004000 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00004001 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00004002
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00004003 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004004 if (EVTBits == 64) {
4005 if (NumNonZero == 1) {
4006 // One half is zero or undef.
4007 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00004008 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00004009 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00004010 return getShuffleVectorZeroOrUndef(V2, Idx, true,
4011 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00004012 }
Dan Gohman475871a2008-07-27 21:46:04 +00004013 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00004014 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004015
4016 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00004017 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004018 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00004019 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004020 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004021 }
4022
Bill Wendling826f36f2007-03-28 00:57:11 +00004023 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00004024 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00004025 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004026 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004027 }
4028
4029 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004030 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00004031 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004032 if (NumElems == 4 && NumZero > 0) {
4033 for (unsigned i = 0; i < 4; ++i) {
4034 bool isZero = !(NonZeros & (1 << i));
4035 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00004036 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004037 else
Dale Johannesenace16102009-02-03 19:33:06 +00004038 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004039 }
4040
4041 for (unsigned i = 0; i < 2; ++i) {
4042 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
4043 default: break;
4044 case 0:
4045 V[i] = V[i*2]; // Must be a zero vector.
4046 break;
4047 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00004048 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004049 break;
4050 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00004051 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004052 break;
4053 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00004054 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004055 break;
4056 }
4057 }
4058
Nate Begeman9008ca62009-04-27 18:41:29 +00004059 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004060 bool Reverse = (NonZeros & 0x3) == 2;
4061 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004062 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004063 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
4064 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004065 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
4066 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004067 }
4068
Nate Begemanfdea31a2010-03-24 20:49:50 +00004069 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
4070 // Check for a build vector of consecutive loads.
4071 for (unsigned i = 0; i < NumElems; ++i)
4072 V[i] = Op.getOperand(i);
4073
4074 // Check for elements which are consecutive loads.
4075 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
4076 if (LD.getNode())
4077 return LD;
4078
4079 // For SSE 4.1, use inserts into undef.
4080 if (getSubtarget()->hasSSE41()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004081 V[0] = DAG.getUNDEF(VT);
4082 for (unsigned i = 0; i < NumElems; ++i)
4083 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
4084 V[0] = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, V[0],
4085 Op.getOperand(i), DAG.getIntPtrConstant(i));
4086 return V[0];
4087 }
Nate Begemanfdea31a2010-03-24 20:49:50 +00004088
4089 // Otherwise, expand into a number of unpckl*
Evan Cheng0db9fe62006-04-25 20:13:52 +00004090 // e.g. for v4f32
4091 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
4092 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
4093 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Evan Cheng0db9fe62006-04-25 20:13:52 +00004094 for (unsigned i = 0; i < NumElems; ++i)
Dale Johannesenace16102009-02-03 19:33:06 +00004095 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004096 NumElems >>= 1;
4097 while (NumElems != 0) {
4098 for (unsigned i = 0; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004099 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + NumElems]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004100 NumElems >>= 1;
4101 }
4102 return V[0];
4103 }
Dan Gohman475871a2008-07-27 21:46:04 +00004104 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004105}
4106
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004107SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004108X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004109 // We support concatenate two MMX registers and place them in a MMX
4110 // register. This is better than doing a stack convert.
4111 DebugLoc dl = Op.getDebugLoc();
4112 EVT ResVT = Op.getValueType();
4113 assert(Op.getNumOperands() == 2);
4114 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
4115 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
4116 int Mask[2];
4117 SDValue InVec = DAG.getNode(ISD::BIT_CONVERT,dl, MVT::v1i64, Op.getOperand(0));
4118 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4119 InVec = Op.getOperand(1);
4120 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4121 unsigned NumElts = ResVT.getVectorNumElements();
4122 VecOp = DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
4123 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
4124 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
4125 } else {
4126 InVec = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v1i64, InVec);
4127 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4128 Mask[0] = 0; Mask[1] = 2;
4129 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
4130 }
4131 return DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
4132}
4133
Nate Begemanb9a47b82009-02-23 08:49:38 +00004134// v8i16 shuffles - Prefer shuffles in the following order:
4135// 1. [all] pshuflw, pshufhw, optional move
4136// 2. [ssse3] 1 x pshufb
4137// 3. [ssse3] 2 x pshufb + 1 x por
4138// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004139static
Nate Begeman9008ca62009-04-27 18:41:29 +00004140SDValue LowerVECTOR_SHUFFLEv8i16(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00004141 SelectionDAG &DAG,
4142 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004143 SDValue V1 = SVOp->getOperand(0);
4144 SDValue V2 = SVOp->getOperand(1);
4145 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004146 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00004147
Nate Begemanb9a47b82009-02-23 08:49:38 +00004148 // Determine if more than 1 of the words in each of the low and high quadwords
4149 // of the result come from the same quadword of one of the two inputs. Undef
4150 // mask values count as coming from any quadword, for better codegen.
4151 SmallVector<unsigned, 4> LoQuad(4);
4152 SmallVector<unsigned, 4> HiQuad(4);
4153 BitVector InputQuads(4);
4154 for (unsigned i = 0; i < 8; ++i) {
4155 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00004156 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004157 MaskVals.push_back(EltIdx);
4158 if (EltIdx < 0) {
4159 ++Quad[0];
4160 ++Quad[1];
4161 ++Quad[2];
4162 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00004163 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004164 }
4165 ++Quad[EltIdx / 4];
4166 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00004167 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004168
Nate Begemanb9a47b82009-02-23 08:49:38 +00004169 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004170 unsigned MaxQuad = 1;
4171 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004172 if (LoQuad[i] > MaxQuad) {
4173 BestLoQuad = i;
4174 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004175 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004176 }
4177
Nate Begemanb9a47b82009-02-23 08:49:38 +00004178 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004179 MaxQuad = 1;
4180 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004181 if (HiQuad[i] > MaxQuad) {
4182 BestHiQuad = i;
4183 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004184 }
4185 }
4186
Nate Begemanb9a47b82009-02-23 08:49:38 +00004187 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00004188 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00004189 // single pshufb instruction is necessary. If There are more than 2 input
4190 // quads, disable the next transformation since it does not help SSSE3.
4191 bool V1Used = InputQuads[0] || InputQuads[1];
4192 bool V2Used = InputQuads[2] || InputQuads[3];
4193 if (TLI.getSubtarget()->hasSSSE3()) {
4194 if (InputQuads.count() == 2 && V1Used && V2Used) {
4195 BestLoQuad = InputQuads.find_first();
4196 BestHiQuad = InputQuads.find_next(BestLoQuad);
4197 }
4198 if (InputQuads.count() > 2) {
4199 BestLoQuad = -1;
4200 BestHiQuad = -1;
4201 }
4202 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004203
Nate Begemanb9a47b82009-02-23 08:49:38 +00004204 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
4205 // the shuffle mask. If a quad is scored as -1, that means that it contains
4206 // words from all 4 input quadwords.
4207 SDValue NewV;
4208 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004209 SmallVector<int, 8> MaskV;
4210 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
4211 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00004212 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004213 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V1),
4214 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V2), &MaskV[0]);
4215 NewV = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004216
Nate Begemanb9a47b82009-02-23 08:49:38 +00004217 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
4218 // source words for the shuffle, to aid later transformations.
4219 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00004220 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00004221 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004222 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00004223 if (idx != (int)i)
4224 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004225 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00004226 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004227 AllWordsInNewV = false;
4228 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00004229 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004230
Nate Begemanb9a47b82009-02-23 08:49:38 +00004231 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
4232 if (AllWordsInNewV) {
4233 for (int i = 0; i != 8; ++i) {
4234 int idx = MaskVals[i];
4235 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004236 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004237 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004238 if ((idx != i) && idx < 4)
4239 pshufhw = false;
4240 if ((idx != i) && idx > 3)
4241 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00004242 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00004243 V1 = NewV;
4244 V2Used = false;
4245 BestLoQuad = 0;
4246 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004247 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004248
Nate Begemanb9a47b82009-02-23 08:49:38 +00004249 // If we've eliminated the use of V2, and the new mask is a pshuflw or
4250 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00004251 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Eric Christopherfd179292009-08-27 18:07:15 +00004252 return DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00004253 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Evan Cheng14b32e12007-12-11 01:46:18 +00004254 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004255 }
Eric Christopherfd179292009-08-27 18:07:15 +00004256
Nate Begemanb9a47b82009-02-23 08:49:38 +00004257 // If we have SSSE3, and all words of the result are from 1 input vector,
4258 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
4259 // is present, fall back to case 4.
4260 if (TLI.getSubtarget()->hasSSSE3()) {
4261 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004262
Nate Begemanb9a47b82009-02-23 08:49:38 +00004263 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00004264 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00004265 // mask, and elements that come from V1 in the V2 mask, so that the two
4266 // results can be OR'd together.
4267 bool TwoInputs = V1Used && V2Used;
4268 for (unsigned i = 0; i != 8; ++i) {
4269 int EltIdx = MaskVals[i] * 2;
4270 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004271 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4272 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004273 continue;
4274 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004275 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
4276 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004277 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004278 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004279 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004280 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004281 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004282 if (!TwoInputs)
Owen Anderson825b72b2009-08-11 20:47:22 +00004283 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004284
Nate Begemanb9a47b82009-02-23 08:49:38 +00004285 // Calculate the shuffle mask for the second input, shuffle it, and
4286 // OR it with the first shuffled input.
4287 pshufbMask.clear();
4288 for (unsigned i = 0; i != 8; ++i) {
4289 int EltIdx = MaskVals[i] * 2;
4290 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004291 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4292 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004293 continue;
4294 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004295 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
4296 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004297 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004298 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00004299 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004300 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004301 MVT::v16i8, &pshufbMask[0], 16));
4302 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
4303 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004304 }
4305
4306 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
4307 // and update MaskVals with new element order.
4308 BitVector InOrder(8);
4309 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004310 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004311 for (int i = 0; i != 4; ++i) {
4312 int idx = MaskVals[i];
4313 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004314 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004315 InOrder.set(i);
4316 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004317 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004318 InOrder.set(i);
4319 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004320 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004321 }
4322 }
4323 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004324 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00004325 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004326 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004327 }
Eric Christopherfd179292009-08-27 18:07:15 +00004328
Nate Begemanb9a47b82009-02-23 08:49:38 +00004329 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
4330 // and update MaskVals with the new element order.
4331 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004332 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004333 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004334 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004335 for (unsigned i = 4; i != 8; ++i) {
4336 int idx = MaskVals[i];
4337 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004338 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004339 InOrder.set(i);
4340 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004341 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004342 InOrder.set(i);
4343 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004344 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004345 }
4346 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004347 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004348 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004349 }
Eric Christopherfd179292009-08-27 18:07:15 +00004350
Nate Begemanb9a47b82009-02-23 08:49:38 +00004351 // In case BestHi & BestLo were both -1, which means each quadword has a word
4352 // from each of the four input quadwords, calculate the InOrder bitvector now
4353 // before falling through to the insert/extract cleanup.
4354 if (BestLoQuad == -1 && BestHiQuad == -1) {
4355 NewV = V1;
4356 for (int i = 0; i != 8; ++i)
4357 if (MaskVals[i] < 0 || MaskVals[i] == i)
4358 InOrder.set(i);
4359 }
Eric Christopherfd179292009-08-27 18:07:15 +00004360
Nate Begemanb9a47b82009-02-23 08:49:38 +00004361 // The other elements are put in the right place using pextrw and pinsrw.
4362 for (unsigned i = 0; i != 8; ++i) {
4363 if (InOrder[i])
4364 continue;
4365 int EltIdx = MaskVals[i];
4366 if (EltIdx < 0)
4367 continue;
4368 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004369 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004370 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00004371 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004372 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004373 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004374 DAG.getIntPtrConstant(i));
4375 }
4376 return NewV;
4377}
4378
4379// v16i8 shuffles - Prefer shuffles in the following order:
4380// 1. [ssse3] 1 x pshufb
4381// 2. [ssse3] 2 x pshufb + 1 x por
4382// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
4383static
Nate Begeman9008ca62009-04-27 18:41:29 +00004384SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00004385 SelectionDAG &DAG,
4386 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004387 SDValue V1 = SVOp->getOperand(0);
4388 SDValue V2 = SVOp->getOperand(1);
4389 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004390 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00004391 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00004392
Nate Begemanb9a47b82009-02-23 08:49:38 +00004393 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00004394 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00004395 // present, fall back to case 3.
4396 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
4397 bool V1Only = true;
4398 bool V2Only = true;
4399 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004400 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00004401 if (EltIdx < 0)
4402 continue;
4403 if (EltIdx < 16)
4404 V2Only = false;
4405 else
4406 V1Only = false;
4407 }
Eric Christopherfd179292009-08-27 18:07:15 +00004408
Nate Begemanb9a47b82009-02-23 08:49:38 +00004409 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
4410 if (TLI.getSubtarget()->hasSSSE3()) {
4411 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004412
Nate Begemanb9a47b82009-02-23 08:49:38 +00004413 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00004414 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004415 //
4416 // Otherwise, we have elements from both input vectors, and must zero out
4417 // elements that come from V2 in the first mask, and V1 in the second mask
4418 // so that we can OR them together.
4419 bool TwoInputs = !(V1Only || V2Only);
4420 for (unsigned i = 0; i != 16; ++i) {
4421 int EltIdx = MaskVals[i];
4422 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004423 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004424 continue;
4425 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004426 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004427 }
4428 // If all the elements are from V2, assign it to V1 and return after
4429 // building the first pshufb.
4430 if (V2Only)
4431 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00004432 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004433 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004434 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004435 if (!TwoInputs)
4436 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00004437
Nate Begemanb9a47b82009-02-23 08:49:38 +00004438 // Calculate the shuffle mask for the second input, shuffle it, and
4439 // OR it with the first shuffled input.
4440 pshufbMask.clear();
4441 for (unsigned i = 0; i != 16; ++i) {
4442 int EltIdx = MaskVals[i];
4443 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004444 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004445 continue;
4446 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004447 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004448 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004449 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004450 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004451 MVT::v16i8, &pshufbMask[0], 16));
4452 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004453 }
Eric Christopherfd179292009-08-27 18:07:15 +00004454
Nate Begemanb9a47b82009-02-23 08:49:38 +00004455 // No SSSE3 - Calculate in place words and then fix all out of place words
4456 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
4457 // the 16 different words that comprise the two doublequadword input vectors.
Owen Anderson825b72b2009-08-11 20:47:22 +00004458 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
4459 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004460 SDValue NewV = V2Only ? V2 : V1;
4461 for (int i = 0; i != 8; ++i) {
4462 int Elt0 = MaskVals[i*2];
4463 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00004464
Nate Begemanb9a47b82009-02-23 08:49:38 +00004465 // This word of the result is all undef, skip it.
4466 if (Elt0 < 0 && Elt1 < 0)
4467 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004468
Nate Begemanb9a47b82009-02-23 08:49:38 +00004469 // This word of the result is already in the correct place, skip it.
4470 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
4471 continue;
4472 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
4473 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004474
Nate Begemanb9a47b82009-02-23 08:49:38 +00004475 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
4476 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
4477 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00004478
4479 // If Elt0 and Elt1 are defined, are consecutive, and can be load
4480 // using a single extract together, load it and store it.
4481 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004482 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004483 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00004484 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004485 DAG.getIntPtrConstant(i));
4486 continue;
4487 }
4488
Nate Begemanb9a47b82009-02-23 08:49:38 +00004489 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00004490 // source byte is not also odd, shift the extracted word left 8 bits
4491 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004492 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004493 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004494 DAG.getIntPtrConstant(Elt1 / 2));
4495 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004496 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004497 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004498 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004499 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
4500 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004501 }
4502 // If Elt0 is defined, extract it from the appropriate source. If the
4503 // source byte is not also even, shift the extracted word right 8 bits. If
4504 // Elt1 was also defined, OR the extracted values together before
4505 // inserting them in the result.
4506 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004507 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004508 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
4509 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004510 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004511 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004512 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004513 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
4514 DAG.getConstant(0x00FF, MVT::i16));
4515 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00004516 : InsElt0;
4517 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004518 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004519 DAG.getIntPtrConstant(i));
4520 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004521 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004522}
4523
Evan Cheng7a831ce2007-12-15 03:00:47 +00004524/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Chris Lattnerf172ecd2010-07-04 23:07:25 +00004525/// ones, or rewriting v4i32 / v2i32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00004526/// done when every pair / quad of shuffle mask elements point to elements in
4527/// the right sequence. e.g.
Evan Cheng14b32e12007-12-11 01:46:18 +00004528/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
4529static
Nate Begeman9008ca62009-04-27 18:41:29 +00004530SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
4531 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00004532 const TargetLowering &TLI, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00004533 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00004534 SDValue V1 = SVOp->getOperand(0);
4535 SDValue V2 = SVOp->getOperand(1);
4536 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00004537 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Owen Anderson825b72b2009-08-11 20:47:22 +00004538 EVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Owen Andersone50ed302009-08-10 22:56:29 +00004539 EVT NewVT = MaskVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00004540 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004541 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004542 case MVT::v4f32: NewVT = MVT::v2f64; break;
4543 case MVT::v4i32: NewVT = MVT::v2i64; break;
4544 case MVT::v8i16: NewVT = MVT::v4i32; break;
4545 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004546 }
4547
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004548 if (NewWidth == 2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004549 if (VT.isInteger())
Owen Anderson825b72b2009-08-11 20:47:22 +00004550 NewVT = MVT::v2i64;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004551 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004552 NewVT = MVT::v2f64;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004553 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004554 int Scale = NumElems / NewWidth;
4555 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00004556 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004557 int StartIdx = -1;
4558 for (int j = 0; j < Scale; ++j) {
4559 int EltIdx = SVOp->getMaskElt(i+j);
4560 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004561 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00004562 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00004563 StartIdx = EltIdx - (EltIdx % Scale);
4564 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00004565 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004566 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004567 if (StartIdx == -1)
4568 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00004569 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004570 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004571 }
4572
Dale Johannesenace16102009-02-03 19:33:06 +00004573 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V1);
4574 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00004575 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004576}
4577
Evan Chengd880b972008-05-09 21:53:03 +00004578/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004579///
Owen Andersone50ed302009-08-10 22:56:29 +00004580static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00004581 SDValue SrcOp, SelectionDAG &DAG,
4582 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004583 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004584 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00004585 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00004586 LD = dyn_cast<LoadSDNode>(SrcOp);
4587 if (!LD) {
4588 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
4589 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00004590 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
4591 if ((ExtVT.SimpleTy != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00004592 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4593 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00004594 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004595 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00004596 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Dale Johannesenace16102009-02-03 19:33:06 +00004597 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4598 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
4599 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
4600 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00004601 SrcOp.getOperand(0)
4602 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004603 }
4604 }
4605 }
4606
Dale Johannesenace16102009-02-03 19:33:06 +00004607 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4608 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004609 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00004610 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004611}
4612
Evan Chengace3c172008-07-22 21:13:36 +00004613/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
4614/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004615static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00004616LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
4617 SDValue V1 = SVOp->getOperand(0);
4618 SDValue V2 = SVOp->getOperand(1);
4619 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004620 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00004621
Evan Chengace3c172008-07-22 21:13:36 +00004622 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00004623 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00004624 SmallVector<int, 8> Mask1(4U, -1);
4625 SmallVector<int, 8> PermMask;
4626 SVOp->getMask(PermMask);
4627
Evan Chengace3c172008-07-22 21:13:36 +00004628 unsigned NumHi = 0;
4629 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00004630 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004631 int Idx = PermMask[i];
4632 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004633 Locs[i] = std::make_pair(-1, -1);
4634 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004635 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
4636 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004637 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00004638 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004639 NumLo++;
4640 } else {
4641 Locs[i] = std::make_pair(1, NumHi);
4642 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004643 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004644 NumHi++;
4645 }
4646 }
4647 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004648
Evan Chengace3c172008-07-22 21:13:36 +00004649 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004650 // If no more than two elements come from either vector. This can be
4651 // implemented with two shuffles. First shuffle gather the elements.
4652 // The second shuffle, which takes the first shuffle as both of its
4653 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00004654 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004655
Nate Begeman9008ca62009-04-27 18:41:29 +00004656 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00004657
Evan Chengace3c172008-07-22 21:13:36 +00004658 for (unsigned i = 0; i != 4; ++i) {
4659 if (Locs[i].first == -1)
4660 continue;
4661 else {
4662 unsigned Idx = (i < 2) ? 0 : 4;
4663 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004664 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004665 }
4666 }
4667
Nate Begeman9008ca62009-04-27 18:41:29 +00004668 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004669 } else if (NumLo == 3 || NumHi == 3) {
4670 // Otherwise, we must have three elements from one vector, call it X, and
4671 // one element from the other, call it Y. First, use a shufps to build an
4672 // intermediate vector with the one element from Y and the element from X
4673 // that will be in the same half in the final destination (the indexes don't
4674 // matter). Then, use a shufps to build the final vector, taking the half
4675 // containing the element from Y from the intermediate, and the other half
4676 // from X.
4677 if (NumHi == 3) {
4678 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00004679 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004680 std::swap(V1, V2);
4681 }
4682
4683 // Find the element from V2.
4684 unsigned HiIndex;
4685 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004686 int Val = PermMask[HiIndex];
4687 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004688 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004689 if (Val >= 4)
4690 break;
4691 }
4692
Nate Begeman9008ca62009-04-27 18:41:29 +00004693 Mask1[0] = PermMask[HiIndex];
4694 Mask1[1] = -1;
4695 Mask1[2] = PermMask[HiIndex^1];
4696 Mask1[3] = -1;
4697 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004698
4699 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004700 Mask1[0] = PermMask[0];
4701 Mask1[1] = PermMask[1];
4702 Mask1[2] = HiIndex & 1 ? 6 : 4;
4703 Mask1[3] = HiIndex & 1 ? 4 : 6;
4704 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004705 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004706 Mask1[0] = HiIndex & 1 ? 2 : 0;
4707 Mask1[1] = HiIndex & 1 ? 0 : 2;
4708 Mask1[2] = PermMask[2];
4709 Mask1[3] = PermMask[3];
4710 if (Mask1[2] >= 0)
4711 Mask1[2] += 4;
4712 if (Mask1[3] >= 0)
4713 Mask1[3] += 4;
4714 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004715 }
Evan Chengace3c172008-07-22 21:13:36 +00004716 }
4717
4718 // Break it into (shuffle shuffle_hi, shuffle_lo).
4719 Locs.clear();
Nate Begeman9008ca62009-04-27 18:41:29 +00004720 SmallVector<int,8> LoMask(4U, -1);
4721 SmallVector<int,8> HiMask(4U, -1);
4722
4723 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00004724 unsigned MaskIdx = 0;
4725 unsigned LoIdx = 0;
4726 unsigned HiIdx = 2;
4727 for (unsigned i = 0; i != 4; ++i) {
4728 if (i == 2) {
4729 MaskPtr = &HiMask;
4730 MaskIdx = 1;
4731 LoIdx = 0;
4732 HiIdx = 2;
4733 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004734 int Idx = PermMask[i];
4735 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004736 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00004737 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004738 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004739 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004740 LoIdx++;
4741 } else {
4742 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004743 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004744 HiIdx++;
4745 }
4746 }
4747
Nate Begeman9008ca62009-04-27 18:41:29 +00004748 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
4749 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
4750 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00004751 for (unsigned i = 0; i != 4; ++i) {
4752 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004753 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00004754 } else {
4755 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004756 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00004757 }
4758 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004759 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00004760}
4761
Dan Gohman475871a2008-07-27 21:46:04 +00004762SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004763X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00004764 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00004765 SDValue V1 = Op.getOperand(0);
4766 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00004767 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004768 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00004769 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004770 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004771 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
4772 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00004773 bool V1IsSplat = false;
4774 bool V2IsSplat = false;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004775
Nate Begeman9008ca62009-04-27 18:41:29 +00004776 if (isZeroShuffle(SVOp))
Dale Johannesenace16102009-02-03 19:33:06 +00004777 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004778
Nate Begeman9008ca62009-04-27 18:41:29 +00004779 // Promote splats to v4f32.
4780 if (SVOp->isSplat()) {
Eric Christopherfd179292009-08-27 18:07:15 +00004781 if (isMMX || NumElems < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004782 return Op;
4783 return PromoteSplat(SVOp, DAG, Subtarget->hasSSE2());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004784 }
4785
Evan Cheng7a831ce2007-12-15 03:00:47 +00004786 // If the shuffle can be profitably rewritten as a narrower shuffle, then
4787 // do it!
Owen Anderson825b72b2009-08-11 20:47:22 +00004788 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004789 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004790 if (NewOp.getNode())
Scott Michelfdc40a02009-02-17 22:15:04 +00004791 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004792 LowerVECTOR_SHUFFLE(NewOp, DAG));
Owen Anderson825b72b2009-08-11 20:47:22 +00004793 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Evan Cheng7a831ce2007-12-15 03:00:47 +00004794 // FIXME: Figure out a cleaner way to do this.
4795 // Try to make use of movq to zero out the top part.
Gabor Greifba36cb52008-08-28 21:40:38 +00004796 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004797 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004798 if (NewOp.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004799 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
4800 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
4801 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004802 }
Gabor Greifba36cb52008-08-28 21:40:38 +00004803 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004804 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
4805 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
Evan Chengd880b972008-05-09 21:53:03 +00004806 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Nate Begeman9008ca62009-04-27 18:41:29 +00004807 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004808 }
4809 }
Eric Christopherfd179292009-08-27 18:07:15 +00004810
Nate Begeman9008ca62009-04-27 18:41:29 +00004811 if (X86::isPSHUFDMask(SVOp))
4812 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004813
Evan Chengf26ffe92008-05-29 08:22:04 +00004814 // Check if this can be converted into a logical shift.
4815 bool isLeft = false;
4816 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00004817 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00004818 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00004819 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00004820 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004821 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00004822 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00004823 EVT EltVT = VT.getVectorElementType();
4824 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004825 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004826 }
Eric Christopherfd179292009-08-27 18:07:15 +00004827
Nate Begeman9008ca62009-04-27 18:41:29 +00004828 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004829 if (V1IsUndef)
4830 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00004831 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004832 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Nate Begemanfb8ead02008-07-25 19:05:58 +00004833 if (!isMMX)
4834 return Op;
Evan Cheng7e2ff772008-05-08 00:57:18 +00004835 }
Eric Christopherfd179292009-08-27 18:07:15 +00004836
Nate Begeman9008ca62009-04-27 18:41:29 +00004837 // FIXME: fold these into legal mask.
4838 if (!isMMX && (X86::isMOVSHDUPMask(SVOp) ||
4839 X86::isMOVSLDUPMask(SVOp) ||
4840 X86::isMOVHLPSMask(SVOp) ||
Nate Begeman0b10b912009-11-07 23:17:15 +00004841 X86::isMOVLHPSMask(SVOp) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00004842 X86::isMOVLPMask(SVOp)))
Evan Cheng9bbbb982006-10-25 20:48:19 +00004843 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004844
Nate Begeman9008ca62009-04-27 18:41:29 +00004845 if (ShouldXformToMOVHLPS(SVOp) ||
4846 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
4847 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004848
Evan Chengf26ffe92008-05-29 08:22:04 +00004849 if (isShift) {
4850 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00004851 EVT EltVT = VT.getVectorElementType();
4852 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004853 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004854 }
Eric Christopherfd179292009-08-27 18:07:15 +00004855
Evan Cheng9eca5e82006-10-25 21:49:50 +00004856 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00004857 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4858 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00004859 V1IsSplat = isSplatVector(V1.getNode());
4860 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00004861
Chris Lattner8a594482007-11-25 00:24:49 +00004862 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00004863 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004864 Op = CommuteVectorShuffle(SVOp, DAG);
4865 SVOp = cast<ShuffleVectorSDNode>(Op);
4866 V1 = SVOp->getOperand(0);
4867 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00004868 std::swap(V1IsSplat, V2IsSplat);
4869 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00004870 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00004871 }
4872
Nate Begeman9008ca62009-04-27 18:41:29 +00004873 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
4874 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00004875 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00004876 return V1;
4877 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
4878 // the instruction selector will not match, so get a canonical MOVL with
4879 // swapped operands to undo the commute.
4880 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00004881 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004882
Nate Begeman9008ca62009-04-27 18:41:29 +00004883 if (X86::isUNPCKL_v_undef_Mask(SVOp) ||
4884 X86::isUNPCKH_v_undef_Mask(SVOp) ||
4885 X86::isUNPCKLMask(SVOp) ||
4886 X86::isUNPCKHMask(SVOp))
Evan Chengd9b8e402006-10-16 06:36:00 +00004887 return Op;
Evan Chenge1113032006-10-04 18:33:38 +00004888
Evan Cheng9bbbb982006-10-25 20:48:19 +00004889 if (V2IsSplat) {
4890 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004891 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00004892 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00004893 SDValue NewMask = NormalizeMask(SVOp, DAG);
4894 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
4895 if (NSVOp != SVOp) {
4896 if (X86::isUNPCKLMask(NSVOp, true)) {
4897 return NewMask;
4898 } else if (X86::isUNPCKHMask(NSVOp, true)) {
4899 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004900 }
4901 }
4902 }
4903
Evan Cheng9eca5e82006-10-25 21:49:50 +00004904 if (Commuted) {
4905 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00004906 // FIXME: this seems wrong.
4907 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
4908 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
4909 if (X86::isUNPCKL_v_undef_Mask(NewSVOp) ||
4910 X86::isUNPCKH_v_undef_Mask(NewSVOp) ||
4911 X86::isUNPCKLMask(NewSVOp) ||
4912 X86::isUNPCKHMask(NewSVOp))
4913 return NewOp;
Evan Cheng9eca5e82006-10-25 21:49:50 +00004914 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004915
Nate Begemanb9a47b82009-02-23 08:49:38 +00004916 // FIXME: for mmx, bitcast v2i32 to v4i16 for shuffle.
Nate Begeman9008ca62009-04-27 18:41:29 +00004917
4918 // Normalize the node to match x86 shuffle ops if needed
4919 if (!isMMX && V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
4920 return CommuteVectorShuffle(SVOp, DAG);
4921
4922 // Check for legal shuffle and return?
4923 SmallVector<int, 16> PermMask;
4924 SVOp->getMask(PermMask);
4925 if (isShuffleMaskLegal(PermMask, VT))
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004926 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004927
Evan Cheng14b32e12007-12-11 01:46:18 +00004928 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00004929 if (VT == MVT::v8i16) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004930 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(SVOp, DAG, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004931 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00004932 return NewOp;
4933 }
4934
Owen Anderson825b72b2009-08-11 20:47:22 +00004935 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004936 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004937 if (NewOp.getNode())
4938 return NewOp;
4939 }
Eric Christopherfd179292009-08-27 18:07:15 +00004940
Evan Chengace3c172008-07-22 21:13:36 +00004941 // Handle all 4 wide cases with a number of shuffles except for MMX.
4942 if (NumElems == 4 && !isMMX)
Nate Begeman9008ca62009-04-27 18:41:29 +00004943 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004944
Dan Gohman475871a2008-07-27 21:46:04 +00004945 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004946}
4947
Dan Gohman475871a2008-07-27 21:46:04 +00004948SDValue
4949X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00004950 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00004951 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004952 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004953 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004954 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004955 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004956 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004957 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004958 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004959 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00004960 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
4961 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
4962 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004963 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4964 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004965 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004966 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00004967 Op.getOperand(0)),
4968 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00004969 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004970 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004971 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004972 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004973 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00004974 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00004975 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4976 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004977 // result has a single use which is a store or a bitcast to i32. And in
4978 // the case of a store, it's not worth it if the index is a constant 0,
4979 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00004980 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00004981 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004982 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004983 if ((User->getOpcode() != ISD::STORE ||
4984 (isa<ConstantSDNode>(Op.getOperand(1)) &&
4985 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman171c11e2008-04-16 02:32:24 +00004986 (User->getOpcode() != ISD::BIT_CONVERT ||
Owen Anderson825b72b2009-08-11 20:47:22 +00004987 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00004988 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00004989 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
4990 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004991 Op.getOperand(0)),
4992 Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004993 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Extract);
4994 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00004995 // ExtractPS works with constant index.
4996 if (isa<ConstantSDNode>(Op.getOperand(1)))
4997 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004998 }
Dan Gohman475871a2008-07-27 21:46:04 +00004999 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005000}
5001
5002
Dan Gohman475871a2008-07-27 21:46:04 +00005003SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005004X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
5005 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005006 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00005007 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005008
Evan Cheng62a3f152008-03-24 21:52:23 +00005009 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005010 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005011 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00005012 return Res;
5013 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00005014
Owen Andersone50ed302009-08-10 22:56:29 +00005015 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005016 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005017 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005018 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005019 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005020 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005021 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005022 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
5023 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Scott Michelfdc40a02009-02-17 22:15:04 +00005024 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005025 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00005026 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005027 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00005028 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00005029 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00005030 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00005031 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00005032 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005033 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005034 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005035 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005036 if (Idx == 0)
5037 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00005038
Evan Cheng0db9fe62006-04-25 20:13:52 +00005039 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00005040 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00005041 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00005042 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00005043 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00005044 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00005045 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00005046 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00005047 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
5048 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
5049 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005050 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005051 if (Idx == 0)
5052 return Op;
5053
5054 // UNPCKHPD the element to the lowest double word, then movsd.
5055 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
5056 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00005057 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00005058 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00005059 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00005060 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00005061 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00005062 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005063 }
5064
Dan Gohman475871a2008-07-27 21:46:04 +00005065 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005066}
5067
Dan Gohman475871a2008-07-27 21:46:04 +00005068SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005069X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
5070 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005071 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00005072 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005073 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005074
Dan Gohman475871a2008-07-27 21:46:04 +00005075 SDValue N0 = Op.getOperand(0);
5076 SDValue N1 = Op.getOperand(1);
5077 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00005078
Dan Gohman8a55ce42009-09-23 21:02:20 +00005079 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00005080 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00005081 unsigned Opc;
5082 if (VT == MVT::v8i16)
5083 Opc = X86ISD::PINSRW;
5084 else if (VT == MVT::v4i16)
5085 Opc = X86ISD::MMX_PINSRW;
5086 else if (VT == MVT::v16i8)
5087 Opc = X86ISD::PINSRB;
5088 else
5089 Opc = X86ISD::PINSRB;
5090
Nate Begeman14d12ca2008-02-11 04:19:36 +00005091 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
5092 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00005093 if (N1.getValueType() != MVT::i32)
5094 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
5095 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005096 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00005097 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00005098 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00005099 // Bits [7:6] of the constant are the source select. This will always be
5100 // zero here. The DAG Combiner may combine an extract_elt index into these
5101 // bits. For example (insert (extract, 3), 2) could be matched by putting
5102 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00005103 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00005104 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00005105 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00005106 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005107 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00005108 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00005109 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00005110 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00005111 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00005112 // PINSR* works with constant index.
5113 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00005114 }
Dan Gohman475871a2008-07-27 21:46:04 +00005115 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005116}
5117
Dan Gohman475871a2008-07-27 21:46:04 +00005118SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005119X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005120 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00005121 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005122
5123 if (Subtarget->hasSSE41())
5124 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
5125
Dan Gohman8a55ce42009-09-23 21:02:20 +00005126 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00005127 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00005128
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005129 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005130 SDValue N0 = Op.getOperand(0);
5131 SDValue N1 = Op.getOperand(1);
5132 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00005133
Dan Gohman8a55ce42009-09-23 21:02:20 +00005134 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00005135 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
5136 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00005137 if (N1.getValueType() != MVT::i32)
5138 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
5139 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005140 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00005141 return DAG.getNode(VT == MVT::v8i16 ? X86ISD::PINSRW : X86ISD::MMX_PINSRW,
5142 dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005143 }
Dan Gohman475871a2008-07-27 21:46:04 +00005144 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005145}
5146
Dan Gohman475871a2008-07-27 21:46:04 +00005147SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005148X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005149 DebugLoc dl = Op.getDebugLoc();
Chris Lattnerf172ecd2010-07-04 23:07:25 +00005150
5151 if (Op.getValueType() == MVT::v1i64 &&
5152 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00005153 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00005154
Owen Anderson825b72b2009-08-11 20:47:22 +00005155 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
5156 EVT VT = MVT::v2i32;
5157 switch (Op.getValueType().getSimpleVT().SimpleTy) {
Evan Chengefec7512008-02-18 23:04:32 +00005158 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00005159 case MVT::v16i8:
5160 case MVT::v8i16:
5161 VT = MVT::v4i32;
Evan Chengefec7512008-02-18 23:04:32 +00005162 break;
5163 }
Dale Johannesenace16102009-02-03 19:33:06 +00005164 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(),
5165 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005166}
5167
Bill Wendling056292f2008-09-16 21:48:12 +00005168// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
5169// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
5170// one of the above mentioned nodes. It has to be wrapped because otherwise
5171// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
5172// be used to form addressing mode. These wrapped nodes will be selected
5173// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00005174SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005175X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005176 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00005177
Chris Lattner41621a22009-06-26 19:22:52 +00005178 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5179 // global base reg.
5180 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005181 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005182 CodeModel::Model M = getTargetMachine().getCodeModel();
5183
Chris Lattner4f066492009-07-11 20:29:19 +00005184 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005185 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005186 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005187 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005188 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005189 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005190 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005191
Evan Cheng1606e8e2009-03-13 07:51:59 +00005192 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00005193 CP->getAlignment(),
5194 CP->getOffset(), OpFlag);
5195 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00005196 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005197 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00005198 if (OpFlag) {
5199 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005200 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005201 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005202 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005203 }
5204
5205 return Result;
5206}
5207
Dan Gohmand858e902010-04-17 15:26:15 +00005208SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00005209 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00005210
Chris Lattner18c59872009-06-27 04:16:01 +00005211 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5212 // global base reg.
5213 unsigned char OpFlag = 0;
5214 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005215 CodeModel::Model M = getTargetMachine().getCodeModel();
5216
Chris Lattner4f066492009-07-11 20:29:19 +00005217 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005218 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005219 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005220 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005221 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005222 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005223 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005224
Chris Lattner18c59872009-06-27 04:16:01 +00005225 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
5226 OpFlag);
5227 DebugLoc DL = JT->getDebugLoc();
5228 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005229
Chris Lattner18c59872009-06-27 04:16:01 +00005230 // With PIC, the address is actually $g + Offset.
5231 if (OpFlag) {
5232 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5233 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005234 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00005235 Result);
5236 }
Eric Christopherfd179292009-08-27 18:07:15 +00005237
Chris Lattner18c59872009-06-27 04:16:01 +00005238 return Result;
5239}
5240
5241SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005242X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00005243 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00005244
Chris Lattner18c59872009-06-27 04:16:01 +00005245 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5246 // global base reg.
5247 unsigned char OpFlag = 0;
5248 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005249 CodeModel::Model M = getTargetMachine().getCodeModel();
5250
Chris Lattner4f066492009-07-11 20:29:19 +00005251 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005252 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005253 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005254 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005255 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005256 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005257 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005258
Chris Lattner18c59872009-06-27 04:16:01 +00005259 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00005260
Chris Lattner18c59872009-06-27 04:16:01 +00005261 DebugLoc DL = Op.getDebugLoc();
5262 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005263
5264
Chris Lattner18c59872009-06-27 04:16:01 +00005265 // With PIC, the address is actually $g + Offset.
5266 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00005267 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00005268 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5269 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005270 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00005271 Result);
5272 }
Eric Christopherfd179292009-08-27 18:07:15 +00005273
Chris Lattner18c59872009-06-27 04:16:01 +00005274 return Result;
5275}
5276
Dan Gohman475871a2008-07-27 21:46:04 +00005277SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005278X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00005279 // Create the TargetBlockAddressAddress node.
5280 unsigned char OpFlags =
5281 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00005282 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00005283 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00005284 DebugLoc dl = Op.getDebugLoc();
5285 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
5286 /*isTarget=*/true, OpFlags);
5287
Dan Gohmanf705adb2009-10-30 01:28:02 +00005288 if (Subtarget->isPICStyleRIPRel() &&
5289 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00005290 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
5291 else
5292 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00005293
Dan Gohman29cbade2009-11-20 23:18:13 +00005294 // With PIC, the address is actually $g + Offset.
5295 if (isGlobalRelativeToPICBase(OpFlags)) {
5296 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5297 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
5298 Result);
5299 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00005300
5301 return Result;
5302}
5303
5304SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00005305X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00005306 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00005307 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00005308 // Create the TargetGlobalAddress node, folding in the constant
5309 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00005310 unsigned char OpFlags =
5311 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005312 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00005313 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005314 if (OpFlags == X86II::MO_NO_FLAG &&
5315 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00005316 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00005317 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00005318 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005319 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00005320 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00005321 }
Eric Christopherfd179292009-08-27 18:07:15 +00005322
Chris Lattner4f066492009-07-11 20:29:19 +00005323 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005324 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00005325 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
5326 else
5327 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00005328
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005329 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00005330 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00005331 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5332 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005333 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005334 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005335
Chris Lattner36c25012009-07-10 07:34:39 +00005336 // For globals that require a load from a stub to get the address, emit the
5337 // load.
5338 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00005339 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
David Greene67c9d422010-02-15 16:53:33 +00005340 PseudoSourceValue::getGOT(), 0, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005341
Dan Gohman6520e202008-10-18 02:06:02 +00005342 // If there was a non-zero offset that we didn't fold, create an explicit
5343 // addition for it.
5344 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00005345 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00005346 DAG.getConstant(Offset, getPointerTy()));
5347
Evan Cheng0db9fe62006-04-25 20:13:52 +00005348 return Result;
5349}
5350
Evan Chengda43bcf2008-09-24 00:05:32 +00005351SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005352X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00005353 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00005354 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005355 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00005356}
5357
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005358static SDValue
5359GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00005360 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00005361 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00005362 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Owen Anderson825b72b2009-08-11 20:47:22 +00005363 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005364 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00005365 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005366 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00005367 GA->getOffset(),
5368 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005369 if (InFlag) {
5370 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00005371 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005372 } else {
5373 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00005374 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005375 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00005376
5377 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00005378 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00005379
Rafael Espindola15f1b662009-04-24 12:59:40 +00005380 SDValue Flag = Chain.getValue(1);
5381 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005382}
5383
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005384// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00005385static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005386LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005387 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005388 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00005389 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
5390 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005391 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005392 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005393 InFlag = Chain.getValue(1);
5394
Chris Lattnerb903bed2009-06-26 21:20:29 +00005395 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005396}
5397
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005398// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00005399static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005400LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005401 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00005402 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
5403 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005404}
5405
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005406// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
5407// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00005408static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005409 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00005410 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00005411 DebugLoc dl = GA->getDebugLoc();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005412 // Get the Thread Pointer
Rafael Espindola094fad32009-04-08 21:14:34 +00005413 SDValue Base = DAG.getNode(X86ISD::SegmentBaseAddress,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005414 DebugLoc(), PtrVT,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00005415 DAG.getRegister(is64Bit? X86::FS : X86::GS,
Owen Anderson825b72b2009-08-11 20:47:22 +00005416 MVT::i32));
Rafael Espindola094fad32009-04-08 21:14:34 +00005417
5418 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Base,
David Greene67c9d422010-02-15 16:53:33 +00005419 NULL, 0, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00005420
Chris Lattnerb903bed2009-06-26 21:20:29 +00005421 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005422 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
5423 // initialexec.
5424 unsigned WrapperKind = X86ISD::Wrapper;
5425 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00005426 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00005427 } else if (is64Bit) {
5428 assert(model == TLSModel::InitialExec);
5429 OperandFlags = X86II::MO_GOTTPOFF;
5430 WrapperKind = X86ISD::WrapperRIP;
5431 } else {
5432 assert(model == TLSModel::InitialExec);
5433 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00005434 }
Eric Christopherfd179292009-08-27 18:07:15 +00005435
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005436 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
5437 // exec)
Devang Patel0d881da2010-07-06 22:08:15 +00005438 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
5439 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00005440 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00005441 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00005442
Rafael Espindola9a580232009-02-27 13:37:18 +00005443 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00005444 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
David Greene67c9d422010-02-15 16:53:33 +00005445 PseudoSourceValue::getGOT(), 0, false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00005446
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005447 // The address of the thread local variable is the add of the thread
5448 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00005449 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005450}
5451
Dan Gohman475871a2008-07-27 21:46:04 +00005452SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005453X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Eric Christopher30ef0e52010-06-03 04:07:48 +00005454
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005455 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00005456 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00005457
Eric Christopher30ef0e52010-06-03 04:07:48 +00005458 if (Subtarget->isTargetELF()) {
5459 // TODO: implement the "local dynamic" model
5460 // TODO: implement the "initial exec"model for pic executables
5461
5462 // If GV is an alias then use the aliasee for determining
5463 // thread-localness.
5464 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
5465 GV = GA->resolveAliasedGlobal(false);
5466
5467 TLSModel::Model model
5468 = getTLSModel(GV, getTargetMachine().getRelocationModel());
5469
5470 switch (model) {
5471 case TLSModel::GeneralDynamic:
5472 case TLSModel::LocalDynamic: // not implemented
5473 if (Subtarget->is64Bit())
5474 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
5475 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
5476
5477 case TLSModel::InitialExec:
5478 case TLSModel::LocalExec:
5479 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
5480 Subtarget->is64Bit());
5481 }
5482 } else if (Subtarget->isTargetDarwin()) {
5483 // Darwin only has one model of TLS. Lower to that.
5484 unsigned char OpFlag = 0;
5485 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
5486 X86ISD::WrapperRIP : X86ISD::Wrapper;
5487
5488 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5489 // global base reg.
5490 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
5491 !Subtarget->is64Bit();
5492 if (PIC32)
5493 OpFlag = X86II::MO_TLVP_PIC_BASE;
5494 else
5495 OpFlag = X86II::MO_TLVP;
Devang Patel0d881da2010-07-06 22:08:15 +00005496 DebugLoc DL = Op.getDebugLoc();
5497 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopher30ef0e52010-06-03 04:07:48 +00005498 getPointerTy(),
5499 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00005500 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
5501
5502 // With PIC32, the address is actually $g + Offset.
5503 if (PIC32)
5504 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5505 DAG.getNode(X86ISD::GlobalBaseReg,
5506 DebugLoc(), getPointerTy()),
5507 Offset);
5508
5509 // Lowering the machine isd will make sure everything is in the right
5510 // location.
5511 SDValue Args[] = { Offset };
5512 SDValue Chain = DAG.getNode(X86ISD::TLSCALL, DL, MVT::Other, Args, 1);
5513
5514 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
5515 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
5516 MFI->setAdjustsStack(true);
Eric Christopherfd179292009-08-27 18:07:15 +00005517
Eric Christopher30ef0e52010-06-03 04:07:48 +00005518 // And our return value (tls address) is in the standard call return value
5519 // location.
5520 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
5521 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005522 }
Eric Christopher30ef0e52010-06-03 04:07:48 +00005523
5524 assert(false &&
5525 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00005526
Torok Edwinc23197a2009-07-14 16:55:14 +00005527 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00005528 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005529}
5530
Evan Cheng0db9fe62006-04-25 20:13:52 +00005531
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005532/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00005533/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00005534SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00005535 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00005536 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005537 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005538 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005539 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00005540 SDValue ShOpLo = Op.getOperand(0);
5541 SDValue ShOpHi = Op.getOperand(1);
5542 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00005543 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00005544 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00005545 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00005546
Dan Gohman475871a2008-07-27 21:46:04 +00005547 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005548 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00005549 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
5550 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005551 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005552 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
5553 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005554 }
Evan Chenge3413162006-01-09 18:33:28 +00005555
Owen Anderson825b72b2009-08-11 20:47:22 +00005556 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
5557 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00005558 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00005559 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00005560
Dan Gohman475871a2008-07-27 21:46:04 +00005561 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00005562 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00005563 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
5564 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00005565
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005566 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00005567 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
5568 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005569 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005570 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
5571 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005572 }
5573
Dan Gohman475871a2008-07-27 21:46:04 +00005574 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00005575 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005576}
Evan Chenga3195e82006-01-12 22:54:21 +00005577
Dan Gohmand858e902010-04-17 15:26:15 +00005578SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
5579 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005580 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00005581
5582 if (SrcVT.isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005583 if (SrcVT == MVT::v2i32 && Op.getValueType() == MVT::v2f64) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005584 return Op;
5585 }
5586 return SDValue();
5587 }
5588
Owen Anderson825b72b2009-08-11 20:47:22 +00005589 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00005590 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00005591
Eli Friedman36df4992009-05-27 00:47:34 +00005592 // These are really Legal; return the operand so the caller accepts it as
5593 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00005594 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00005595 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00005596 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00005597 Subtarget->is64Bit()) {
5598 return Op;
5599 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005600
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005601 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005602 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005603 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005604 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005605 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00005606 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00005607 StackSlot,
David Greene67c9d422010-02-15 16:53:33 +00005608 PseudoSourceValue::getFixedStack(SSFI), 0,
5609 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00005610 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
5611}
Evan Cheng0db9fe62006-04-25 20:13:52 +00005612
Owen Andersone50ed302009-08-10 22:56:29 +00005613SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Dale Johannesen8d908eb2010-05-15 18:51:12 +00005614 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00005615 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005616 // Build the FILD
Eli Friedman948e95a2009-05-23 09:59:16 +00005617 DebugLoc dl = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00005618 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00005619 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005620 if (useSSE)
Owen Anderson825b72b2009-08-11 20:47:22 +00005621 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00005622 else
Owen Anderson825b72b2009-08-11 20:47:22 +00005623 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005624 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Dale Johannesenace16102009-02-03 19:33:06 +00005625 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD, dl,
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005626 Tys, Ops, array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005627
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005628 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005629 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00005630 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005631
5632 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
5633 // shouldn't be necessary except that RFP cannot be live across
5634 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005635 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005636 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005637 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00005638 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005639 SDValue Ops[] = {
5640 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
5641 };
5642 Chain = DAG.getNode(X86ISD::FST, dl, Tys, Ops, array_lengthof(Ops));
Dale Johannesenace16102009-02-03 19:33:06 +00005643 Result = DAG.getLoad(Op.getValueType(), dl, Chain, StackSlot,
David Greene67c9d422010-02-15 16:53:33 +00005644 PseudoSourceValue::getFixedStack(SSFI), 0,
5645 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005646 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005647
Evan Cheng0db9fe62006-04-25 20:13:52 +00005648 return Result;
5649}
5650
Bill Wendling8b8a6362009-01-17 03:56:04 +00005651// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00005652SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
5653 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00005654 // This algorithm is not obvious. Here it is in C code, more or less:
5655 /*
5656 double uint64_to_double( uint32_t hi, uint32_t lo ) {
5657 static const __m128i exp = { 0x4330000045300000ULL, 0 };
5658 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00005659
Bill Wendling8b8a6362009-01-17 03:56:04 +00005660 // Copy ints to xmm registers.
5661 __m128i xh = _mm_cvtsi32_si128( hi );
5662 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00005663
Bill Wendling8b8a6362009-01-17 03:56:04 +00005664 // Combine into low half of a single xmm register.
5665 __m128i x = _mm_unpacklo_epi32( xh, xl );
5666 __m128d d;
5667 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00005668
Bill Wendling8b8a6362009-01-17 03:56:04 +00005669 // Merge in appropriate exponents to give the integer bits the right
5670 // magnitude.
5671 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00005672
Bill Wendling8b8a6362009-01-17 03:56:04 +00005673 // Subtract away the biases to deal with the IEEE-754 double precision
5674 // implicit 1.
5675 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00005676
Bill Wendling8b8a6362009-01-17 03:56:04 +00005677 // All conversions up to here are exact. The correctly rounded result is
5678 // calculated using the current rounding mode using the following
5679 // horizontal add.
5680 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
5681 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
5682 // store doesn't really need to be here (except
5683 // maybe to zero the other double)
5684 return sd;
5685 }
5686 */
Dale Johannesen040225f2008-10-21 23:07:49 +00005687
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005688 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00005689 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00005690
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005691 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00005692 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00005693 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
5694 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
5695 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
5696 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00005697 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005698 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005699
Bill Wendling8b8a6362009-01-17 03:56:04 +00005700 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00005701 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005702 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
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, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00005705 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005706 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005707
Owen Anderson825b72b2009-08-11 20:47:22 +00005708 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5709 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00005710 Op.getOperand(0),
5711 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005712 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5713 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00005714 Op.getOperand(0),
5715 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005716 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
5717 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005718 PseudoSourceValue::getConstantPool(), 0,
David Greene67c9d422010-02-15 16:53:33 +00005719 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00005720 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
5721 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Unpck2);
5722 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005723 PseudoSourceValue::getConstantPool(), 0,
David Greene67c9d422010-02-15 16:53:33 +00005724 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00005725 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005726
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005727 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00005728 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00005729 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
5730 DAG.getUNDEF(MVT::v2f64), ShufMask);
5731 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
5732 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005733 DAG.getIntPtrConstant(0));
5734}
5735
Bill Wendling8b8a6362009-01-17 03:56:04 +00005736// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00005737SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
5738 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005739 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00005740 // FP constant to bias correct the final result.
5741 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00005742 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005743
5744 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00005745 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5746 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005747 Op.getOperand(0),
5748 DAG.getIntPtrConstant(0)));
5749
Owen Anderson825b72b2009-08-11 20:47:22 +00005750 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5751 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00005752 DAG.getIntPtrConstant(0));
5753
5754 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00005755 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
5756 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005757 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005758 MVT::v2f64, Load)),
5759 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005760 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005761 MVT::v2f64, Bias)));
5762 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5763 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00005764 DAG.getIntPtrConstant(0));
5765
5766 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00005767 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005768
5769 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00005770 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00005771
Owen Anderson825b72b2009-08-11 20:47:22 +00005772 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005773 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00005774 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00005775 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005776 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00005777 }
5778
5779 // Handle final rounding.
5780 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00005781}
5782
Dan Gohmand858e902010-04-17 15:26:15 +00005783SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
5784 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00005785 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005786 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00005787
Dale Johannesen8d908eb2010-05-15 18:51:12 +00005788 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00005789 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
5790 // the optimization here.
5791 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00005792 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00005793
Owen Andersone50ed302009-08-10 22:56:29 +00005794 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00005795 EVT DstVT = Op.getValueType();
5796 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00005797 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00005798 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00005799 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00005800
5801 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00005802 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00005803 if (SrcVT == MVT::i32) {
5804 SDValue WordOff = DAG.getConstant(4, getPointerTy());
5805 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
5806 getPointerTy(), StackSlot, WordOff);
5807 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
5808 StackSlot, NULL, 0, false, false, 0);
5809 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
5810 OffsetSlot, NULL, 0, false, false, 0);
5811 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
5812 return Fild;
5813 }
5814
5815 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
5816 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
David Greene67c9d422010-02-15 16:53:33 +00005817 StackSlot, NULL, 0, false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00005818 // For i64 source, we need to add the appropriate power of 2 if the input
5819 // was negative. This is the same as the optimization in
5820 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
5821 // we must be careful to do the computation in x87 extended precision, not
5822 // in SSE. (The generic code can't know it's OK to do this, or how to.)
5823 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
5824 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
5825 SDValue Fild = DAG.getNode(X86ISD::FILD, dl, Tys, Ops, 3);
5826
5827 APInt FF(32, 0x5F800000ULL);
5828
5829 // Check whether the sign bit is set.
5830 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
5831 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
5832 ISD::SETLT);
5833
5834 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
5835 SDValue FudgePtr = DAG.getConstantPool(
5836 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
5837 getPointerTy());
5838
5839 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
5840 SDValue Zero = DAG.getIntPtrConstant(0);
5841 SDValue Four = DAG.getIntPtrConstant(4);
5842 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
5843 Zero, Four);
5844 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
5845
5846 // Load the value out, extending it from f32 to f80.
5847 // FIXME: Avoid the extend by constructing the right constant pool?
Evan Chengbcc80172010-07-07 22:15:37 +00005848 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, MVT::f80, dl, DAG.getEntryNode(),
Dale Johannesen8d908eb2010-05-15 18:51:12 +00005849 FudgePtr, PseudoSourceValue::getConstantPool(),
5850 0, MVT::f32, false, false, 4);
5851 // Extend everything to 80 bits to force it to be done on x87.
5852 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
5853 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00005854}
5855
Dan Gohman475871a2008-07-27 21:46:04 +00005856std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00005857FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005858 DebugLoc dl = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00005859
Owen Andersone50ed302009-08-10 22:56:29 +00005860 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00005861
5862 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005863 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
5864 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00005865 }
5866
Owen Anderson825b72b2009-08-11 20:47:22 +00005867 assert(DstTy.getSimpleVT() <= MVT::i64 &&
5868 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00005869 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00005870
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005871 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00005872 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00005873 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005874 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00005875 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00005876 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00005877 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005878 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005879
Evan Cheng87c89352007-10-15 20:11:21 +00005880 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
5881 // stack slot.
5882 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00005883 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00005884 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005885 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00005886
Evan Cheng0db9fe62006-04-25 20:13:52 +00005887 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00005888 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005889 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005890 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
5891 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
5892 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005893 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005894
Dan Gohman475871a2008-07-27 21:46:04 +00005895 SDValue Chain = DAG.getEntryNode();
5896 SDValue Value = Op.getOperand(0);
Chris Lattner78631162008-01-16 06:24:21 +00005897 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005898 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dale Johannesenace16102009-02-03 19:33:06 +00005899 Chain = DAG.getStore(Chain, dl, Value, StackSlot,
David Greene67c9d422010-02-15 16:53:33 +00005900 PseudoSourceValue::getFixedStack(SSFI), 0,
5901 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005902 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00005903 SDValue Ops[] = {
Chris Lattner5a88b832007-02-25 07:10:00 +00005904 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
5905 };
Dale Johannesenace16102009-02-03 19:33:06 +00005906 Value = DAG.getNode(X86ISD::FLD, dl, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005907 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00005908 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005909 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5910 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005911
Evan Cheng0db9fe62006-04-25 20:13:52 +00005912 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00005913 SDValue Ops[] = { Chain, Value, StackSlot };
Owen Anderson825b72b2009-08-11 20:47:22 +00005914 SDValue FIST = DAG.getNode(Opc, dl, MVT::Other, Ops, 3);
Evan Chengd9558e02006-01-06 00:43:03 +00005915
Chris Lattner27a6c732007-11-24 07:07:01 +00005916 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005917}
5918
Dan Gohmand858e902010-04-17 15:26:15 +00005919SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
5920 SelectionDAG &DAG) const {
Eli Friedman23ef1052009-06-06 03:57:58 +00005921 if (Op.getValueType().isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005922 if (Op.getValueType() == MVT::v2i32 &&
5923 Op.getOperand(0).getValueType() == MVT::v2f64) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005924 return Op;
5925 }
5926 return SDValue();
5927 }
5928
Eli Friedman948e95a2009-05-23 09:59:16 +00005929 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00005930 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00005931 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
5932 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00005933
Chris Lattner27a6c732007-11-24 07:07:01 +00005934 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005935 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
David Greene67c9d422010-02-15 16:53:33 +00005936 FIST, StackSlot, NULL, 0, false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00005937}
5938
Dan Gohmand858e902010-04-17 15:26:15 +00005939SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
5940 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00005941 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
5942 SDValue FIST = Vals.first, StackSlot = Vals.second;
5943 assert(FIST.getNode() && "Unexpected failure");
5944
5945 // Load the result.
5946 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
David Greene67c9d422010-02-15 16:53:33 +00005947 FIST, StackSlot, NULL, 0, false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00005948}
5949
Dan Gohmand858e902010-04-17 15:26:15 +00005950SDValue X86TargetLowering::LowerFABS(SDValue Op,
5951 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005952 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005953 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005954 EVT VT = Op.getValueType();
5955 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005956 if (VT.isVector())
5957 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005958 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005959 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005960 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00005961 CV.push_back(C);
5962 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005963 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005964 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00005965 CV.push_back(C);
5966 CV.push_back(C);
5967 CV.push_back(C);
5968 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005969 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005970 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005971 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005972 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005973 PseudoSourceValue::getConstantPool(), 0,
5974 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005975 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005976}
5977
Dan Gohmand858e902010-04-17 15:26:15 +00005978SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005979 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005980 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005981 EVT VT = Op.getValueType();
5982 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00005983 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00005984 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005985 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005986 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005987 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00005988 CV.push_back(C);
5989 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005990 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005991 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00005992 CV.push_back(C);
5993 CV.push_back(C);
5994 CV.push_back(C);
5995 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005996 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005997 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005998 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005999 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00006000 PseudoSourceValue::getConstantPool(), 0,
6001 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006002 if (VT.isVector()) {
Dale Johannesenace16102009-02-03 19:33:06 +00006003 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00006004 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
6005 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006006 Op.getOperand(0)),
Owen Anderson825b72b2009-08-11 20:47:22 +00006007 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00006008 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006009 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00006010 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006011}
6012
Dan Gohmand858e902010-04-17 15:26:15 +00006013SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00006014 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00006015 SDValue Op0 = Op.getOperand(0);
6016 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006017 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006018 EVT VT = Op.getValueType();
6019 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00006020
6021 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00006022 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006023 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00006024 SrcVT = VT;
6025 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00006026 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00006027 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006028 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00006029 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00006030 }
6031
6032 // At this point the operands and the result should have the same
6033 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00006034
Evan Cheng68c47cb2007-01-05 07:55:56 +00006035 // First get the sign bit of second operand.
6036 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00006037 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006038 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
6039 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00006040 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006041 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
6042 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6043 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6044 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00006045 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006046 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006047 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006048 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00006049 PseudoSourceValue::getConstantPool(), 0,
6050 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006051 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00006052
6053 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00006054 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006055 // Op0 is MVT::f32, Op1 is MVT::f64.
6056 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
6057 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
6058 DAG.getConstant(32, MVT::i32));
6059 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, SignBit);
6060 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00006061 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00006062 }
6063
Evan Cheng73d6cf12007-01-05 21:37:56 +00006064 // Clear first operand sign bit.
6065 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00006066 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006067 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
6068 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00006069 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006070 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
6071 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6072 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6073 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00006074 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006075 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006076 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006077 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00006078 PseudoSourceValue::getConstantPool(), 0,
6079 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006080 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00006081
6082 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00006083 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00006084}
6085
Dan Gohman076aee32009-03-04 19:44:21 +00006086/// Emit nodes that will be selected as "test Op0,Op0", or something
6087/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00006088SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00006089 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00006090 DebugLoc dl = Op.getDebugLoc();
6091
Dan Gohman31125812009-03-07 01:58:32 +00006092 // CF and OF aren't always set the way we want. Determine which
6093 // of these we need.
6094 bool NeedCF = false;
6095 bool NeedOF = false;
6096 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006097 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00006098 case X86::COND_A: case X86::COND_AE:
6099 case X86::COND_B: case X86::COND_BE:
6100 NeedCF = true;
6101 break;
6102 case X86::COND_G: case X86::COND_GE:
6103 case X86::COND_L: case X86::COND_LE:
6104 case X86::COND_O: case X86::COND_NO:
6105 NeedOF = true;
6106 break;
Dan Gohman31125812009-03-07 01:58:32 +00006107 }
6108
Dan Gohman076aee32009-03-04 19:44:21 +00006109 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00006110 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
6111 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006112 if (Op.getResNo() != 0 || NeedOF || NeedCF)
6113 // Emit a CMP with 0, which is the TEST pattern.
6114 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
6115 DAG.getConstant(0, Op.getValueType()));
6116
6117 unsigned Opcode = 0;
6118 unsigned NumOperands = 0;
6119 switch (Op.getNode()->getOpcode()) {
6120 case ISD::ADD:
6121 // Due to an isel shortcoming, be conservative if this add is likely to be
6122 // selected as part of a load-modify-store instruction. When the root node
6123 // in a match is a store, isel doesn't know how to remap non-chain non-flag
6124 // uses of other nodes in the match, such as the ADD in this case. This
6125 // leads to the ADD being left around and reselected, with the result being
6126 // two adds in the output. Alas, even if none our users are stores, that
6127 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
6128 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
6129 // climbing the DAG back to the root, and it doesn't seem to be worth the
6130 // effort.
6131 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00006132 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006133 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
6134 goto default_case;
6135
6136 if (ConstantSDNode *C =
6137 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
6138 // An add of one will be selected as an INC.
6139 if (C->getAPIntValue() == 1) {
6140 Opcode = X86ISD::INC;
6141 NumOperands = 1;
6142 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00006143 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006144
6145 // An add of negative one (subtract of one) will be selected as a DEC.
6146 if (C->getAPIntValue().isAllOnesValue()) {
6147 Opcode = X86ISD::DEC;
6148 NumOperands = 1;
6149 break;
6150 }
Dan Gohman076aee32009-03-04 19:44:21 +00006151 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006152
6153 // Otherwise use a regular EFLAGS-setting add.
6154 Opcode = X86ISD::ADD;
6155 NumOperands = 2;
6156 break;
6157 case ISD::AND: {
6158 // If the primary and result isn't used, don't bother using X86ISD::AND,
6159 // because a TEST instruction will be better.
6160 bool NonFlagUse = false;
6161 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
6162 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
6163 SDNode *User = *UI;
6164 unsigned UOpNo = UI.getOperandNo();
6165 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
6166 // Look pass truncate.
6167 UOpNo = User->use_begin().getOperandNo();
6168 User = *User->use_begin();
6169 }
6170
6171 if (User->getOpcode() != ISD::BRCOND &&
6172 User->getOpcode() != ISD::SETCC &&
6173 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
6174 NonFlagUse = true;
6175 break;
6176 }
Dan Gohman076aee32009-03-04 19:44:21 +00006177 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006178
6179 if (!NonFlagUse)
6180 break;
6181 }
6182 // FALL THROUGH
6183 case ISD::SUB:
6184 case ISD::OR:
6185 case ISD::XOR:
6186 // Due to the ISEL shortcoming noted above, be conservative if this op is
6187 // likely to be selected as part of a load-modify-store instruction.
6188 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
6189 UE = Op.getNode()->use_end(); UI != UE; ++UI)
6190 if (UI->getOpcode() == ISD::STORE)
6191 goto default_case;
6192
6193 // Otherwise use a regular EFLAGS-setting instruction.
6194 switch (Op.getNode()->getOpcode()) {
6195 default: llvm_unreachable("unexpected operator!");
6196 case ISD::SUB: Opcode = X86ISD::SUB; break;
6197 case ISD::OR: Opcode = X86ISD::OR; break;
6198 case ISD::XOR: Opcode = X86ISD::XOR; break;
6199 case ISD::AND: Opcode = X86ISD::AND; break;
6200 }
6201
6202 NumOperands = 2;
6203 break;
6204 case X86ISD::ADD:
6205 case X86ISD::SUB:
6206 case X86ISD::INC:
6207 case X86ISD::DEC:
6208 case X86ISD::OR:
6209 case X86ISD::XOR:
6210 case X86ISD::AND:
6211 return SDValue(Op.getNode(), 1);
6212 default:
6213 default_case:
6214 break;
Dan Gohman076aee32009-03-04 19:44:21 +00006215 }
6216
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006217 if (Opcode == 0)
6218 // Emit a CMP with 0, which is the TEST pattern.
6219 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
6220 DAG.getConstant(0, Op.getValueType()));
6221
6222 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
6223 SmallVector<SDValue, 4> Ops;
6224 for (unsigned i = 0; i != NumOperands; ++i)
6225 Ops.push_back(Op.getOperand(i));
6226
6227 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
6228 DAG.ReplaceAllUsesWith(Op, New);
6229 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00006230}
6231
6232/// Emit nodes that will be selected as "cmp Op0,Op1", or something
6233/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00006234SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00006235 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00006236 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
6237 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00006238 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00006239
6240 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00006241 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00006242}
6243
Evan Chengd40d03e2010-01-06 19:38:29 +00006244/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
6245/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00006246SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
6247 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00006248 SDValue Op0 = And.getOperand(0);
6249 SDValue Op1 = And.getOperand(1);
6250 if (Op0.getOpcode() == ISD::TRUNCATE)
6251 Op0 = Op0.getOperand(0);
6252 if (Op1.getOpcode() == ISD::TRUNCATE)
6253 Op1 = Op1.getOperand(0);
6254
Evan Chengd40d03e2010-01-06 19:38:29 +00006255 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00006256 if (Op1.getOpcode() == ISD::SHL)
6257 std::swap(Op0, Op1);
6258 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00006259 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
6260 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00006261 // If we looked past a truncate, check that it's only truncating away
6262 // known zeros.
6263 unsigned BitWidth = Op0.getValueSizeInBits();
6264 unsigned AndBitWidth = And.getValueSizeInBits();
6265 if (BitWidth > AndBitWidth) {
6266 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
6267 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
6268 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
6269 return SDValue();
6270 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00006271 LHS = Op1;
6272 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00006273 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00006274 } else if (Op1.getOpcode() == ISD::Constant) {
6275 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
6276 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00006277 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
6278 LHS = AndLHS.getOperand(0);
6279 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00006280 }
Evan Chengd40d03e2010-01-06 19:38:29 +00006281 }
Evan Cheng0488db92007-09-25 01:57:46 +00006282
Evan Chengd40d03e2010-01-06 19:38:29 +00006283 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00006284 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00006285 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00006286 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00006287 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00006288 // Also promote i16 to i32 for performance / code size reason.
6289 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00006290 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00006291 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00006292
Evan Chengd40d03e2010-01-06 19:38:29 +00006293 // If the operand types disagree, extend the shift amount to match. Since
6294 // BT ignores high bits (like shifts) we can use anyextend.
6295 if (LHS.getValueType() != RHS.getValueType())
6296 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00006297
Evan Chengd40d03e2010-01-06 19:38:29 +00006298 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
6299 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
6300 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6301 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00006302 }
6303
Evan Cheng54de3ea2010-01-05 06:52:31 +00006304 return SDValue();
6305}
6306
Dan Gohmand858e902010-04-17 15:26:15 +00006307SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng54de3ea2010-01-05 06:52:31 +00006308 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
6309 SDValue Op0 = Op.getOperand(0);
6310 SDValue Op1 = Op.getOperand(1);
6311 DebugLoc dl = Op.getDebugLoc();
6312 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
6313
6314 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00006315 // Lower (X & (1 << N)) == 0 to BT(X, N).
6316 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
6317 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
6318 if (Op0.getOpcode() == ISD::AND &&
6319 Op0.hasOneUse() &&
6320 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00006321 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00006322 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
6323 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
6324 if (NewSetCC.getNode())
6325 return NewSetCC;
6326 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00006327
Evan Cheng2c755ba2010-02-27 07:36:59 +00006328 // Look for "(setcc) == / != 1" to avoid unncessary setcc.
6329 if (Op0.getOpcode() == X86ISD::SETCC &&
6330 Op1.getOpcode() == ISD::Constant &&
6331 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
6332 cast<ConstantSDNode>(Op1)->isNullValue()) &&
6333 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
6334 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
6335 bool Invert = (CC == ISD::SETNE) ^
6336 cast<ConstantSDNode>(Op1)->isNullValue();
6337 if (Invert)
6338 CCode = X86::GetOppositeBranchCondition(CCode);
6339 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6340 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
6341 }
6342
Evan Chenge5b51ac2010-04-17 06:13:15 +00006343 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00006344 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00006345 if (X86CC == X86::COND_INVALID)
6346 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00006347
Evan Cheng552f09a2010-04-26 19:06:11 +00006348 SDValue Cond = EmitCmp(Op0, Op1, X86CC, DAG);
Evan Chengad9c0a32009-12-15 00:53:42 +00006349
6350 // Use sbb x, x to materialize carry bit into a GPR.
Evan Cheng2e489c42009-12-16 00:53:11 +00006351 if (X86CC == X86::COND_B)
Evan Chengad9c0a32009-12-15 00:53:42 +00006352 return DAG.getNode(ISD::AND, dl, MVT::i8,
6353 DAG.getNode(X86ISD::SETCC_CARRY, dl, MVT::i8,
6354 DAG.getConstant(X86CC, MVT::i8), Cond),
6355 DAG.getConstant(1, MVT::i8));
Evan Chengad9c0a32009-12-15 00:53:42 +00006356
Owen Anderson825b72b2009-08-11 20:47:22 +00006357 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6358 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00006359}
6360
Dan Gohmand858e902010-04-17 15:26:15 +00006361SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00006362 SDValue Cond;
6363 SDValue Op0 = Op.getOperand(0);
6364 SDValue Op1 = Op.getOperand(1);
6365 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00006366 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00006367 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
6368 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006369 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00006370
6371 if (isFP) {
6372 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00006373 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00006374 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
6375 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00006376 bool Swap = false;
6377
6378 switch (SetCCOpcode) {
6379 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00006380 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00006381 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00006382 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00006383 case ISD::SETGT: Swap = true; // Fallthrough
6384 case ISD::SETLT:
6385 case ISD::SETOLT: SSECC = 1; break;
6386 case ISD::SETOGE:
6387 case ISD::SETGE: Swap = true; // Fallthrough
6388 case ISD::SETLE:
6389 case ISD::SETOLE: SSECC = 2; break;
6390 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00006391 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00006392 case ISD::SETNE: SSECC = 4; break;
6393 case ISD::SETULE: Swap = true;
6394 case ISD::SETUGE: SSECC = 5; break;
6395 case ISD::SETULT: Swap = true;
6396 case ISD::SETUGT: SSECC = 6; break;
6397 case ISD::SETO: SSECC = 7; break;
6398 }
6399 if (Swap)
6400 std::swap(Op0, Op1);
6401
Nate Begemanfb8ead02008-07-25 19:05:58 +00006402 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00006403 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00006404 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00006405 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00006406 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
6407 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00006408 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00006409 }
6410 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00006411 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00006412 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
6413 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00006414 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00006415 }
Torok Edwinc23197a2009-07-14 16:55:14 +00006416 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00006417 }
6418 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00006419 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00006420 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006421
Nate Begeman30a0de92008-07-17 16:51:19 +00006422 // We are handling one of the integer comparisons here. Since SSE only has
6423 // GT and EQ comparisons for integer, swapping operands and multiple
6424 // operations may be required for some comparisons.
6425 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
6426 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00006427
Owen Anderson825b72b2009-08-11 20:47:22 +00006428 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00006429 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00006430 case MVT::v8i8:
6431 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
6432 case MVT::v4i16:
6433 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
6434 case MVT::v2i32:
6435 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
6436 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00006437 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006438
Nate Begeman30a0de92008-07-17 16:51:19 +00006439 switch (SetCCOpcode) {
6440 default: break;
6441 case ISD::SETNE: Invert = true;
6442 case ISD::SETEQ: Opc = EQOpc; break;
6443 case ISD::SETLT: Swap = true;
6444 case ISD::SETGT: Opc = GTOpc; break;
6445 case ISD::SETGE: Swap = true;
6446 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
6447 case ISD::SETULT: Swap = true;
6448 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
6449 case ISD::SETUGE: Swap = true;
6450 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
6451 }
6452 if (Swap)
6453 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006454
Nate Begeman30a0de92008-07-17 16:51:19 +00006455 // Since SSE has no unsigned integer comparisons, we need to flip the sign
6456 // bits of the inputs before performing those operations.
6457 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00006458 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00006459 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
6460 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00006461 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00006462 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
6463 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00006464 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
6465 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00006466 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006467
Dale Johannesenace16102009-02-03 19:33:06 +00006468 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00006469
6470 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00006471 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00006472 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00006473
Nate Begeman30a0de92008-07-17 16:51:19 +00006474 return Result;
6475}
Evan Cheng0488db92007-09-25 01:57:46 +00006476
Evan Cheng370e5342008-12-03 08:38:43 +00006477// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00006478static bool isX86LogicalCmp(SDValue Op) {
6479 unsigned Opc = Op.getNode()->getOpcode();
6480 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
6481 return true;
6482 if (Op.getResNo() == 1 &&
6483 (Opc == X86ISD::ADD ||
6484 Opc == X86ISD::SUB ||
6485 Opc == X86ISD::SMUL ||
6486 Opc == X86ISD::UMUL ||
6487 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00006488 Opc == X86ISD::DEC ||
6489 Opc == X86ISD::OR ||
6490 Opc == X86ISD::XOR ||
6491 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00006492 return true;
6493
6494 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00006495}
6496
Dan Gohmand858e902010-04-17 15:26:15 +00006497SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00006498 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00006499 SDValue Cond = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006500 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00006501 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00006502
Dan Gohman1a492952009-10-20 16:22:37 +00006503 if (Cond.getOpcode() == ISD::SETCC) {
6504 SDValue NewCond = LowerSETCC(Cond, DAG);
6505 if (NewCond.getNode())
6506 Cond = NewCond;
6507 }
Evan Cheng734503b2006-09-11 02:19:56 +00006508
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006509 // (select (x == 0), -1, 0) -> (sign_bit (x - 1))
6510 SDValue Op1 = Op.getOperand(1);
6511 SDValue Op2 = Op.getOperand(2);
6512 if (Cond.getOpcode() == X86ISD::SETCC &&
6513 cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue() == X86::COND_E) {
6514 SDValue Cmp = Cond.getOperand(1);
6515 if (Cmp.getOpcode() == X86ISD::CMP) {
6516 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op1);
6517 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
6518 ConstantSDNode *RHSC =
6519 dyn_cast<ConstantSDNode>(Cmp.getOperand(1).getNode());
6520 if (N1C && N1C->isAllOnesValue() &&
6521 N2C && N2C->isNullValue() &&
6522 RHSC && RHSC->isNullValue()) {
6523 SDValue CmpOp0 = Cmp.getOperand(0);
Chris Lattnerda0688e2010-03-14 18:44:35 +00006524 Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006525 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
6526 return DAG.getNode(X86ISD::SETCC_CARRY, dl, Op.getValueType(),
6527 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
6528 }
6529 }
6530 }
6531
Evan Chengad9c0a32009-12-15 00:53:42 +00006532 // Look pass (and (setcc_carry (cmp ...)), 1).
6533 if (Cond.getOpcode() == ISD::AND &&
6534 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
6535 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
6536 if (C && C->getAPIntValue() == 1)
6537 Cond = Cond.getOperand(0);
6538 }
6539
Evan Cheng3f41d662007-10-08 22:16:29 +00006540 // If condition flag is set by a X86ISD::CMP, then use it as the condition
6541 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00006542 if (Cond.getOpcode() == X86ISD::SETCC ||
6543 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00006544 CC = Cond.getOperand(0);
6545
Dan Gohman475871a2008-07-27 21:46:04 +00006546 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00006547 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00006548 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00006549
Evan Cheng3f41d662007-10-08 22:16:29 +00006550 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006551 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00006552 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00006553 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00006554
Chris Lattnerd1980a52009-03-12 06:52:53 +00006555 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
6556 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00006557 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00006558 addTest = false;
6559 }
6560 }
6561
6562 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00006563 // Look pass the truncate.
6564 if (Cond.getOpcode() == ISD::TRUNCATE)
6565 Cond = Cond.getOperand(0);
6566
6567 // We know the result of AND is compared against zero. Try to match
6568 // it to BT.
6569 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
6570 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
6571 if (NewSetCC.getNode()) {
6572 CC = NewSetCC.getOperand(0);
6573 Cond = NewSetCC.getOperand(1);
6574 addTest = false;
6575 }
6576 }
6577 }
6578
6579 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006580 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00006581 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00006582 }
6583
Evan Cheng0488db92007-09-25 01:57:46 +00006584 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
6585 // condition is true.
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006586 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
6587 SDValue Ops[] = { Op2, Op1, CC, Cond };
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006588 return DAG.getNode(X86ISD::CMOV, dl, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00006589}
6590
Evan Cheng370e5342008-12-03 08:38:43 +00006591// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
6592// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
6593// from the AND / OR.
6594static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
6595 Opc = Op.getOpcode();
6596 if (Opc != ISD::OR && Opc != ISD::AND)
6597 return false;
6598 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
6599 Op.getOperand(0).hasOneUse() &&
6600 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
6601 Op.getOperand(1).hasOneUse());
6602}
6603
Evan Cheng961d6d42009-02-02 08:19:07 +00006604// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
6605// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00006606static bool isXor1OfSetCC(SDValue Op) {
6607 if (Op.getOpcode() != ISD::XOR)
6608 return false;
6609 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6610 if (N1C && N1C->getAPIntValue() == 1) {
6611 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
6612 Op.getOperand(0).hasOneUse();
6613 }
6614 return false;
6615}
6616
Dan Gohmand858e902010-04-17 15:26:15 +00006617SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00006618 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00006619 SDValue Chain = Op.getOperand(0);
6620 SDValue Cond = Op.getOperand(1);
6621 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006622 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00006623 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00006624
Dan Gohman1a492952009-10-20 16:22:37 +00006625 if (Cond.getOpcode() == ISD::SETCC) {
6626 SDValue NewCond = LowerSETCC(Cond, DAG);
6627 if (NewCond.getNode())
6628 Cond = NewCond;
6629 }
Chris Lattnere55484e2008-12-25 05:34:37 +00006630#if 0
6631 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00006632 else if (Cond.getOpcode() == X86ISD::ADD ||
6633 Cond.getOpcode() == X86ISD::SUB ||
6634 Cond.getOpcode() == X86ISD::SMUL ||
6635 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00006636 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00006637#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00006638
Evan Chengad9c0a32009-12-15 00:53:42 +00006639 // Look pass (and (setcc_carry (cmp ...)), 1).
6640 if (Cond.getOpcode() == ISD::AND &&
6641 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
6642 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
6643 if (C && C->getAPIntValue() == 1)
6644 Cond = Cond.getOperand(0);
6645 }
6646
Evan Cheng3f41d662007-10-08 22:16:29 +00006647 // If condition flag is set by a X86ISD::CMP, then use it as the condition
6648 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00006649 if (Cond.getOpcode() == X86ISD::SETCC ||
6650 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00006651 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006652
Dan Gohman475871a2008-07-27 21:46:04 +00006653 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00006654 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00006655 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00006656 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00006657 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00006658 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00006659 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00006660 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00006661 default: break;
6662 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00006663 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00006664 // These can only come from an arithmetic instruction with overflow,
6665 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00006666 Cond = Cond.getNode()->getOperand(1);
6667 addTest = false;
6668 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00006669 }
Evan Cheng0488db92007-09-25 01:57:46 +00006670 }
Evan Cheng370e5342008-12-03 08:38:43 +00006671 } else {
6672 unsigned CondOpc;
6673 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
6674 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00006675 if (CondOpc == ISD::OR) {
6676 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
6677 // two branches instead of an explicit OR instruction with a
6678 // separate test.
6679 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00006680 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00006681 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006682 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00006683 Chain, Dest, CC, Cmp);
6684 CC = Cond.getOperand(1).getOperand(0);
6685 Cond = Cmp;
6686 addTest = false;
6687 }
6688 } else { // ISD::AND
6689 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
6690 // two branches instead of an explicit AND instruction with a
6691 // separate test. However, we only do this if this block doesn't
6692 // have a fall-through edge, because this requires an explicit
6693 // jmp when the condition is false.
6694 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00006695 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00006696 Op.getNode()->hasOneUse()) {
6697 X86::CondCode CCode =
6698 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
6699 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006700 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00006701 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00006702 // Look for an unconditional branch following this conditional branch.
6703 // We need this because we need to reverse the successors in order
6704 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00006705 if (User->getOpcode() == ISD::BR) {
6706 SDValue FalseBB = User->getOperand(1);
6707 SDNode *NewBR =
6708 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00006709 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00006710 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00006711 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00006712
Dale Johannesene4d209d2009-02-03 20:21:25 +00006713 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00006714 Chain, Dest, CC, Cmp);
6715 X86::CondCode CCode =
6716 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
6717 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006718 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00006719 Cond = Cmp;
6720 addTest = false;
6721 }
6722 }
Dan Gohman279c22e2008-10-21 03:29:32 +00006723 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00006724 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
6725 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
6726 // It should be transformed during dag combiner except when the condition
6727 // is set by a arithmetics with overflow node.
6728 X86::CondCode CCode =
6729 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
6730 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006731 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00006732 Cond = Cond.getOperand(0).getOperand(1);
6733 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00006734 }
Evan Cheng0488db92007-09-25 01:57:46 +00006735 }
6736
6737 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00006738 // Look pass the truncate.
6739 if (Cond.getOpcode() == ISD::TRUNCATE)
6740 Cond = Cond.getOperand(0);
6741
6742 // We know the result of AND is compared against zero. Try to match
6743 // it to BT.
6744 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
6745 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
6746 if (NewSetCC.getNode()) {
6747 CC = NewSetCC.getOperand(0);
6748 Cond = NewSetCC.getOperand(1);
6749 addTest = false;
6750 }
6751 }
6752 }
6753
6754 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006755 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00006756 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00006757 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00006758 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00006759 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00006760}
6761
Anton Korobeynikove060b532007-04-17 19:34:00 +00006762
6763// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
6764// Calls to _alloca is needed to probe the stack when allocating more than 4k
6765// bytes in one go. Touching the stack at 4K increments is necessary to ensure
6766// that the guard pages used by the OS virtual memory manager are allocated in
6767// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00006768SDValue
6769X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006770 SelectionDAG &DAG) const {
Anton Korobeynikove060b532007-04-17 19:34:00 +00006771 assert(Subtarget->isTargetCygMing() &&
6772 "This should be used only on Cygwin/Mingw targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006773 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006774
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006775 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00006776 SDValue Chain = Op.getOperand(0);
6777 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006778 // FIXME: Ensure alignment here
6779
Dan Gohman475871a2008-07-27 21:46:04 +00006780 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006781
Owen Anderson825b72b2009-08-11 20:47:22 +00006782 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006783
Dale Johannesendd64c412009-02-04 00:33:20 +00006784 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006785 Flag = Chain.getValue(1);
6786
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00006787 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006788
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00006789 Chain = DAG.getNode(X86ISD::MINGW_ALLOCA, dl, NodeTys, Chain, Flag);
6790 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006791
Dale Johannesendd64c412009-02-04 00:33:20 +00006792 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006793
Dan Gohman475871a2008-07-27 21:46:04 +00006794 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006795 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006796}
6797
Dan Gohmand858e902010-04-17 15:26:15 +00006798SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00006799 MachineFunction &MF = DAG.getMachineFunction();
6800 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
6801
Dan Gohman69de1932008-02-06 22:27:42 +00006802 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006803 DebugLoc dl = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00006804
Evan Cheng25ab6902006-09-08 06:48:29 +00006805 if (!Subtarget->is64Bit()) {
6806 // vastart just stores the address of the VarArgsFrameIndex slot into the
6807 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00006808 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
6809 getPointerTy());
David Greene67c9d422010-02-15 16:53:33 +00006810 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0,
6811 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006812 }
6813
6814 // __va_list_tag:
6815 // gp_offset (0 - 6 * 8)
6816 // fp_offset (48 - 48 + 8 * 16)
6817 // overflow_arg_area (point to parameters coming in memory).
6818 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00006819 SmallVector<SDValue, 8> MemOps;
6820 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00006821 // Store gp_offset
Dale Johannesene4d209d2009-02-03 20:21:25 +00006822 SDValue Store = DAG.getStore(Op.getOperand(0), dl,
Dan Gohman1e93df62010-04-17 14:41:14 +00006823 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
6824 MVT::i32),
David Greene67c9d422010-02-15 16:53:33 +00006825 FIN, SV, 0, false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006826 MemOps.push_back(Store);
6827
6828 // Store fp_offset
Scott Michelfdc40a02009-02-17 22:15:04 +00006829 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006830 FIN, DAG.getIntPtrConstant(4));
6831 Store = DAG.getStore(Op.getOperand(0), dl,
Dan Gohman1e93df62010-04-17 14:41:14 +00006832 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
6833 MVT::i32),
Dan Gohman01dcb182010-07-09 01:06:48 +00006834 FIN, SV, 4, false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006835 MemOps.push_back(Store);
6836
6837 // Store ptr to overflow_arg_area
Scott Michelfdc40a02009-02-17 22:15:04 +00006838 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006839 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00006840 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
6841 getPointerTy());
Dan Gohman01dcb182010-07-09 01:06:48 +00006842 Store = DAG.getStore(Op.getOperand(0), dl, OVFIN, FIN, SV, 8,
David Greene67c9d422010-02-15 16:53:33 +00006843 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006844 MemOps.push_back(Store);
6845
6846 // Store ptr to reg_save_area.
Scott Michelfdc40a02009-02-17 22:15:04 +00006847 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006848 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00006849 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
6850 getPointerTy());
Dan Gohman01dcb182010-07-09 01:06:48 +00006851 Store = DAG.getStore(Op.getOperand(0), dl, RSFIN, FIN, SV, 16,
David Greene67c9d422010-02-15 16:53:33 +00006852 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006853 MemOps.push_back(Store);
Owen Anderson825b72b2009-08-11 20:47:22 +00006854 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006855 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00006856}
6857
Dan Gohmand858e902010-04-17 15:26:15 +00006858SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman9018e832008-05-10 01:26:14 +00006859 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
6860 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman9018e832008-05-10 01:26:14 +00006861
Chris Lattner75361b62010-04-07 22:58:41 +00006862 report_fatal_error("VAArgInst is not yet implemented for x86-64!");
Dan Gohman475871a2008-07-27 21:46:04 +00006863 return SDValue();
Dan Gohman9018e832008-05-10 01:26:14 +00006864}
6865
Dan Gohmand858e902010-04-17 15:26:15 +00006866SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00006867 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00006868 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00006869 SDValue Chain = Op.getOperand(0);
6870 SDValue DstPtr = Op.getOperand(1);
6871 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00006872 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
6873 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006874 DebugLoc dl = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00006875
Dale Johannesendd64c412009-02-04 00:33:20 +00006876 return DAG.getMemcpy(Chain, dl, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00006877 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
6878 false, DstSV, 0, SrcSV, 0);
Evan Chengae642192007-03-02 23:16:35 +00006879}
6880
Dan Gohman475871a2008-07-27 21:46:04 +00006881SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006882X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006883 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006884 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006885 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00006886 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00006887 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00006888 case Intrinsic::x86_sse_comieq_ss:
6889 case Intrinsic::x86_sse_comilt_ss:
6890 case Intrinsic::x86_sse_comile_ss:
6891 case Intrinsic::x86_sse_comigt_ss:
6892 case Intrinsic::x86_sse_comige_ss:
6893 case Intrinsic::x86_sse_comineq_ss:
6894 case Intrinsic::x86_sse_ucomieq_ss:
6895 case Intrinsic::x86_sse_ucomilt_ss:
6896 case Intrinsic::x86_sse_ucomile_ss:
6897 case Intrinsic::x86_sse_ucomigt_ss:
6898 case Intrinsic::x86_sse_ucomige_ss:
6899 case Intrinsic::x86_sse_ucomineq_ss:
6900 case Intrinsic::x86_sse2_comieq_sd:
6901 case Intrinsic::x86_sse2_comilt_sd:
6902 case Intrinsic::x86_sse2_comile_sd:
6903 case Intrinsic::x86_sse2_comigt_sd:
6904 case Intrinsic::x86_sse2_comige_sd:
6905 case Intrinsic::x86_sse2_comineq_sd:
6906 case Intrinsic::x86_sse2_ucomieq_sd:
6907 case Intrinsic::x86_sse2_ucomilt_sd:
6908 case Intrinsic::x86_sse2_ucomile_sd:
6909 case Intrinsic::x86_sse2_ucomigt_sd:
6910 case Intrinsic::x86_sse2_ucomige_sd:
6911 case Intrinsic::x86_sse2_ucomineq_sd: {
6912 unsigned Opc = 0;
6913 ISD::CondCode CC = ISD::SETCC_INVALID;
6914 switch (IntNo) {
6915 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006916 case Intrinsic::x86_sse_comieq_ss:
6917 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006918 Opc = X86ISD::COMI;
6919 CC = ISD::SETEQ;
6920 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006921 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006922 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006923 Opc = X86ISD::COMI;
6924 CC = ISD::SETLT;
6925 break;
6926 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006927 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006928 Opc = X86ISD::COMI;
6929 CC = ISD::SETLE;
6930 break;
6931 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006932 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006933 Opc = X86ISD::COMI;
6934 CC = ISD::SETGT;
6935 break;
6936 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006937 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006938 Opc = X86ISD::COMI;
6939 CC = ISD::SETGE;
6940 break;
6941 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006942 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006943 Opc = X86ISD::COMI;
6944 CC = ISD::SETNE;
6945 break;
6946 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006947 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006948 Opc = X86ISD::UCOMI;
6949 CC = ISD::SETEQ;
6950 break;
6951 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006952 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006953 Opc = X86ISD::UCOMI;
6954 CC = ISD::SETLT;
6955 break;
6956 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006957 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006958 Opc = X86ISD::UCOMI;
6959 CC = ISD::SETLE;
6960 break;
6961 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006962 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006963 Opc = X86ISD::UCOMI;
6964 CC = ISD::SETGT;
6965 break;
6966 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006967 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006968 Opc = X86ISD::UCOMI;
6969 CC = ISD::SETGE;
6970 break;
6971 case Intrinsic::x86_sse_ucomineq_ss:
6972 case Intrinsic::x86_sse2_ucomineq_sd:
6973 Opc = X86ISD::UCOMI;
6974 CC = ISD::SETNE;
6975 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006976 }
Evan Cheng734503b2006-09-11 02:19:56 +00006977
Dan Gohman475871a2008-07-27 21:46:04 +00006978 SDValue LHS = Op.getOperand(1);
6979 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00006980 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00006981 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00006982 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
6983 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6984 DAG.getConstant(X86CC, MVT::i8), Cond);
6985 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00006986 }
Eric Christopher71c67532009-07-29 00:28:05 +00006987 // ptest intrinsics. The intrinsic these come from are designed to return
Eric Christopher794bfed2009-07-29 01:01:19 +00006988 // an integer value, not just an instruction so lower it to the ptest
6989 // pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00006990 case Intrinsic::x86_sse41_ptestz:
6991 case Intrinsic::x86_sse41_ptestc:
6992 case Intrinsic::x86_sse41_ptestnzc:{
6993 unsigned X86CC = 0;
6994 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00006995 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Eric Christopher71c67532009-07-29 00:28:05 +00006996 case Intrinsic::x86_sse41_ptestz:
6997 // ZF = 1
6998 X86CC = X86::COND_E;
6999 break;
7000 case Intrinsic::x86_sse41_ptestc:
7001 // CF = 1
7002 X86CC = X86::COND_B;
7003 break;
Eric Christopherfd179292009-08-27 18:07:15 +00007004 case Intrinsic::x86_sse41_ptestnzc:
Eric Christopher71c67532009-07-29 00:28:05 +00007005 // ZF and CF = 0
7006 X86CC = X86::COND_A;
7007 break;
7008 }
Eric Christopherfd179292009-08-27 18:07:15 +00007009
Eric Christopher71c67532009-07-29 00:28:05 +00007010 SDValue LHS = Op.getOperand(1);
7011 SDValue RHS = Op.getOperand(2);
Owen Anderson825b72b2009-08-11 20:47:22 +00007012 SDValue Test = DAG.getNode(X86ISD::PTEST, dl, MVT::i32, LHS, RHS);
7013 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
7014 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
7015 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00007016 }
Evan Cheng5759f972008-05-04 09:15:50 +00007017
7018 // Fix vector shift instructions where the last operand is a non-immediate
7019 // i32 value.
7020 case Intrinsic::x86_sse2_pslli_w:
7021 case Intrinsic::x86_sse2_pslli_d:
7022 case Intrinsic::x86_sse2_pslli_q:
7023 case Intrinsic::x86_sse2_psrli_w:
7024 case Intrinsic::x86_sse2_psrli_d:
7025 case Intrinsic::x86_sse2_psrli_q:
7026 case Intrinsic::x86_sse2_psrai_w:
7027 case Intrinsic::x86_sse2_psrai_d:
7028 case Intrinsic::x86_mmx_pslli_w:
7029 case Intrinsic::x86_mmx_pslli_d:
7030 case Intrinsic::x86_mmx_pslli_q:
7031 case Intrinsic::x86_mmx_psrli_w:
7032 case Intrinsic::x86_mmx_psrli_d:
7033 case Intrinsic::x86_mmx_psrli_q:
7034 case Intrinsic::x86_mmx_psrai_w:
7035 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00007036 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00007037 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00007038 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00007039
7040 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00007041 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00007042 switch (IntNo) {
7043 case Intrinsic::x86_sse2_pslli_w:
7044 NewIntNo = Intrinsic::x86_sse2_psll_w;
7045 break;
7046 case Intrinsic::x86_sse2_pslli_d:
7047 NewIntNo = Intrinsic::x86_sse2_psll_d;
7048 break;
7049 case Intrinsic::x86_sse2_pslli_q:
7050 NewIntNo = Intrinsic::x86_sse2_psll_q;
7051 break;
7052 case Intrinsic::x86_sse2_psrli_w:
7053 NewIntNo = Intrinsic::x86_sse2_psrl_w;
7054 break;
7055 case Intrinsic::x86_sse2_psrli_d:
7056 NewIntNo = Intrinsic::x86_sse2_psrl_d;
7057 break;
7058 case Intrinsic::x86_sse2_psrli_q:
7059 NewIntNo = Intrinsic::x86_sse2_psrl_q;
7060 break;
7061 case Intrinsic::x86_sse2_psrai_w:
7062 NewIntNo = Intrinsic::x86_sse2_psra_w;
7063 break;
7064 case Intrinsic::x86_sse2_psrai_d:
7065 NewIntNo = Intrinsic::x86_sse2_psra_d;
7066 break;
7067 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00007068 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00007069 switch (IntNo) {
7070 case Intrinsic::x86_mmx_pslli_w:
7071 NewIntNo = Intrinsic::x86_mmx_psll_w;
7072 break;
7073 case Intrinsic::x86_mmx_pslli_d:
7074 NewIntNo = Intrinsic::x86_mmx_psll_d;
7075 break;
7076 case Intrinsic::x86_mmx_pslli_q:
7077 NewIntNo = Intrinsic::x86_mmx_psll_q;
7078 break;
7079 case Intrinsic::x86_mmx_psrli_w:
7080 NewIntNo = Intrinsic::x86_mmx_psrl_w;
7081 break;
7082 case Intrinsic::x86_mmx_psrli_d:
7083 NewIntNo = Intrinsic::x86_mmx_psrl_d;
7084 break;
7085 case Intrinsic::x86_mmx_psrli_q:
7086 NewIntNo = Intrinsic::x86_mmx_psrl_q;
7087 break;
7088 case Intrinsic::x86_mmx_psrai_w:
7089 NewIntNo = Intrinsic::x86_mmx_psra_w;
7090 break;
7091 case Intrinsic::x86_mmx_psrai_d:
7092 NewIntNo = Intrinsic::x86_mmx_psra_d;
7093 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00007094 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00007095 }
7096 break;
7097 }
7098 }
Mon P Wangefa42202009-09-03 19:56:25 +00007099
7100 // The vector shift intrinsics with scalars uses 32b shift amounts but
7101 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
7102 // to be zero.
7103 SDValue ShOps[4];
7104 ShOps[0] = ShAmt;
7105 ShOps[1] = DAG.getConstant(0, MVT::i32);
7106 if (ShAmtVT == MVT::v4i32) {
7107 ShOps[2] = DAG.getUNDEF(MVT::i32);
7108 ShOps[3] = DAG.getUNDEF(MVT::i32);
7109 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
7110 } else {
7111 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
7112 }
7113
Owen Andersone50ed302009-08-10 22:56:29 +00007114 EVT VT = Op.getValueType();
Mon P Wangefa42202009-09-03 19:56:25 +00007115 ShAmt = DAG.getNode(ISD::BIT_CONVERT, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007116 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007117 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00007118 Op.getOperand(1), ShAmt);
7119 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00007120 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007121}
Evan Cheng72261582005-12-20 06:22:03 +00007122
Dan Gohmand858e902010-04-17 15:26:15 +00007123SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
7124 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00007125 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7126 MFI->setReturnAddressIsTaken(true);
7127
Bill Wendling64e87322009-01-16 19:25:27 +00007128 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007129 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00007130
7131 if (Depth > 0) {
7132 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
7133 SDValue Offset =
7134 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00007135 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007136 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00007137 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007138 FrameAddr, Offset),
David Greene67c9d422010-02-15 16:53:33 +00007139 NULL, 0, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00007140 }
7141
7142 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00007143 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00007144 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
David Greene67c9d422010-02-15 16:53:33 +00007145 RetAddrFI, NULL, 0, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00007146}
7147
Dan Gohmand858e902010-04-17 15:26:15 +00007148SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00007149 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7150 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00007151
Owen Andersone50ed302009-08-10 22:56:29 +00007152 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007153 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00007154 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
7155 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00007156 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00007157 while (Depth--)
David Greene67c9d422010-02-15 16:53:33 +00007158 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0,
7159 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00007160 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00007161}
7162
Dan Gohman475871a2008-07-27 21:46:04 +00007163SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007164 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00007165 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007166}
7167
Dan Gohmand858e902010-04-17 15:26:15 +00007168SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007169 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00007170 SDValue Chain = Op.getOperand(0);
7171 SDValue Offset = Op.getOperand(1);
7172 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007173 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007174
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007175 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
7176 getPointerTy());
7177 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007178
Dale Johannesene4d209d2009-02-03 20:21:25 +00007179 SDValue StoreAddr = DAG.getNode(ISD::SUB, dl, getPointerTy(), Frame,
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00007180 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007181 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
David Greene67c9d422010-02-15 16:53:33 +00007182 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, NULL, 0, false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00007183 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007184 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007185
Dale Johannesene4d209d2009-02-03 20:21:25 +00007186 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007187 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007188 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007189}
7190
Dan Gohman475871a2008-07-27 21:46:04 +00007191SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007192 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007193 SDValue Root = Op.getOperand(0);
7194 SDValue Trmp = Op.getOperand(1); // trampoline
7195 SDValue FPtr = Op.getOperand(2); // nested function
7196 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007197 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00007198
Dan Gohman69de1932008-02-06 22:27:42 +00007199 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00007200
7201 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00007202 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00007203
7204 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00007205 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
7206 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00007207
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00007208 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
7209 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00007210
7211 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
7212
7213 // Load the pointer to the nested function into R11.
7214 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00007215 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00007216 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
David Greene67c9d422010-02-15 16:53:33 +00007217 Addr, TrmpAddr, 0, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007218
Owen Anderson825b72b2009-08-11 20:47:22 +00007219 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7220 DAG.getConstant(2, MVT::i64));
David Greene67c9d422010-02-15 16:53:33 +00007221 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr, TrmpAddr, 2,
7222 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00007223
7224 // Load the 'nest' parameter value into R10.
7225 // R10 is specified in X86CallingConv.td
7226 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00007227 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7228 DAG.getConstant(10, MVT::i64));
7229 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
David Greene67c9d422010-02-15 16:53:33 +00007230 Addr, TrmpAddr, 10, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007231
Owen Anderson825b72b2009-08-11 20:47:22 +00007232 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7233 DAG.getConstant(12, MVT::i64));
David Greene67c9d422010-02-15 16:53:33 +00007234 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 12,
7235 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00007236
7237 // Jump to the nested function.
7238 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00007239 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7240 DAG.getConstant(20, MVT::i64));
7241 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
David Greene67c9d422010-02-15 16:53:33 +00007242 Addr, TrmpAddr, 20, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007243
7244 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00007245 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7246 DAG.getConstant(22, MVT::i64));
7247 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
David Greene67c9d422010-02-15 16:53:33 +00007248 TrmpAddr, 22, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007249
Dan Gohman475871a2008-07-27 21:46:04 +00007250 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00007251 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007252 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007253 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00007254 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00007255 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00007256 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00007257 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007258
7259 switch (CC) {
7260 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00007261 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00007262 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00007263 case CallingConv::X86_StdCall: {
7264 // Pass 'nest' parameter in ECX.
7265 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00007266 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007267
7268 // Check that ECX wasn't needed by an 'inreg' parameter.
7269 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00007270 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00007271
Chris Lattner58d74912008-03-12 17:45:29 +00007272 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00007273 unsigned InRegCount = 0;
7274 unsigned Idx = 1;
7275
7276 for (FunctionType::param_iterator I = FTy->param_begin(),
7277 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00007278 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00007279 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00007280 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007281
7282 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00007283 report_fatal_error("Nest register in use - reduce number of inreg"
7284 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00007285 }
7286 }
7287 break;
7288 }
7289 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00007290 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00007291 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00007292 // Pass 'nest' parameter in EAX.
7293 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00007294 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007295 break;
7296 }
7297
Dan Gohman475871a2008-07-27 21:46:04 +00007298 SDValue OutChains[4];
7299 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007300
Owen Anderson825b72b2009-08-11 20:47:22 +00007301 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7302 DAG.getConstant(10, MVT::i32));
7303 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007304
Chris Lattnera62fe662010-02-05 19:20:30 +00007305 // This is storing the opcode for MOV32ri.
7306 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00007307 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00007308 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007309 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
David Greene67c9d422010-02-15 16:53:33 +00007310 Trmp, TrmpAddr, 0, false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007311
Owen Anderson825b72b2009-08-11 20:47:22 +00007312 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7313 DAG.getConstant(1, MVT::i32));
David Greene67c9d422010-02-15 16:53:33 +00007314 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 1,
7315 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007316
Chris Lattnera62fe662010-02-05 19:20:30 +00007317 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00007318 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7319 DAG.getConstant(5, MVT::i32));
7320 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
David Greene67c9d422010-02-15 16:53:33 +00007321 TrmpAddr, 5, false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007322
Owen Anderson825b72b2009-08-11 20:47:22 +00007323 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7324 DAG.getConstant(6, MVT::i32));
David Greene67c9d422010-02-15 16:53:33 +00007325 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr, TrmpAddr, 6,
7326 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007327
Dan Gohman475871a2008-07-27 21:46:04 +00007328 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00007329 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007330 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007331 }
7332}
7333
Dan Gohmand858e902010-04-17 15:26:15 +00007334SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
7335 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007336 /*
7337 The rounding mode is in bits 11:10 of FPSR, and has the following
7338 settings:
7339 00 Round to nearest
7340 01 Round to -inf
7341 10 Round to +inf
7342 11 Round to 0
7343
7344 FLT_ROUNDS, on the other hand, expects the following:
7345 -1 Undefined
7346 0 Round to 0
7347 1 Round to nearest
7348 2 Round to +inf
7349 3 Round to -inf
7350
7351 To perform the conversion, we do:
7352 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
7353 */
7354
7355 MachineFunction &MF = DAG.getMachineFunction();
7356 const TargetMachine &TM = MF.getTarget();
7357 const TargetFrameInfo &TFI = *TM.getFrameInfo();
7358 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00007359 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007360 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007361
7362 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00007363 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007364 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007365
Owen Anderson825b72b2009-08-11 20:47:22 +00007366 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, dl, MVT::Other,
Evan Cheng8a186ae2008-09-24 23:26:36 +00007367 DAG.getEntryNode(), StackSlot);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007368
7369 // Load FP Control Word from stack slot
David Greene67c9d422010-02-15 16:53:33 +00007370 SDValue CWD = DAG.getLoad(MVT::i16, dl, Chain, StackSlot, NULL, 0,
7371 false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007372
7373 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00007374 SDValue CWD1 =
Owen Anderson825b72b2009-08-11 20:47:22 +00007375 DAG.getNode(ISD::SRL, dl, MVT::i16,
7376 DAG.getNode(ISD::AND, dl, MVT::i16,
7377 CWD, DAG.getConstant(0x800, MVT::i16)),
7378 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00007379 SDValue CWD2 =
Owen Anderson825b72b2009-08-11 20:47:22 +00007380 DAG.getNode(ISD::SRL, dl, MVT::i16,
7381 DAG.getNode(ISD::AND, dl, MVT::i16,
7382 CWD, DAG.getConstant(0x400, MVT::i16)),
7383 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007384
Dan Gohman475871a2008-07-27 21:46:04 +00007385 SDValue RetVal =
Owen Anderson825b72b2009-08-11 20:47:22 +00007386 DAG.getNode(ISD::AND, dl, MVT::i16,
7387 DAG.getNode(ISD::ADD, dl, MVT::i16,
7388 DAG.getNode(ISD::OR, dl, MVT::i16, CWD1, CWD2),
7389 DAG.getConstant(1, MVT::i16)),
7390 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007391
7392
Duncan Sands83ec4b62008-06-06 12:08:01 +00007393 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007394 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007395}
7396
Dan Gohmand858e902010-04-17 15:26:15 +00007397SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007398 EVT VT = Op.getValueType();
7399 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007400 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007401 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00007402
7403 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007404 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00007405 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00007406 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007407 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007408 }
Evan Cheng18efe262007-12-14 02:13:44 +00007409
Evan Cheng152804e2007-12-14 08:30:15 +00007410 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007411 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007412 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00007413
7414 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007415 SDValue Ops[] = {
7416 Op,
7417 DAG.getConstant(NumBits+NumBits-1, OpVT),
7418 DAG.getConstant(X86::COND_E, MVT::i8),
7419 Op.getValue(1)
7420 };
7421 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00007422
7423 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00007424 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00007425
Owen Anderson825b72b2009-08-11 20:47:22 +00007426 if (VT == MVT::i8)
7427 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007428 return Op;
7429}
7430
Dan Gohmand858e902010-04-17 15:26:15 +00007431SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007432 EVT VT = Op.getValueType();
7433 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007434 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007435 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00007436
7437 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007438 if (VT == MVT::i8) {
7439 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007440 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007441 }
Evan Cheng152804e2007-12-14 08:30:15 +00007442
7443 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007444 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007445 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00007446
7447 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007448 SDValue Ops[] = {
7449 Op,
7450 DAG.getConstant(NumBits, OpVT),
7451 DAG.getConstant(X86::COND_E, MVT::i8),
7452 Op.getValue(1)
7453 };
7454 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00007455
Owen Anderson825b72b2009-08-11 20:47:22 +00007456 if (VT == MVT::i8)
7457 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007458 return Op;
7459}
7460
Dan Gohmand858e902010-04-17 15:26:15 +00007461SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007462 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007463 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007464 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00007465
Mon P Wangaf9b9522008-12-18 21:42:19 +00007466 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
7467 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
7468 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
7469 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
7470 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
7471 //
7472 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
7473 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
7474 // return AloBlo + AloBhi + AhiBlo;
7475
7476 SDValue A = Op.getOperand(0);
7477 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007478
Dale Johannesene4d209d2009-02-03 20:21:25 +00007479 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007480 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7481 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007482 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007483 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7484 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007485 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007486 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007487 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007488 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007489 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007490 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007491 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007492 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007493 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007494 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007495 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7496 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007497 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007498 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7499 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007500 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
7501 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00007502 return Res;
7503}
7504
Nate Begemanbdcb5af2010-07-27 22:37:06 +00007505SDValue X86TargetLowering::LowerSHL(SDValue Op, SelectionDAG &DAG) const {
7506 EVT VT = Op.getValueType();
7507 DebugLoc dl = Op.getDebugLoc();
7508 SDValue R = Op.getOperand(0);
7509
Nate Begemanbdcb5af2010-07-27 22:37:06 +00007510 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00007511
Nate Begeman51409212010-07-28 00:21:48 +00007512 assert(Subtarget->hasSSE41() && "Cannot lower SHL without SSE4.1 or later");
7513
7514 if (VT == MVT::v4i32) {
7515 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
7516 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
7517 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
7518
7519 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
7520
7521 std::vector<Constant*> CV(4, CI);
7522 Constant *C = ConstantVector::get(CV);
7523 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
7524 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
7525 PseudoSourceValue::getConstantPool(), 0,
7526 false, false, 16);
7527
7528 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
7529 Op = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, Op);
7530 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
7531 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
7532 }
7533 if (VT == MVT::v16i8) {
7534 // a = a << 5;
7535 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
7536 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
7537 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
7538
7539 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
7540 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
7541
7542 std::vector<Constant*> CVM1(16, CM1);
7543 std::vector<Constant*> CVM2(16, CM2);
7544 Constant *C = ConstantVector::get(CVM1);
7545 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
7546 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
7547 PseudoSourceValue::getConstantPool(), 0,
7548 false, false, 16);
7549
7550 // r = pblendv(r, psllw(r & (char16)15, 4), a);
7551 M = DAG.getNode(ISD::AND, dl, VT, R, M);
7552 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
7553 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
7554 DAG.getConstant(4, MVT::i32));
7555 R = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
7556 DAG.getConstant(Intrinsic::x86_sse41_pblendvb, MVT::i32),
7557 R, M, Op);
7558 // a += a
7559 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
7560
7561 C = ConstantVector::get(CVM2);
7562 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
7563 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
7564 PseudoSourceValue::getConstantPool(), 0, false, false, 16);
7565
7566 // r = pblendv(r, psllw(r & (char16)63, 2), a);
7567 M = DAG.getNode(ISD::AND, dl, VT, R, M);
7568 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
7569 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
7570 DAG.getConstant(2, MVT::i32));
7571 R = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
7572 DAG.getConstant(Intrinsic::x86_sse41_pblendvb, MVT::i32),
7573 R, M, Op);
7574 // a += a
7575 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
7576
7577 // return pblendv(r, r+r, a);
7578 R = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
7579 DAG.getConstant(Intrinsic::x86_sse41_pblendvb, MVT::i32),
7580 R, DAG.getNode(ISD::ADD, dl, VT, R, R), Op);
7581 return R;
7582 }
7583 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00007584}
Mon P Wangaf9b9522008-12-18 21:42:19 +00007585
Dan Gohmand858e902010-04-17 15:26:15 +00007586SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +00007587 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
7588 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00007589 // looks for this combo and may remove the "setcc" instruction if the "setcc"
7590 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00007591 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00007592 SDValue LHS = N->getOperand(0);
7593 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00007594 unsigned BaseOp = 0;
7595 unsigned Cond = 0;
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007596 DebugLoc dl = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00007597
7598 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007599 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00007600 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00007601 // A subtract of one will be selected as a INC. Note that INC doesn't
7602 // set CF, so we can't do this for UADDO.
7603 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
7604 if (C->getAPIntValue() == 1) {
7605 BaseOp = X86ISD::INC;
7606 Cond = X86::COND_O;
7607 break;
7608 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007609 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00007610 Cond = X86::COND_O;
7611 break;
7612 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007613 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00007614 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007615 break;
7616 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00007617 // A subtract of one will be selected as a DEC. Note that DEC doesn't
7618 // set CF, so we can't do this for USUBO.
7619 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
7620 if (C->getAPIntValue() == 1) {
7621 BaseOp = X86ISD::DEC;
7622 Cond = X86::COND_O;
7623 break;
7624 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007625 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00007626 Cond = X86::COND_O;
7627 break;
7628 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007629 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00007630 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007631 break;
7632 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00007633 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00007634 Cond = X86::COND_O;
7635 break;
7636 case ISD::UMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00007637 BaseOp = X86ISD::UMUL;
Dan Gohman653456c2009-01-07 00:15:08 +00007638 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007639 break;
7640 }
Bill Wendling3fafd932008-11-26 22:37:40 +00007641
Bill Wendling61edeb52008-12-02 01:06:39 +00007642 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007643 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007644 SDValue Sum = DAG.getNode(BaseOp, dl, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00007645
Bill Wendling61edeb52008-12-02 01:06:39 +00007646 SDValue SetCC =
Dale Johannesene4d209d2009-02-03 20:21:25 +00007647 DAG.getNode(X86ISD::SETCC, dl, N->getValueType(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00007648 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00007649
Bill Wendling61edeb52008-12-02 01:06:39 +00007650 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
7651 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00007652}
7653
Eric Christopher9a9d2752010-07-22 02:48:34 +00007654SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
7655 DebugLoc dl = Op.getDebugLoc();
7656
7657 if (!Subtarget->hasSSE2())
7658 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
7659 DAG.getConstant(0, MVT::i32));
7660
7661 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
7662 if(!isDev)
7663 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
7664 else {
7665 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
7666 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
7667 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
7668 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
7669
7670 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
7671 if (!Op1 && !Op2 && !Op3 && Op4)
7672 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
7673
7674 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
7675 if (Op1 && !Op2 && !Op3 && !Op4)
7676 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
7677
7678 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
7679 // (MFENCE)>;
7680 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
7681 }
7682}
7683
Dan Gohmand858e902010-04-17 15:26:15 +00007684SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007685 EVT T = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007686 DebugLoc dl = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00007687 unsigned Reg = 0;
7688 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00007689 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007690 default:
7691 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007692 case MVT::i8: Reg = X86::AL; size = 1; break;
7693 case MVT::i16: Reg = X86::AX; size = 2; break;
7694 case MVT::i32: Reg = X86::EAX; size = 4; break;
7695 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00007696 assert(Subtarget->is64Bit() && "Node not type legal!");
7697 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00007698 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007699 }
Dale Johannesendd64c412009-02-04 00:33:20 +00007700 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), dl, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00007701 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00007702 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00007703 Op.getOperand(1),
7704 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00007705 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00007706 cpIn.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00007707 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007708 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, dl, Tys, Ops, 5);
Scott Michelfdc40a02009-02-17 22:15:04 +00007709 SDValue cpOut =
Dale Johannesendd64c412009-02-04 00:33:20 +00007710 DAG.getCopyFromReg(Result.getValue(0), dl, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00007711 return cpOut;
7712}
7713
Duncan Sands1607f052008-12-01 11:39:25 +00007714SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007715 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +00007716 assert(Subtarget->is64Bit() && "Result not type legalized?");
Owen Anderson825b72b2009-08-11 20:47:22 +00007717 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00007718 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007719 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007720 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007721 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
7722 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00007723 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00007724 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
7725 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00007726 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00007727 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00007728 rdx.getValue(1)
7729 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007730 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007731}
7732
Dale Johannesen7d07b482010-05-21 00:52:33 +00007733SDValue X86TargetLowering::LowerBIT_CONVERT(SDValue Op,
7734 SelectionDAG &DAG) const {
7735 EVT SrcVT = Op.getOperand(0).getValueType();
7736 EVT DstVT = Op.getValueType();
7737 assert((Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
7738 Subtarget->hasMMX() && !DisableMMX) &&
7739 "Unexpected custom BIT_CONVERT");
7740 assert((DstVT == MVT::i64 ||
7741 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
7742 "Unexpected custom BIT_CONVERT");
7743 // i64 <=> MMX conversions are Legal.
7744 if (SrcVT==MVT::i64 && DstVT.isVector())
7745 return Op;
7746 if (DstVT==MVT::i64 && SrcVT.isVector())
7747 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +00007748 // MMX <=> MMX conversions are Legal.
7749 if (SrcVT.isVector() && DstVT.isVector())
7750 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +00007751 // All other conversions need to be expanded.
7752 return SDValue();
7753}
Dan Gohmand858e902010-04-17 15:26:15 +00007754SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +00007755 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007756 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007757 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007758 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00007759 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007760 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007761 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00007762 Node->getOperand(0),
7763 Node->getOperand(1), negOp,
7764 cast<AtomicSDNode>(Node)->getSrcValue(),
7765 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00007766}
7767
Evan Cheng0db9fe62006-04-25 20:13:52 +00007768/// LowerOperation - Provide custom lowering hooks for some operations.
7769///
Dan Gohmand858e902010-04-17 15:26:15 +00007770SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007771 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007772 default: llvm_unreachable("Should not custom lower this!");
Eric Christopher9a9d2752010-07-22 02:48:34 +00007773 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007774 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
7775 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007776 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00007777 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007778 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
7779 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
7780 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
7781 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
7782 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
7783 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007784 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00007785 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007786 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007787 case ISD::SHL_PARTS:
7788 case ISD::SRA_PARTS:
7789 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
7790 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007791 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007792 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00007793 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007794 case ISD::FABS: return LowerFABS(Op, DAG);
7795 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007796 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00007797 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00007798 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00007799 case ISD::SELECT: return LowerSELECT(Op, DAG);
7800 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007801 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007802 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00007803 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00007804 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007805 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00007806 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
7807 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007808 case ISD::FRAME_TO_ARGS_OFFSET:
7809 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007810 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007811 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007812 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00007813 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00007814 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
7815 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00007816 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00007817 case ISD::SHL: return LowerSHL(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00007818 case ISD::SADDO:
7819 case ISD::UADDO:
7820 case ISD::SSUBO:
7821 case ISD::USUBO:
7822 case ISD::SMULO:
7823 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00007824 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Dale Johannesen7d07b482010-05-21 00:52:33 +00007825 case ISD::BIT_CONVERT: return LowerBIT_CONVERT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007826 }
Chris Lattner27a6c732007-11-24 07:07:01 +00007827}
7828
Duncan Sands1607f052008-12-01 11:39:25 +00007829void X86TargetLowering::
7830ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00007831 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007832 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007833 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00007834 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00007835
7836 SDValue Chain = Node->getOperand(0);
7837 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007838 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007839 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007840 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007841 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00007842 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00007843 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00007844 SDValue Result =
7845 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
7846 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00007847 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00007848 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007849 Results.push_back(Result.getValue(2));
7850}
7851
Duncan Sands126d9072008-07-04 11:47:58 +00007852/// ReplaceNodeResults - Replace a node with an illegal result type
7853/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00007854void X86TargetLowering::ReplaceNodeResults(SDNode *N,
7855 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00007856 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007857 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00007858 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00007859 default:
Duncan Sands1607f052008-12-01 11:39:25 +00007860 assert(false && "Do not know how to custom type legalize this operation!");
7861 return;
7862 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00007863 std::pair<SDValue,SDValue> Vals =
7864 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00007865 SDValue FIST = Vals.first, StackSlot = Vals.second;
7866 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00007867 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00007868 // Return a load from the stack slot.
David Greene67c9d422010-02-15 16:53:33 +00007869 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot, NULL, 0,
7870 false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00007871 }
7872 return;
7873 }
7874 case ISD::READCYCLECOUNTER: {
Owen Anderson825b72b2009-08-11 20:47:22 +00007875 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00007876 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007877 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007878 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00007879 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007880 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007881 eax.getValue(2));
7882 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
7883 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00007884 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007885 Results.push_back(edx.getValue(1));
7886 return;
7887 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007888 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00007889 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007890 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00007891 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00007892 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
7893 DAG.getConstant(0, MVT::i32));
7894 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
7895 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00007896 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
7897 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00007898 cpInL.getValue(1));
7899 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00007900 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
7901 DAG.getConstant(0, MVT::i32));
7902 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
7903 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00007904 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00007905 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00007906 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00007907 swapInL.getValue(1));
7908 SDValue Ops[] = { swapInH.getValue(0),
7909 N->getOperand(1),
7910 swapInH.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00007911 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007912 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, dl, Tys, Ops, 3);
Dale Johannesendd64c412009-02-04 00:33:20 +00007913 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00007914 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00007915 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00007916 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00007917 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00007918 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007919 Results.push_back(cpOutH.getValue(1));
7920 return;
7921 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007922 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00007923 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
7924 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007925 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00007926 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
7927 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007928 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00007929 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
7930 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007931 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00007932 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
7933 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007934 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00007935 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
7936 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007937 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00007938 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
7939 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007940 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00007941 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
7942 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00007943 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007944}
7945
Evan Cheng72261582005-12-20 06:22:03 +00007946const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
7947 switch (Opcode) {
7948 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00007949 case X86ISD::BSF: return "X86ISD::BSF";
7950 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00007951 case X86ISD::SHLD: return "X86ISD::SHLD";
7952 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00007953 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00007954 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00007955 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00007956 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00007957 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00007958 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00007959 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
7960 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
7961 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00007962 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00007963 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00007964 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00007965 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00007966 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00007967 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00007968 case X86ISD::COMI: return "X86ISD::COMI";
7969 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00007970 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00007971 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Evan Cheng72261582005-12-20 06:22:03 +00007972 case X86ISD::CMOV: return "X86ISD::CMOV";
7973 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00007974 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00007975 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
7976 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00007977 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00007978 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00007979 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00007980 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00007981 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00007982 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
7983 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00007984 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007985 case X86ISD::MMX_PINSRW: return "X86ISD::MMX_PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00007986 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Evan Cheng8ca29322006-11-10 21:43:37 +00007987 case X86ISD::FMAX: return "X86ISD::FMAX";
7988 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00007989 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
7990 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007991 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +00007992 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Rafael Espindola094fad32009-04-08 21:14:34 +00007993 case X86ISD::SegmentBaseAddress: return "X86ISD::SegmentBaseAddress";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007994 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00007995 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007996 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00007997 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
7998 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007999 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
8000 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
8001 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
8002 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
8003 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
8004 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00008005 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
8006 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00008007 case X86ISD::VSHL: return "X86ISD::VSHL";
8008 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00008009 case X86ISD::CMPPD: return "X86ISD::CMPPD";
8010 case X86ISD::CMPPS: return "X86ISD::CMPPS";
8011 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
8012 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
8013 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
8014 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
8015 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
8016 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
8017 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
8018 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008019 case X86ISD::ADD: return "X86ISD::ADD";
8020 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingd350e022008-12-12 21:15:41 +00008021 case X86ISD::SMUL: return "X86ISD::SMUL";
8022 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00008023 case X86ISD::INC: return "X86ISD::INC";
8024 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00008025 case X86ISD::OR: return "X86ISD::OR";
8026 case X86ISD::XOR: return "X86ISD::XOR";
8027 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00008028 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00008029 case X86ISD::PTEST: return "X86ISD::PTEST";
Dan Gohmand6708ea2009-08-15 01:38:56 +00008030 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008031 case X86ISD::MINGW_ALLOCA: return "X86ISD::MINGW_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00008032 }
8033}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008034
Chris Lattnerc9addb72007-03-30 23:15:24 +00008035// isLegalAddressingMode - Return true if the addressing mode represented
8036// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00008037bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00008038 const Type *Ty) const {
8039 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008040 CodeModel::Model M = getTargetMachine().getCodeModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00008041
Chris Lattnerc9addb72007-03-30 23:15:24 +00008042 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008043 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00008044 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00008045
Chris Lattnerc9addb72007-03-30 23:15:24 +00008046 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00008047 unsigned GVFlags =
8048 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008049
Chris Lattnerdfed4132009-07-10 07:38:24 +00008050 // If a reference to this global requires an extra load, we can't fold it.
8051 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00008052 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008053
Chris Lattnerdfed4132009-07-10 07:38:24 +00008054 // If BaseGV requires a register for the PIC base, we cannot also have a
8055 // BaseReg specified.
8056 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00008057 return false;
Evan Cheng52787842007-08-01 23:46:47 +00008058
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008059 // If lower 4G is not available, then we must use rip-relative addressing.
8060 if (Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
8061 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00008062 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008063
Chris Lattnerc9addb72007-03-30 23:15:24 +00008064 switch (AM.Scale) {
8065 case 0:
8066 case 1:
8067 case 2:
8068 case 4:
8069 case 8:
8070 // These scales always work.
8071 break;
8072 case 3:
8073 case 5:
8074 case 9:
8075 // These scales are formed with basereg+scalereg. Only accept if there is
8076 // no basereg yet.
8077 if (AM.HasBaseReg)
8078 return false;
8079 break;
8080 default: // Other stuff never works.
8081 return false;
8082 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008083
Chris Lattnerc9addb72007-03-30 23:15:24 +00008084 return true;
8085}
8086
8087
Evan Cheng2bd122c2007-10-26 01:56:11 +00008088bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00008089 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00008090 return false;
Evan Chenge127a732007-10-29 07:57:50 +00008091 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
8092 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00008093 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00008094 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00008095 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00008096}
8097
Owen Andersone50ed302009-08-10 22:56:29 +00008098bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008099 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00008100 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008101 unsigned NumBits1 = VT1.getSizeInBits();
8102 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00008103 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00008104 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00008105 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00008106}
Evan Cheng2bd122c2007-10-26 01:56:11 +00008107
Dan Gohman97121ba2009-04-08 00:15:30 +00008108bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00008109 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00008110 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00008111}
8112
Owen Andersone50ed302009-08-10 22:56:29 +00008113bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00008114 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00008115 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00008116}
8117
Owen Andersone50ed302009-08-10 22:56:29 +00008118bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00008119 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00008120 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00008121}
8122
Evan Cheng60c07e12006-07-05 22:17:51 +00008123/// isShuffleMaskLegal - Targets can use this to indicate that they only
8124/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
8125/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
8126/// are assumed to be legal.
8127bool
Eric Christopherfd179292009-08-27 18:07:15 +00008128X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00008129 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +00008130 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +00008131 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +00008132 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +00008133
Nate Begemana09008b2009-10-19 02:17:23 +00008134 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00008135 return (VT.getVectorNumElements() == 2 ||
8136 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
8137 isMOVLMask(M, VT) ||
8138 isSHUFPMask(M, VT) ||
8139 isPSHUFDMask(M, VT) ||
8140 isPSHUFHWMask(M, VT) ||
8141 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00008142 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00008143 isUNPCKLMask(M, VT) ||
8144 isUNPCKHMask(M, VT) ||
8145 isUNPCKL_v_undef_Mask(M, VT) ||
8146 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00008147}
8148
Dan Gohman7d8143f2008-04-09 20:09:42 +00008149bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00008150X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00008151 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00008152 unsigned NumElts = VT.getVectorNumElements();
8153 // FIXME: This collection of masks seems suspect.
8154 if (NumElts == 2)
8155 return true;
8156 if (NumElts == 4 && VT.getSizeInBits() == 128) {
8157 return (isMOVLMask(Mask, VT) ||
8158 isCommutedMOVLMask(Mask, VT, true) ||
8159 isSHUFPMask(Mask, VT) ||
8160 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00008161 }
8162 return false;
8163}
8164
8165//===----------------------------------------------------------------------===//
8166// X86 Scheduler Hooks
8167//===----------------------------------------------------------------------===//
8168
Mon P Wang63307c32008-05-05 19:05:59 +00008169// private utility function
8170MachineBasicBlock *
8171X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
8172 MachineBasicBlock *MBB,
8173 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008174 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008175 unsigned LoadOpc,
8176 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008177 unsigned notOpc,
8178 unsigned EAXreg,
8179 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00008180 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00008181 // For the atomic bitwise operator, we generate
8182 // thisMBB:
8183 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00008184 // ld t1 = [bitinstr.addr]
8185 // op t2 = t1, [bitinstr.val]
8186 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00008187 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
8188 // bz newMBB
8189 // fallthrough -->nextMBB
8190 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8191 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008192 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00008193 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00008194
Mon P Wang63307c32008-05-05 19:05:59 +00008195 /// First build the CFG
8196 MachineFunction *F = MBB->getParent();
8197 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008198 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
8199 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
8200 F->insert(MBBIter, newMBB);
8201 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008202
Dan Gohman14152b42010-07-06 20:24:04 +00008203 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
8204 nextMBB->splice(nextMBB->begin(), thisMBB,
8205 llvm::next(MachineBasicBlock::iterator(bInstr)),
8206 thisMBB->end());
8207 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008208
Mon P Wang63307c32008-05-05 19:05:59 +00008209 // Update thisMBB to fall through to newMBB
8210 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008211
Mon P Wang63307c32008-05-05 19:05:59 +00008212 // newMBB jumps to itself and fall through to nextMBB
8213 newMBB->addSuccessor(nextMBB);
8214 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008215
Mon P Wang63307c32008-05-05 19:05:59 +00008216 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00008217 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00008218 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00008219 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00008220 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00008221 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00008222 int numArgs = bInstr->getNumOperands() - 1;
8223 for (int i=0; i < numArgs; ++i)
8224 argOpers[i] = &bInstr->getOperand(i+1);
8225
8226 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00008227 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008228 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00008229
Dale Johannesen140be2d2008-08-19 18:47:28 +00008230 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008231 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00008232 for (int i=0; i <= lastAddrIndx; ++i)
8233 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008234
Dale Johannesen140be2d2008-08-19 18:47:28 +00008235 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008236 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00008237 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008238 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008239 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008240 tt = t1;
8241
Dale Johannesen140be2d2008-08-19 18:47:28 +00008242 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00008243 assert((argOpers[valArgIndx]->isReg() ||
8244 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00008245 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00008246 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008247 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00008248 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008249 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008250 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00008251 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008252
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00008253 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00008254 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008255
Dale Johannesene4d209d2009-02-03 20:21:25 +00008256 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00008257 for (int i=0; i <= lastAddrIndx; ++i)
8258 (*MIB).addOperand(*argOpers[i]);
8259 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00008260 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00008261 (*MIB).setMemRefs(bInstr->memoperands_begin(),
8262 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00008263
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00008264 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00008265 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00008266
Mon P Wang63307c32008-05-05 19:05:59 +00008267 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008268 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00008269
Dan Gohman14152b42010-07-06 20:24:04 +00008270 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00008271 return nextMBB;
8272}
8273
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00008274// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00008275MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008276X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
8277 MachineBasicBlock *MBB,
8278 unsigned regOpcL,
8279 unsigned regOpcH,
8280 unsigned immOpcL,
8281 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00008282 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008283 // For the atomic bitwise operator, we generate
8284 // thisMBB (instructions are in pairs, except cmpxchg8b)
8285 // ld t1,t2 = [bitinstr.addr]
8286 // newMBB:
8287 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
8288 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00008289 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008290 // mov ECX, EBX <- t5, t6
8291 // mov EAX, EDX <- t1, t2
8292 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
8293 // mov t3, t4 <- EAX, EDX
8294 // bz newMBB
8295 // result in out1, out2
8296 // fallthrough -->nextMBB
8297
8298 const TargetRegisterClass *RC = X86::GR32RegisterClass;
8299 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008300 const unsigned NotOpc = X86::NOT32r;
8301 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8302 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
8303 MachineFunction::iterator MBBIter = MBB;
8304 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00008305
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008306 /// First build the CFG
8307 MachineFunction *F = MBB->getParent();
8308 MachineBasicBlock *thisMBB = MBB;
8309 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
8310 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
8311 F->insert(MBBIter, newMBB);
8312 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008313
Dan Gohman14152b42010-07-06 20:24:04 +00008314 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
8315 nextMBB->splice(nextMBB->begin(), thisMBB,
8316 llvm::next(MachineBasicBlock::iterator(bInstr)),
8317 thisMBB->end());
8318 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008319
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008320 // Update thisMBB to fall through to newMBB
8321 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008322
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008323 // newMBB jumps to itself and fall through to nextMBB
8324 newMBB->addSuccessor(nextMBB);
8325 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008326
Dale Johannesene4d209d2009-02-03 20:21:25 +00008327 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008328 // Insert instructions into newMBB based on incoming instruction
8329 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00008330 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00008331 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008332 MachineOperand& dest1Oper = bInstr->getOperand(0);
8333 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00008334 MachineOperand* argOpers[2 + X86::AddrNumOperands];
8335 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008336 argOpers[i] = &bInstr->getOperand(i+2);
8337
Dan Gohman71ea4e52010-05-14 21:01:44 +00008338 // We use some of the operands multiple times, so conservatively just
8339 // clear any kill flags that might be present.
8340 if (argOpers[i]->isReg() && argOpers[i]->isUse())
8341 argOpers[i]->setIsKill(false);
8342 }
8343
Evan Chengad5b52f2010-01-08 19:14:57 +00008344 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00008345 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00008346
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008347 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008348 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008349 for (int i=0; i <= lastAddrIndx; ++i)
8350 (*MIB).addOperand(*argOpers[i]);
8351 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008352 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00008353 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00008354 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008355 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00008356 MachineOperand newOp3 = *(argOpers[3]);
8357 if (newOp3.isImm())
8358 newOp3.setImm(newOp3.getImm()+4);
8359 else
8360 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008361 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00008362 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008363
8364 // t3/4 are defined later, at the bottom of the loop
8365 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
8366 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008367 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008368 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008369 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008370 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
8371
Evan Cheng306b4ca2010-01-08 23:41:50 +00008372 // The subsequent operations should be using the destination registers of
8373 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00008374 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00008375 t1 = F->getRegInfo().createVirtualRegister(RC);
8376 t2 = F->getRegInfo().createVirtualRegister(RC);
8377 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
8378 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008379 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00008380 t1 = dest1Oper.getReg();
8381 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008382 }
8383
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008384 int valArgIndx = lastAddrIndx + 1;
8385 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00008386 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008387 "invalid operand");
8388 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
8389 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008390 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008391 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008392 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008393 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00008394 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00008395 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008396 (*MIB).addOperand(*argOpers[valArgIndx]);
8397 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00008398 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008399 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00008400 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008401 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008402 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008403 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008404 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00008405 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00008406 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008407 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008408
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00008409 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008410 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00008411 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008412 MIB.addReg(t2);
8413
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00008414 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008415 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00008416 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008417 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00008418
Dale Johannesene4d209d2009-02-03 20:21:25 +00008419 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008420 for (int i=0; i <= lastAddrIndx; ++i)
8421 (*MIB).addOperand(*argOpers[i]);
8422
8423 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00008424 (*MIB).setMemRefs(bInstr->memoperands_begin(),
8425 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008426
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00008427 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008428 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00008429 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008430 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00008431
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008432 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008433 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008434
Dan Gohman14152b42010-07-06 20:24:04 +00008435 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008436 return nextMBB;
8437}
8438
8439// private utility function
8440MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00008441X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
8442 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00008443 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00008444 // For the atomic min/max operator, we generate
8445 // thisMBB:
8446 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00008447 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00008448 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00008449 // cmp t1, t2
8450 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00008451 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00008452 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
8453 // bz newMBB
8454 // fallthrough -->nextMBB
8455 //
8456 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8457 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008458 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00008459 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00008460
Mon P Wang63307c32008-05-05 19:05:59 +00008461 /// First build the CFG
8462 MachineFunction *F = MBB->getParent();
8463 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008464 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
8465 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
8466 F->insert(MBBIter, newMBB);
8467 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008468
Dan Gohman14152b42010-07-06 20:24:04 +00008469 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
8470 nextMBB->splice(nextMBB->begin(), thisMBB,
8471 llvm::next(MachineBasicBlock::iterator(mInstr)),
8472 thisMBB->end());
8473 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008474
Mon P Wang63307c32008-05-05 19:05:59 +00008475 // Update thisMBB to fall through to newMBB
8476 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008477
Mon P Wang63307c32008-05-05 19:05:59 +00008478 // newMBB jumps to newMBB and fall through to nextMBB
8479 newMBB->addSuccessor(nextMBB);
8480 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008481
Dale Johannesene4d209d2009-02-03 20:21:25 +00008482 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00008483 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00008484 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00008485 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00008486 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00008487 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00008488 int numArgs = mInstr->getNumOperands() - 1;
8489 for (int i=0; i < numArgs; ++i)
8490 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008491
Mon P Wang63307c32008-05-05 19:05:59 +00008492 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00008493 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008494 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00008495
Mon P Wangab3e7472008-05-05 22:56:23 +00008496 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008497 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00008498 for (int i=0; i <= lastAddrIndx; ++i)
8499 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00008500
Mon P Wang63307c32008-05-05 19:05:59 +00008501 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00008502 assert((argOpers[valArgIndx]->isReg() ||
8503 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00008504 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00008505
8506 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00008507 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00008508 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00008509 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008510 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00008511 (*MIB).addOperand(*argOpers[valArgIndx]);
8512
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00008513 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00008514 MIB.addReg(t1);
8515
Dale Johannesene4d209d2009-02-03 20:21:25 +00008516 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00008517 MIB.addReg(t1);
8518 MIB.addReg(t2);
8519
8520 // Generate movc
8521 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008522 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00008523 MIB.addReg(t2);
8524 MIB.addReg(t1);
8525
8526 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00008527 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00008528 for (int i=0; i <= lastAddrIndx; ++i)
8529 (*MIB).addOperand(*argOpers[i]);
8530 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00008531 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00008532 (*MIB).setMemRefs(mInstr->memoperands_begin(),
8533 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +00008534
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00008535 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00008536 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00008537
Mon P Wang63307c32008-05-05 19:05:59 +00008538 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008539 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00008540
Dan Gohman14152b42010-07-06 20:24:04 +00008541 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00008542 return nextMBB;
8543}
8544
Eric Christopherf83a5de2009-08-27 18:08:16 +00008545// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
8546// all of this code can be replaced with that in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +00008547MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +00008548X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +00008549 unsigned numArgs, bool memArg) const {
Eric Christopherb120ab42009-08-18 22:50:32 +00008550
Eric Christopherb120ab42009-08-18 22:50:32 +00008551 DebugLoc dl = MI->getDebugLoc();
8552 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8553
8554 unsigned Opc;
Evan Chengce319102009-09-19 09:51:03 +00008555 if (memArg)
8556 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
8557 else
8558 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
Eric Christopherb120ab42009-08-18 22:50:32 +00008559
8560 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(Opc));
8561
8562 for (unsigned i = 0; i < numArgs; ++i) {
8563 MachineOperand &Op = MI->getOperand(i+1);
8564
8565 if (!(Op.isReg() && Op.isImplicit()))
8566 MIB.addOperand(Op);
8567 }
8568
8569 BuildMI(BB, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
8570 .addReg(X86::XMM0);
8571
Dan Gohman14152b42010-07-06 20:24:04 +00008572 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +00008573
8574 return BB;
8575}
8576
8577MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +00008578X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
8579 MachineInstr *MI,
8580 MachineBasicBlock *MBB) const {
8581 // Emit code to save XMM registers to the stack. The ABI says that the
8582 // number of registers to save is given in %al, so it's theoretically
8583 // possible to do an indirect jump trick to avoid saving all of them,
8584 // however this code takes a simpler approach and just executes all
8585 // of the stores if %al is non-zero. It's less code, and it's probably
8586 // easier on the hardware branch predictor, and stores aren't all that
8587 // expensive anyway.
8588
8589 // Create the new basic blocks. One block contains all the XMM stores,
8590 // and one block is the final destination regardless of whether any
8591 // stores were performed.
8592 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
8593 MachineFunction *F = MBB->getParent();
8594 MachineFunction::iterator MBBIter = MBB;
8595 ++MBBIter;
8596 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
8597 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
8598 F->insert(MBBIter, XMMSaveMBB);
8599 F->insert(MBBIter, EndMBB);
8600
Dan Gohman14152b42010-07-06 20:24:04 +00008601 // Transfer the remainder of MBB and its successor edges to EndMBB.
8602 EndMBB->splice(EndMBB->begin(), MBB,
8603 llvm::next(MachineBasicBlock::iterator(MI)),
8604 MBB->end());
8605 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
8606
Dan Gohmand6708ea2009-08-15 01:38:56 +00008607 // The original block will now fall through to the XMM save block.
8608 MBB->addSuccessor(XMMSaveMBB);
8609 // The XMMSaveMBB will fall through to the end block.
8610 XMMSaveMBB->addSuccessor(EndMBB);
8611
8612 // Now add the instructions.
8613 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8614 DebugLoc DL = MI->getDebugLoc();
8615
8616 unsigned CountReg = MI->getOperand(0).getReg();
8617 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
8618 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
8619
8620 if (!Subtarget->isTargetWin64()) {
8621 // If %al is 0, branch around the XMM save block.
8622 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008623 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008624 MBB->addSuccessor(EndMBB);
8625 }
8626
8627 // In the XMM save block, save all the XMM argument registers.
8628 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
8629 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +00008630 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +00008631 F->getMachineMemOperand(
8632 PseudoSourceValue::getFixedStack(RegSaveFrameIndex),
8633 MachineMemOperand::MOStore, Offset,
8634 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008635 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
8636 .addFrameIndex(RegSaveFrameIndex)
8637 .addImm(/*Scale=*/1)
8638 .addReg(/*IndexReg=*/0)
8639 .addImm(/*Disp=*/Offset)
8640 .addReg(/*Segment=*/0)
8641 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +00008642 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008643 }
8644
Dan Gohman14152b42010-07-06 20:24:04 +00008645 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +00008646
8647 return EndMBB;
8648}
Mon P Wang63307c32008-05-05 19:05:59 +00008649
Evan Cheng60c07e12006-07-05 22:17:51 +00008650MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +00008651X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00008652 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +00008653 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8654 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +00008655
Chris Lattner52600972009-09-02 05:57:00 +00008656 // To "insert" a SELECT_CC instruction, we actually have to insert the
8657 // diamond control-flow pattern. The incoming instruction knows the
8658 // destination vreg to set, the condition code register to branch on, the
8659 // true/false values to select between, and a branch opcode to use.
8660 const BasicBlock *LLVM_BB = BB->getBasicBlock();
8661 MachineFunction::iterator It = BB;
8662 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +00008663
Chris Lattner52600972009-09-02 05:57:00 +00008664 // thisMBB:
8665 // ...
8666 // TrueVal = ...
8667 // cmpTY ccX, r1, r2
8668 // bCC copy1MBB
8669 // fallthrough --> copy0MBB
8670 MachineBasicBlock *thisMBB = BB;
8671 MachineFunction *F = BB->getParent();
8672 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
8673 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +00008674 F->insert(It, copy0MBB);
8675 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +00008676
Bill Wendling730c07e2010-06-25 20:48:10 +00008677 // If the EFLAGS register isn't dead in the terminator, then claim that it's
8678 // live into the sink and copy blocks.
8679 const MachineFunction *MF = BB->getParent();
8680 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
8681 BitVector ReservedRegs = TRI->getReservedRegs(*MF);
Bill Wendling730c07e2010-06-25 20:48:10 +00008682
Dan Gohman14152b42010-07-06 20:24:04 +00008683 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
8684 const MachineOperand &MO = MI->getOperand(I);
8685 if (!MO.isReg() || !MO.isUse() || MO.isKill()) continue;
Bill Wendling730c07e2010-06-25 20:48:10 +00008686 unsigned Reg = MO.getReg();
8687 if (Reg != X86::EFLAGS) continue;
8688 copy0MBB->addLiveIn(Reg);
8689 sinkMBB->addLiveIn(Reg);
8690 }
8691
Dan Gohman14152b42010-07-06 20:24:04 +00008692 // Transfer the remainder of BB and its successor edges to sinkMBB.
8693 sinkMBB->splice(sinkMBB->begin(), BB,
8694 llvm::next(MachineBasicBlock::iterator(MI)),
8695 BB->end());
8696 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
8697
8698 // Add the true and fallthrough blocks as its successors.
8699 BB->addSuccessor(copy0MBB);
8700 BB->addSuccessor(sinkMBB);
8701
8702 // Create the conditional branch instruction.
8703 unsigned Opc =
8704 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
8705 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
8706
Chris Lattner52600972009-09-02 05:57:00 +00008707 // copy0MBB:
8708 // %FalseValue = ...
8709 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +00008710 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00008711
Chris Lattner52600972009-09-02 05:57:00 +00008712 // sinkMBB:
8713 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
8714 // ...
Dan Gohman14152b42010-07-06 20:24:04 +00008715 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
8716 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +00008717 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
8718 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
8719
Dan Gohman14152b42010-07-06 20:24:04 +00008720 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +00008721 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +00008722}
8723
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008724MachineBasicBlock *
8725X86TargetLowering::EmitLoweredMingwAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00008726 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008727 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8728 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008729
8730 // The lowering is pretty easy: we're just emitting the call to _alloca. The
8731 // non-trivial part is impdef of ESP.
8732 // FIXME: The code should be tweaked as soon as we'll try to do codegen for
8733 // mingw-w64.
8734
Dan Gohman14152b42010-07-06 20:24:04 +00008735 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008736 .addExternalSymbol("_alloca")
8737 .addReg(X86::EAX, RegState::Implicit)
8738 .addReg(X86::ESP, RegState::Implicit)
8739 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
8740 .addReg(X86::ESP, RegState::Define | RegState::Implicit);
8741
Dan Gohman14152b42010-07-06 20:24:04 +00008742 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008743 return BB;
8744}
Chris Lattner52600972009-09-02 05:57:00 +00008745
8746MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +00008747X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
8748 MachineBasicBlock *BB) const {
8749 // This is pretty easy. We're taking the value that we received from
8750 // our load from the relocation, sticking it in either RDI (x86-64)
8751 // or EAX and doing an indirect call. The return value will then
8752 // be in the normal return register.
Eric Christopher54415362010-06-08 22:04:25 +00008753 const X86InstrInfo *TII
8754 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +00008755 DebugLoc DL = MI->getDebugLoc();
8756 MachineFunction *F = BB->getParent();
8757
Eric Christopher54415362010-06-08 22:04:25 +00008758 assert(MI->getOperand(3).isGlobal() && "This should be a global");
8759
Eric Christopher30ef0e52010-06-03 04:07:48 +00008760 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +00008761 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
8762 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +00008763 .addReg(X86::RIP)
8764 .addImm(0).addReg(0)
8765 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
8766 MI->getOperand(3).getTargetFlags())
8767 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +00008768 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +00008769 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +00008770 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +00008771 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
8772 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +00008773 .addReg(0)
8774 .addImm(0).addReg(0)
8775 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
8776 MI->getOperand(3).getTargetFlags())
8777 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +00008778 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +00008779 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +00008780 } else {
Dan Gohman14152b42010-07-06 20:24:04 +00008781 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
8782 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +00008783 .addReg(TII->getGlobalBaseReg(F))
8784 .addImm(0).addReg(0)
8785 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
8786 MI->getOperand(3).getTargetFlags())
8787 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +00008788 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +00008789 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +00008790 }
8791
Dan Gohman14152b42010-07-06 20:24:04 +00008792 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +00008793 return BB;
8794}
8795
8796MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00008797X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00008798 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00008799 switch (MI->getOpcode()) {
8800 default: assert(false && "Unexpected instr type to insert");
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008801 case X86::MINGW_ALLOCA:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00008802 return EmitLoweredMingwAlloca(MI, BB);
Eric Christopher30ef0e52010-06-03 04:07:48 +00008803 case X86::TLSCall_32:
8804 case X86::TLSCall_64:
8805 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +00008806 case X86::CMOV_GR8:
Mon P Wang9e5ecb82008-12-12 01:25:51 +00008807 case X86::CMOV_V1I64:
Evan Cheng60c07e12006-07-05 22:17:51 +00008808 case X86::CMOV_FR32:
8809 case X86::CMOV_FR64:
8810 case X86::CMOV_V4F32:
8811 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +00008812 case X86::CMOV_V2I64:
Chris Lattner314a1132010-03-14 18:31:44 +00008813 case X86::CMOV_GR16:
8814 case X86::CMOV_GR32:
8815 case X86::CMOV_RFP32:
8816 case X86::CMOV_RFP64:
8817 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00008818 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +00008819
Dale Johannesen849f2142007-07-03 00:53:03 +00008820 case X86::FP32_TO_INT16_IN_MEM:
8821 case X86::FP32_TO_INT32_IN_MEM:
8822 case X86::FP32_TO_INT64_IN_MEM:
8823 case X86::FP64_TO_INT16_IN_MEM:
8824 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00008825 case X86::FP64_TO_INT64_IN_MEM:
8826 case X86::FP80_TO_INT16_IN_MEM:
8827 case X86::FP80_TO_INT32_IN_MEM:
8828 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +00008829 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8830 DebugLoc DL = MI->getDebugLoc();
8831
Evan Cheng60c07e12006-07-05 22:17:51 +00008832 // Change the floating point control register to use "round towards zero"
8833 // mode when truncating to an integer value.
8834 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +00008835 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +00008836 addFrameReference(BuildMI(*BB, MI, DL,
8837 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008838
8839 // Load the old value of the high byte of the control word...
8840 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +00008841 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +00008842 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008843 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008844
8845 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +00008846 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00008847 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00008848
8849 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +00008850 addFrameReference(BuildMI(*BB, MI, DL,
8851 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008852
8853 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +00008854 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00008855 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00008856
8857 // Get the X86 opcode to use.
8858 unsigned Opc;
8859 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008860 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00008861 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
8862 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
8863 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
8864 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
8865 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
8866 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00008867 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
8868 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
8869 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00008870 }
8871
8872 X86AddressMode AM;
8873 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +00008874 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00008875 AM.BaseType = X86AddressMode::RegBase;
8876 AM.Base.Reg = Op.getReg();
8877 } else {
8878 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +00008879 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +00008880 }
8881 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +00008882 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00008883 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008884 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +00008885 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00008886 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008887 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +00008888 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00008889 AM.GV = Op.getGlobal();
8890 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00008891 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008892 }
Dan Gohman14152b42010-07-06 20:24:04 +00008893 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00008894 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00008895
8896 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +00008897 addFrameReference(BuildMI(*BB, MI, DL,
8898 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008899
Dan Gohman14152b42010-07-06 20:24:04 +00008900 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00008901 return BB;
8902 }
Eric Christopherb120ab42009-08-18 22:50:32 +00008903 // String/text processing lowering.
8904 case X86::PCMPISTRM128REG:
8905 return EmitPCMP(MI, BB, 3, false /* in-mem */);
8906 case X86::PCMPISTRM128MEM:
8907 return EmitPCMP(MI, BB, 3, true /* in-mem */);
8908 case X86::PCMPESTRM128REG:
8909 return EmitPCMP(MI, BB, 5, false /* in mem */);
8910 case X86::PCMPESTRM128MEM:
8911 return EmitPCMP(MI, BB, 5, true /* in mem */);
8912
8913 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +00008914 case X86::ATOMAND32:
8915 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008916 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00008917 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008918 X86::NOT32r, X86::EAX,
8919 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00008920 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +00008921 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
8922 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00008923 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008924 X86::NOT32r, X86::EAX,
8925 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00008926 case X86::ATOMXOR32:
8927 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008928 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00008929 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008930 X86::NOT32r, X86::EAX,
8931 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008932 case X86::ATOMNAND32:
8933 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008934 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00008935 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008936 X86::NOT32r, X86::EAX,
8937 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +00008938 case X86::ATOMMIN32:
8939 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
8940 case X86::ATOMMAX32:
8941 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
8942 case X86::ATOMUMIN32:
8943 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
8944 case X86::ATOMUMAX32:
8945 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +00008946
8947 case X86::ATOMAND16:
8948 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
8949 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00008950 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008951 X86::NOT16r, X86::AX,
8952 X86::GR16RegisterClass);
8953 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +00008954 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008955 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00008956 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008957 X86::NOT16r, X86::AX,
8958 X86::GR16RegisterClass);
8959 case X86::ATOMXOR16:
8960 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
8961 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00008962 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008963 X86::NOT16r, X86::AX,
8964 X86::GR16RegisterClass);
8965 case X86::ATOMNAND16:
8966 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
8967 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00008968 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008969 X86::NOT16r, X86::AX,
8970 X86::GR16RegisterClass, true);
8971 case X86::ATOMMIN16:
8972 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
8973 case X86::ATOMMAX16:
8974 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
8975 case X86::ATOMUMIN16:
8976 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
8977 case X86::ATOMUMAX16:
8978 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
8979
8980 case X86::ATOMAND8:
8981 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
8982 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00008983 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008984 X86::NOT8r, X86::AL,
8985 X86::GR8RegisterClass);
8986 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +00008987 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008988 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00008989 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008990 X86::NOT8r, X86::AL,
8991 X86::GR8RegisterClass);
8992 case X86::ATOMXOR8:
8993 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
8994 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00008995 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008996 X86::NOT8r, X86::AL,
8997 X86::GR8RegisterClass);
8998 case X86::ATOMNAND8:
8999 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
9000 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009001 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009002 X86::NOT8r, X86::AL,
9003 X86::GR8RegisterClass, true);
9004 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009005 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +00009006 case X86::ATOMAND64:
9007 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00009008 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009009 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +00009010 X86::NOT64r, X86::RAX,
9011 X86::GR64RegisterClass);
9012 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +00009013 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
9014 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009015 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +00009016 X86::NOT64r, X86::RAX,
9017 X86::GR64RegisterClass);
9018 case X86::ATOMXOR64:
9019 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00009020 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009021 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +00009022 X86::NOT64r, X86::RAX,
9023 X86::GR64RegisterClass);
9024 case X86::ATOMNAND64:
9025 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
9026 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009027 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +00009028 X86::NOT64r, X86::RAX,
9029 X86::GR64RegisterClass, true);
9030 case X86::ATOMMIN64:
9031 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
9032 case X86::ATOMMAX64:
9033 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
9034 case X86::ATOMUMIN64:
9035 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
9036 case X86::ATOMUMAX64:
9037 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009038
9039 // This group does 64-bit operations on a 32-bit host.
9040 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00009041 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009042 X86::AND32rr, X86::AND32rr,
9043 X86::AND32ri, X86::AND32ri,
9044 false);
9045 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00009046 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009047 X86::OR32rr, X86::OR32rr,
9048 X86::OR32ri, X86::OR32ri,
9049 false);
9050 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00009051 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009052 X86::XOR32rr, X86::XOR32rr,
9053 X86::XOR32ri, X86::XOR32ri,
9054 false);
9055 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00009056 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009057 X86::AND32rr, X86::AND32rr,
9058 X86::AND32ri, X86::AND32ri,
9059 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009060 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00009061 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009062 X86::ADD32rr, X86::ADC32rr,
9063 X86::ADD32ri, X86::ADC32ri,
9064 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009065 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00009066 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009067 X86::SUB32rr, X86::SBB32rr,
9068 X86::SUB32ri, X86::SBB32ri,
9069 false);
Dale Johannesen880ae362008-10-03 22:25:52 +00009070 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00009071 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +00009072 X86::MOV32rr, X86::MOV32rr,
9073 X86::MOV32ri, X86::MOV32ri,
9074 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +00009075 case X86::VASTART_SAVE_XMM_REGS:
9076 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +00009077 }
9078}
9079
9080//===----------------------------------------------------------------------===//
9081// X86 Optimization Hooks
9082//===----------------------------------------------------------------------===//
9083
Dan Gohman475871a2008-07-27 21:46:04 +00009084void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00009085 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00009086 APInt &KnownZero,
9087 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00009088 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00009089 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009090 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00009091 assert((Opc >= ISD::BUILTIN_OP_END ||
9092 Opc == ISD::INTRINSIC_WO_CHAIN ||
9093 Opc == ISD::INTRINSIC_W_CHAIN ||
9094 Opc == ISD::INTRINSIC_VOID) &&
9095 "Should use MaskedValueIsZero if you don't know whether Op"
9096 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009097
Dan Gohmanf4f92f52008-02-13 23:07:24 +00009098 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009099 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00009100 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +00009101 case X86ISD::ADD:
9102 case X86ISD::SUB:
9103 case X86ISD::SMUL:
9104 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +00009105 case X86ISD::INC:
9106 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +00009107 case X86ISD::OR:
9108 case X86ISD::XOR:
9109 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +00009110 // These nodes' second result is a boolean.
9111 if (Op.getResNo() == 0)
9112 break;
9113 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009114 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00009115 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
9116 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +00009117 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009118 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009119}
Chris Lattner259e97c2006-01-31 19:43:35 +00009120
Evan Cheng206ee9d2006-07-07 08:33:52 +00009121/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +00009122/// node is a GlobalAddress + offset.
9123bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +00009124 const GlobalValue* &GA,
9125 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +00009126 if (N->getOpcode() == X86ISD::Wrapper) {
9127 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00009128 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00009129 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +00009130 return true;
9131 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00009132 }
Evan Chengad4196b2008-05-12 19:56:52 +00009133 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +00009134}
9135
Evan Cheng206ee9d2006-07-07 08:33:52 +00009136/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
9137/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
9138/// if the load addresses are consecutive, non-overlapping, and in the right
Nate Begemanfdea31a2010-03-24 20:49:50 +00009139/// order.
Dan Gohman475871a2008-07-27 21:46:04 +00009140static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00009141 const TargetLowering &TLI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009142 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00009143 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00009144 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
Mon P Wang1e955802009-04-03 02:43:30 +00009145
Eli Friedman7a5e5552009-06-07 06:52:44 +00009146 if (VT.getSizeInBits() != 128)
9147 return SDValue();
9148
Nate Begemanfdea31a2010-03-24 20:49:50 +00009149 SmallVector<SDValue, 16> Elts;
9150 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
9151 Elts.push_back(DAG.getShuffleScalarElt(SVN, i));
9152
9153 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00009154}
Evan Chengd880b972008-05-09 21:53:03 +00009155
Dan Gohman1bbf72b2010-03-15 23:23:03 +00009156/// PerformShuffleCombine - Detect vector gather/scatter index generation
9157/// and convert it from being a bunch of shuffles and extracts to a simple
9158/// store and scalar loads to extract the elements.
9159static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
9160 const TargetLowering &TLI) {
9161 SDValue InputVector = N->getOperand(0);
9162
9163 // Only operate on vectors of 4 elements, where the alternative shuffling
9164 // gets to be more expensive.
9165 if (InputVector.getValueType() != MVT::v4i32)
9166 return SDValue();
9167
9168 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
9169 // single use which is a sign-extend or zero-extend, and all elements are
9170 // used.
9171 SmallVector<SDNode *, 4> Uses;
9172 unsigned ExtractedElements = 0;
9173 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
9174 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
9175 if (UI.getUse().getResNo() != InputVector.getResNo())
9176 return SDValue();
9177
9178 SDNode *Extract = *UI;
9179 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
9180 return SDValue();
9181
9182 if (Extract->getValueType(0) != MVT::i32)
9183 return SDValue();
9184 if (!Extract->hasOneUse())
9185 return SDValue();
9186 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
9187 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
9188 return SDValue();
9189 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
9190 return SDValue();
9191
9192 // Record which element was extracted.
9193 ExtractedElements |=
9194 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
9195
9196 Uses.push_back(Extract);
9197 }
9198
9199 // If not all the elements were used, this may not be worthwhile.
9200 if (ExtractedElements != 15)
9201 return SDValue();
9202
9203 // Ok, we've now decided to do the transformation.
9204 DebugLoc dl = InputVector.getDebugLoc();
9205
9206 // Store the value to a temporary stack slot.
9207 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Eric Christopher90eb4022010-07-22 00:26:08 +00009208 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr, NULL,
9209 0, false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00009210
9211 // Replace each use (extract) with a load of the appropriate element.
9212 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
9213 UE = Uses.end(); UI != UE; ++UI) {
9214 SDNode *Extract = *UI;
9215
9216 // Compute the element's address.
9217 SDValue Idx = Extract->getOperand(1);
9218 unsigned EltSize =
9219 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
9220 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
9221 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
9222
Eric Christopher90eb4022010-07-22 00:26:08 +00009223 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(),
9224 OffsetVal, StackPtr);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00009225
9226 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +00009227 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
9228 ScalarAddr, NULL, 0, false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00009229
9230 // Replace the exact with the load.
9231 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
9232 }
9233
9234 // The replacement was made in place; don't return anything.
9235 return SDValue();
9236}
9237
Chris Lattner83e6c992006-10-04 06:57:07 +00009238/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009239static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +00009240 const X86Subtarget *Subtarget) {
9241 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009242 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +00009243 // Get the LHS/RHS of the select.
9244 SDValue LHS = N->getOperand(1);
9245 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +00009246
Dan Gohman670e5392009-09-21 18:03:22 +00009247 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +00009248 // instructions match the semantics of the common C idiom x<y?x:y but not
9249 // x<=y?x:y, because of how they handle negative zero (which can be
9250 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +00009251 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00009252 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +00009253 Cond.getOpcode() == ISD::SETCC) {
9254 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009255
Chris Lattner47b4ce82009-03-11 05:48:52 +00009256 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +00009257 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +00009258 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
9259 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +00009260 switch (CC) {
9261 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +00009262 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +00009263 // Converting this to a min would handle NaNs incorrectly, and swapping
9264 // the operands would cause it to handle comparisons between positive
9265 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +00009266 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +00009267 if (!UnsafeFPMath &&
9268 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9269 break;
9270 std::swap(LHS, RHS);
9271 }
Dan Gohman670e5392009-09-21 18:03:22 +00009272 Opcode = X86ISD::FMIN;
9273 break;
9274 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +00009275 // Converting this to a min would handle comparisons between positive
9276 // and negative zero incorrectly.
9277 if (!UnsafeFPMath &&
9278 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
9279 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009280 Opcode = X86ISD::FMIN;
9281 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +00009282 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +00009283 // Converting this to a min would handle both negative zeros and NaNs
9284 // incorrectly, but we can swap the operands to fix both.
9285 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00009286 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009287 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +00009288 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009289 Opcode = X86ISD::FMIN;
9290 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009291
Dan Gohman670e5392009-09-21 18:03:22 +00009292 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +00009293 // Converting this to a max would handle comparisons between positive
9294 // and negative zero incorrectly.
9295 if (!UnsafeFPMath &&
9296 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
9297 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009298 Opcode = X86ISD::FMAX;
9299 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +00009300 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +00009301 // Converting this to a max would handle NaNs incorrectly, and swapping
9302 // the operands would cause it to handle comparisons between positive
9303 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +00009304 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +00009305 if (!UnsafeFPMath &&
9306 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9307 break;
9308 std::swap(LHS, RHS);
9309 }
Dan Gohman670e5392009-09-21 18:03:22 +00009310 Opcode = X86ISD::FMAX;
9311 break;
9312 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +00009313 // Converting this to a max would handle both negative zeros and NaNs
9314 // incorrectly, but we can swap the operands to fix both.
9315 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00009316 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009317 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009318 case ISD::SETGE:
9319 Opcode = X86ISD::FMAX;
9320 break;
Chris Lattner83e6c992006-10-04 06:57:07 +00009321 }
Dan Gohman670e5392009-09-21 18:03:22 +00009322 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +00009323 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
9324 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +00009325 switch (CC) {
9326 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +00009327 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +00009328 // Converting this to a min would handle comparisons between positive
9329 // and negative zero incorrectly, and swapping the operands would
9330 // cause it to handle NaNs incorrectly.
9331 if (!UnsafeFPMath &&
9332 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +00009333 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +00009334 break;
9335 std::swap(LHS, RHS);
9336 }
Dan Gohman670e5392009-09-21 18:03:22 +00009337 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +00009338 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009339 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +00009340 // Converting this to a min would handle NaNs incorrectly.
9341 if (!UnsafeFPMath &&
9342 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
9343 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009344 Opcode = X86ISD::FMIN;
9345 break;
9346 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +00009347 // Converting this to a min would handle both negative zeros and NaNs
9348 // incorrectly, but we can swap the operands to fix both.
9349 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00009350 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009351 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009352 case ISD::SETGE:
9353 Opcode = X86ISD::FMIN;
9354 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009355
Dan Gohman670e5392009-09-21 18:03:22 +00009356 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +00009357 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +00009358 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +00009359 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009360 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +00009361 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009362 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +00009363 // Converting this to a max would handle comparisons between positive
9364 // and negative zero incorrectly, and swapping the operands would
9365 // cause it to handle NaNs incorrectly.
9366 if (!UnsafeFPMath &&
9367 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +00009368 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +00009369 break;
9370 std::swap(LHS, RHS);
9371 }
Dan Gohman670e5392009-09-21 18:03:22 +00009372 Opcode = X86ISD::FMAX;
9373 break;
9374 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +00009375 // Converting this to a max would handle both negative zeros and NaNs
9376 // incorrectly, but we can swap the operands to fix both.
9377 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00009378 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009379 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +00009380 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009381 Opcode = X86ISD::FMAX;
9382 break;
9383 }
Chris Lattner83e6c992006-10-04 06:57:07 +00009384 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009385
Chris Lattner47b4ce82009-03-11 05:48:52 +00009386 if (Opcode)
9387 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +00009388 }
Eric Christopherfd179292009-08-27 18:07:15 +00009389
Chris Lattnerd1980a52009-03-12 06:52:53 +00009390 // If this is a select between two integer constants, try to do some
9391 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +00009392 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
9393 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +00009394 // Don't do this for crazy integer types.
9395 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
9396 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +00009397 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +00009398 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +00009399
Chris Lattnercee56e72009-03-13 05:53:31 +00009400 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +00009401 // Efficiently invertible.
9402 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
9403 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
9404 isa<ConstantSDNode>(Cond.getOperand(1))))) {
9405 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +00009406 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +00009407 }
Eric Christopherfd179292009-08-27 18:07:15 +00009408
Chris Lattnerd1980a52009-03-12 06:52:53 +00009409 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00009410 if (FalseC->getAPIntValue() == 0 &&
9411 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00009412 if (NeedsCondInvert) // Invert the condition if needed.
9413 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
9414 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009415
Chris Lattnerd1980a52009-03-12 06:52:53 +00009416 // Zero extend the condition if needed.
9417 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009418
Chris Lattnercee56e72009-03-13 05:53:31 +00009419 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +00009420 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +00009421 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +00009422 }
Eric Christopherfd179292009-08-27 18:07:15 +00009423
Chris Lattner97a29a52009-03-13 05:22:11 +00009424 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +00009425 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +00009426 if (NeedsCondInvert) // Invert the condition if needed.
9427 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
9428 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009429
Chris Lattner97a29a52009-03-13 05:22:11 +00009430 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00009431 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
9432 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00009433 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +00009434 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +00009435 }
Eric Christopherfd179292009-08-27 18:07:15 +00009436
Chris Lattnercee56e72009-03-13 05:53:31 +00009437 // Optimize cases that will turn into an LEA instruction. This requires
9438 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +00009439 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +00009440 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00009441 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +00009442
Chris Lattnercee56e72009-03-13 05:53:31 +00009443 bool isFastMultiplier = false;
9444 if (Diff < 10) {
9445 switch ((unsigned char)Diff) {
9446 default: break;
9447 case 1: // result = add base, cond
9448 case 2: // result = lea base( , cond*2)
9449 case 3: // result = lea base(cond, cond*2)
9450 case 4: // result = lea base( , cond*4)
9451 case 5: // result = lea base(cond, cond*4)
9452 case 8: // result = lea base( , cond*8)
9453 case 9: // result = lea base(cond, cond*8)
9454 isFastMultiplier = true;
9455 break;
9456 }
9457 }
Eric Christopherfd179292009-08-27 18:07:15 +00009458
Chris Lattnercee56e72009-03-13 05:53:31 +00009459 if (isFastMultiplier) {
9460 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
9461 if (NeedsCondInvert) // Invert the condition if needed.
9462 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
9463 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009464
Chris Lattnercee56e72009-03-13 05:53:31 +00009465 // Zero extend the condition if needed.
9466 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
9467 Cond);
9468 // Scale the condition by the difference.
9469 if (Diff != 1)
9470 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
9471 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009472
Chris Lattnercee56e72009-03-13 05:53:31 +00009473 // Add the base if non-zero.
9474 if (FalseC->getAPIntValue() != 0)
9475 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9476 SDValue(FalseC, 0));
9477 return Cond;
9478 }
Eric Christopherfd179292009-08-27 18:07:15 +00009479 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00009480 }
9481 }
Eric Christopherfd179292009-08-27 18:07:15 +00009482
Dan Gohman475871a2008-07-27 21:46:04 +00009483 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +00009484}
9485
Chris Lattnerd1980a52009-03-12 06:52:53 +00009486/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
9487static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
9488 TargetLowering::DAGCombinerInfo &DCI) {
9489 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +00009490
Chris Lattnerd1980a52009-03-12 06:52:53 +00009491 // If the flag operand isn't dead, don't touch this CMOV.
9492 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
9493 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +00009494
Chris Lattnerd1980a52009-03-12 06:52:53 +00009495 // If this is a select between two integer constants, try to do some
9496 // optimizations. Note that the operands are ordered the opposite of SELECT
9497 // operands.
9498 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
9499 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
9500 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
9501 // larger than FalseC (the false value).
9502 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
Eric Christopherfd179292009-08-27 18:07:15 +00009503
Chris Lattnerd1980a52009-03-12 06:52:53 +00009504 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
9505 CC = X86::GetOppositeBranchCondition(CC);
9506 std::swap(TrueC, FalseC);
9507 }
Eric Christopherfd179292009-08-27 18:07:15 +00009508
Chris Lattnerd1980a52009-03-12 06:52:53 +00009509 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00009510 // This is efficient for any integer data type (including i8/i16) and
9511 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +00009512 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
9513 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00009514 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9515 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009516
Chris Lattnerd1980a52009-03-12 06:52:53 +00009517 // Zero extend the condition if needed.
9518 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009519
Chris Lattnerd1980a52009-03-12 06:52:53 +00009520 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
9521 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +00009522 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +00009523 if (N->getNumValues() == 2) // Dead flag value?
9524 return DCI.CombineTo(N, Cond, SDValue());
9525 return Cond;
9526 }
Eric Christopherfd179292009-08-27 18:07:15 +00009527
Chris Lattnercee56e72009-03-13 05:53:31 +00009528 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
9529 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +00009530 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
9531 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00009532 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9533 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009534
Chris Lattner97a29a52009-03-13 05:22:11 +00009535 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00009536 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
9537 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00009538 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9539 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +00009540
Chris Lattner97a29a52009-03-13 05:22:11 +00009541 if (N->getNumValues() == 2) // Dead flag value?
9542 return DCI.CombineTo(N, Cond, SDValue());
9543 return Cond;
9544 }
Eric Christopherfd179292009-08-27 18:07:15 +00009545
Chris Lattnercee56e72009-03-13 05:53:31 +00009546 // Optimize cases that will turn into an LEA instruction. This requires
9547 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +00009548 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +00009549 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00009550 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +00009551
Chris Lattnercee56e72009-03-13 05:53:31 +00009552 bool isFastMultiplier = false;
9553 if (Diff < 10) {
9554 switch ((unsigned char)Diff) {
9555 default: break;
9556 case 1: // result = add base, cond
9557 case 2: // result = lea base( , cond*2)
9558 case 3: // result = lea base(cond, cond*2)
9559 case 4: // result = lea base( , cond*4)
9560 case 5: // result = lea base(cond, cond*4)
9561 case 8: // result = lea base( , cond*8)
9562 case 9: // result = lea base(cond, cond*8)
9563 isFastMultiplier = true;
9564 break;
9565 }
9566 }
Eric Christopherfd179292009-08-27 18:07:15 +00009567
Chris Lattnercee56e72009-03-13 05:53:31 +00009568 if (isFastMultiplier) {
9569 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
9570 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00009571 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9572 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +00009573 // Zero extend the condition if needed.
9574 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
9575 Cond);
9576 // Scale the condition by the difference.
9577 if (Diff != 1)
9578 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
9579 DAG.getConstant(Diff, Cond.getValueType()));
9580
9581 // Add the base if non-zero.
9582 if (FalseC->getAPIntValue() != 0)
9583 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9584 SDValue(FalseC, 0));
9585 if (N->getNumValues() == 2) // Dead flag value?
9586 return DCI.CombineTo(N, Cond, SDValue());
9587 return Cond;
9588 }
Eric Christopherfd179292009-08-27 18:07:15 +00009589 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00009590 }
9591 }
9592 return SDValue();
9593}
9594
9595
Evan Cheng0b0cd912009-03-28 05:57:29 +00009596/// PerformMulCombine - Optimize a single multiply with constant into two
9597/// in order to implement it with two cheaper instructions, e.g.
9598/// LEA + SHL, LEA + LEA.
9599static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
9600 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +00009601 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
9602 return SDValue();
9603
Owen Andersone50ed302009-08-10 22:56:29 +00009604 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009605 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +00009606 return SDValue();
9607
9608 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
9609 if (!C)
9610 return SDValue();
9611 uint64_t MulAmt = C->getZExtValue();
9612 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
9613 return SDValue();
9614
9615 uint64_t MulAmt1 = 0;
9616 uint64_t MulAmt2 = 0;
9617 if ((MulAmt % 9) == 0) {
9618 MulAmt1 = 9;
9619 MulAmt2 = MulAmt / 9;
9620 } else if ((MulAmt % 5) == 0) {
9621 MulAmt1 = 5;
9622 MulAmt2 = MulAmt / 5;
9623 } else if ((MulAmt % 3) == 0) {
9624 MulAmt1 = 3;
9625 MulAmt2 = MulAmt / 3;
9626 }
9627 if (MulAmt2 &&
9628 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
9629 DebugLoc DL = N->getDebugLoc();
9630
9631 if (isPowerOf2_64(MulAmt2) &&
9632 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
9633 // If second multiplifer is pow2, issue it first. We want the multiply by
9634 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
9635 // is an add.
9636 std::swap(MulAmt1, MulAmt2);
9637
9638 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +00009639 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +00009640 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00009641 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +00009642 else
Evan Cheng73f24c92009-03-30 21:36:47 +00009643 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +00009644 DAG.getConstant(MulAmt1, VT));
9645
Eric Christopherfd179292009-08-27 18:07:15 +00009646 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +00009647 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +00009648 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +00009649 else
Evan Cheng73f24c92009-03-30 21:36:47 +00009650 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +00009651 DAG.getConstant(MulAmt2, VT));
9652
9653 // Do not add new nodes to DAG combiner worklist.
9654 DCI.CombineTo(N, NewMul, false);
9655 }
9656 return SDValue();
9657}
9658
Evan Chengad9c0a32009-12-15 00:53:42 +00009659static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
9660 SDValue N0 = N->getOperand(0);
9661 SDValue N1 = N->getOperand(1);
9662 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
9663 EVT VT = N0.getValueType();
9664
9665 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
9666 // since the result of setcc_c is all zero's or all ones.
9667 if (N1C && N0.getOpcode() == ISD::AND &&
9668 N0.getOperand(1).getOpcode() == ISD::Constant) {
9669 SDValue N00 = N0.getOperand(0);
9670 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
9671 ((N00.getOpcode() == ISD::ANY_EXTEND ||
9672 N00.getOpcode() == ISD::ZERO_EXTEND) &&
9673 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
9674 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
9675 APInt ShAmt = N1C->getAPIntValue();
9676 Mask = Mask.shl(ShAmt);
9677 if (Mask != 0)
9678 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
9679 N00, DAG.getConstant(Mask, VT));
9680 }
9681 }
9682
9683 return SDValue();
9684}
Evan Cheng0b0cd912009-03-28 05:57:29 +00009685
Nate Begeman740ab032009-01-26 00:52:55 +00009686/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
9687/// when possible.
9688static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
9689 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +00009690 EVT VT = N->getValueType(0);
9691 if (!VT.isVector() && VT.isInteger() &&
9692 N->getOpcode() == ISD::SHL)
9693 return PerformSHLCombine(N, DAG);
9694
Nate Begeman740ab032009-01-26 00:52:55 +00009695 // On X86 with SSE2 support, we can transform this to a vector shift if
9696 // all elements are shifted by the same amount. We can't do this in legalize
9697 // because the a constant vector is typically transformed to a constant pool
9698 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009699 if (!Subtarget->hasSSE2())
9700 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009701
Owen Anderson825b72b2009-08-11 20:47:22 +00009702 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009703 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009704
Mon P Wang3becd092009-01-28 08:12:05 +00009705 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00009706 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +00009707 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +00009708 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +00009709 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
9710 unsigned NumElts = VT.getVectorNumElements();
9711 unsigned i = 0;
9712 for (; i != NumElts; ++i) {
9713 SDValue Arg = ShAmtOp.getOperand(i);
9714 if (Arg.getOpcode() == ISD::UNDEF) continue;
9715 BaseShAmt = Arg;
9716 break;
9717 }
9718 for (; i != NumElts; ++i) {
9719 SDValue Arg = ShAmtOp.getOperand(i);
9720 if (Arg.getOpcode() == ISD::UNDEF) continue;
9721 if (Arg != BaseShAmt) {
9722 return SDValue();
9723 }
9724 }
9725 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +00009726 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +00009727 SDValue InVec = ShAmtOp.getOperand(0);
9728 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
9729 unsigned NumElts = InVec.getValueType().getVectorNumElements();
9730 unsigned i = 0;
9731 for (; i != NumElts; ++i) {
9732 SDValue Arg = InVec.getOperand(i);
9733 if (Arg.getOpcode() == ISD::UNDEF) continue;
9734 BaseShAmt = Arg;
9735 break;
9736 }
9737 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
9738 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +00009739 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +00009740 if (C->getZExtValue() == SplatIdx)
9741 BaseShAmt = InVec.getOperand(1);
9742 }
9743 }
9744 if (BaseShAmt.getNode() == 0)
9745 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
9746 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +00009747 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009748 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +00009749
Mon P Wangefa42202009-09-03 19:56:25 +00009750 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +00009751 if (EltVT.bitsGT(MVT::i32))
9752 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
9753 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +00009754 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +00009755
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009756 // The shift amount is identical so we can do a vector shift.
9757 SDValue ValOp = N->getOperand(0);
9758 switch (N->getOpcode()) {
9759 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009760 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009761 break;
9762 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +00009763 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009764 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009765 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009766 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009767 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009768 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009769 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009770 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009771 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009772 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009773 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009774 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009775 break;
9776 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +00009777 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009778 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009779 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009780 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009781 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009782 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009783 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009784 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009785 break;
9786 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +00009787 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009788 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009789 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009790 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009791 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009792 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009793 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009794 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009795 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009796 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009797 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009798 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009799 break;
Nate Begeman740ab032009-01-26 00:52:55 +00009800 }
9801 return SDValue();
9802}
9803
Evan Cheng760d1942010-01-04 21:22:48 +00009804static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +00009805 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +00009806 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +00009807 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +00009808 return SDValue();
9809
Evan Cheng760d1942010-01-04 21:22:48 +00009810 EVT VT = N->getValueType(0);
Evan Cheng8b1190a2010-04-28 01:18:01 +00009811 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
Evan Cheng760d1942010-01-04 21:22:48 +00009812 return SDValue();
9813
9814 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
9815 SDValue N0 = N->getOperand(0);
9816 SDValue N1 = N->getOperand(1);
9817 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
9818 std::swap(N0, N1);
9819 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
9820 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +00009821 if (!N0.hasOneUse() || !N1.hasOneUse())
9822 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +00009823
9824 SDValue ShAmt0 = N0.getOperand(1);
9825 if (ShAmt0.getValueType() != MVT::i8)
9826 return SDValue();
9827 SDValue ShAmt1 = N1.getOperand(1);
9828 if (ShAmt1.getValueType() != MVT::i8)
9829 return SDValue();
9830 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
9831 ShAmt0 = ShAmt0.getOperand(0);
9832 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
9833 ShAmt1 = ShAmt1.getOperand(0);
9834
9835 DebugLoc DL = N->getDebugLoc();
9836 unsigned Opc = X86ISD::SHLD;
9837 SDValue Op0 = N0.getOperand(0);
9838 SDValue Op1 = N1.getOperand(0);
9839 if (ShAmt0.getOpcode() == ISD::SUB) {
9840 Opc = X86ISD::SHRD;
9841 std::swap(Op0, Op1);
9842 std::swap(ShAmt0, ShAmt1);
9843 }
9844
Evan Cheng8b1190a2010-04-28 01:18:01 +00009845 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +00009846 if (ShAmt1.getOpcode() == ISD::SUB) {
9847 SDValue Sum = ShAmt1.getOperand(0);
9848 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +00009849 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
9850 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
9851 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
9852 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +00009853 return DAG.getNode(Opc, DL, VT,
9854 Op0, Op1,
9855 DAG.getNode(ISD::TRUNCATE, DL,
9856 MVT::i8, ShAmt0));
9857 }
9858 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
9859 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
9860 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +00009861 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +00009862 return DAG.getNode(Opc, DL, VT,
9863 N0.getOperand(0), N1.getOperand(0),
9864 DAG.getNode(ISD::TRUNCATE, DL,
9865 MVT::i8, ShAmt0));
9866 }
9867
9868 return SDValue();
9869}
9870
Chris Lattner149a4e52008-02-22 02:09:43 +00009871/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009872static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +00009873 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +00009874 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
9875 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +00009876 // A preferable solution to the general problem is to figure out the right
9877 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +00009878
9879 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +00009880 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +00009881 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +00009882 if (VT.getSizeInBits() != 64)
9883 return SDValue();
9884
Devang Patel578efa92009-06-05 21:57:13 +00009885 const Function *F = DAG.getMachineFunction().getFunction();
9886 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +00009887 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +00009888 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +00009889 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +00009890 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +00009891 isa<LoadSDNode>(St->getValue()) &&
9892 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
9893 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00009894 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009895 LoadSDNode *Ld = 0;
9896 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +00009897 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +00009898 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009899 // Must be a store of a load. We currently handle two cases: the load
9900 // is a direct child, and it's under an intervening TokenFactor. It is
9901 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +00009902 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +00009903 Ld = cast<LoadSDNode>(St->getChain());
9904 else if (St->getValue().hasOneUse() &&
9905 ChainVal->getOpcode() == ISD::TokenFactor) {
9906 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +00009907 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +00009908 TokenFactorIndex = i;
9909 Ld = cast<LoadSDNode>(St->getValue());
9910 } else
9911 Ops.push_back(ChainVal->getOperand(i));
9912 }
9913 }
Dale Johannesen079f2a62008-02-25 19:20:14 +00009914
Evan Cheng536e6672009-03-12 05:59:15 +00009915 if (!Ld || !ISD::isNormalLoad(Ld))
9916 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009917
Evan Cheng536e6672009-03-12 05:59:15 +00009918 // If this is not the MMX case, i.e. we are just turning i64 load/store
9919 // into f64 load/store, avoid the transformation if there are multiple
9920 // uses of the loaded value.
9921 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
9922 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009923
Evan Cheng536e6672009-03-12 05:59:15 +00009924 DebugLoc LdDL = Ld->getDebugLoc();
9925 DebugLoc StDL = N->getDebugLoc();
9926 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
9927 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
9928 // pair instead.
9929 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009930 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Evan Cheng536e6672009-03-12 05:59:15 +00009931 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(),
9932 Ld->getBasePtr(), Ld->getSrcValue(),
9933 Ld->getSrcValueOffset(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +00009934 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +00009935 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +00009936 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +00009937 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +00009938 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +00009939 Ops.size());
9940 }
Evan Cheng536e6672009-03-12 05:59:15 +00009941 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner149a4e52008-02-22 02:09:43 +00009942 St->getSrcValue(), St->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00009943 St->isVolatile(), St->isNonTemporal(),
9944 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +00009945 }
Evan Cheng536e6672009-03-12 05:59:15 +00009946
9947 // Otherwise, lower to two pairs of 32-bit loads / stores.
9948 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +00009949 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
9950 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +00009951
Owen Anderson825b72b2009-08-11 20:47:22 +00009952 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Evan Cheng536e6672009-03-12 05:59:15 +00009953 Ld->getSrcValue(), Ld->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00009954 Ld->isVolatile(), Ld->isNonTemporal(),
9955 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00009956 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Evan Cheng536e6672009-03-12 05:59:15 +00009957 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
David Greene67c9d422010-02-15 16:53:33 +00009958 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +00009959 MinAlign(Ld->getAlignment(), 4));
9960
9961 SDValue NewChain = LoLd.getValue(1);
9962 if (TokenFactorIndex != -1) {
9963 Ops.push_back(LoLd);
9964 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +00009965 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +00009966 Ops.size());
9967 }
9968
9969 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +00009970 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
9971 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +00009972
9973 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
9974 St->getSrcValue(), St->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00009975 St->isVolatile(), St->isNonTemporal(),
9976 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +00009977 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
9978 St->getSrcValue(),
9979 St->getSrcValueOffset() + 4,
9980 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +00009981 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +00009982 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +00009983 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +00009984 }
Dan Gohman475871a2008-07-27 21:46:04 +00009985 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +00009986}
9987
Chris Lattner6cf73262008-01-25 06:14:17 +00009988/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
9989/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009990static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +00009991 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
9992 // F[X]OR(0.0, x) -> x
9993 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +00009994 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
9995 if (C->getValueAPF().isPosZero())
9996 return N->getOperand(1);
9997 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
9998 if (C->getValueAPF().isPosZero())
9999 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000010000 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000010001}
10002
10003/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000010004static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000010005 // FAND(0.0, x) -> 0.0
10006 // FAND(x, 0.0) -> 0.0
10007 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
10008 if (C->getValueAPF().isPosZero())
10009 return N->getOperand(0);
10010 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
10011 if (C->getValueAPF().isPosZero())
10012 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000010013 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000010014}
10015
Dan Gohmane5af2d32009-01-29 01:59:02 +000010016static SDValue PerformBTCombine(SDNode *N,
10017 SelectionDAG &DAG,
10018 TargetLowering::DAGCombinerInfo &DCI) {
10019 // BT ignores high bits in the bit index operand.
10020 SDValue Op1 = N->getOperand(1);
10021 if (Op1.hasOneUse()) {
10022 unsigned BitWidth = Op1.getValueSizeInBits();
10023 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
10024 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000010025 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
10026 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000010027 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000010028 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
10029 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
10030 DCI.CommitTargetLoweringOpt(TLO);
10031 }
10032 return SDValue();
10033}
Chris Lattner83e6c992006-10-04 06:57:07 +000010034
Eli Friedman7a5e5552009-06-07 06:52:44 +000010035static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
10036 SDValue Op = N->getOperand(0);
10037 if (Op.getOpcode() == ISD::BIT_CONVERT)
10038 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000010039 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000010040 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000010041 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000010042 OpVT.getVectorElementType().getSizeInBits()) {
10043 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT, Op);
10044 }
10045 return SDValue();
10046}
10047
Evan Cheng2e489c42009-12-16 00:53:11 +000010048static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
10049 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
10050 // (and (i32 x86isd::setcc_carry), 1)
10051 // This eliminates the zext. This transformation is necessary because
10052 // ISD::SETCC is always legalized to i8.
10053 DebugLoc dl = N->getDebugLoc();
10054 SDValue N0 = N->getOperand(0);
10055 EVT VT = N->getValueType(0);
10056 if (N0.getOpcode() == ISD::AND &&
10057 N0.hasOneUse() &&
10058 N0.getOperand(0).hasOneUse()) {
10059 SDValue N00 = N0.getOperand(0);
10060 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
10061 return SDValue();
10062 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
10063 if (!C || C->getZExtValue() != 1)
10064 return SDValue();
10065 return DAG.getNode(ISD::AND, dl, VT,
10066 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
10067 N00.getOperand(0), N00.getOperand(1)),
10068 DAG.getConstant(1, VT));
10069 }
10070
10071 return SDValue();
10072}
10073
Dan Gohman475871a2008-07-27 21:46:04 +000010074SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000010075 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000010076 SelectionDAG &DAG = DCI.DAG;
10077 switch (N->getOpcode()) {
10078 default: break;
Evan Chengad4196b2008-05-12 19:56:52 +000010079 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010080 case ISD::EXTRACT_VECTOR_ELT:
10081 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +000010082 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000010083 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000010084 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000010085 case ISD::SHL:
10086 case ISD::SRA:
10087 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000010088 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000010089 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000010090 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000010091 case X86ISD::FOR: return PerformFORCombine(N, DAG);
10092 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000010093 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000010094 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000010095 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Evan Cheng206ee9d2006-07-07 08:33:52 +000010096 }
10097
Dan Gohman475871a2008-07-27 21:46:04 +000010098 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000010099}
10100
Evan Chenge5b51ac2010-04-17 06:13:15 +000010101/// isTypeDesirableForOp - Return true if the target has native support for
10102/// the specified value type and it is 'desirable' to use the type for the
10103/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
10104/// instruction encodings are longer and some i16 instructions are slow.
10105bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
10106 if (!isTypeLegal(VT))
10107 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000010108 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000010109 return true;
10110
10111 switch (Opc) {
10112 default:
10113 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000010114 case ISD::LOAD:
10115 case ISD::SIGN_EXTEND:
10116 case ISD::ZERO_EXTEND:
10117 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000010118 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000010119 case ISD::SRL:
10120 case ISD::SUB:
10121 case ISD::ADD:
10122 case ISD::MUL:
10123 case ISD::AND:
10124 case ISD::OR:
10125 case ISD::XOR:
10126 return false;
10127 }
10128}
10129
Evan Chengc82c20b2010-04-24 04:44:57 +000010130static bool MayFoldLoad(SDValue Op) {
10131 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
10132}
10133
10134static bool MayFoldIntoStore(SDValue Op) {
10135 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
10136}
10137
Evan Chenge5b51ac2010-04-17 06:13:15 +000010138/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000010139/// beneficial for dag combiner to promote the specified node. If true, it
10140/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000010141bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000010142 EVT VT = Op.getValueType();
10143 if (VT != MVT::i16)
10144 return false;
10145
Evan Cheng4c26e932010-04-19 19:29:22 +000010146 bool Promote = false;
10147 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000010148 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000010149 default: break;
10150 case ISD::LOAD: {
10151 LoadSDNode *LD = cast<LoadSDNode>(Op);
10152 // If the non-extending load has a single use and it's not live out, then it
10153 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000010154 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
10155 Op.hasOneUse()*/) {
10156 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
10157 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
10158 // The only case where we'd want to promote LOAD (rather then it being
10159 // promoted as an operand is when it's only use is liveout.
10160 if (UI->getOpcode() != ISD::CopyToReg)
10161 return false;
10162 }
10163 }
Evan Cheng4c26e932010-04-19 19:29:22 +000010164 Promote = true;
10165 break;
10166 }
10167 case ISD::SIGN_EXTEND:
10168 case ISD::ZERO_EXTEND:
10169 case ISD::ANY_EXTEND:
10170 Promote = true;
10171 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000010172 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000010173 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000010174 SDValue N0 = Op.getOperand(0);
10175 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000010176 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000010177 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000010178 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000010179 break;
10180 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000010181 case ISD::ADD:
10182 case ISD::MUL:
10183 case ISD::AND:
10184 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000010185 case ISD::XOR:
10186 Commute = true;
10187 // fallthrough
10188 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000010189 SDValue N0 = Op.getOperand(0);
10190 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000010191 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000010192 return false;
10193 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000010194 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000010195 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000010196 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000010197 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000010198 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000010199 }
10200 }
10201
10202 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000010203 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000010204}
10205
Evan Cheng60c07e12006-07-05 22:17:51 +000010206//===----------------------------------------------------------------------===//
10207// X86 Inline Assembly Support
10208//===----------------------------------------------------------------------===//
10209
Chris Lattnerb8105652009-07-20 17:51:36 +000010210static bool LowerToBSwap(CallInst *CI) {
10211 // FIXME: this should verify that we are targetting a 486 or better. If not,
10212 // we will turn this bswap into something that will be lowered to logical ops
10213 // instead of emitting the bswap asm. For now, we don't support 486 or lower
10214 // so don't worry about this.
Eric Christopherfd179292009-08-27 18:07:15 +000010215
Chris Lattnerb8105652009-07-20 17:51:36 +000010216 // Verify this is a simple bswap.
Gabor Greife1c2b9c2010-06-30 13:03:37 +000010217 if (CI->getNumArgOperands() != 1 ||
Gabor Greif1cfe44a2010-06-26 11:51:52 +000010218 CI->getType() != CI->getArgOperand(0)->getType() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000010219 !CI->getType()->isIntegerTy())
Chris Lattnerb8105652009-07-20 17:51:36 +000010220 return false;
Eric Christopherfd179292009-08-27 18:07:15 +000010221
Chris Lattnerb8105652009-07-20 17:51:36 +000010222 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
10223 if (!Ty || Ty->getBitWidth() % 16 != 0)
10224 return false;
Eric Christopherfd179292009-08-27 18:07:15 +000010225
Chris Lattnerb8105652009-07-20 17:51:36 +000010226 // Okay, we can do this xform, do so now.
10227 const Type *Tys[] = { Ty };
10228 Module *M = CI->getParent()->getParent()->getParent();
10229 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
Eric Christopherfd179292009-08-27 18:07:15 +000010230
Gabor Greif1cfe44a2010-06-26 11:51:52 +000010231 Value *Op = CI->getArgOperand(0);
Chris Lattnerb8105652009-07-20 17:51:36 +000010232 Op = CallInst::Create(Int, Op, CI->getName(), CI);
Eric Christopherfd179292009-08-27 18:07:15 +000010233
Chris Lattnerb8105652009-07-20 17:51:36 +000010234 CI->replaceAllUsesWith(Op);
10235 CI->eraseFromParent();
10236 return true;
10237}
10238
10239bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
10240 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
10241 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
10242
10243 std::string AsmStr = IA->getAsmString();
10244
10245 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000010246 SmallVector<StringRef, 4> AsmPieces;
Chris Lattnerb8105652009-07-20 17:51:36 +000010247 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
10248
10249 switch (AsmPieces.size()) {
10250 default: return false;
10251 case 1:
10252 AsmStr = AsmPieces[0];
10253 AsmPieces.clear();
10254 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
10255
10256 // bswap $0
10257 if (AsmPieces.size() == 2 &&
10258 (AsmPieces[0] == "bswap" ||
10259 AsmPieces[0] == "bswapq" ||
10260 AsmPieces[0] == "bswapl") &&
10261 (AsmPieces[1] == "$0" ||
10262 AsmPieces[1] == "${0:q}")) {
10263 // No need to check constraints, nothing other than the equivalent of
10264 // "=r,0" would be valid here.
10265 return LowerToBSwap(CI);
10266 }
10267 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000010268 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000010269 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000010270 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000010271 AsmPieces[1] == "$$8," &&
10272 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000010273 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
10274 AsmPieces.clear();
Benjamin Kramer018cbd52010-03-12 13:54:59 +000010275 const std::string &Constraints = IA->getConstraintString();
10276 SplitString(StringRef(Constraints).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000010277 std::sort(AsmPieces.begin(), AsmPieces.end());
10278 if (AsmPieces.size() == 4 &&
10279 AsmPieces[0] == "~{cc}" &&
10280 AsmPieces[1] == "~{dirflag}" &&
10281 AsmPieces[2] == "~{flags}" &&
10282 AsmPieces[3] == "~{fpsr}") {
10283 return LowerToBSwap(CI);
10284 }
Chris Lattnerb8105652009-07-20 17:51:36 +000010285 }
10286 break;
10287 case 3:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000010288 if (CI->getType()->isIntegerTy(64) &&
Owen Anderson1d0be152009-08-13 21:58:54 +000010289 Constraints.size() >= 2 &&
Chris Lattnerb8105652009-07-20 17:51:36 +000010290 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
10291 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
10292 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramerd4f19592010-01-11 18:03:24 +000010293 SmallVector<StringRef, 4> Words;
Chris Lattnerb8105652009-07-20 17:51:36 +000010294 SplitString(AsmPieces[0], Words, " \t");
10295 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
10296 Words.clear();
10297 SplitString(AsmPieces[1], Words, " \t");
10298 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
10299 Words.clear();
10300 SplitString(AsmPieces[2], Words, " \t,");
10301 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
10302 Words[2] == "%edx") {
10303 return LowerToBSwap(CI);
10304 }
10305 }
10306 }
10307 }
10308 break;
10309 }
10310 return false;
10311}
10312
10313
10314
Chris Lattnerf4dff842006-07-11 02:54:03 +000010315/// getConstraintType - Given a constraint letter, return the type of
10316/// constraint it is for this target.
10317X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000010318X86TargetLowering::getConstraintType(const std::string &Constraint) const {
10319 if (Constraint.size() == 1) {
10320 switch (Constraint[0]) {
10321 case 'A':
Dale Johannesen330169f2008-11-13 21:52:36 +000010322 return C_Register;
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010323 case 'f':
Chris Lattner4234f572007-03-25 02:14:49 +000010324 case 'r':
10325 case 'R':
10326 case 'l':
10327 case 'q':
10328 case 'Q':
10329 case 'x':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000010330 case 'y':
Chris Lattner4234f572007-03-25 02:14:49 +000010331 case 'Y':
10332 return C_RegisterClass;
Dale Johannesen78e3e522009-02-12 20:58:09 +000010333 case 'e':
10334 case 'Z':
10335 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000010336 default:
10337 break;
10338 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000010339 }
Chris Lattner4234f572007-03-25 02:14:49 +000010340 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000010341}
10342
Dale Johannesenba2a0b92008-01-29 02:21:21 +000010343/// LowerXConstraint - try to replace an X constraint, which matches anything,
10344/// with another that has more specific requirements based on the type of the
10345/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000010346const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000010347LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000010348 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
10349 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000010350 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesenba2a0b92008-01-29 02:21:21 +000010351 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +000010352 return "Y";
10353 if (Subtarget->hasSSE1())
10354 return "x";
10355 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010356
Chris Lattner5e764232008-04-26 23:02:14 +000010357 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000010358}
10359
Chris Lattner48884cd2007-08-25 00:47:38 +000010360/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
10361/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000010362void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +000010363 char Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000010364 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000010365 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010366 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000010367
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010368 switch (Constraint) {
10369 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000010370 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000010371 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000010372 if (C->getZExtValue() <= 31) {
10373 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000010374 break;
10375 }
Devang Patel84f7fd22007-03-17 00:13:28 +000010376 }
Chris Lattner48884cd2007-08-25 00:47:38 +000010377 return;
Evan Cheng364091e2008-09-22 23:57:37 +000010378 case 'J':
10379 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000010380 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000010381 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
10382 break;
10383 }
10384 }
10385 return;
10386 case 'K':
10387 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000010388 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000010389 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
10390 break;
10391 }
10392 }
10393 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000010394 case 'N':
10395 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000010396 if (C->getZExtValue() <= 255) {
10397 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000010398 break;
10399 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000010400 }
Chris Lattner48884cd2007-08-25 00:47:38 +000010401 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000010402 case 'e': {
10403 // 32-bit signed value
10404 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000010405 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
10406 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000010407 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000010408 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000010409 break;
10410 }
10411 // FIXME gcc accepts some relocatable values here too, but only in certain
10412 // memory models; it's complicated.
10413 }
10414 return;
10415 }
10416 case 'Z': {
10417 // 32-bit unsigned value
10418 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000010419 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
10420 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000010421 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
10422 break;
10423 }
10424 }
10425 // FIXME gcc accepts some relocatable values here too, but only in certain
10426 // memory models; it's complicated.
10427 return;
10428 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000010429 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010430 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000010431 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000010432 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000010433 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000010434 break;
10435 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010436
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000010437 // In any sort of PIC mode addresses need to be computed at runtime by
10438 // adding in a register or some sort of table lookup. These can't
10439 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000010440 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000010441 return;
10442
Chris Lattnerdc43a882007-05-03 16:52:29 +000010443 // If we are in non-pic codegen mode, we allow the address of a global (with
10444 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000010445 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000010446 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000010447
Chris Lattner49921962009-05-08 18:23:14 +000010448 // Match either (GA), (GA+C), (GA+C1+C2), etc.
10449 while (1) {
10450 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
10451 Offset += GA->getOffset();
10452 break;
10453 } else if (Op.getOpcode() == ISD::ADD) {
10454 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
10455 Offset += C->getZExtValue();
10456 Op = Op.getOperand(0);
10457 continue;
10458 }
10459 } else if (Op.getOpcode() == ISD::SUB) {
10460 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
10461 Offset += -C->getZExtValue();
10462 Op = Op.getOperand(0);
10463 continue;
10464 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000010465 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000010466
Chris Lattner49921962009-05-08 18:23:14 +000010467 // Otherwise, this isn't something we can handle, reject it.
10468 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000010469 }
Eric Christopherfd179292009-08-27 18:07:15 +000010470
Dan Gohman46510a72010-04-15 01:51:59 +000010471 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000010472 // If we require an extra load to get this address, as in PIC mode, we
10473 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000010474 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
10475 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000010476 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000010477
Devang Patel0d881da2010-07-06 22:08:15 +000010478 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
10479 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000010480 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010481 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000010482 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010483
Gabor Greifba36cb52008-08-28 21:40:38 +000010484 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000010485 Ops.push_back(Result);
10486 return;
10487 }
Dale Johannesen1784d162010-06-25 21:55:36 +000010488 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010489}
10490
Chris Lattner259e97c2006-01-31 19:43:35 +000010491std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +000010492getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000010493 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +000010494 if (Constraint.size() == 1) {
10495 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +000010496 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +000010497 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +000010498 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
10499 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000010500 if (VT == MVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010501 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
10502 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
10503 X86::R10D,X86::R11D,X86::R12D,
10504 X86::R13D,X86::R14D,X86::R15D,
10505 X86::EBP, X86::ESP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010506 else if (VT == MVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010507 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
10508 X86::SI, X86::DI, X86::R8W,X86::R9W,
10509 X86::R10W,X86::R11W,X86::R12W,
10510 X86::R13W,X86::R14W,X86::R15W,
10511 X86::BP, X86::SP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010512 else if (VT == MVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010513 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
10514 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
10515 X86::R10B,X86::R11B,X86::R12B,
10516 X86::R13B,X86::R14B,X86::R15B,
10517 X86::BPL, X86::SPL, 0);
10518
Owen Anderson825b72b2009-08-11 20:47:22 +000010519 else if (VT == MVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010520 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
10521 X86::RSI, X86::RDI, X86::R8, X86::R9,
10522 X86::R10, X86::R11, X86::R12,
10523 X86::R13, X86::R14, X86::R15,
10524 X86::RBP, X86::RSP, 0);
10525
10526 break;
10527 }
Eric Christopherfd179292009-08-27 18:07:15 +000010528 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +000010529 case 'Q': // Q_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000010530 if (VT == MVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000010531 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010532 else if (VT == MVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000010533 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010534 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +000010535 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010536 else if (VT == MVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +000010537 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
10538 break;
Chris Lattner259e97c2006-01-31 19:43:35 +000010539 }
10540 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010541
Chris Lattner1efa40f2006-02-22 00:56:39 +000010542 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +000010543}
Chris Lattnerf76d1802006-07-31 23:26:50 +000010544
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010545std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000010546X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000010547 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000010548 // First, see if this is a constraint that directly corresponds to an LLVM
10549 // register class.
10550 if (Constraint.size() == 1) {
10551 // GCC Constraint Letters
10552 switch (Constraint[0]) {
10553 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000010554 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000010555 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000010556 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +000010557 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010558 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000010559 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010560 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000010561 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000010562 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000010563 case 'R': // LEGACY_REGS
10564 if (VT == MVT::i8)
10565 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
10566 if (VT == MVT::i16)
10567 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
10568 if (VT == MVT::i32 || !Subtarget->is64Bit())
10569 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
10570 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010571 case 'f': // FP Stack registers.
10572 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
10573 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000010574 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010575 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010576 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010577 return std::make_pair(0U, X86::RFP64RegisterClass);
10578 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000010579 case 'y': // MMX_REGS if MMX allowed.
10580 if (!Subtarget->hasMMX()) break;
10581 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000010582 case 'Y': // SSE_REGS if SSE2 allowed
10583 if (!Subtarget->hasSSE2()) break;
10584 // FALL THROUGH.
10585 case 'x': // SSE_REGS if SSE1 allowed
10586 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010587
Owen Anderson825b72b2009-08-11 20:47:22 +000010588 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000010589 default: break;
10590 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000010591 case MVT::f32:
10592 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000010593 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010594 case MVT::f64:
10595 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000010596 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000010597 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000010598 case MVT::v16i8:
10599 case MVT::v8i16:
10600 case MVT::v4i32:
10601 case MVT::v2i64:
10602 case MVT::v4f32:
10603 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000010604 return std::make_pair(0U, X86::VR128RegisterClass);
10605 }
Chris Lattnerad043e82007-04-09 05:11:28 +000010606 break;
10607 }
10608 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010609
Chris Lattnerf76d1802006-07-31 23:26:50 +000010610 // Use the default implementation in TargetLowering to convert the register
10611 // constraint into a member of a register class.
10612 std::pair<unsigned, const TargetRegisterClass*> Res;
10613 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000010614
10615 // Not found as a standard register?
10616 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000010617 // Map st(0) -> st(7) -> ST0
10618 if (Constraint.size() == 7 && Constraint[0] == '{' &&
10619 tolower(Constraint[1]) == 's' &&
10620 tolower(Constraint[2]) == 't' &&
10621 Constraint[3] == '(' &&
10622 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
10623 Constraint[5] == ')' &&
10624 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000010625
Chris Lattner56d77c72009-09-13 22:41:48 +000010626 Res.first = X86::ST0+Constraint[4]-'0';
10627 Res.second = X86::RFP80RegisterClass;
10628 return Res;
10629 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000010630
Chris Lattner56d77c72009-09-13 22:41:48 +000010631 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000010632 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000010633 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000010634 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000010635 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000010636 }
Chris Lattner56d77c72009-09-13 22:41:48 +000010637
10638 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000010639 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000010640 Res.first = X86::EFLAGS;
10641 Res.second = X86::CCRRegisterClass;
10642 return Res;
10643 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000010644
Dale Johannesen330169f2008-11-13 21:52:36 +000010645 // 'A' means EAX + EDX.
10646 if (Constraint == "A") {
10647 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000010648 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000010649 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000010650 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000010651 return Res;
10652 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010653
Chris Lattnerf76d1802006-07-31 23:26:50 +000010654 // Otherwise, check to see if this is a register class of the wrong value
10655 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
10656 // turn into {ax},{dx}.
10657 if (Res.second->hasType(VT))
10658 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010659
Chris Lattnerf76d1802006-07-31 23:26:50 +000010660 // All of the single-register GCC register classes map their values onto
10661 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
10662 // really want an 8-bit or 32-bit register, map to the appropriate register
10663 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000010664 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000010665 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010666 unsigned DestReg = 0;
10667 switch (Res.first) {
10668 default: break;
10669 case X86::AX: DestReg = X86::AL; break;
10670 case X86::DX: DestReg = X86::DL; break;
10671 case X86::CX: DestReg = X86::CL; break;
10672 case X86::BX: DestReg = X86::BL; break;
10673 }
10674 if (DestReg) {
10675 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010676 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010677 }
Owen Anderson825b72b2009-08-11 20:47:22 +000010678 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010679 unsigned DestReg = 0;
10680 switch (Res.first) {
10681 default: break;
10682 case X86::AX: DestReg = X86::EAX; break;
10683 case X86::DX: DestReg = X86::EDX; break;
10684 case X86::CX: DestReg = X86::ECX; break;
10685 case X86::BX: DestReg = X86::EBX; break;
10686 case X86::SI: DestReg = X86::ESI; break;
10687 case X86::DI: DestReg = X86::EDI; break;
10688 case X86::BP: DestReg = X86::EBP; break;
10689 case X86::SP: DestReg = X86::ESP; break;
10690 }
10691 if (DestReg) {
10692 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010693 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010694 }
Owen Anderson825b72b2009-08-11 20:47:22 +000010695 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010696 unsigned DestReg = 0;
10697 switch (Res.first) {
10698 default: break;
10699 case X86::AX: DestReg = X86::RAX; break;
10700 case X86::DX: DestReg = X86::RDX; break;
10701 case X86::CX: DestReg = X86::RCX; break;
10702 case X86::BX: DestReg = X86::RBX; break;
10703 case X86::SI: DestReg = X86::RSI; break;
10704 case X86::DI: DestReg = X86::RDI; break;
10705 case X86::BP: DestReg = X86::RBP; break;
10706 case X86::SP: DestReg = X86::RSP; break;
10707 }
10708 if (DestReg) {
10709 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010710 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010711 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000010712 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000010713 } else if (Res.second == X86::FR32RegisterClass ||
10714 Res.second == X86::FR64RegisterClass ||
10715 Res.second == X86::VR128RegisterClass) {
10716 // Handle references to XMM physical registers that got mapped into the
10717 // wrong class. This can happen with constraints like {xmm0} where the
10718 // target independent register mapper will just pick the first match it can
10719 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000010720 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000010721 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000010722 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000010723 Res.second = X86::FR64RegisterClass;
10724 else if (X86::VR128RegisterClass->hasType(VT))
10725 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000010726 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010727
Chris Lattnerf76d1802006-07-31 23:26:50 +000010728 return Res;
10729}