blob: 66f9612e02759cfca51c155bf0764e608bee1a7f [file] [log] [blame]
Arnold Schwaighofer92226dd2007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Evan Chengb1712452010-01-27 06:25:16 +000015#define DEBUG_TYPE "x86-isel"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000016#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000017#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000018#include "X86ISelLowering.h"
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000019#include "X86ShuffleDecode.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000020#include "X86TargetMachine.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000021#include "X86TargetObjectFile.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000022#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000023#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000024#include "llvm/DerivedTypes.h"
Chris Lattnerb903bed2009-06-26 21:20:29 +000025#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000026#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000027#include "llvm/Function.h"
Chris Lattnerb8105652009-07-20 17:51:36 +000028#include "llvm/Instructions.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000029#include "llvm/Intrinsics.h"
Owen Andersona90b3dc2009-07-15 21:51:10 +000030#include "llvm/LLVMContext.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000031#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000032#include "llvm/CodeGen/MachineFunction.h"
33#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner5e1df8d2010-01-25 23:38:14 +000034#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000035#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000036#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000037#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner589c6f62010-01-26 06:28:43 +000038#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000039#include "llvm/MC/MCContext.h"
Daniel Dunbar4e815f82010-03-15 23:51:06 +000040#include "llvm/MC/MCExpr.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000041#include "llvm/MC/MCSymbol.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000042#include "llvm/ADT/BitVector.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000043#include "llvm/ADT/SmallSet.h"
Evan Chengb1712452010-01-27 06:25:16 +000044#include "llvm/ADT/Statistic.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000045#include "llvm/ADT/StringExtras.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000046#include "llvm/ADT/VectorExtras.h"
Mon P Wang3c81d352008-11-23 04:37:22 +000047#include "llvm/Support/CommandLine.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000048#include "llvm/Support/Debug.h"
Bill Wendlingec041eb2010-03-12 19:20:40 +000049#include "llvm/Support/Dwarf.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000050#include "llvm/Support/ErrorHandling.h"
51#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000052#include "llvm/Support/raw_ostream.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000053using namespace llvm;
Bill Wendlingec041eb2010-03-12 19:20:40 +000054using namespace dwarf;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000055
Evan Chengb1712452010-01-27 06:25:16 +000056STATISTIC(NumTailCalls, "Number of tail calls");
57
Mon P Wang3c81d352008-11-23 04:37:22 +000058static cl::opt<bool>
Mon P Wang9f22a4a2008-11-24 02:10:43 +000059DisableMMX("disable-mmx", cl::Hidden, cl::desc("Disable use of MMX"));
Mon P Wang3c81d352008-11-23 04:37:22 +000060
Evan Cheng10e86422008-04-25 19:11:04 +000061// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000062static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000063 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000064
Chris Lattnerf0144122009-07-28 03:13:23 +000065static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
Eric Christopher62f35a22010-07-05 19:26:33 +000066
67 bool is64Bit = TM.getSubtarget<X86Subtarget>().is64Bit();
68
69 if (TM.getSubtarget<X86Subtarget>().isTargetDarwin()) {
70 if (is64Bit) return new X8664_MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +000071 return new TargetLoweringObjectFileMachO();
Eric Christopher62f35a22010-07-05 19:26:33 +000072 } else if (TM.getSubtarget<X86Subtarget>().isTargetELF() ){
73 if (is64Bit) return new X8664_ELFTargetObjectFile(TM);
Anton Korobeynikov9184b252010-02-15 22:35:59 +000074 return new X8632_ELFTargetObjectFile(TM);
Eric Christopher62f35a22010-07-05 19:26:33 +000075 } else if (TM.getSubtarget<X86Subtarget>().isTargetCOFF()) {
Chris Lattnerf0144122009-07-28 03:13:23 +000076 return new TargetLoweringObjectFileCOFF();
Eric Christopher62f35a22010-07-05 19:26:33 +000077 }
78 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +000079}
80
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +000081X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +000082 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +000083 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesenf1fc3a82007-09-23 14:52:20 +000084 X86ScalarSSEf64 = Subtarget->hasSSE2();
85 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng25ab6902006-09-08 06:48:29 +000086 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000087
Anton Korobeynikov2365f512007-07-14 14:06:15 +000088 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000089 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000090
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000091 // Set up the TargetLowering object.
92
93 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Owen Anderson825b72b2009-08-11 20:47:22 +000094 setShiftAmountType(MVT::i8);
Duncan Sands03228082008-11-23 15:47:28 +000095 setBooleanContents(ZeroOrOneBooleanContent);
Evan Cheng211ffa12010-05-19 20:19:50 +000096 setSchedulingPreference(Sched::RegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +000097 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +000098
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000099 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000100 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000101 setUseUnderscoreSetJmp(false);
102 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000103 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000104 // MS runtime is weird: it exports _setjmp, but longjmp!
105 setUseUnderscoreSetJmp(true);
106 setUseUnderscoreLongJmp(false);
107 } else {
108 setUseUnderscoreSetJmp(true);
109 setUseUnderscoreLongJmp(true);
110 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000111
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000112 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000113 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman71edb242010-04-30 18:30:26 +0000114 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000115 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000116 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000117 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000118
Owen Anderson825b72b2009-08-11 20:47:22 +0000119 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000120
Scott Michelfdc40a02009-02-17 22:15:04 +0000121 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000122 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000123 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000124 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000125 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000126 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
127 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000128
129 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000130 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
131 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
132 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
133 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
134 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
135 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000136
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000137 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
138 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000139 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
140 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
141 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000142
Evan Cheng25ab6902006-09-08 06:48:29 +0000143 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000144 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
145 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000146 } else if (!UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000147 // We have an algorithm for SSE2->double, and we turn this into a
148 // 64-bit FILD followed by conditional FADD for other targets.
149 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000150 // We have an algorithm for SSE2, and we turn this into a 64-bit
151 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000152 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000153 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000154
155 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
156 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000157 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
158 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000159
Devang Patel6a784892009-06-05 18:48:29 +0000160 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000161 // SSE has no i16 to fp conversion, only i32
162 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000163 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000164 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000165 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000166 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000167 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
168 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000169 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000170 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000171 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
172 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000173 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000174
Dale Johannesen73328d12007-09-19 23:55:34 +0000175 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
176 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000177 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
178 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000179
Evan Cheng02568ff2006-01-30 22:13:22 +0000180 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
181 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000182 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
183 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000184
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000185 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000186 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000187 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000188 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000189 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000190 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
191 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000192 }
193
194 // Handle FP_TO_UINT by promoting the destination to a larger signed
195 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000196 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
197 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
198 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000199
Evan Cheng25ab6902006-09-08 06:48:29 +0000200 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000201 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
202 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000203 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000204 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000205 // Expand FP_TO_UINT into a select.
206 // FIXME: We would like to use a Custom expander here eventually to do
207 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000208 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000209 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000210 // With SSE3 we can use fisttpll to convert to a signed i64; without
211 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000212 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000213 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000214
Chris Lattner399610a2006-12-05 18:22:22 +0000215 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesenacbf6342010-05-21 18:44:47 +0000216 if (!X86ScalarSSEf64) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000217 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
218 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000219 if (Subtarget->is64Bit()) {
Dale Johannesen7d07b482010-05-21 00:52:33 +0000220 setOperationAction(ISD::BIT_CONVERT , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000221 // Without SSE, i64->f64 goes through memory.
222 setOperationAction(ISD::BIT_CONVERT , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000223 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000224 }
Chris Lattner21f66852005-12-23 05:15:23 +0000225
Dan Gohmanb00ee212008-02-18 19:34:53 +0000226 // Scalar integer divide and remainder are lowered to use operations that
227 // produce two results, to match the available instructions. This exposes
228 // the two-result form to trivial CSE, which is able to combine x/y and x%y
229 // into a single instruction.
230 //
231 // Scalar integer multiply-high is also lowered to use two-result
232 // operations, to match the available instructions. However, plain multiply
233 // (low) operations are left as Legal, as there are single-result
234 // instructions for this in x86. Using the two-result multiply instructions
235 // when both high and low results are needed must be arranged by dagcombine.
Owen Anderson825b72b2009-08-11 20:47:22 +0000236 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
237 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
238 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
239 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
240 setOperationAction(ISD::SREM , MVT::i8 , Expand);
241 setOperationAction(ISD::UREM , MVT::i8 , Expand);
242 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
243 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
244 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
245 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
246 setOperationAction(ISD::SREM , MVT::i16 , Expand);
247 setOperationAction(ISD::UREM , MVT::i16 , Expand);
248 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
249 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
250 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
251 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
252 setOperationAction(ISD::SREM , MVT::i32 , Expand);
253 setOperationAction(ISD::UREM , MVT::i32 , Expand);
254 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
255 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
256 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
257 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
258 setOperationAction(ISD::SREM , MVT::i64 , Expand);
259 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohmana37c9f72007-09-25 18:23:27 +0000260
Owen Anderson825b72b2009-08-11 20:47:22 +0000261 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
262 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
263 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
264 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000265 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000266 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
267 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
268 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
269 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
270 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
271 setOperationAction(ISD::FREM , MVT::f32 , Expand);
272 setOperationAction(ISD::FREM , MVT::f64 , Expand);
273 setOperationAction(ISD::FREM , MVT::f80 , Expand);
274 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000275
Owen Anderson825b72b2009-08-11 20:47:22 +0000276 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
277 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
278 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
279 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000280 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
281 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000282 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
283 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
284 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000285 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000286 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
287 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
288 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000289 }
290
Owen Anderson825b72b2009-08-11 20:47:22 +0000291 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
292 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000293
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000294 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000295 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000296 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000297 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000298 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000299 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
300 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
301 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
302 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
303 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000304 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000305 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
306 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
307 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
308 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000309 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000310 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
311 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000312 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000313 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000314
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000315 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000316 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
317 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
318 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
319 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000320 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000321 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
322 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000323 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000324 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000325 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
326 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
327 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
328 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000329 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000330 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000331 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000332 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
333 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
334 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000335 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000336 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
337 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
338 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000339 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000340
Evan Chengd2cde682008-03-10 19:38:10 +0000341 if (Subtarget->hasSSE1())
Owen Anderson825b72b2009-08-11 20:47:22 +0000342 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000343
Eric Christopher9a9d2752010-07-22 02:48:34 +0000344 // We may not have a libcall for MEMBARRIER so we should lower this.
345 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
346
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000347 // On X86 and X86-64, atomic operations are lowered to locked instructions.
348 // Locked instructions, in turn, have implicit fence semantics (all memory
349 // operations are flushed before issuing the locked instruction, and they
350 // are not buffered), so we can fold away the common pattern of
351 // fence-atomic-fence.
352 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000353
Mon P Wang63307c32008-05-05 19:05:59 +0000354 // Expand certain atomics
Owen Anderson825b72b2009-08-11 20:47:22 +0000355 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Custom);
356 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Custom);
357 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
358 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Bill Wendling5bf1b4e2008-08-20 00:28:16 +0000359
Owen Anderson825b72b2009-08-11 20:47:22 +0000360 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Custom);
361 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Custom);
362 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
363 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000364
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000365 if (!Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000366 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
367 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
368 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
369 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
370 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
371 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
372 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000373 }
374
Evan Cheng3c992d22006-03-07 02:02:57 +0000375 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000376 if (!Subtarget->isTargetDarwin() &&
377 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000378 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000379 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000380 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000381
Owen Anderson825b72b2009-08-11 20:47:22 +0000382 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
383 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
384 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
385 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000386 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000387 setExceptionPointerRegister(X86::RAX);
388 setExceptionSelectorRegister(X86::RDX);
389 } else {
390 setExceptionPointerRegister(X86::EAX);
391 setExceptionSelectorRegister(X86::EDX);
392 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000393 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
394 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000395
Owen Anderson825b72b2009-08-11 20:47:22 +0000396 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000397
Owen Anderson825b72b2009-08-11 20:47:22 +0000398 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000399
Nate Begemanacc398c2006-01-25 18:21:52 +0000400 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000401 setOperationAction(ISD::VASTART , MVT::Other, Custom);
402 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000403 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000404 setOperationAction(ISD::VAARG , MVT::Other, Custom);
405 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000406 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000407 setOperationAction(ISD::VAARG , MVT::Other, Expand);
408 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000409 }
Evan Chengae642192007-03-02 23:16:35 +0000410
Owen Anderson825b72b2009-08-11 20:47:22 +0000411 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
412 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000413 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000414 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000415 if (Subtarget->isTargetCygMing())
Owen Anderson825b72b2009-08-11 20:47:22 +0000416 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000417 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000418 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000419
Evan Chengc7ce29b2009-02-13 22:36:38 +0000420 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000421 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000422 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000423 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
424 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000425
Evan Cheng223547a2006-01-31 22:28:30 +0000426 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000427 setOperationAction(ISD::FABS , MVT::f64, Custom);
428 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000429
430 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000431 setOperationAction(ISD::FNEG , MVT::f64, Custom);
432 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000433
Evan Cheng68c47cb2007-01-05 07:55:56 +0000434 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000435 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
436 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000437
Evan Chengd25e9e82006-02-02 00:28:23 +0000438 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000439 setOperationAction(ISD::FSIN , MVT::f64, Expand);
440 setOperationAction(ISD::FCOS , MVT::f64, Expand);
441 setOperationAction(ISD::FSIN , MVT::f32, Expand);
442 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000443
Chris Lattnera54aa942006-01-29 06:26:08 +0000444 // Expand FP immediates into loads from the stack, except for the special
445 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000446 addLegalFPImmediate(APFloat(+0.0)); // xorpd
447 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000448 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000449 // Use SSE for f32, x87 for f64.
450 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000451 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
452 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000453
454 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000455 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000456
457 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000458 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000459
Owen Anderson825b72b2009-08-11 20:47:22 +0000460 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000461
462 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000463 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
464 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000465
466 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000467 setOperationAction(ISD::FSIN , MVT::f32, Expand);
468 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000469
Nate Begemane1795842008-02-14 08:57:00 +0000470 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000471 addLegalFPImmediate(APFloat(+0.0f)); // xorps
472 addLegalFPImmediate(APFloat(+0.0)); // FLD0
473 addLegalFPImmediate(APFloat(+1.0)); // FLD1
474 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
475 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
476
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000477 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000478 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
479 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000480 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000481 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000482 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000483 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000484 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
485 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000486
Owen Anderson825b72b2009-08-11 20:47:22 +0000487 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
488 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
489 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
490 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000491
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000492 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000493 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
494 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000495 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000496 addLegalFPImmediate(APFloat(+0.0)); // FLD0
497 addLegalFPImmediate(APFloat(+1.0)); // FLD1
498 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
499 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000500 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
501 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
502 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
503 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000504 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000505
Dale Johannesen59a58732007-08-05 18:49:15 +0000506 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000507 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000508 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
509 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
510 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000511 {
512 bool ignored;
513 APFloat TmpFlt(+0.0);
514 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
515 &ignored);
516 addLegalFPImmediate(TmpFlt); // FLD0
517 TmpFlt.changeSign();
518 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
519 APFloat TmpFlt2(+1.0);
520 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
521 &ignored);
522 addLegalFPImmediate(TmpFlt2); // FLD1
523 TmpFlt2.changeSign();
524 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
525 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000526
Evan Chengc7ce29b2009-02-13 22:36:38 +0000527 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000528 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
529 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000530 }
Dale Johannesen2f429012007-09-26 21:10:55 +0000531 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000532
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000533 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000534 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
535 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
536 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000537
Owen Anderson825b72b2009-08-11 20:47:22 +0000538 setOperationAction(ISD::FLOG, MVT::f80, Expand);
539 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
540 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
541 setOperationAction(ISD::FEXP, MVT::f80, Expand);
542 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000543
Mon P Wangf007a8b2008-11-06 05:31:54 +0000544 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000545 // (for widening) or expand (for scalarization). Then we will selectively
546 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000547 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
548 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
549 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
550 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
551 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
552 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
553 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
554 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
555 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
556 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
557 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
558 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
559 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
560 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
561 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
562 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
563 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
564 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
565 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
566 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
567 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
568 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
569 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
570 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
571 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
572 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
573 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
574 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
575 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
576 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
577 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
578 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
579 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
580 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
581 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
582 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
583 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
584 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
585 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
586 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
587 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
588 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
589 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
590 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
591 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
592 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
593 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
594 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
595 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
596 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000597 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000598 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
599 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
600 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
601 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
602 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
603 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
604 setTruncStoreAction((MVT::SimpleValueType)VT,
605 (MVT::SimpleValueType)InnerVT, Expand);
606 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
607 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
608 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000609 }
610
Evan Chengc7ce29b2009-02-13 22:36:38 +0000611 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
612 // with -msoft-float, disable use of MMX as well.
Evan Cheng92722532009-03-26 23:06:32 +0000613 if (!UseSoftFloat && !DisableMMX && Subtarget->hasMMX()) {
Bill Wendlingd8dd5752010-09-07 20:03:56 +0000614 addRegisterClass(MVT::x86mmx, X86::VR64RegisterClass, false);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000615 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000616 }
617
Dale Johannesen0488fb62010-09-30 23:57:10 +0000618 // MMX-sized vectors (other than x86mmx) are expected to be expanded
619 // into smaller operations.
620 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
621 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
622 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
623 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
624 setOperationAction(ISD::AND, MVT::v8i8, Expand);
625 setOperationAction(ISD::AND, MVT::v4i16, Expand);
626 setOperationAction(ISD::AND, MVT::v2i32, Expand);
627 setOperationAction(ISD::AND, MVT::v1i64, Expand);
628 setOperationAction(ISD::OR, MVT::v8i8, Expand);
629 setOperationAction(ISD::OR, MVT::v4i16, Expand);
630 setOperationAction(ISD::OR, MVT::v2i32, Expand);
631 setOperationAction(ISD::OR, MVT::v1i64, Expand);
632 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
633 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
634 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
635 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
636 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
637 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
638 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
639 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
640 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
641 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
642 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
643 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
644 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
645 setOperationAction(ISD::BIT_CONVERT, MVT::v8i8, Expand);
646 setOperationAction(ISD::BIT_CONVERT, MVT::v4i16, Expand);
647 setOperationAction(ISD::BIT_CONVERT, MVT::v2i32, Expand);
648 setOperationAction(ISD::BIT_CONVERT, MVT::v1i64, Expand);
649
Evan Cheng92722532009-03-26 23:06:32 +0000650 if (!UseSoftFloat && Subtarget->hasSSE1()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000651 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000652
Owen Anderson825b72b2009-08-11 20:47:22 +0000653 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
654 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
655 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
656 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
657 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
658 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
659 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
660 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
661 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
662 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
663 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
664 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000665 }
666
Evan Cheng92722532009-03-26 23:06:32 +0000667 if (!UseSoftFloat && Subtarget->hasSSE2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000668 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000669
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000670 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
671 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000672 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
673 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
674 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
675 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000676
Owen Anderson825b72b2009-08-11 20:47:22 +0000677 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
678 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
679 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
680 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
681 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
682 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
683 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
684 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
685 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
686 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
687 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
688 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
689 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
690 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
691 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
692 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000693
Owen Anderson825b72b2009-08-11 20:47:22 +0000694 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
695 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
696 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
697 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000698
Owen Anderson825b72b2009-08-11 20:47:22 +0000699 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
700 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
701 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
702 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
703 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000704
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000705 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
706 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
707 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
708 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
709 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
710
Evan Cheng2c3ae372006-04-12 21:21:57 +0000711 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000712 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
713 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000714 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000715 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000716 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000717 // Do not attempt to custom lower non-128-bit vectors
718 if (!VT.is128BitVector())
719 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000720 setOperationAction(ISD::BUILD_VECTOR,
721 VT.getSimpleVT().SimpleTy, Custom);
722 setOperationAction(ISD::VECTOR_SHUFFLE,
723 VT.getSimpleVT().SimpleTy, Custom);
724 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
725 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000726 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000727
Owen Anderson825b72b2009-08-11 20:47:22 +0000728 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
729 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
730 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
731 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
732 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
733 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000734
Nate Begemancdd1eec2008-02-12 22:51:28 +0000735 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000736 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
737 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000738 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000739
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000740 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000741 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
742 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000743 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000744
745 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000746 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000747 continue;
Eric Christopher4bd24c22010-03-30 01:04:59 +0000748
Owen Andersond6662ad2009-08-10 20:46:15 +0000749 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000750 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000751 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000752 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000753 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000754 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000755 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000756 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000757 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000758 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000759 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000760
Owen Anderson825b72b2009-08-11 20:47:22 +0000761 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000762
Evan Cheng2c3ae372006-04-12 21:21:57 +0000763 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000764 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
765 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
766 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
767 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000768
Owen Anderson825b72b2009-08-11 20:47:22 +0000769 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
770 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000771 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000772
Nate Begeman14d12ca2008-02-11 04:19:36 +0000773 if (Subtarget->hasSSE41()) {
Dale Johannesen54feef22010-05-27 20:12:41 +0000774 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
775 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
776 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
777 setOperationAction(ISD::FRINT, MVT::f32, Legal);
778 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
779 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
780 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
781 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
782 setOperationAction(ISD::FRINT, MVT::f64, Legal);
783 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
784
Nate Begeman14d12ca2008-02-11 04:19:36 +0000785 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000786 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000787
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000788 // Can turn SHL into an integer multiply.
789 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
Nate Begeman51409212010-07-28 00:21:48 +0000790 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000791
Nate Begeman14d12ca2008-02-11 04:19:36 +0000792 // i8 and i16 vectors are custom , because the source register and source
793 // source memory operand types are not the same width. f32 vectors are
794 // custom since the immediate controlling the insert encodes additional
795 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000796 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
797 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
798 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
799 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000800
Owen Anderson825b72b2009-08-11 20:47:22 +0000801 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
802 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
803 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
804 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000805
806 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000807 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
808 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000809 }
810 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000811
Nate Begeman30a0de92008-07-17 16:51:19 +0000812 if (Subtarget->hasSSE42()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000813 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
Nate Begeman30a0de92008-07-17 16:51:19 +0000814 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000815
David Greene9b9838d2009-06-29 16:47:10 +0000816 if (!UseSoftFloat && Subtarget->hasAVX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000817 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
818 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
819 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
820 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
Bruno Cardoso Lopes405f11b2010-08-10 01:43:16 +0000821 addRegisterClass(MVT::v32i8, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000822
Owen Anderson825b72b2009-08-11 20:47:22 +0000823 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
824 setOperationAction(ISD::LOAD, MVT::v8i32, Legal);
825 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
826 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
827 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
828 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
829 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
830 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
831 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
832 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +0000833 setOperationAction(ISD::BUILD_VECTOR, MVT::v8f32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000834 //setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Custom);
835 //setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8f32, Custom);
836 //setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
837 //setOperationAction(ISD::VSETCC, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000838
839 // Operations to consider commented out -v16i16 v32i8
Owen Anderson825b72b2009-08-11 20:47:22 +0000840 //setOperationAction(ISD::ADD, MVT::v16i16, Legal);
841 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
842 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
843 //setOperationAction(ISD::SUB, MVT::v32i8, Legal);
844 //setOperationAction(ISD::SUB, MVT::v16i16, Legal);
845 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
846 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
847 //setOperationAction(ISD::MUL, MVT::v16i16, Legal);
848 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
849 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
850 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
851 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
852 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
853 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000854
Owen Anderson825b72b2009-08-11 20:47:22 +0000855 setOperationAction(ISD::VSETCC, MVT::v4f64, Custom);
856 // setOperationAction(ISD::VSETCC, MVT::v32i8, Custom);
857 // setOperationAction(ISD::VSETCC, MVT::v16i16, Custom);
858 setOperationAction(ISD::VSETCC, MVT::v8i32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000859
Owen Anderson825b72b2009-08-11 20:47:22 +0000860 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v32i8, Custom);
861 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i16, Custom);
862 // setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i16, Custom);
863 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i32, Custom);
864 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000865
Owen Anderson825b72b2009-08-11 20:47:22 +0000866 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f64, Custom);
867 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i64, Custom);
868 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f64, Custom);
869 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i64, Custom);
870 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f64, Custom);
871 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000872
873#if 0
874 // Not sure we want to do this since there are no 256-bit integer
875 // operations in AVX
876
877 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
878 // This includes 256-bit vectors
Owen Anderson825b72b2009-08-11 20:47:22 +0000879 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; ++i) {
880 EVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000881
882 // Do not attempt to custom lower non-power-of-2 vectors
883 if (!isPowerOf2_32(VT.getVectorNumElements()))
884 continue;
885
886 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
887 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
888 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
889 }
890
891 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000892 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i64, Custom);
893 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i64, Custom);
Eric Christopherfd179292009-08-27 18:07:15 +0000894 }
David Greene9b9838d2009-06-29 16:47:10 +0000895#endif
896
897#if 0
898 // Not sure we want to do this since there are no 256-bit integer
899 // operations in AVX
900
901 // Promote v32i8, v16i16, v8i32 load, select, and, or, xor to v4i64.
902 // Including 256-bit vectors
Owen Anderson825b72b2009-08-11 20:47:22 +0000903 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; i++) {
904 EVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000905
906 if (!VT.is256BitVector()) {
907 continue;
908 }
909 setOperationAction(ISD::AND, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000910 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000911 setOperationAction(ISD::OR, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000912 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000913 setOperationAction(ISD::XOR, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000914 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000915 setOperationAction(ISD::LOAD, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000916 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000917 setOperationAction(ISD::SELECT, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000918 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000919 }
920
Owen Anderson825b72b2009-08-11 20:47:22 +0000921 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
David Greene9b9838d2009-06-29 16:47:10 +0000922#endif
923 }
924
Evan Cheng6be2c582006-04-05 23:38:46 +0000925 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000926 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +0000927
Bill Wendling74c37652008-12-09 22:08:41 +0000928 // Add/Sub/Mul with overflow operations are custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000929 setOperationAction(ISD::SADDO, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000930 setOperationAction(ISD::UADDO, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000931 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000932 setOperationAction(ISD::USUBO, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000933 setOperationAction(ISD::SMULO, MVT::i32, Custom);
Dan Gohman71c62a22010-06-02 19:13:40 +0000934
Eli Friedman962f5492010-06-02 19:35:46 +0000935 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
936 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +0000937 //
Eli Friedman962f5492010-06-02 19:35:46 +0000938 // FIXME: We really should do custom legalization for addition and
939 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
940 // than generic legalization for 64-bit multiplication-with-overflow, though.
Eli Friedmana993f0a2010-06-02 00:27:18 +0000941 if (Subtarget->is64Bit()) {
942 setOperationAction(ISD::SADDO, MVT::i64, Custom);
943 setOperationAction(ISD::UADDO, MVT::i64, Custom);
944 setOperationAction(ISD::SSUBO, MVT::i64, Custom);
945 setOperationAction(ISD::USUBO, MVT::i64, Custom);
946 setOperationAction(ISD::SMULO, MVT::i64, Custom);
947 }
Bill Wendling41ea7e72008-11-24 19:21:46 +0000948
Evan Chengd54f2d52009-03-31 19:38:51 +0000949 if (!Subtarget->is64Bit()) {
950 // These libcalls are not available in 32-bit.
951 setLibcallName(RTLIB::SHL_I128, 0);
952 setLibcallName(RTLIB::SRL_I128, 0);
953 setLibcallName(RTLIB::SRA_I128, 0);
954 }
955
Evan Cheng206ee9d2006-07-07 08:33:52 +0000956 // We have target-specific dag combine patterns for the following nodes:
957 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +0000958 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +0000959 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +0000960 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +0000961 setTargetDAGCombine(ISD::SHL);
962 setTargetDAGCombine(ISD::SRA);
963 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +0000964 setTargetDAGCombine(ISD::OR);
Chris Lattner149a4e52008-02-22 02:09:43 +0000965 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +0000966 setTargetDAGCombine(ISD::ZERO_EXTEND);
Evan Cheng0b0cd912009-03-28 05:57:29 +0000967 if (Subtarget->is64Bit())
968 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +0000969
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000970 computeRegisterProperties();
971
Evan Cheng87ed7162006-02-14 08:25:08 +0000972 // FIXME: These should be based on subtarget info. Plus, the values should
973 // be smaller when we are in optimizing for size mode.
Dan Gohman87060f52008-06-30 21:00:56 +0000974 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng255f20f2010-04-01 06:04:33 +0000975 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Dan Gohman87060f52008-06-30 21:00:56 +0000976 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Evan Chengfb8075d2008-02-28 00:43:03 +0000977 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +0000978 benefitFromCodePlacementOpt = true;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000979}
980
Scott Michel5b8f82e2008-03-10 15:42:14 +0000981
Owen Anderson825b72b2009-08-11 20:47:22 +0000982MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
983 return MVT::i8;
Scott Michel5b8f82e2008-03-10 15:42:14 +0000984}
985
986
Evan Cheng29286502008-01-23 23:17:41 +0000987/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
988/// the desired ByVal argument alignment.
989static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
990 if (MaxAlign == 16)
991 return;
992 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
993 if (VTy->getBitWidth() == 128)
994 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +0000995 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
996 unsigned EltAlign = 0;
997 getMaxByValAlign(ATy->getElementType(), EltAlign);
998 if (EltAlign > MaxAlign)
999 MaxAlign = EltAlign;
1000 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1001 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1002 unsigned EltAlign = 0;
1003 getMaxByValAlign(STy->getElementType(i), EltAlign);
1004 if (EltAlign > MaxAlign)
1005 MaxAlign = EltAlign;
1006 if (MaxAlign == 16)
1007 break;
1008 }
1009 }
1010 return;
1011}
1012
1013/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1014/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001015/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1016/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +00001017unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001018 if (Subtarget->is64Bit()) {
1019 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001020 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001021 if (TyAlign > 8)
1022 return TyAlign;
1023 return 8;
1024 }
1025
Evan Cheng29286502008-01-23 23:17:41 +00001026 unsigned Align = 4;
Dale Johannesen0c191872008-02-08 19:48:20 +00001027 if (Subtarget->hasSSE1())
1028 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001029 return Align;
1030}
Chris Lattner2b02a442007-02-25 08:29:00 +00001031
Evan Chengf0df0312008-05-15 08:39:06 +00001032/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001033/// and store operations as a result of memset, memcpy, and memmove
1034/// lowering. If DstAlign is zero that means it's safe to destination
1035/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1036/// means there isn't a need to check it against alignment requirement,
1037/// probably because the source does not need to be loaded. If
1038/// 'NonScalarIntSafe' is true, that means it's safe to return a
1039/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1040/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1041/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001042/// It returns EVT::Other if the type should be determined using generic
1043/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001044EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001045X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1046 unsigned DstAlign, unsigned SrcAlign,
Evan Chengf28f8bc2010-04-02 19:36:14 +00001047 bool NonScalarIntSafe,
Evan Chengc3b0c342010-04-08 07:37:57 +00001048 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001049 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001050 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1051 // linux. This is because the stack realignment code can't handle certain
1052 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001053 const Function *F = MF.getFunction();
Evan Chengf28f8bc2010-04-02 19:36:14 +00001054 if (NonScalarIntSafe &&
1055 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001056 if (Size >= 16 &&
1057 (Subtarget->isUnalignedMemAccessFast() ||
Chandler Carruthae1d41c2010-04-02 01:31:24 +00001058 ((DstAlign == 0 || DstAlign >= 16) &&
1059 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001060 Subtarget->getStackAlignment() >= 16) {
1061 if (Subtarget->hasSSE2())
1062 return MVT::v4i32;
Evan Chengf28f8bc2010-04-02 19:36:14 +00001063 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001064 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001065 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001066 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001067 Subtarget->getStackAlignment() >= 8 &&
Evan Chengc3b0c342010-04-08 07:37:57 +00001068 Subtarget->hasSSE2()) {
1069 // Do not use f64 to lower memcpy if source is string constant. It's
1070 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001071 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001072 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001073 }
Evan Chengf0df0312008-05-15 08:39:06 +00001074 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001075 return MVT::i64;
1076 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001077}
1078
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001079/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1080/// current function. The returned value is a member of the
1081/// MachineJumpTableInfo::JTEntryKind enum.
1082unsigned X86TargetLowering::getJumpTableEncoding() const {
1083 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1084 // symbol.
1085 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1086 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001087 return MachineJumpTableInfo::EK_Custom32;
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001088
1089 // Otherwise, use the normal jump table encoding heuristics.
1090 return TargetLowering::getJumpTableEncoding();
1091}
1092
Chris Lattner589c6f62010-01-26 06:28:43 +00001093/// getPICBaseSymbol - Return the X86-32 PIC base.
1094MCSymbol *
1095X86TargetLowering::getPICBaseSymbol(const MachineFunction *MF,
1096 MCContext &Ctx) const {
1097 const MCAsmInfo &MAI = *getTargetMachine().getMCAsmInfo();
Chris Lattner9b97a732010-03-30 18:10:53 +00001098 return Ctx.GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix())+
1099 Twine(MF->getFunctionNumber())+"$pb");
Chris Lattner589c6f62010-01-26 06:28:43 +00001100}
1101
1102
Chris Lattnerc64daab2010-01-26 05:02:42 +00001103const MCExpr *
1104X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1105 const MachineBasicBlock *MBB,
1106 unsigned uid,MCContext &Ctx) const{
1107 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1108 Subtarget->isPICStyleGOT());
1109 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1110 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001111 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1112 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001113}
1114
Evan Chengcc415862007-11-09 01:32:10 +00001115/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1116/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001117SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001118 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001119 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001120 // This doesn't have DebugLoc associated with it, but is not really the
1121 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001122 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001123 return Table;
1124}
1125
Chris Lattner589c6f62010-01-26 06:28:43 +00001126/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1127/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1128/// MCExpr.
1129const MCExpr *X86TargetLowering::
1130getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1131 MCContext &Ctx) const {
1132 // X86-64 uses RIP relative addressing based on the jump table label.
1133 if (Subtarget->isPICStyleRIPRel())
1134 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1135
1136 // Otherwise, the reference is relative to the PIC base.
1137 return MCSymbolRefExpr::Create(getPICBaseSymbol(MF, Ctx), Ctx);
1138}
1139
Bill Wendlingb4202b82009-07-01 18:50:55 +00001140/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +00001141unsigned X86TargetLowering::getFunctionAlignment(const Function *F) const {
Dan Gohman25103a22009-08-18 00:20:06 +00001142 return F->hasFnAttr(Attribute::OptimizeForSize) ? 0 : 4;
Bill Wendling20c568f2009-06-30 22:38:32 +00001143}
1144
Evan Chengdee81012010-07-26 21:50:05 +00001145std::pair<const TargetRegisterClass*, uint8_t>
1146X86TargetLowering::findRepresentativeClass(EVT VT) const{
1147 const TargetRegisterClass *RRC = 0;
1148 uint8_t Cost = 1;
1149 switch (VT.getSimpleVT().SimpleTy) {
1150 default:
1151 return TargetLowering::findRepresentativeClass(VT);
1152 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1153 RRC = (Subtarget->is64Bit()
1154 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1155 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001156 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001157 RRC = X86::VR64RegisterClass;
1158 break;
1159 case MVT::f32: case MVT::f64:
1160 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1161 case MVT::v4f32: case MVT::v2f64:
1162 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1163 case MVT::v4f64:
1164 RRC = X86::VR128RegisterClass;
1165 break;
1166 }
1167 return std::make_pair(RRC, Cost);
1168}
1169
Evan Cheng70017e42010-07-24 00:39:05 +00001170unsigned
1171X86TargetLowering::getRegPressureLimit(const TargetRegisterClass *RC,
1172 MachineFunction &MF) const {
1173 unsigned FPDiff = RegInfo->hasFP(MF) ? 1 : 0;
1174 switch (RC->getID()) {
1175 default:
1176 return 0;
1177 case X86::GR32RegClassID:
1178 return 4 - FPDiff;
1179 case X86::GR64RegClassID:
1180 return 8 - FPDiff;
1181 case X86::VR128RegClassID:
1182 return Subtarget->is64Bit() ? 10 : 4;
1183 case X86::VR64RegClassID:
1184 return 4;
1185 }
1186}
1187
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001188bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1189 unsigned &Offset) const {
1190 if (!Subtarget->isTargetLinux())
1191 return false;
1192
1193 if (Subtarget->is64Bit()) {
1194 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1195 Offset = 0x28;
1196 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1197 AddressSpace = 256;
1198 else
1199 AddressSpace = 257;
1200 } else {
1201 // %gs:0x14 on i386
1202 Offset = 0x14;
1203 AddressSpace = 256;
1204 }
1205 return true;
1206}
1207
1208
Chris Lattner2b02a442007-02-25 08:29:00 +00001209//===----------------------------------------------------------------------===//
1210// Return Value Calling Convention Implementation
1211//===----------------------------------------------------------------------===//
1212
Chris Lattner59ed56b2007-02-28 04:55:35 +00001213#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001214
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001215bool
1216X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001217 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001218 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001219 SmallVector<CCValAssign, 16> RVLocs;
1220 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001221 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001222 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001223}
1224
Dan Gohman98ca4f22009-08-05 01:29:28 +00001225SDValue
1226X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001227 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001228 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001229 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001230 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001231 MachineFunction &MF = DAG.getMachineFunction();
1232 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001233
Chris Lattner9774c912007-02-27 05:28:59 +00001234 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001235 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1236 RVLocs, *DAG.getContext());
1237 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001238
Evan Chengdcea1632010-02-04 02:40:39 +00001239 // Add the regs to the liveout set for the function.
1240 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1241 for (unsigned i = 0; i != RVLocs.size(); ++i)
1242 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1243 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001244
Dan Gohman475871a2008-07-27 21:46:04 +00001245 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001246
Dan Gohman475871a2008-07-27 21:46:04 +00001247 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001248 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1249 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001250 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1251 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001252
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001253 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001254 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1255 CCValAssign &VA = RVLocs[i];
1256 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001257 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001258 EVT ValVT = ValToCopy.getValueType();
1259
Dale Johannesenc4510512010-09-24 19:05:48 +00001260 // If this is x86-64, and we disabled SSE, we can't return FP values,
1261 // or SSE or MMX vectors.
1262 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1263 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
1264 (Subtarget->is64Bit() && !Subtarget->hasSSE1())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001265 report_fatal_error("SSE register return with SSE disabled");
1266 }
1267 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1268 // llvm-gcc has never done it right and no one has noticed, so this
1269 // should be OK for now.
1270 if (ValVT == MVT::f64 &&
Chris Lattner83069682010-08-26 05:51:22 +00001271 (Subtarget->is64Bit() && !Subtarget->hasSSE2()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001272 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001273
Chris Lattner447ff682008-03-11 03:23:40 +00001274 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1275 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001276 if (VA.getLocReg() == X86::ST0 ||
1277 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001278 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1279 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001280 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001281 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001282 RetOps.push_back(ValToCopy);
1283 // Don't emit a copytoreg.
1284 continue;
1285 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001286
Evan Cheng242b38b2009-02-23 09:03:22 +00001287 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1288 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001289 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001290 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001291 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001292 ValToCopy = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001293 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1294 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001295 // If we don't have SSE2 available, convert to v4f32 so the generated
1296 // register is legal.
1297 if (!Subtarget->hasSSE2())
1298 ValToCopy = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32,ValToCopy);
1299 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001300 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001301 }
Chris Lattner97a2a562010-08-26 05:24:29 +00001302
Dale Johannesendd64c412009-02-04 00:33:20 +00001303 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001304 Flag = Chain.getValue(1);
1305 }
Dan Gohman61a92132008-04-21 23:59:07 +00001306
1307 // The x86-64 ABI for returning structs by value requires that we copy
1308 // the sret argument into %rax for the return. We saved the argument into
1309 // a virtual register in the entry block, so now we copy the value out
1310 // and into %rax.
1311 if (Subtarget->is64Bit() &&
1312 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1313 MachineFunction &MF = DAG.getMachineFunction();
1314 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1315 unsigned Reg = FuncInfo->getSRetReturnReg();
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001316 assert(Reg &&
1317 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001318 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001319
Dale Johannesendd64c412009-02-04 00:33:20 +00001320 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001321 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001322
1323 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001324 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001325 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001326
Chris Lattner447ff682008-03-11 03:23:40 +00001327 RetOps[0] = Chain; // Update chain.
1328
1329 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001330 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001331 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001332
1333 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001334 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001335}
1336
Dan Gohman98ca4f22009-08-05 01:29:28 +00001337/// LowerCallResult - Lower the result values of a call into the
1338/// appropriate copies out of appropriate physical registers.
1339///
1340SDValue
1341X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001342 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001343 const SmallVectorImpl<ISD::InputArg> &Ins,
1344 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001345 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001346
Chris Lattnere32bbf62007-02-28 07:09:55 +00001347 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001348 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001349 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001350 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001351 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001352 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001353
Chris Lattner3085e152007-02-25 08:59:22 +00001354 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001355 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001356 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001357 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001358
Torok Edwin3f142c32009-02-01 18:15:56 +00001359 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001360 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Dan Gohman98ca4f22009-08-05 01:29:28 +00001361 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001362 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001363 }
1364
Evan Cheng79fb3b42009-02-20 20:43:02 +00001365 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001366
1367 // If this is a call to a function that returns an fp value on the floating
1368 // point stack, we must guarantee the the value is popped from the stack, so
1369 // a CopyFromReg is not good enough - the copy instruction may be eliminated
1370 // if the return value is not used. We use the FpGET_ST0 instructions
1371 // instead.
1372 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1373 // If we prefer to use the value in xmm registers, copy it out as f80 and
1374 // use a truncate to move it from fp stack reg to xmm reg.
1375 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
1376 bool isST0 = VA.getLocReg() == X86::ST0;
1377 unsigned Opc = 0;
1378 if (CopyVT == MVT::f32) Opc = isST0 ? X86::FpGET_ST0_32:X86::FpGET_ST1_32;
1379 if (CopyVT == MVT::f64) Opc = isST0 ? X86::FpGET_ST0_64:X86::FpGET_ST1_64;
1380 if (CopyVT == MVT::f80) Opc = isST0 ? X86::FpGET_ST0_80:X86::FpGET_ST1_80;
1381 SDValue Ops[] = { Chain, InFlag };
1382 Chain = SDValue(DAG.getMachineNode(Opc, dl, CopyVT, MVT::Other, MVT::Flag,
1383 Ops, 2), 1);
1384 Val = Chain.getValue(0);
1385
1386 // Round the f80 to the right size, which also moves it to the appropriate
1387 // xmm register.
1388 if (CopyVT != VA.getValVT())
1389 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1390 // This truncation won't change the value.
1391 DAG.getIntPtrConstant(1));
1392 } else if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001393 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1394 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1395 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001396 MVT::v2i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001397 Val = Chain.getValue(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001398 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1399 Val, DAG.getConstant(0, MVT::i64));
Evan Cheng242b38b2009-02-23 09:03:22 +00001400 } else {
1401 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001402 MVT::i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001403 Val = Chain.getValue(0);
1404 }
Evan Cheng79fb3b42009-02-20 20:43:02 +00001405 Val = DAG.getNode(ISD::BIT_CONVERT, dl, CopyVT, Val);
1406 } else {
1407 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1408 CopyVT, InFlag).getValue(1);
1409 Val = Chain.getValue(0);
1410 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001411 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001412 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001413 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001414
Dan Gohman98ca4f22009-08-05 01:29:28 +00001415 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001416}
1417
1418
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001419//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001420// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001421//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001422// StdCall calling convention seems to be standard for many Windows' API
1423// routines and around. It differs from C calling convention just a little:
1424// callee should clean up the stack, not caller. Symbols should be also
1425// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001426// For info on fast calling convention see Fast Calling Convention (tail call)
1427// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001428
Dan Gohman98ca4f22009-08-05 01:29:28 +00001429/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001430/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001431static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1432 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001433 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001434
Dan Gohman98ca4f22009-08-05 01:29:28 +00001435 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001436}
1437
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001438/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001439/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001440static bool
1441ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1442 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001443 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001444
Dan Gohman98ca4f22009-08-05 01:29:28 +00001445 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001446}
1447
Dan Gohman095cc292008-09-13 01:54:27 +00001448/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1449/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001450CCAssignFn *X86TargetLowering::CCAssignFnForNode(CallingConv::ID CC) const {
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001451 if (Subtarget->is64Bit()) {
Chris Lattner29689432010-03-11 00:22:57 +00001452 if (CC == CallingConv::GHC)
1453 return CC_X86_64_GHC;
1454 else if (Subtarget->isTargetWin64())
Anton Korobeynikov8f88cb02008-03-22 20:37:30 +00001455 return CC_X86_Win64_C;
Evan Chenge9ac9e62008-09-07 09:07:23 +00001456 else
1457 return CC_X86_64_C;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001458 }
1459
Gordon Henriksen86737662008-01-05 16:56:59 +00001460 if (CC == CallingConv::X86_FastCall)
1461 return CC_X86_32_FastCall;
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001462 else if (CC == CallingConv::X86_ThisCall)
1463 return CC_X86_32_ThisCall;
Evan Chengb188dd92008-09-10 18:25:29 +00001464 else if (CC == CallingConv::Fast)
1465 return CC_X86_32_FastCC;
Chris Lattner29689432010-03-11 00:22:57 +00001466 else if (CC == CallingConv::GHC)
1467 return CC_X86_32_GHC;
Gordon Henriksen86737662008-01-05 16:56:59 +00001468 else
1469 return CC_X86_32_C;
1470}
1471
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001472/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1473/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001474/// the specific parameter attribute. The copy will be passed as a byval
1475/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001476static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001477CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001478 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1479 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001480 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
1481
Dale Johannesendd64c412009-02-04 00:33:20 +00001482 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001483 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001484 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001485}
1486
Chris Lattner29689432010-03-11 00:22:57 +00001487/// IsTailCallConvention - Return true if the calling convention is one that
1488/// supports tail call optimization.
1489static bool IsTailCallConvention(CallingConv::ID CC) {
1490 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1491}
1492
Evan Cheng0c439eb2010-01-27 00:07:07 +00001493/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1494/// a tailcall target by changing its ABI.
1495static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001496 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001497}
1498
Dan Gohman98ca4f22009-08-05 01:29:28 +00001499SDValue
1500X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001501 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001502 const SmallVectorImpl<ISD::InputArg> &Ins,
1503 DebugLoc dl, SelectionDAG &DAG,
1504 const CCValAssign &VA,
1505 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001506 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001507 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001508 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001509 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001510 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001511 EVT ValVT;
1512
1513 // If value is passed by pointer we have address passed instead of the value
1514 // itself.
1515 if (VA.getLocInfo() == CCValAssign::Indirect)
1516 ValVT = VA.getLocVT();
1517 else
1518 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001519
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001520 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001521 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001522 // In case of tail call optimization mark all arguments mutable. Since they
1523 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001524 if (Flags.isByVal()) {
1525 int FI = MFI->CreateFixedObject(Flags.getByValSize(),
Evan Chenged2ae132010-07-03 00:40:23 +00001526 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001527 return DAG.getFrameIndex(FI, getPointerTy());
1528 } else {
1529 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001530 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001531 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1532 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001533 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001534 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001535 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001536}
1537
Dan Gohman475871a2008-07-27 21:46:04 +00001538SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001539X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001540 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001541 bool isVarArg,
1542 const SmallVectorImpl<ISD::InputArg> &Ins,
1543 DebugLoc dl,
1544 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001545 SmallVectorImpl<SDValue> &InVals)
1546 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001547 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001548 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001549
Gordon Henriksen86737662008-01-05 16:56:59 +00001550 const Function* Fn = MF.getFunction();
1551 if (Fn->hasExternalLinkage() &&
1552 Subtarget->isTargetCygMing() &&
1553 Fn->getName() == "main")
1554 FuncInfo->setForceFramePointer(true);
1555
Evan Cheng1bc78042006-04-26 01:20:17 +00001556 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001557 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001558 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001559
Chris Lattner29689432010-03-11 00:22:57 +00001560 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1561 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001562
Chris Lattner638402b2007-02-28 07:00:42 +00001563 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001564 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001565 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1566 ArgLocs, *DAG.getContext());
1567 CCInfo.AnalyzeFormalArguments(Ins, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001568
Chris Lattnerf39f7712007-02-28 05:46:49 +00001569 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001570 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001571 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1572 CCValAssign &VA = ArgLocs[i];
1573 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1574 // places.
1575 assert(VA.getValNo() != LastVal &&
1576 "Don't support value assigned to multiple locs yet");
1577 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001578
Chris Lattnerf39f7712007-02-28 05:46:49 +00001579 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001580 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001581 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001582 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001583 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001584 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001585 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001586 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001587 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001588 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001589 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001590 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1591 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001592 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001593 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001594 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001595 RC = X86::VR64RegisterClass;
1596 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001597 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001598
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001599 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001600 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001601
Chris Lattnerf39f7712007-02-28 05:46:49 +00001602 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1603 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1604 // right size.
1605 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001606 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001607 DAG.getValueType(VA.getValVT()));
1608 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001609 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001610 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001611 else if (VA.getLocInfo() == CCValAssign::BCvt)
Anton Korobeynikov6dde14b2009-08-03 08:14:14 +00001612 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001613
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001614 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001615 // Handle MMX values passed in XMM regs.
1616 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001617 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1618 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001619 } else
1620 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001621 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001622 } else {
1623 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001624 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001625 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001626
1627 // If value is passed via pointer - do a load.
1628 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001629 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
1630 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001631
Dan Gohman98ca4f22009-08-05 01:29:28 +00001632 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001633 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001634
Dan Gohman61a92132008-04-21 23:59:07 +00001635 // The x86-64 ABI for returning structs by value requires that we copy
1636 // the sret argument into %rax for the return. Save the argument into
1637 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001638 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001639 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1640 unsigned Reg = FuncInfo->getSRetReturnReg();
1641 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001642 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001643 FuncInfo->setSRetReturnReg(Reg);
1644 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001645 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001646 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001647 }
1648
Chris Lattnerf39f7712007-02-28 05:46:49 +00001649 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001650 // Align stack specially for tail calls.
1651 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001652 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001653
Evan Cheng1bc78042006-04-26 01:20:17 +00001654 // If the function takes variable number of arguments, make a frame index for
1655 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001656 if (isVarArg) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001657 if (!IsWin64 && (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1658 CallConv != CallingConv::X86_ThisCall))) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001659 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001660 }
1661 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001662 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1663
1664 // FIXME: We should really autogenerate these arrays
1665 static const unsigned GPR64ArgRegsWin64[] = {
1666 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001667 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001668 static const unsigned GPR64ArgRegs64Bit[] = {
1669 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1670 };
1671 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001672 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1673 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1674 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001675 const unsigned *GPR64ArgRegs;
1676 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001677
1678 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001679 // The XMM registers which might contain var arg parameters are shadowed
1680 // in their paired GPR. So we only need to save the GPR to their home
1681 // slots.
1682 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001683 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001684 } else {
1685 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1686 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001687
1688 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001689 }
1690 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1691 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001692
Devang Patel578efa92009-06-05 21:57:13 +00001693 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Evan Chengc7ce29b2009-02-13 22:36:38 +00001694 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001695 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001696 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001697 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001698 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00001699 // Kernel mode asks for SSE to be disabled, so don't push them
1700 // on the stack.
1701 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001702
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001703 if (IsWin64) {
1704 FuncInfo->setRegSaveFrameIndex(
Andrew Trick59002012010-10-08 17:22:42 +00001705 MFI->CreateFixedObject(1, NumIntRegs * 8, false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001706 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
1707 } else {
1708 // For X86-64, if there are vararg parameters that are passed via
1709 // registers, then we must store them to their spots on the stack so they
1710 // may be loaded by deferencing the result of va_next.
1711 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1712 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1713 FuncInfo->setRegSaveFrameIndex(
1714 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001715 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001716 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001717
Gordon Henriksen86737662008-01-05 16:56:59 +00001718 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001719 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001720 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1721 getPointerTy());
1722 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001723 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001724 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1725 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001726 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
1727 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001728 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001729 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001730 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001731 MachinePointerInfo::getFixedStack(
1732 FuncInfo->getRegSaveFrameIndex(), Offset),
1733 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001734 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001735 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001736 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001737
Dan Gohmanface41a2009-08-16 21:24:25 +00001738 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1739 // Now store the XMM (fp + vector) parameter registers.
1740 SmallVector<SDValue, 11> SaveXMMOps;
1741 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001742
Dan Gohmanface41a2009-08-16 21:24:25 +00001743 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
1744 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1745 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001746
Dan Gohman1e93df62010-04-17 14:41:14 +00001747 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1748 FuncInfo->getRegSaveFrameIndex()));
1749 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1750 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001751
Dan Gohmanface41a2009-08-16 21:24:25 +00001752 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001753 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Dan Gohmanface41a2009-08-16 21:24:25 +00001754 X86::VR128RegisterClass);
1755 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1756 SaveXMMOps.push_back(Val);
1757 }
1758 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1759 MVT::Other,
1760 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001761 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001762
1763 if (!MemOps.empty())
1764 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1765 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001766 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001767 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001768
Gordon Henriksen86737662008-01-05 16:56:59 +00001769 // Some CCs need callee pop.
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001770 if (Subtarget->IsCalleePop(isVarArg, CallConv)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001771 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001772 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001773 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001774 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001775 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001776 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001777 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001778
Gordon Henriksen86737662008-01-05 16:56:59 +00001779 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001780 // RegSaveFrameIndex is X86-64 only.
1781 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001782 if (CallConv == CallingConv::X86_FastCall ||
1783 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001784 // fastcc functions can't have varargs.
1785 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001786 }
Evan Cheng25caf632006-05-23 21:06:34 +00001787
Dan Gohman98ca4f22009-08-05 01:29:28 +00001788 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001789}
1790
Dan Gohman475871a2008-07-27 21:46:04 +00001791SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001792X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1793 SDValue StackPtr, SDValue Arg,
1794 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001795 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001796 ISD::ArgFlagsTy Flags) const {
Anton Korobeynikovc7c62bb2010-09-02 22:31:32 +00001797 const unsigned FirstStackArgOffset = (Subtarget->isTargetWin64() ? 32 : 0);
1798 unsigned LocMemOffset = FirstStackArgOffset + VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001799 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001800 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001801 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00001802 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001803
1804 return DAG.getStore(Chain, dl, Arg, PtrOff,
1805 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00001806 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001807}
1808
Bill Wendling64e87322009-01-16 19:25:27 +00001809/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001810/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001811SDValue
1812X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001813 SDValue &OutRetAddr, SDValue Chain,
1814 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001815 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001816 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001817 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001818 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001819
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001820 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00001821 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
1822 false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001823 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001824}
1825
1826/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1827/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001828static SDValue
1829EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001830 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001831 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001832 // Store the return address to the appropriate stack slot.
1833 if (!FPDiff) return Chain;
1834 // Calculate the new stack slot for the return address.
1835 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001836 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00001837 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001838 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001839 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001840 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00001841 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00001842 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001843 return Chain;
1844}
1845
Dan Gohman98ca4f22009-08-05 01:29:28 +00001846SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001847X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001848 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001849 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001850 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001851 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001852 const SmallVectorImpl<ISD::InputArg> &Ins,
1853 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001854 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001855 MachineFunction &MF = DAG.getMachineFunction();
1856 bool Is64Bit = Subtarget->is64Bit();
1857 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00001858 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001859
Evan Cheng5f941932010-02-05 02:21:12 +00001860 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001861 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00001862 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1863 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001864 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00001865
1866 // Sibcalls are automatically detected tailcalls which do not require
1867 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00001868 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00001869 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00001870
1871 if (isTailCall)
1872 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00001873 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00001874
Chris Lattner29689432010-03-11 00:22:57 +00001875 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1876 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001877
Chris Lattner638402b2007-02-28 07:00:42 +00001878 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001879 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001880 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1881 ArgLocs, *DAG.getContext());
1882 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001883
Chris Lattner423c5f42007-02-28 05:31:48 +00001884 // Get a count of how many bytes are to be pushed on the stack.
1885 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00001886 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00001887 // This is a sibcall. The memory operands are available in caller's
1888 // own caller's stack.
1889 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00001890 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00001891 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001892
Gordon Henriksen86737662008-01-05 16:56:59 +00001893 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00001894 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001895 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00001896 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00001897 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1898 FPDiff = NumBytesCallerPushed - NumBytes;
1899
1900 // Set the delta of movement of the returnaddr stackslot.
1901 // But only set if delta is greater than previous delta.
1902 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1903 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1904 }
1905
Evan Chengf22f9b32010-02-06 03:28:46 +00001906 if (!IsSibcall)
1907 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001908
Dan Gohman475871a2008-07-27 21:46:04 +00001909 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001910 // Load return adress for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00001911 if (isTailCall && FPDiff)
1912 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
1913 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001914
Dan Gohman475871a2008-07-27 21:46:04 +00001915 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1916 SmallVector<SDValue, 8> MemOpChains;
1917 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001918
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001919 // Walk the register/memloc assignments, inserting copies/loads. In the case
1920 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00001921 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1922 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001923 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001924 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001925 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00001926 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00001927
Chris Lattner423c5f42007-02-28 05:31:48 +00001928 // Promote the value if needed.
1929 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001930 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00001931 case CCValAssign::Full: break;
1932 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001933 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001934 break;
1935 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001936 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001937 break;
1938 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001939 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
1940 // Special case: passing MMX values in XMM registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001941 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
1942 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
1943 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001944 } else
1945 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
1946 break;
1947 case CCValAssign::BCvt:
1948 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001949 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001950 case CCValAssign::Indirect: {
1951 // Store the argument.
1952 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00001953 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001954 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00001955 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001956 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001957 Arg = SpillSlot;
1958 break;
1959 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00001960 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001961
Chris Lattner423c5f42007-02-28 05:31:48 +00001962 if (VA.isRegLoc()) {
1963 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00001964 if (isVarArg && Subtarget->isTargetWin64()) {
1965 // Win64 ABI requires argument XMM reg to be copied to the corresponding
1966 // shadow reg if callee is a varargs function.
1967 unsigned ShadowReg = 0;
1968 switch (VA.getLocReg()) {
1969 case X86::XMM0: ShadowReg = X86::RCX; break;
1970 case X86::XMM1: ShadowReg = X86::RDX; break;
1971 case X86::XMM2: ShadowReg = X86::R8; break;
1972 case X86::XMM3: ShadowReg = X86::R9; break;
1973 }
1974 if (ShadowReg)
1975 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
1976 }
Evan Chengf22f9b32010-02-06 03:28:46 +00001977 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00001978 assert(VA.isMemLoc());
1979 if (StackPtr.getNode() == 0)
1980 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
1981 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1982 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001983 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001984 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001985
Evan Cheng32fe1032006-05-25 00:59:30 +00001986 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001987 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001988 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001989
Evan Cheng347d5f72006-04-28 21:29:37 +00001990 // Build a sequence of copy-to-reg nodes chained together with token chain
1991 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001992 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001993 // Tail call byval lowering might overwrite argument registers so in case of
1994 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001995 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001996 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001997 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001998 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001999 InFlag = Chain.getValue(1);
2000 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002001
Chris Lattner88e1fd52009-07-09 04:24:46 +00002002 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002003 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2004 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002005 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002006 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2007 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002008 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002009 InFlag);
2010 InFlag = Chain.getValue(1);
2011 } else {
2012 // If we are tail calling and generating PIC/GOT style code load the
2013 // address of the callee into ECX. The value in ecx is used as target of
2014 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2015 // for tail calls on PIC/GOT architectures. Normally we would just put the
2016 // address of GOT into ebx and then call target@PLT. But for tail calls
2017 // ebx would be restored (since ebx is callee saved) before jumping to the
2018 // target@PLT.
2019
2020 // Note: The actual moving to ECX is done further down.
2021 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2022 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2023 !G->getGlobal()->hasProtectedVisibility())
2024 Callee = LowerGlobalAddress(Callee, DAG);
2025 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002026 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002027 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002028 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002029
Nate Begemanc8ea6732010-07-21 20:49:52 +00002030 if (Is64Bit && isVarArg && !Subtarget->isTargetWin64()) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002031 // From AMD64 ABI document:
2032 // For calls that may call functions that use varargs or stdargs
2033 // (prototype-less calls or calls to functions containing ellipsis (...) in
2034 // the declaration) %al is used as hidden argument to specify the number
2035 // of SSE registers used. The contents of %al do not need to match exactly
2036 // the number of registers, but must be an ubound on the number of SSE
2037 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002038
Gordon Henriksen86737662008-01-05 16:56:59 +00002039 // Count the number of XMM registers allocated.
2040 static const unsigned XMMArgRegs[] = {
2041 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2042 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2043 };
2044 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Scott Michelfdc40a02009-02-17 22:15:04 +00002045 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002046 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002047
Dale Johannesendd64c412009-02-04 00:33:20 +00002048 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002049 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002050 InFlag = Chain.getValue(1);
2051 }
2052
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002053
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002054 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002055 if (isTailCall) {
2056 // Force all the incoming stack arguments to be loaded from the stack
2057 // before any new outgoing arguments are stored to the stack, because the
2058 // outgoing stack slots may alias the incoming argument stack slots, and
2059 // the alias isn't otherwise explicit. This is slightly more conservative
2060 // than necessary, because it means that each store effectively depends
2061 // on every argument instead of just those arguments it would clobber.
2062 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2063
Dan Gohman475871a2008-07-27 21:46:04 +00002064 SmallVector<SDValue, 8> MemOpChains2;
2065 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002066 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002067 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002068 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002069 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002070 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2071 CCValAssign &VA = ArgLocs[i];
2072 if (VA.isRegLoc())
2073 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002074 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002075 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002076 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002077 // Create frame index.
2078 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002079 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002080 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002081 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002082
Duncan Sands276dcbd2008-03-21 09:14:45 +00002083 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002084 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002085 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002086 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002087 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002088 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002089 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002090
Dan Gohman98ca4f22009-08-05 01:29:28 +00002091 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2092 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002093 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002094 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002095 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002096 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002097 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002098 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002099 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002100 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002101 }
2102 }
2103
2104 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002105 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002106 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002107
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002108 // Copy arguments to their registers.
2109 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002110 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002111 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002112 InFlag = Chain.getValue(1);
2113 }
Dan Gohman475871a2008-07-27 21:46:04 +00002114 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002115
Gordon Henriksen86737662008-01-05 16:56:59 +00002116 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002117 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002118 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002119 }
2120
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002121 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2122 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2123 // In the 64-bit large code model, we have to make all calls
2124 // through a register, since the call instruction's 32-bit
2125 // pc-relative offset may not be large enough to hold the whole
2126 // address.
2127 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002128 // If the callee is a GlobalAddress node (quite common, every direct call
2129 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2130 // it.
2131
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002132 // We should use extra load for direct calls to dllimported functions in
2133 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002134 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002135 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002136 unsigned char OpFlags = 0;
Eric Christopherfd179292009-08-27 18:07:15 +00002137
Chris Lattner48a7d022009-07-09 05:02:21 +00002138 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2139 // external symbols most go through the PLT in PIC mode. If the symbol
2140 // has hidden or protected visibility, or if it is static or local, then
2141 // we don't need to use the PLT - we can directly call it.
2142 if (Subtarget->isTargetELF() &&
2143 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002144 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002145 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002146 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002147 (GV->isDeclaration() || GV->isWeakForLinker()) &&
2148 Subtarget->getDarwinVers() < 9) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002149 // PC-relative references to external symbols should go through $stub,
2150 // unless we're building with the leopard linker or later, which
2151 // automatically synthesizes these stubs.
2152 OpFlags = X86II::MO_DARWIN_STUB;
2153 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002154
Devang Patel0d881da2010-07-06 22:08:15 +00002155 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002156 G->getOffset(), OpFlags);
2157 }
Bill Wendling056292f2008-09-16 21:48:12 +00002158 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002159 unsigned char OpFlags = 0;
2160
2161 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to external
2162 // symbols should go through the PLT.
2163 if (Subtarget->isTargetELF() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002164 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002165 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002166 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002167 Subtarget->getDarwinVers() < 9) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002168 // PC-relative references to external symbols should go through $stub,
2169 // unless we're building with the leopard linker or later, which
2170 // automatically synthesizes these stubs.
2171 OpFlags = X86II::MO_DARWIN_STUB;
2172 }
Eric Christopherfd179292009-08-27 18:07:15 +00002173
Chris Lattner48a7d022009-07-09 05:02:21 +00002174 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2175 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002176 }
2177
Chris Lattnerd96d0722007-02-25 06:40:16 +00002178 // Returns a chain & a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +00002179 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00002180 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002181
Evan Chengf22f9b32010-02-06 03:28:46 +00002182 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002183 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2184 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002185 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002186 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002187
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002188 Ops.push_back(Chain);
2189 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002190
Dan Gohman98ca4f22009-08-05 01:29:28 +00002191 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002192 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002193
Gordon Henriksen86737662008-01-05 16:56:59 +00002194 // Add argument registers to the end of the list so that they are known live
2195 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002196 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2197 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2198 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002199
Evan Cheng586ccac2008-03-18 23:36:35 +00002200 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002201 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002202 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2203
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002204 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
2205 if (Is64Bit && isVarArg && !Subtarget->isTargetWin64())
Owen Anderson825b72b2009-08-11 20:47:22 +00002206 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002207
Gabor Greifba36cb52008-08-28 21:40:38 +00002208 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002209 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002210
Dan Gohman98ca4f22009-08-05 01:29:28 +00002211 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002212 // We used to do:
2213 //// If this is the first return lowered for this function, add the regs
2214 //// to the liveout set for the function.
2215 // This isn't right, although it's probably harmless on x86; liveouts
2216 // should be computed from returns not tail calls. Consider a void
2217 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002218 return DAG.getNode(X86ISD::TC_RETURN, dl,
2219 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002220 }
2221
Dale Johannesenace16102009-02-03 19:33:06 +00002222 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002223 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002224
Chris Lattner2d297092006-05-23 18:50:38 +00002225 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002226 unsigned NumBytesForCalleeToPush;
Dan Gohman4d3d6e12010-05-27 18:43:40 +00002227 if (Subtarget->IsCalleePop(isVarArg, CallConv))
Gordon Henriksen86737662008-01-05 16:56:59 +00002228 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002229 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002230 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002231 // pops the hidden struct pointer, so we have to push it back.
2232 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002233 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002234 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002235 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002236
Gordon Henriksenae636f82008-01-03 16:47:34 +00002237 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002238 if (!IsSibcall) {
2239 Chain = DAG.getCALLSEQ_END(Chain,
2240 DAG.getIntPtrConstant(NumBytes, true),
2241 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2242 true),
2243 InFlag);
2244 InFlag = Chain.getValue(1);
2245 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002246
Chris Lattner3085e152007-02-25 08:59:22 +00002247 // Handle result values, copying them out of physregs into vregs that we
2248 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002249 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2250 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002251}
2252
Evan Cheng25ab6902006-09-08 06:48:29 +00002253
2254//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002255// Fast Calling Convention (tail call) implementation
2256//===----------------------------------------------------------------------===//
2257
2258// Like std call, callee cleans arguments, convention except that ECX is
2259// reserved for storing the tail called function address. Only 2 registers are
2260// free for argument passing (inreg). Tail call optimization is performed
2261// provided:
2262// * tailcallopt is enabled
2263// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002264// On X86_64 architecture with GOT-style position independent code only local
2265// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002266// To keep the stack aligned according to platform abi the function
2267// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2268// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002269// If a tail called function callee has more arguments than the caller the
2270// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002271// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002272// original REtADDR, but before the saved framepointer or the spilled registers
2273// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2274// stack layout:
2275// arg1
2276// arg2
2277// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002278// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002279// move area ]
2280// (possible EBP)
2281// ESI
2282// EDI
2283// local1 ..
2284
2285/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2286/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002287unsigned
2288X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2289 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002290 MachineFunction &MF = DAG.getMachineFunction();
2291 const TargetMachine &TM = MF.getTarget();
2292 const TargetFrameInfo &TFI = *TM.getFrameInfo();
2293 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002294 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002295 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002296 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002297 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2298 // Number smaller than 12 so just add the difference.
2299 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2300 } else {
2301 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002302 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002303 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002304 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002305 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002306}
2307
Evan Cheng5f941932010-02-05 02:21:12 +00002308/// MatchingStackOffset - Return true if the given stack call argument is
2309/// already available in the same position (relatively) of the caller's
2310/// incoming argument stack.
2311static
2312bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2313 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2314 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002315 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2316 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002317 if (Arg.getOpcode() == ISD::CopyFromReg) {
2318 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
2319 if (!VR || TargetRegisterInfo::isPhysicalRegister(VR))
2320 return false;
2321 MachineInstr *Def = MRI->getVRegDef(VR);
2322 if (!Def)
2323 return false;
2324 if (!Flags.isByVal()) {
2325 if (!TII->isLoadFromStackSlot(Def, FI))
2326 return false;
2327 } else {
2328 unsigned Opcode = Def->getOpcode();
2329 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2330 Def->getOperand(1).isFI()) {
2331 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002332 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002333 } else
2334 return false;
2335 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002336 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2337 if (Flags.isByVal())
2338 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002339 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002340 // define @foo(%struct.X* %A) {
2341 // tail call @bar(%struct.X* byval %A)
2342 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002343 return false;
2344 SDValue Ptr = Ld->getBasePtr();
2345 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2346 if (!FINode)
2347 return false;
2348 FI = FINode->getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002349 } else
2350 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002351
Evan Cheng4cae1332010-03-05 08:38:04 +00002352 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002353 if (!MFI->isFixedObjectIndex(FI))
2354 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002355 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002356}
2357
Dan Gohman98ca4f22009-08-05 01:29:28 +00002358/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2359/// for tail call optimization. Targets which want to do tail call
2360/// optimization should implement this function.
2361bool
2362X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002363 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002364 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002365 bool isCalleeStructRet,
2366 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002367 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002368 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002369 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002370 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002371 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002372 CalleeCC != CallingConv::C)
2373 return false;
2374
Evan Cheng7096ae42010-01-29 06:45:59 +00002375 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002376 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002377 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002378 CallingConv::ID CallerCC = CallerF->getCallingConv();
2379 bool CCMatch = CallerCC == CalleeCC;
2380
Dan Gohman1797ed52010-02-08 20:27:50 +00002381 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002382 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002383 return true;
2384 return false;
2385 }
2386
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002387 // Look for obvious safe cases to perform tail call optimization that do not
2388 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002389
Evan Cheng2c12cb42010-03-26 16:26:03 +00002390 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2391 // emit a special epilogue.
2392 if (RegInfo->needsStackRealignment(MF))
2393 return false;
2394
Eric Christopher90eb4022010-07-22 00:26:08 +00002395 // Do not sibcall optimize vararg calls unless the call site is not passing
2396 // any arguments.
Evan Cheng3c262ee2010-03-26 02:13:13 +00002397 if (isVarArg && !Outs.empty())
Evan Cheng843bd692010-01-31 06:44:49 +00002398 return false;
2399
Evan Chenga375d472010-03-15 18:54:48 +00002400 // Also avoid sibcall optimization if either caller or callee uses struct
2401 // return semantics.
2402 if (isCalleeStructRet || isCallerStructRet)
2403 return false;
2404
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002405 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2406 // Therefore if it's not used by the call it is not safe to optimize this into
2407 // a sibcall.
2408 bool Unused = false;
2409 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2410 if (!Ins[i].Used) {
2411 Unused = true;
2412 break;
2413 }
2414 }
2415 if (Unused) {
2416 SmallVector<CCValAssign, 16> RVLocs;
2417 CCState CCInfo(CalleeCC, false, getTargetMachine(),
2418 RVLocs, *DAG.getContext());
2419 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002420 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002421 CCValAssign &VA = RVLocs[i];
2422 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2423 return false;
2424 }
2425 }
2426
Evan Cheng13617962010-04-30 01:12:32 +00002427 // If the calling conventions do not match, then we'd better make sure the
2428 // results are returned in the same way as what the caller expects.
2429 if (!CCMatch) {
2430 SmallVector<CCValAssign, 16> RVLocs1;
2431 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
2432 RVLocs1, *DAG.getContext());
2433 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2434
2435 SmallVector<CCValAssign, 16> RVLocs2;
2436 CCState CCInfo2(CallerCC, false, getTargetMachine(),
2437 RVLocs2, *DAG.getContext());
2438 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2439
2440 if (RVLocs1.size() != RVLocs2.size())
2441 return false;
2442 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2443 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2444 return false;
2445 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2446 return false;
2447 if (RVLocs1[i].isRegLoc()) {
2448 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2449 return false;
2450 } else {
2451 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2452 return false;
2453 }
2454 }
2455 }
2456
Evan Chenga6bff982010-01-30 01:22:00 +00002457 // If the callee takes no arguments then go on to check the results of the
2458 // call.
2459 if (!Outs.empty()) {
2460 // Check if stack adjustment is needed. For now, do not do this if any
2461 // argument is passed on the stack.
2462 SmallVector<CCValAssign, 16> ArgLocs;
2463 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
2464 ArgLocs, *DAG.getContext());
2465 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CalleeCC));
Evan Chengb2c92902010-02-02 02:22:50 +00002466 if (CCInfo.getNextStackOffset()) {
2467 MachineFunction &MF = DAG.getMachineFunction();
2468 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2469 return false;
2470 if (Subtarget->isTargetWin64())
2471 // Win64 ABI has additional complications.
2472 return false;
2473
2474 // Check if the arguments are already laid out in the right way as
2475 // the caller's fixed stack objects.
2476 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002477 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2478 const X86InstrInfo *TII =
2479 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002480 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2481 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002482 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002483 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002484 if (VA.getLocInfo() == CCValAssign::Indirect)
2485 return false;
2486 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002487 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2488 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002489 return false;
2490 }
2491 }
2492 }
Evan Cheng9c044672010-05-29 01:35:22 +00002493
2494 // If the tailcall address may be in a register, then make sure it's
2495 // possible to register allocate for it. In 32-bit, the call address can
2496 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002497 // callee-saved registers are restored. These happen to be the same
2498 // registers used to pass 'inreg' arguments so watch out for those.
2499 if (!Subtarget->is64Bit() &&
2500 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002501 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002502 unsigned NumInRegs = 0;
2503 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2504 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002505 if (!VA.isRegLoc())
2506 continue;
2507 unsigned Reg = VA.getLocReg();
2508 switch (Reg) {
2509 default: break;
2510 case X86::EAX: case X86::EDX: case X86::ECX:
2511 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002512 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002513 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002514 }
2515 }
2516 }
Evan Chenga6bff982010-01-30 01:22:00 +00002517 }
Evan Chengb1712452010-01-27 06:25:16 +00002518
Evan Cheng86809cc2010-02-03 03:28:02 +00002519 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002520}
2521
Dan Gohman3df24e62008-09-03 23:12:08 +00002522FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002523X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2524 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002525}
2526
2527
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002528//===----------------------------------------------------------------------===//
2529// Other Lowering Hooks
2530//===----------------------------------------------------------------------===//
2531
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002532static bool MayFoldLoad(SDValue Op) {
2533 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2534}
2535
2536static bool MayFoldIntoStore(SDValue Op) {
2537 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2538}
2539
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002540static bool isTargetShuffle(unsigned Opcode) {
2541 switch(Opcode) {
2542 default: return false;
2543 case X86ISD::PSHUFD:
2544 case X86ISD::PSHUFHW:
2545 case X86ISD::PSHUFLW:
2546 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002547 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002548 case X86ISD::SHUFPS:
2549 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002550 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002551 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002552 case X86ISD::MOVLPS:
2553 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002554 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002555 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002556 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002557 case X86ISD::MOVSS:
2558 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002559 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002560 case X86ISD::UNPCKLPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002561 case X86ISD::PUNPCKLWD:
2562 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002563 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002564 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002565 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002566 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002567 case X86ISD::PUNPCKHWD:
2568 case X86ISD::PUNPCKHBW:
2569 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002570 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002571 return true;
2572 }
2573 return false;
2574}
2575
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002576static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002577 SDValue V1, SelectionDAG &DAG) {
2578 switch(Opc) {
2579 default: llvm_unreachable("Unknown x86 shuffle node");
2580 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002581 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002582 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002583 return DAG.getNode(Opc, dl, VT, V1);
2584 }
2585
2586 return SDValue();
2587}
2588
2589static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002590 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002591 switch(Opc) {
2592 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002593 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002594 case X86ISD::PSHUFHW:
2595 case X86ISD::PSHUFLW:
2596 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2597 }
2598
2599 return SDValue();
2600}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002601
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002602static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2603 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2604 switch(Opc) {
2605 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002606 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002607 case X86ISD::SHUFPD:
2608 case X86ISD::SHUFPS:
2609 return DAG.getNode(Opc, dl, VT, V1, V2,
2610 DAG.getConstant(TargetMask, MVT::i8));
2611 }
2612 return SDValue();
2613}
2614
2615static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2616 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2617 switch(Opc) {
2618 default: llvm_unreachable("Unknown x86 shuffle node");
2619 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002620 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002621 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002622 case X86ISD::MOVLPS:
2623 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002624 case X86ISD::MOVSS:
2625 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002626 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002627 case X86ISD::UNPCKLPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002628 case X86ISD::PUNPCKLWD:
2629 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002630 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002631 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002632 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002633 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002634 case X86ISD::PUNPCKHWD:
2635 case X86ISD::PUNPCKHBW:
2636 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002637 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002638 return DAG.getNode(Opc, dl, VT, V1, V2);
2639 }
2640 return SDValue();
2641}
2642
Dan Gohmand858e902010-04-17 15:26:15 +00002643SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002644 MachineFunction &MF = DAG.getMachineFunction();
2645 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2646 int ReturnAddrIndex = FuncInfo->getRAIndex();
2647
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002648 if (ReturnAddrIndex == 0) {
2649 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002650 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002651 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002652 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002653 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002654 }
2655
Evan Cheng25ab6902006-09-08 06:48:29 +00002656 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002657}
2658
2659
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002660bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2661 bool hasSymbolicDisplacement) {
2662 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002663 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002664 return false;
2665
2666 // If we don't have a symbolic displacement - we don't have any extra
2667 // restrictions.
2668 if (!hasSymbolicDisplacement)
2669 return true;
2670
2671 // FIXME: Some tweaks might be needed for medium code model.
2672 if (M != CodeModel::Small && M != CodeModel::Kernel)
2673 return false;
2674
2675 // For small code model we assume that latest object is 16MB before end of 31
2676 // bits boundary. We may also accept pretty large negative constants knowing
2677 // that all objects are in the positive half of address space.
2678 if (M == CodeModel::Small && Offset < 16*1024*1024)
2679 return true;
2680
2681 // For kernel code model we know that all object resist in the negative half
2682 // of 32bits address space. We may not accept negative offsets, since they may
2683 // be just off and we may accept pretty large positive ones.
2684 if (M == CodeModel::Kernel && Offset > 0)
2685 return true;
2686
2687 return false;
2688}
2689
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002690/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2691/// specific condition code, returning the condition code and the LHS/RHS of the
2692/// comparison to make.
2693static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2694 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002695 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002696 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2697 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2698 // X > -1 -> X == 0, jump !sign.
2699 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002700 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002701 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2702 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002703 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002704 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002705 // X < 1 -> X <= 0
2706 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002707 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002708 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002709 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002710
Evan Chengd9558e02006-01-06 00:43:03 +00002711 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002712 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002713 case ISD::SETEQ: return X86::COND_E;
2714 case ISD::SETGT: return X86::COND_G;
2715 case ISD::SETGE: return X86::COND_GE;
2716 case ISD::SETLT: return X86::COND_L;
2717 case ISD::SETLE: return X86::COND_LE;
2718 case ISD::SETNE: return X86::COND_NE;
2719 case ISD::SETULT: return X86::COND_B;
2720 case ISD::SETUGT: return X86::COND_A;
2721 case ISD::SETULE: return X86::COND_BE;
2722 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002723 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002724 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002725
Chris Lattner4c78e022008-12-23 23:42:27 +00002726 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002727
Chris Lattner4c78e022008-12-23 23:42:27 +00002728 // If LHS is a foldable load, but RHS is not, flip the condition.
2729 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2730 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2731 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2732 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002733 }
2734
Chris Lattner4c78e022008-12-23 23:42:27 +00002735 switch (SetCCOpcode) {
2736 default: break;
2737 case ISD::SETOLT:
2738 case ISD::SETOLE:
2739 case ISD::SETUGT:
2740 case ISD::SETUGE:
2741 std::swap(LHS, RHS);
2742 break;
2743 }
2744
2745 // On a floating point condition, the flags are set as follows:
2746 // ZF PF CF op
2747 // 0 | 0 | 0 | X > Y
2748 // 0 | 0 | 1 | X < Y
2749 // 1 | 0 | 0 | X == Y
2750 // 1 | 1 | 1 | unordered
2751 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002752 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002753 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002754 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002755 case ISD::SETOLT: // flipped
2756 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002757 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002758 case ISD::SETOLE: // flipped
2759 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002760 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002761 case ISD::SETUGT: // flipped
2762 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002763 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002764 case ISD::SETUGE: // flipped
2765 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002766 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002767 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002768 case ISD::SETNE: return X86::COND_NE;
2769 case ISD::SETUO: return X86::COND_P;
2770 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002771 case ISD::SETOEQ:
2772 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002773 }
Evan Chengd9558e02006-01-06 00:43:03 +00002774}
2775
Evan Cheng4a460802006-01-11 00:33:36 +00002776/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2777/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002778/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002779static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002780 switch (X86CC) {
2781 default:
2782 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002783 case X86::COND_B:
2784 case X86::COND_BE:
2785 case X86::COND_E:
2786 case X86::COND_P:
2787 case X86::COND_A:
2788 case X86::COND_AE:
2789 case X86::COND_NE:
2790 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002791 return true;
2792 }
2793}
2794
Evan Chengeb2f9692009-10-27 19:56:55 +00002795/// isFPImmLegal - Returns true if the target can instruction select the
2796/// specified FP immediate natively. If false, the legalizer will
2797/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002798bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00002799 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
2800 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
2801 return true;
2802 }
2803 return false;
2804}
2805
Nate Begeman9008ca62009-04-27 18:41:29 +00002806/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2807/// the specified range (L, H].
2808static bool isUndefOrInRange(int Val, int Low, int Hi) {
2809 return (Val < 0) || (Val >= Low && Val < Hi);
2810}
2811
2812/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2813/// specified value.
2814static bool isUndefOrEqual(int Val, int CmpVal) {
2815 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002816 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002817 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002818}
2819
Nate Begeman9008ca62009-04-27 18:41:29 +00002820/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2821/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2822/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00002823static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00002824 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00002825 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002826 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00002827 return (Mask[0] < 2 && Mask[1] < 2);
2828 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002829}
2830
Nate Begeman9008ca62009-04-27 18:41:29 +00002831bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002832 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002833 N->getMask(M);
2834 return ::isPSHUFDMask(M, N->getValueType(0));
2835}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002836
Nate Begeman9008ca62009-04-27 18:41:29 +00002837/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2838/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00002839static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002840 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002841 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002842
Nate Begeman9008ca62009-04-27 18:41:29 +00002843 // Lower quadword copied in order or undef.
2844 for (int i = 0; i != 4; ++i)
2845 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002846 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002847
Evan Cheng506d3df2006-03-29 23:07:14 +00002848 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002849 for (int i = 4; i != 8; ++i)
2850 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002851 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002852
Evan Cheng506d3df2006-03-29 23:07:14 +00002853 return true;
2854}
2855
Nate Begeman9008ca62009-04-27 18:41:29 +00002856bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002857 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002858 N->getMask(M);
2859 return ::isPSHUFHWMask(M, N->getValueType(0));
2860}
Evan Cheng506d3df2006-03-29 23:07:14 +00002861
Nate Begeman9008ca62009-04-27 18:41:29 +00002862/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2863/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00002864static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002865 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002866 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002867
Rafael Espindola15684b22009-04-24 12:40:33 +00002868 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00002869 for (int i = 4; i != 8; ++i)
2870 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00002871 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002872
Rafael Espindola15684b22009-04-24 12:40:33 +00002873 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002874 for (int i = 0; i != 4; ++i)
2875 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00002876 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002877
Rafael Espindola15684b22009-04-24 12:40:33 +00002878 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002879}
2880
Nate Begeman9008ca62009-04-27 18:41:29 +00002881bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002882 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002883 N->getMask(M);
2884 return ::isPSHUFLWMask(M, N->getValueType(0));
2885}
2886
Nate Begemana09008b2009-10-19 02:17:23 +00002887/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
2888/// is suitable for input to PALIGNR.
2889static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
2890 bool hasSSSE3) {
2891 int i, e = VT.getVectorNumElements();
2892
2893 // Do not handle v2i64 / v2f64 shuffles with palignr.
2894 if (e < 4 || !hasSSSE3)
2895 return false;
2896
2897 for (i = 0; i != e; ++i)
2898 if (Mask[i] >= 0)
2899 break;
2900
2901 // All undef, not a palignr.
2902 if (i == e)
2903 return false;
2904
2905 // Determine if it's ok to perform a palignr with only the LHS, since we
2906 // don't have access to the actual shuffle elements to see if RHS is undef.
2907 bool Unary = Mask[i] < (int)e;
2908 bool NeedsUnary = false;
2909
2910 int s = Mask[i] - i;
2911
2912 // Check the rest of the elements to see if they are consecutive.
2913 for (++i; i != e; ++i) {
2914 int m = Mask[i];
2915 if (m < 0)
2916 continue;
2917
2918 Unary = Unary && (m < (int)e);
2919 NeedsUnary = NeedsUnary || (m < s);
2920
2921 if (NeedsUnary && !Unary)
2922 return false;
2923 if (Unary && m != ((s+i) & (e-1)))
2924 return false;
2925 if (!Unary && m != (s+i))
2926 return false;
2927 }
2928 return true;
2929}
2930
2931bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
2932 SmallVector<int, 8> M;
2933 N->getMask(M);
2934 return ::isPALIGNRMask(M, N->getValueType(0), true);
2935}
2936
Evan Cheng14aed5e2006-03-24 01:18:28 +00002937/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2938/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00002939static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002940 int NumElems = VT.getVectorNumElements();
2941 if (NumElems != 2 && NumElems != 4)
2942 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002943
Nate Begeman9008ca62009-04-27 18:41:29 +00002944 int Half = NumElems / 2;
2945 for (int i = 0; i < Half; ++i)
2946 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002947 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002948 for (int i = Half; i < NumElems; ++i)
2949 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002950 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002951
Evan Cheng14aed5e2006-03-24 01:18:28 +00002952 return true;
2953}
2954
Nate Begeman9008ca62009-04-27 18:41:29 +00002955bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
2956 SmallVector<int, 8> M;
2957 N->getMask(M);
2958 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002959}
2960
Evan Cheng213d2cf2007-05-17 18:45:50 +00002961/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002962/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2963/// half elements to come from vector 1 (which would equal the dest.) and
2964/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00002965static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002966 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002967
2968 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00002969 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002970
Nate Begeman9008ca62009-04-27 18:41:29 +00002971 int Half = NumElems / 2;
2972 for (int i = 0; i < Half; ++i)
2973 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002974 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002975 for (int i = Half; i < NumElems; ++i)
2976 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002977 return false;
2978 return true;
2979}
2980
Nate Begeman9008ca62009-04-27 18:41:29 +00002981static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
2982 SmallVector<int, 8> M;
2983 N->getMask(M);
2984 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002985}
2986
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002987/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2988/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002989bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
2990 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002991 return false;
2992
Evan Cheng2064a2b2006-03-28 06:50:32 +00002993 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00002994 return isUndefOrEqual(N->getMaskElt(0), 6) &&
2995 isUndefOrEqual(N->getMaskElt(1), 7) &&
2996 isUndefOrEqual(N->getMaskElt(2), 2) &&
2997 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002998}
2999
Nate Begeman0b10b912009-11-07 23:17:15 +00003000/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3001/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3002/// <2, 3, 2, 3>
3003bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
3004 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3005
3006 if (NumElems != 4)
3007 return false;
3008
3009 return isUndefOrEqual(N->getMaskElt(0), 2) &&
3010 isUndefOrEqual(N->getMaskElt(1), 3) &&
3011 isUndefOrEqual(N->getMaskElt(2), 2) &&
3012 isUndefOrEqual(N->getMaskElt(3), 3);
3013}
3014
Evan Cheng5ced1d82006-04-06 23:23:56 +00003015/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3016/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003017bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3018 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003019
Evan Cheng5ced1d82006-04-06 23:23:56 +00003020 if (NumElems != 2 && NumElems != 4)
3021 return false;
3022
Evan Chengc5cdff22006-04-07 21:53:05 +00003023 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003024 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003025 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003026
Evan Chengc5cdff22006-04-07 21:53:05 +00003027 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003028 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003029 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003030
3031 return true;
3032}
3033
Nate Begeman0b10b912009-11-07 23:17:15 +00003034/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3035/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3036bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003037 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003038
Evan Cheng5ced1d82006-04-06 23:23:56 +00003039 if (NumElems != 2 && NumElems != 4)
3040 return false;
3041
Evan Chengc5cdff22006-04-07 21:53:05 +00003042 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003043 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003044 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003045
Nate Begeman9008ca62009-04-27 18:41:29 +00003046 for (unsigned i = 0; i < NumElems/2; ++i)
3047 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003048 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003049
3050 return true;
3051}
3052
Evan Cheng0038e592006-03-28 00:39:58 +00003053/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3054/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003055static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003056 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003057 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003058 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00003059 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003060
Nate Begeman9008ca62009-04-27 18:41:29 +00003061 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3062 int BitI = Mask[i];
3063 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00003064 if (!isUndefOrEqual(BitI, j))
3065 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003066 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003067 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003068 return false;
3069 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003070 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003071 return false;
3072 }
Evan Cheng0038e592006-03-28 00:39:58 +00003073 }
Evan Cheng0038e592006-03-28 00:39:58 +00003074 return true;
3075}
3076
Nate Begeman9008ca62009-04-27 18:41:29 +00003077bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3078 SmallVector<int, 8> M;
3079 N->getMask(M);
3080 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003081}
3082
Evan Cheng4fcb9222006-03-28 02:43:26 +00003083/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3084/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003085static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003086 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003087 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003088 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003089 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003090
Nate Begeman9008ca62009-04-27 18:41:29 +00003091 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3092 int BitI = Mask[i];
3093 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00003094 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00003095 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003096 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00003097 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003098 return false;
3099 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003100 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003101 return false;
3102 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003103 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003104 return true;
3105}
3106
Nate Begeman9008ca62009-04-27 18:41:29 +00003107bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3108 SmallVector<int, 8> M;
3109 N->getMask(M);
3110 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003111}
3112
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003113/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3114/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3115/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003116static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003117 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003118 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003119 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003120
Nate Begeman9008ca62009-04-27 18:41:29 +00003121 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
3122 int BitI = Mask[i];
3123 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00003124 if (!isUndefOrEqual(BitI, j))
3125 return false;
3126 if (!isUndefOrEqual(BitI1, j))
3127 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003128 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003129 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003130}
3131
Nate Begeman9008ca62009-04-27 18:41:29 +00003132bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3133 SmallVector<int, 8> M;
3134 N->getMask(M);
3135 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3136}
3137
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003138/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3139/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3140/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003141static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003142 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003143 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3144 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003145
Nate Begeman9008ca62009-04-27 18:41:29 +00003146 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3147 int BitI = Mask[i];
3148 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003149 if (!isUndefOrEqual(BitI, j))
3150 return false;
3151 if (!isUndefOrEqual(BitI1, j))
3152 return false;
3153 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003154 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003155}
3156
Nate Begeman9008ca62009-04-27 18:41:29 +00003157bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3158 SmallVector<int, 8> M;
3159 N->getMask(M);
3160 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3161}
3162
Evan Cheng017dcc62006-04-21 01:05:10 +00003163/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3164/// specifies a shuffle of elements that is suitable for input to MOVSS,
3165/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003166static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003167 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003168 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003169
3170 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003171
Nate Begeman9008ca62009-04-27 18:41:29 +00003172 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003173 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003174
Nate Begeman9008ca62009-04-27 18:41:29 +00003175 for (int i = 1; i < NumElts; ++i)
3176 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003177 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003178
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003179 return true;
3180}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003181
Nate Begeman9008ca62009-04-27 18:41:29 +00003182bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3183 SmallVector<int, 8> M;
3184 N->getMask(M);
3185 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003186}
3187
Evan Cheng017dcc62006-04-21 01:05:10 +00003188/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3189/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003190/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003191static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003192 bool V2IsSplat = false, bool V2IsUndef = false) {
3193 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003194 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003195 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003196
Nate Begeman9008ca62009-04-27 18:41:29 +00003197 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003198 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003199
Nate Begeman9008ca62009-04-27 18:41:29 +00003200 for (int i = 1; i < NumOps; ++i)
3201 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3202 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3203 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003204 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003205
Evan Cheng39623da2006-04-20 08:58:49 +00003206 return true;
3207}
3208
Nate Begeman9008ca62009-04-27 18:41:29 +00003209static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003210 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003211 SmallVector<int, 8> M;
3212 N->getMask(M);
3213 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003214}
3215
Evan Chengd9539472006-04-14 21:59:03 +00003216/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3217/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003218bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
3219 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003220 return false;
3221
3222 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00003223 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003224 int Elt = N->getMaskElt(i);
3225 if (Elt >= 0 && Elt != 1)
3226 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00003227 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003228
3229 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003230 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003231 int Elt = N->getMaskElt(i);
3232 if (Elt >= 0 && Elt != 3)
3233 return false;
3234 if (Elt == 3)
3235 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003236 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003237 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00003238 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003239 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003240}
3241
3242/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3243/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003244bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
3245 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003246 return false;
3247
3248 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00003249 for (unsigned i = 0; i < 2; ++i)
3250 if (N->getMaskElt(i) > 0)
3251 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003252
3253 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003254 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003255 int Elt = N->getMaskElt(i);
3256 if (Elt >= 0 && Elt != 2)
3257 return false;
3258 if (Elt == 2)
3259 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003260 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003261 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003262 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003263}
3264
Evan Cheng0b457f02008-09-25 20:50:48 +00003265/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3266/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003267bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3268 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003269
Nate Begeman9008ca62009-04-27 18:41:29 +00003270 for (int i = 0; i < e; ++i)
3271 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003272 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003273 for (int i = 0; i < e; ++i)
3274 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003275 return false;
3276 return true;
3277}
3278
Evan Cheng63d33002006-03-22 08:01:21 +00003279/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003280/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003281unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003282 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3283 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3284
Evan Chengb9df0ca2006-03-22 02:53:00 +00003285 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3286 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003287 for (int i = 0; i < NumOperands; ++i) {
3288 int Val = SVOp->getMaskElt(NumOperands-i-1);
3289 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003290 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003291 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003292 if (i != NumOperands - 1)
3293 Mask <<= Shift;
3294 }
Evan Cheng63d33002006-03-22 08:01:21 +00003295 return Mask;
3296}
3297
Evan Cheng506d3df2006-03-29 23:07:14 +00003298/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003299/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003300unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003301 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003302 unsigned Mask = 0;
3303 // 8 nodes, but we only care about the last 4.
3304 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003305 int Val = SVOp->getMaskElt(i);
3306 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003307 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003308 if (i != 4)
3309 Mask <<= 2;
3310 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003311 return Mask;
3312}
3313
3314/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003315/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003316unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003317 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003318 unsigned Mask = 0;
3319 // 8 nodes, but we only care about the first 4.
3320 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003321 int Val = SVOp->getMaskElt(i);
3322 if (Val >= 0)
3323 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003324 if (i != 0)
3325 Mask <<= 2;
3326 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003327 return Mask;
3328}
3329
Nate Begemana09008b2009-10-19 02:17:23 +00003330/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3331/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3332unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3333 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3334 EVT VVT = N->getValueType(0);
3335 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3336 int Val = 0;
3337
3338 unsigned i, e;
3339 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3340 Val = SVOp->getMaskElt(i);
3341 if (Val >= 0)
3342 break;
3343 }
3344 return (Val - i) * EltSize;
3345}
3346
Evan Cheng37b73872009-07-30 08:33:02 +00003347/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3348/// constant +0.0.
3349bool X86::isZeroNode(SDValue Elt) {
3350 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00003351 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00003352 (isa<ConstantFPSDNode>(Elt) &&
3353 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3354}
3355
Nate Begeman9008ca62009-04-27 18:41:29 +00003356/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3357/// their permute mask.
3358static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3359 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003360 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003361 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003362 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003363
Nate Begeman5a5ca152009-04-29 05:20:52 +00003364 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003365 int idx = SVOp->getMaskElt(i);
3366 if (idx < 0)
3367 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003368 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003369 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003370 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003371 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003372 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003373 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3374 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003375}
3376
Evan Cheng779ccea2007-12-07 21:30:01 +00003377/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3378/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003379static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003380 unsigned NumElems = VT.getVectorNumElements();
3381 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003382 int idx = Mask[i];
3383 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003384 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003385 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003386 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003387 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003388 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003389 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003390}
3391
Evan Cheng533a0aa2006-04-19 20:35:22 +00003392/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3393/// match movhlps. The lower half elements should come from upper half of
3394/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003395/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003396static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3397 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003398 return false;
3399 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003400 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003401 return false;
3402 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003403 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003404 return false;
3405 return true;
3406}
3407
Evan Cheng5ced1d82006-04-06 23:23:56 +00003408/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003409/// is promoted to a vector. It also returns the LoadSDNode by reference if
3410/// required.
3411static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003412 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3413 return false;
3414 N = N->getOperand(0).getNode();
3415 if (!ISD::isNON_EXTLoad(N))
3416 return false;
3417 if (LD)
3418 *LD = cast<LoadSDNode>(N);
3419 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003420}
3421
Evan Cheng533a0aa2006-04-19 20:35:22 +00003422/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3423/// match movlp{s|d}. The lower half elements should come from lower half of
3424/// V1 (and in order), and the upper half elements should come from the upper
3425/// half of V2 (and in order). And since V1 will become the source of the
3426/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003427static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3428 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003429 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003430 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003431 // Is V2 is a vector load, don't do this transformation. We will try to use
3432 // load folding shufps op.
3433 if (ISD::isNON_EXTLoad(V2))
3434 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003435
Nate Begeman5a5ca152009-04-29 05:20:52 +00003436 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003437
Evan Cheng533a0aa2006-04-19 20:35:22 +00003438 if (NumElems != 2 && NumElems != 4)
3439 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003440 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003441 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003442 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003443 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003444 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003445 return false;
3446 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003447}
3448
Evan Cheng39623da2006-04-20 08:58:49 +00003449/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3450/// all the same.
3451static bool isSplatVector(SDNode *N) {
3452 if (N->getOpcode() != ISD::BUILD_VECTOR)
3453 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003454
Dan Gohman475871a2008-07-27 21:46:04 +00003455 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003456 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3457 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003458 return false;
3459 return true;
3460}
3461
Evan Cheng213d2cf2007-05-17 18:45:50 +00003462/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003463/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003464/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003465static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003466 SDValue V1 = N->getOperand(0);
3467 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003468 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3469 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003470 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003471 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003472 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003473 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3474 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003475 if (Opc != ISD::BUILD_VECTOR ||
3476 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003477 return false;
3478 } else if (Idx >= 0) {
3479 unsigned Opc = V1.getOpcode();
3480 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3481 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003482 if (Opc != ISD::BUILD_VECTOR ||
3483 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003484 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003485 }
3486 }
3487 return true;
3488}
3489
3490/// getZeroVector - Returns a vector of specified type with all zero elements.
3491///
Owen Andersone50ed302009-08-10 22:56:29 +00003492static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003493 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003494 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003495
Dale Johannesen0488fb62010-09-30 23:57:10 +00003496 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003497 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003498 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003499 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003500 if (HasSSE2) { // SSE2
3501 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3502 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
3503 } else { // SSE1
3504 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3505 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
3506 }
3507 } else if (VT.getSizeInBits() == 256) { // AVX
3508 // 256-bit logic and arithmetic instructions in AVX are
3509 // all floating-point, no support for integer ops. Default
3510 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00003511 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003512 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
3513 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00003514 }
Dale Johannesenace16102009-02-03 19:33:06 +00003515 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003516}
3517
Chris Lattner8a594482007-11-25 00:24:49 +00003518/// getOnesVector - Returns a vector of specified type with all bits set.
3519///
Owen Andersone50ed302009-08-10 22:56:29 +00003520static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003521 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003522
Chris Lattner8a594482007-11-25 00:24:49 +00003523 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3524 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003525 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003526 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003527 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Dale Johannesenace16102009-02-03 19:33:06 +00003528 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003529}
3530
3531
Evan Cheng39623da2006-04-20 08:58:49 +00003532/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3533/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003534static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003535 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003536 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003537
Evan Cheng39623da2006-04-20 08:58:49 +00003538 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003539 SmallVector<int, 8> MaskVec;
3540 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003541
Nate Begeman5a5ca152009-04-29 05:20:52 +00003542 for (unsigned i = 0; i != NumElems; ++i) {
3543 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003544 MaskVec[i] = NumElems;
3545 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003546 }
Evan Cheng39623da2006-04-20 08:58:49 +00003547 }
Evan Cheng39623da2006-04-20 08:58:49 +00003548 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003549 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3550 SVOp->getOperand(1), &MaskVec[0]);
3551 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003552}
3553
Evan Cheng017dcc62006-04-21 01:05:10 +00003554/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3555/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003556static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003557 SDValue V2) {
3558 unsigned NumElems = VT.getVectorNumElements();
3559 SmallVector<int, 8> Mask;
3560 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003561 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003562 Mask.push_back(i);
3563 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003564}
3565
Nate Begeman9008ca62009-04-27 18:41:29 +00003566/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003567static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003568 SDValue V2) {
3569 unsigned NumElems = VT.getVectorNumElements();
3570 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003571 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003572 Mask.push_back(i);
3573 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003574 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003575 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003576}
3577
Nate Begeman9008ca62009-04-27 18:41:29 +00003578/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003579static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003580 SDValue V2) {
3581 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003582 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003583 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003584 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003585 Mask.push_back(i + Half);
3586 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003587 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003588 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003589}
3590
Bruno Cardoso Lopesbb0a9482010-08-13 17:50:47 +00003591/// PromoteSplat - Promote a splat of v4i32, v8i16 or v16i8 to v4f32.
3592static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003593 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003594 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003595 DebugLoc dl = SV->getDebugLoc();
3596 SDValue V1 = SV->getOperand(0);
3597 int NumElems = VT.getVectorNumElements();
3598 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003599
Nate Begeman9008ca62009-04-27 18:41:29 +00003600 // unpack elements to the correct location
3601 while (NumElems > 4) {
3602 if (EltNo < NumElems/2) {
3603 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3604 } else {
3605 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3606 EltNo -= NumElems/2;
3607 }
3608 NumElems >>= 1;
3609 }
Eric Christopherfd179292009-08-27 18:07:15 +00003610
Nate Begeman9008ca62009-04-27 18:41:29 +00003611 // Perform the splat.
3612 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Dale Johannesenace16102009-02-03 19:33:06 +00003613 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003614 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
3615 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003616}
3617
Evan Chengba05f722006-04-21 23:03:30 +00003618/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003619/// vector of zero or undef vector. This produces a shuffle where the low
3620/// element of V2 is swizzled into the zero/undef vector, landing at element
3621/// 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 +00003622static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003623 bool isZero, bool HasSSE2,
3624 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003625 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003626 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003627 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3628 unsigned NumElems = VT.getVectorNumElements();
3629 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003630 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003631 // If this is the insertion idx, put the low elt of V2 here.
3632 MaskVec.push_back(i == Idx ? NumElems : i);
3633 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003634}
3635
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003636/// getShuffleScalarElt - Returns the scalar element that will make up the ith
3637/// element of the result of the vector shuffle.
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003638SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
3639 unsigned Depth) {
3640 if (Depth == 6)
3641 return SDValue(); // Limit search depth.
3642
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003643 SDValue V = SDValue(N, 0);
3644 EVT VT = V.getValueType();
3645 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003646
3647 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
3648 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
3649 Index = SV->getMaskElt(Index);
3650
3651 if (Index < 0)
3652 return DAG.getUNDEF(VT.getVectorElementType());
3653
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003654 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003655 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003656 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003657 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003658
3659 // Recurse into target specific vector shuffles to find scalars.
3660 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003661 int NumElems = VT.getVectorNumElements();
3662 SmallVector<unsigned, 16> ShuffleMask;
3663 SDValue ImmN;
3664
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003665 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003666 case X86ISD::SHUFPS:
3667 case X86ISD::SHUFPD:
3668 ImmN = N->getOperand(N->getNumOperands()-1);
3669 DecodeSHUFPSMask(NumElems,
3670 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3671 ShuffleMask);
3672 break;
3673 case X86ISD::PUNPCKHBW:
3674 case X86ISD::PUNPCKHWD:
3675 case X86ISD::PUNPCKHDQ:
3676 case X86ISD::PUNPCKHQDQ:
3677 DecodePUNPCKHMask(NumElems, ShuffleMask);
3678 break;
3679 case X86ISD::UNPCKHPS:
3680 case X86ISD::UNPCKHPD:
3681 DecodeUNPCKHPMask(NumElems, ShuffleMask);
3682 break;
3683 case X86ISD::PUNPCKLBW:
3684 case X86ISD::PUNPCKLWD:
3685 case X86ISD::PUNPCKLDQ:
3686 case X86ISD::PUNPCKLQDQ:
3687 DecodePUNPCKLMask(NumElems, ShuffleMask);
3688 break;
3689 case X86ISD::UNPCKLPS:
3690 case X86ISD::UNPCKLPD:
3691 DecodeUNPCKLPMask(NumElems, ShuffleMask);
3692 break;
3693 case X86ISD::MOVHLPS:
3694 DecodeMOVHLPSMask(NumElems, ShuffleMask);
3695 break;
3696 case X86ISD::MOVLHPS:
3697 DecodeMOVLHPSMask(NumElems, ShuffleMask);
3698 break;
3699 case X86ISD::PSHUFD:
3700 ImmN = N->getOperand(N->getNumOperands()-1);
3701 DecodePSHUFMask(NumElems,
3702 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3703 ShuffleMask);
3704 break;
3705 case X86ISD::PSHUFHW:
3706 ImmN = N->getOperand(N->getNumOperands()-1);
3707 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3708 ShuffleMask);
3709 break;
3710 case X86ISD::PSHUFLW:
3711 ImmN = N->getOperand(N->getNumOperands()-1);
3712 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3713 ShuffleMask);
3714 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003715 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00003716 case X86ISD::MOVSD: {
3717 // The index 0 always comes from the first element of the second source,
3718 // this is why MOVSS and MOVSD are used in the first place. The other
3719 // elements come from the other positions of the first source vector.
3720 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003721 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
3722 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00003723 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003724 default:
3725 assert("not implemented for target shuffle node");
3726 return SDValue();
3727 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003728
3729 Index = ShuffleMask[Index];
3730 if (Index < 0)
3731 return DAG.getUNDEF(VT.getVectorElementType());
3732
3733 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
3734 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
3735 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003736 }
3737
3738 // Actual nodes that may contain scalar elements
3739 if (Opcode == ISD::BIT_CONVERT) {
3740 V = V.getOperand(0);
3741 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003742 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003743
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003744 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003745 return SDValue();
3746 }
3747
3748 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
3749 return (Index == 0) ? V.getOperand(0)
3750 : DAG.getUNDEF(VT.getVectorElementType());
3751
3752 if (V.getOpcode() == ISD::BUILD_VECTOR)
3753 return V.getOperand(Index);
3754
3755 return SDValue();
3756}
3757
3758/// getNumOfConsecutiveZeros - Return the number of elements of a vector
3759/// shuffle operation which come from a consecutively from a zero. The
3760/// search can start in two diferent directions, from left or right.
3761static
3762unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
3763 bool ZerosFromLeft, SelectionDAG &DAG) {
3764 int i = 0;
3765
3766 while (i < NumElems) {
3767 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003768 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003769 if (!(Elt.getNode() &&
3770 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
3771 break;
3772 ++i;
3773 }
3774
3775 return i;
3776}
3777
3778/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
3779/// MaskE correspond consecutively to elements from one of the vector operands,
3780/// starting from its index OpIdx. Also tell OpNum which source vector operand.
3781static
3782bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
3783 int OpIdx, int NumElems, unsigned &OpNum) {
3784 bool SeenV1 = false;
3785 bool SeenV2 = false;
3786
3787 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
3788 int Idx = SVOp->getMaskElt(i);
3789 // Ignore undef indicies
3790 if (Idx < 0)
3791 continue;
3792
3793 if (Idx < NumElems)
3794 SeenV1 = true;
3795 else
3796 SeenV2 = true;
3797
3798 // Only accept consecutive elements from the same vector
3799 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
3800 return false;
3801 }
3802
3803 OpNum = SeenV1 ? 0 : 1;
3804 return true;
3805}
3806
3807/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
3808/// logical left shift of a vector.
3809static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
3810 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
3811 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
3812 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
3813 false /* check zeros from right */, DAG);
3814 unsigned OpSrc;
3815
3816 if (!NumZeros)
3817 return false;
3818
3819 // Considering the elements in the mask that are not consecutive zeros,
3820 // check if they consecutively come from only one of the source vectors.
3821 //
3822 // V1 = {X, A, B, C} 0
3823 // \ \ \ /
3824 // vector_shuffle V1, V2 <1, 2, 3, X>
3825 //
3826 if (!isShuffleMaskConsecutive(SVOp,
3827 0, // Mask Start Index
3828 NumElems-NumZeros-1, // Mask End Index
3829 NumZeros, // Where to start looking in the src vector
3830 NumElems, // Number of elements in vector
3831 OpSrc)) // Which source operand ?
3832 return false;
3833
3834 isLeft = false;
3835 ShAmt = NumZeros;
3836 ShVal = SVOp->getOperand(OpSrc);
3837 return true;
3838}
3839
3840/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
3841/// logical left shift of a vector.
3842static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
3843 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
3844 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
3845 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
3846 true /* check zeros from left */, DAG);
3847 unsigned OpSrc;
3848
3849 if (!NumZeros)
3850 return false;
3851
3852 // Considering the elements in the mask that are not consecutive zeros,
3853 // check if they consecutively come from only one of the source vectors.
3854 //
3855 // 0 { A, B, X, X } = V2
3856 // / \ / /
3857 // vector_shuffle V1, V2 <X, X, 4, 5>
3858 //
3859 if (!isShuffleMaskConsecutive(SVOp,
3860 NumZeros, // Mask Start Index
3861 NumElems-1, // Mask End Index
3862 0, // Where to start looking in the src vector
3863 NumElems, // Number of elements in vector
3864 OpSrc)) // Which source operand ?
3865 return false;
3866
3867 isLeft = true;
3868 ShAmt = NumZeros;
3869 ShVal = SVOp->getOperand(OpSrc);
3870 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00003871}
3872
3873/// isVectorShift - Returns true if the shuffle can be implemented as a
3874/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003875static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00003876 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003877 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
3878 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
3879 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00003880
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003881 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00003882}
3883
Evan Chengc78d3b42006-04-24 18:01:45 +00003884/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3885///
Dan Gohman475871a2008-07-27 21:46:04 +00003886static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003887 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00003888 SelectionDAG &DAG,
3889 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003890 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00003891 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003892
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003893 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003894 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003895 bool First = true;
3896 for (unsigned i = 0; i < 16; ++i) {
3897 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3898 if (ThisIsNonZero && First) {
3899 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003900 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003901 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003902 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003903 First = false;
3904 }
3905
3906 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00003907 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003908 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3909 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003910 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003911 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00003912 }
3913 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003914 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
3915 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
3916 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00003917 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003918 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00003919 } else
3920 ThisElt = LastElt;
3921
Gabor Greifba36cb52008-08-28 21:40:38 +00003922 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00003923 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00003924 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00003925 }
3926 }
3927
Owen Anderson825b72b2009-08-11 20:47:22 +00003928 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00003929}
3930
Bill Wendlinga348c562007-03-22 18:42:45 +00003931/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003932///
Dan Gohman475871a2008-07-27 21:46:04 +00003933static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00003934 unsigned NumNonZero, unsigned NumZero,
3935 SelectionDAG &DAG,
3936 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003937 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00003938 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003939
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003940 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003941 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003942 bool First = true;
3943 for (unsigned i = 0; i < 8; ++i) {
3944 bool isNonZero = (NonZeros & (1 << i)) != 0;
3945 if (isNonZero) {
3946 if (First) {
3947 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003948 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003949 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003950 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003951 First = false;
3952 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003953 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003954 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00003955 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00003956 }
3957 }
3958
3959 return V;
3960}
3961
Evan Chengf26ffe92008-05-29 08:22:04 +00003962/// getVShift - Return a vector logical shift node.
3963///
Owen Andersone50ed302009-08-10 22:56:29 +00003964static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00003965 unsigned NumBits, SelectionDAG &DAG,
3966 const TargetLowering &TLI, DebugLoc dl) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003967 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00003968 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Dale Johannesenace16102009-02-03 19:33:06 +00003969 SrcOp = DAG.getNode(ISD::BIT_CONVERT, dl, ShVT, SrcOp);
3970 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3971 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00003972 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00003973}
3974
Dan Gohman475871a2008-07-27 21:46:04 +00003975SDValue
Evan Chengc3630942009-12-09 21:00:30 +00003976X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00003977 SelectionDAG &DAG) const {
Evan Chengc3630942009-12-09 21:00:30 +00003978
3979 // Check if the scalar load can be widened into a vector load. And if
3980 // the address is "base + cst" see if the cst can be "absorbed" into
3981 // the shuffle mask.
3982 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
3983 SDValue Ptr = LD->getBasePtr();
3984 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
3985 return SDValue();
3986 EVT PVT = LD->getValueType(0);
3987 if (PVT != MVT::i32 && PVT != MVT::f32)
3988 return SDValue();
3989
3990 int FI = -1;
3991 int64_t Offset = 0;
3992 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
3993 FI = FINode->getIndex();
3994 Offset = 0;
3995 } else if (Ptr.getOpcode() == ISD::ADD &&
3996 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
3997 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
3998 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
3999 Offset = Ptr.getConstantOperandVal(1);
4000 Ptr = Ptr.getOperand(0);
4001 } else {
4002 return SDValue();
4003 }
4004
4005 SDValue Chain = LD->getChain();
4006 // Make sure the stack object alignment is at least 16.
4007 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
4008 if (DAG.InferPtrAlignment(Ptr) < 16) {
4009 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004010 // Can't change the alignment. FIXME: It's possible to compute
4011 // the exact stack offset and reference FI + adjust offset instead.
4012 // If someone *really* cares about this. That's the way to implement it.
4013 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004014 } else {
4015 MFI->setObjectAlignment(FI, 16);
4016 }
4017 }
4018
4019 // (Offset % 16) must be multiple of 4. Then address is then
4020 // Ptr + (Offset & ~15).
4021 if (Offset < 0)
4022 return SDValue();
4023 if ((Offset % 16) & 3)
4024 return SDValue();
4025 int64_t StartOffset = Offset & ~15;
4026 if (StartOffset)
4027 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4028 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4029
4030 int EltNo = (Offset - StartOffset) >> 2;
4031 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
4032 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
Chris Lattner51abfe42010-09-21 06:02:19 +00004033 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,
4034 LD->getPointerInfo().getWithOffset(StartOffset),
David Greene67c9d422010-02-15 16:53:33 +00004035 false, false, 0);
Evan Chengc3630942009-12-09 21:00:30 +00004036 // Canonicalize it to a v4i32 shuffle.
4037 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32, V1);
4038 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4039 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
Chris Lattner51abfe42010-09-21 06:02:19 +00004040 DAG.getUNDEF(MVT::v4i32),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004041 }
4042
4043 return SDValue();
4044}
4045
Nate Begeman1449f292010-03-24 22:19:06 +00004046/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4047/// vector of type 'VT', see if the elements can be replaced by a single large
4048/// load which has the same value as a build_vector whose operands are 'elts'.
4049///
4050/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
4051///
4052/// FIXME: we'd also like to handle the case where the last elements are zero
4053/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4054/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004055static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004056 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004057 EVT EltVT = VT.getVectorElementType();
4058 unsigned NumElems = Elts.size();
4059
Nate Begemanfdea31a2010-03-24 20:49:50 +00004060 LoadSDNode *LDBase = NULL;
4061 unsigned LastLoadedElt = -1U;
Nate Begeman1449f292010-03-24 22:19:06 +00004062
4063 // For each element in the initializer, see if we've found a load or an undef.
4064 // If we don't find an initial load element, or later load elements are
4065 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004066 for (unsigned i = 0; i < NumElems; ++i) {
4067 SDValue Elt = Elts[i];
4068
4069 if (!Elt.getNode() ||
4070 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4071 return SDValue();
4072 if (!LDBase) {
4073 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4074 return SDValue();
4075 LDBase = cast<LoadSDNode>(Elt.getNode());
4076 LastLoadedElt = i;
4077 continue;
4078 }
4079 if (Elt.getOpcode() == ISD::UNDEF)
4080 continue;
4081
4082 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4083 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4084 return SDValue();
4085 LastLoadedElt = i;
4086 }
Nate Begeman1449f292010-03-24 22:19:06 +00004087
4088 // If we have found an entire vector of loads and undefs, then return a large
4089 // load of the entire vector width starting at the base pointer. If we found
4090 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004091 if (LastLoadedElt == NumElems - 1) {
4092 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004093 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004094 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004095 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004096 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004097 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004098 LDBase->isVolatile(), LDBase->isNonTemporal(),
4099 LDBase->getAlignment());
4100 } else if (NumElems == 4 && LastLoadedElt == 1) {
4101 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4102 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Chris Lattner88641552010-09-22 00:34:38 +00004103 SDValue ResNode = DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys,
4104 Ops, 2, MVT::i32,
4105 LDBase->getMemOperand());
4106 return DAG.getNode(ISD::BIT_CONVERT, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00004107 }
4108 return SDValue();
4109}
4110
Evan Chengc3630942009-12-09 21:00:30 +00004111SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004112X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004113 DebugLoc dl = Op.getDebugLoc();
Chris Lattner6e80e442010-08-28 17:15:43 +00004114 // All zero's are handled with pxor in SSE2 and above, xorps in SSE1.
4115 // All one's are handled with pcmpeqd. In AVX, zero's are handled with
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004116 // vpxor in 128-bit and xor{pd,ps} in 256-bit, but no 256 version of pcmpeqd
4117 // is present, so AllOnes is ignored.
4118 if (ISD::isBuildVectorAllZeros(Op.getNode()) ||
4119 (Op.getValueType().getSizeInBits() != 256 &&
4120 ISD::isBuildVectorAllOnes(Op.getNode()))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004121 // Canonicalize this to <4 x i32> (SSE) to
Chris Lattner8a594482007-11-25 00:24:49 +00004122 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
4123 // eliminated on x86-32 hosts.
Dale Johannesen0488fb62010-09-30 23:57:10 +00004124 if (Op.getValueType() == MVT::v4i32)
Chris Lattner8a594482007-11-25 00:24:49 +00004125 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004126
Gabor Greifba36cb52008-08-28 21:40:38 +00004127 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004128 return getOnesVector(Op.getValueType(), DAG, dl);
4129 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00004130 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004131
Owen Andersone50ed302009-08-10 22:56:29 +00004132 EVT VT = Op.getValueType();
4133 EVT ExtVT = VT.getVectorElementType();
4134 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004135
4136 unsigned NumElems = Op.getNumOperands();
4137 unsigned NumZero = 0;
4138 unsigned NumNonZero = 0;
4139 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004140 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004141 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004142 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004143 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00004144 if (Elt.getOpcode() == ISD::UNDEF)
4145 continue;
4146 Values.insert(Elt);
4147 if (Elt.getOpcode() != ISD::Constant &&
4148 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004149 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00004150 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00004151 NumZero++;
4152 else {
4153 NonZeros |= (1 << i);
4154 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004155 }
4156 }
4157
Chris Lattner97a2a562010-08-26 05:24:29 +00004158 // All undef vector. Return an UNDEF. All zero vectors were handled above.
4159 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00004160 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004161
Chris Lattner67f453a2008-03-09 05:42:06 +00004162 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00004163 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004164 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00004165 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00004166
Chris Lattner62098042008-03-09 01:05:04 +00004167 // If this is an insertion of an i64 value on x86-32, and if the top bits of
4168 // the value are obviously zero, truncate the value to i32 and do the
4169 // insertion that way. Only do this if the value is non-constant or if the
4170 // value is a constant being inserted into element 0. It is cheaper to do
4171 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00004172 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00004173 (!IsAllConstants || Idx == 0)) {
4174 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004175 // Handle SSE only.
4176 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
4177 EVT VecVT = MVT::v4i32;
4178 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00004179
Chris Lattner62098042008-03-09 01:05:04 +00004180 // Truncate the value (which may itself be a constant) to i32, and
4181 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00004182 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00004183 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00004184 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4185 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00004186
Chris Lattner62098042008-03-09 01:05:04 +00004187 // Now we have our 32-bit value zero extended in the low element of
4188 // a vector. If Idx != 0, swizzle it into place.
4189 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004190 SmallVector<int, 4> Mask;
4191 Mask.push_back(Idx);
4192 for (unsigned i = 1; i != VecElts; ++i)
4193 Mask.push_back(i);
4194 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00004195 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00004196 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004197 }
Dale Johannesenace16102009-02-03 19:33:06 +00004198 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00004199 }
4200 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004201
Chris Lattner19f79692008-03-08 22:59:52 +00004202 // If we have a constant or non-constant insertion into the low element of
4203 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
4204 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00004205 // depending on what the source datatype is.
4206 if (Idx == 0) {
4207 if (NumZero == 0) {
4208 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00004209 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
4210 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00004211 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
4212 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
4213 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
4214 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00004215 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
4216 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00004217 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
4218 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00004219 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
4220 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4221 Subtarget->hasSSE2(), DAG);
4222 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Item);
4223 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004224 }
Evan Chengf26ffe92008-05-29 08:22:04 +00004225
4226 // Is it a vector logical left shift?
4227 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00004228 X86::isZeroNode(Op.getOperand(0)) &&
4229 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004230 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00004231 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004232 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004233 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00004234 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004235 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004236
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004237 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00004238 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004239
Chris Lattner19f79692008-03-08 22:59:52 +00004240 // Otherwise, if this is a vector with i32 or f32 elements, and the element
4241 // is a non-constant being inserted into an element other than the low one,
4242 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
4243 // movd/movss) to move this into the low element, then shuffle it into
4244 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004245 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00004246 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00004247
Evan Cheng0db9fe62006-04-25 20:13:52 +00004248 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00004249 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
4250 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00004251 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004252 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00004253 MaskVec.push_back(i == Idx ? 0 : 1);
4254 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004255 }
4256 }
4257
Chris Lattner67f453a2008-03-09 05:42:06 +00004258 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00004259 if (Values.size() == 1) {
4260 if (EVTBits == 32) {
4261 // Instead of a shuffle like this:
4262 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
4263 // Check if it's possible to issue this instead.
4264 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
4265 unsigned Idx = CountTrailingZeros_32(NonZeros);
4266 SDValue Item = Op.getOperand(Idx);
4267 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
4268 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
4269 }
Dan Gohman475871a2008-07-27 21:46:04 +00004270 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004271 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004272
Dan Gohmana3941172007-07-24 22:55:08 +00004273 // A vector full of immediates; various special cases are already
4274 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004275 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00004276 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00004277
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00004278 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004279 if (EVTBits == 64) {
4280 if (NumNonZero == 1) {
4281 // One half is zero or undef.
4282 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00004283 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00004284 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00004285 return getShuffleVectorZeroOrUndef(V2, Idx, true,
4286 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00004287 }
Dan Gohman475871a2008-07-27 21:46:04 +00004288 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00004289 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004290
4291 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00004292 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004293 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00004294 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004295 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004296 }
4297
Bill Wendling826f36f2007-03-28 00:57:11 +00004298 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00004299 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00004300 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004301 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004302 }
4303
4304 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004305 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00004306 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004307 if (NumElems == 4 && NumZero > 0) {
4308 for (unsigned i = 0; i < 4; ++i) {
4309 bool isZero = !(NonZeros & (1 << i));
4310 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00004311 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004312 else
Dale Johannesenace16102009-02-03 19:33:06 +00004313 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004314 }
4315
4316 for (unsigned i = 0; i < 2; ++i) {
4317 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
4318 default: break;
4319 case 0:
4320 V[i] = V[i*2]; // Must be a zero vector.
4321 break;
4322 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00004323 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004324 break;
4325 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00004326 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004327 break;
4328 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00004329 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004330 break;
4331 }
4332 }
4333
Nate Begeman9008ca62009-04-27 18:41:29 +00004334 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004335 bool Reverse = (NonZeros & 0x3) == 2;
4336 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004337 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004338 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
4339 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004340 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
4341 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004342 }
4343
Nate Begemanfdea31a2010-03-24 20:49:50 +00004344 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
4345 // Check for a build vector of consecutive loads.
4346 for (unsigned i = 0; i < NumElems; ++i)
4347 V[i] = Op.getOperand(i);
4348
4349 // Check for elements which are consecutive loads.
4350 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
4351 if (LD.getNode())
4352 return LD;
4353
Chris Lattner24faf612010-08-28 17:59:08 +00004354 // For SSE 4.1, use insertps to put the high elements into the low element.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004355 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00004356 SDValue Result;
4357 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
4358 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
4359 else
4360 Result = DAG.getUNDEF(VT);
4361
4362 for (unsigned i = 1; i < NumElems; ++i) {
4363 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
4364 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00004365 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00004366 }
4367 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00004368 }
Nate Begemanfdea31a2010-03-24 20:49:50 +00004369
Chris Lattner6e80e442010-08-28 17:15:43 +00004370 // Otherwise, expand into a number of unpckl*, start by extending each of
4371 // our (non-undef) elements to the full vector width with the element in the
4372 // bottom slot of the vector (which generates no code for SSE).
4373 for (unsigned i = 0; i < NumElems; ++i) {
4374 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
4375 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
4376 else
4377 V[i] = DAG.getUNDEF(VT);
4378 }
4379
4380 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004381 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
4382 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
4383 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00004384 unsigned EltStride = NumElems >> 1;
4385 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00004386 for (unsigned i = 0; i < EltStride; ++i) {
4387 // If V[i+EltStride] is undef and this is the first round of mixing,
4388 // then it is safe to just drop this shuffle: V[i] is already in the
4389 // right place, the one element (since it's the first round) being
4390 // inserted as undef can be dropped. This isn't safe for successive
4391 // rounds because they will permute elements within both vectors.
4392 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
4393 EltStride == NumElems/2)
4394 continue;
4395
Chris Lattner6e80e442010-08-28 17:15:43 +00004396 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00004397 }
Chris Lattner6e80e442010-08-28 17:15:43 +00004398 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004399 }
4400 return V[0];
4401 }
Dan Gohman475871a2008-07-27 21:46:04 +00004402 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004403}
4404
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004405SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004406X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004407 // We support concatenate two MMX registers and place them in a MMX
4408 // register. This is better than doing a stack convert.
4409 DebugLoc dl = Op.getDebugLoc();
4410 EVT ResVT = Op.getValueType();
4411 assert(Op.getNumOperands() == 2);
4412 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
4413 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
4414 int Mask[2];
4415 SDValue InVec = DAG.getNode(ISD::BIT_CONVERT,dl, MVT::v1i64, Op.getOperand(0));
4416 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4417 InVec = Op.getOperand(1);
4418 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4419 unsigned NumElts = ResVT.getVectorNumElements();
4420 VecOp = DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
4421 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
4422 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
4423 } else {
4424 InVec = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v1i64, InVec);
4425 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4426 Mask[0] = 0; Mask[1] = 2;
4427 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
4428 }
4429 return DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
4430}
4431
Nate Begemanb9a47b82009-02-23 08:49:38 +00004432// v8i16 shuffles - Prefer shuffles in the following order:
4433// 1. [all] pshuflw, pshufhw, optional move
4434// 2. [ssse3] 1 x pshufb
4435// 3. [ssse3] 2 x pshufb + 1 x por
4436// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004437SDValue
4438X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
4439 SelectionDAG &DAG) const {
4440 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00004441 SDValue V1 = SVOp->getOperand(0);
4442 SDValue V2 = SVOp->getOperand(1);
4443 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004444 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00004445
Nate Begemanb9a47b82009-02-23 08:49:38 +00004446 // Determine if more than 1 of the words in each of the low and high quadwords
4447 // of the result come from the same quadword of one of the two inputs. Undef
4448 // mask values count as coming from any quadword, for better codegen.
4449 SmallVector<unsigned, 4> LoQuad(4);
4450 SmallVector<unsigned, 4> HiQuad(4);
4451 BitVector InputQuads(4);
4452 for (unsigned i = 0; i < 8; ++i) {
4453 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00004454 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004455 MaskVals.push_back(EltIdx);
4456 if (EltIdx < 0) {
4457 ++Quad[0];
4458 ++Quad[1];
4459 ++Quad[2];
4460 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00004461 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004462 }
4463 ++Quad[EltIdx / 4];
4464 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00004465 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004466
Nate Begemanb9a47b82009-02-23 08:49:38 +00004467 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004468 unsigned MaxQuad = 1;
4469 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004470 if (LoQuad[i] > MaxQuad) {
4471 BestLoQuad = i;
4472 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004473 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004474 }
4475
Nate Begemanb9a47b82009-02-23 08:49:38 +00004476 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004477 MaxQuad = 1;
4478 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004479 if (HiQuad[i] > MaxQuad) {
4480 BestHiQuad = i;
4481 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004482 }
4483 }
4484
Nate Begemanb9a47b82009-02-23 08:49:38 +00004485 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00004486 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00004487 // single pshufb instruction is necessary. If There are more than 2 input
4488 // quads, disable the next transformation since it does not help SSSE3.
4489 bool V1Used = InputQuads[0] || InputQuads[1];
4490 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004491 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004492 if (InputQuads.count() == 2 && V1Used && V2Used) {
4493 BestLoQuad = InputQuads.find_first();
4494 BestHiQuad = InputQuads.find_next(BestLoQuad);
4495 }
4496 if (InputQuads.count() > 2) {
4497 BestLoQuad = -1;
4498 BestHiQuad = -1;
4499 }
4500 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004501
Nate Begemanb9a47b82009-02-23 08:49:38 +00004502 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
4503 // the shuffle mask. If a quad is scored as -1, that means that it contains
4504 // words from all 4 input quadwords.
4505 SDValue NewV;
4506 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004507 SmallVector<int, 8> MaskV;
4508 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
4509 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00004510 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004511 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V1),
4512 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V2), &MaskV[0]);
4513 NewV = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004514
Nate Begemanb9a47b82009-02-23 08:49:38 +00004515 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
4516 // source words for the shuffle, to aid later transformations.
4517 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00004518 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00004519 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004520 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00004521 if (idx != (int)i)
4522 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004523 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00004524 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004525 AllWordsInNewV = false;
4526 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00004527 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004528
Nate Begemanb9a47b82009-02-23 08:49:38 +00004529 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
4530 if (AllWordsInNewV) {
4531 for (int i = 0; i != 8; ++i) {
4532 int idx = MaskVals[i];
4533 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004534 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004535 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004536 if ((idx != i) && idx < 4)
4537 pshufhw = false;
4538 if ((idx != i) && idx > 3)
4539 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00004540 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00004541 V1 = NewV;
4542 V2Used = false;
4543 BestLoQuad = 0;
4544 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004545 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004546
Nate Begemanb9a47b82009-02-23 08:49:38 +00004547 // If we've eliminated the use of V2, and the new mask is a pshuflw or
4548 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00004549 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004550 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
4551 unsigned TargetMask = 0;
4552 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00004553 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004554 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
4555 X86::getShufflePSHUFLWImmediate(NewV.getNode());
4556 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004557 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00004558 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004559 }
Eric Christopherfd179292009-08-27 18:07:15 +00004560
Nate Begemanb9a47b82009-02-23 08:49:38 +00004561 // If we have SSSE3, and all words of the result are from 1 input vector,
4562 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
4563 // is present, fall back to case 4.
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004564 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004565 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004566
Nate Begemanb9a47b82009-02-23 08:49:38 +00004567 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00004568 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00004569 // mask, and elements that come from V1 in the V2 mask, so that the two
4570 // results can be OR'd together.
4571 bool TwoInputs = V1Used && V2Used;
4572 for (unsigned i = 0; i != 8; ++i) {
4573 int EltIdx = MaskVals[i] * 2;
4574 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004575 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4576 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004577 continue;
4578 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004579 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
4580 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004581 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004582 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004583 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004584 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004585 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004586 if (!TwoInputs)
Owen Anderson825b72b2009-08-11 20:47:22 +00004587 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004588
Nate Begemanb9a47b82009-02-23 08:49:38 +00004589 // Calculate the shuffle mask for the second input, shuffle it, and
4590 // OR it with the first shuffled input.
4591 pshufbMask.clear();
4592 for (unsigned i = 0; i != 8; ++i) {
4593 int EltIdx = MaskVals[i] * 2;
4594 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004595 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4596 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004597 continue;
4598 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004599 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
4600 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004601 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004602 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00004603 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004604 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004605 MVT::v16i8, &pshufbMask[0], 16));
4606 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
4607 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004608 }
4609
4610 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
4611 // and update MaskVals with new element order.
4612 BitVector InOrder(8);
4613 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004614 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004615 for (int i = 0; i != 4; ++i) {
4616 int idx = MaskVals[i];
4617 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004618 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004619 InOrder.set(i);
4620 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004621 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004622 InOrder.set(i);
4623 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004624 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004625 }
4626 }
4627 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004628 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00004629 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004630 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004631
4632 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4633 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
4634 NewV.getOperand(0),
4635 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
4636 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004637 }
Eric Christopherfd179292009-08-27 18:07:15 +00004638
Nate Begemanb9a47b82009-02-23 08:49:38 +00004639 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
4640 // and update MaskVals with the new element order.
4641 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004642 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004643 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004644 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004645 for (unsigned i = 4; i != 8; ++i) {
4646 int idx = MaskVals[i];
4647 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004648 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004649 InOrder.set(i);
4650 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004651 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004652 InOrder.set(i);
4653 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004654 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004655 }
4656 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004657 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004658 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004659
4660 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4661 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
4662 NewV.getOperand(0),
4663 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
4664 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004665 }
Eric Christopherfd179292009-08-27 18:07:15 +00004666
Nate Begemanb9a47b82009-02-23 08:49:38 +00004667 // In case BestHi & BestLo were both -1, which means each quadword has a word
4668 // from each of the four input quadwords, calculate the InOrder bitvector now
4669 // before falling through to the insert/extract cleanup.
4670 if (BestLoQuad == -1 && BestHiQuad == -1) {
4671 NewV = V1;
4672 for (int i = 0; i != 8; ++i)
4673 if (MaskVals[i] < 0 || MaskVals[i] == i)
4674 InOrder.set(i);
4675 }
Eric Christopherfd179292009-08-27 18:07:15 +00004676
Nate Begemanb9a47b82009-02-23 08:49:38 +00004677 // The other elements are put in the right place using pextrw and pinsrw.
4678 for (unsigned i = 0; i != 8; ++i) {
4679 if (InOrder[i])
4680 continue;
4681 int EltIdx = MaskVals[i];
4682 if (EltIdx < 0)
4683 continue;
4684 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004685 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004686 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00004687 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004688 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004689 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004690 DAG.getIntPtrConstant(i));
4691 }
4692 return NewV;
4693}
4694
4695// v16i8 shuffles - Prefer shuffles in the following order:
4696// 1. [ssse3] 1 x pshufb
4697// 2. [ssse3] 2 x pshufb + 1 x por
4698// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
4699static
Nate Begeman9008ca62009-04-27 18:41:29 +00004700SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00004701 SelectionDAG &DAG,
4702 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004703 SDValue V1 = SVOp->getOperand(0);
4704 SDValue V2 = SVOp->getOperand(1);
4705 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004706 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00004707 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00004708
Nate Begemanb9a47b82009-02-23 08:49:38 +00004709 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00004710 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00004711 // present, fall back to case 3.
4712 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
4713 bool V1Only = true;
4714 bool V2Only = true;
4715 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004716 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00004717 if (EltIdx < 0)
4718 continue;
4719 if (EltIdx < 16)
4720 V2Only = false;
4721 else
4722 V1Only = false;
4723 }
Eric Christopherfd179292009-08-27 18:07:15 +00004724
Nate Begemanb9a47b82009-02-23 08:49:38 +00004725 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
4726 if (TLI.getSubtarget()->hasSSSE3()) {
4727 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004728
Nate Begemanb9a47b82009-02-23 08:49:38 +00004729 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00004730 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004731 //
4732 // Otherwise, we have elements from both input vectors, and must zero out
4733 // elements that come from V2 in the first mask, and V1 in the second mask
4734 // so that we can OR them together.
4735 bool TwoInputs = !(V1Only || V2Only);
4736 for (unsigned i = 0; i != 16; ++i) {
4737 int EltIdx = MaskVals[i];
4738 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004739 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004740 continue;
4741 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004742 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004743 }
4744 // If all the elements are from V2, assign it to V1 and return after
4745 // building the first pshufb.
4746 if (V2Only)
4747 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00004748 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004749 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004750 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004751 if (!TwoInputs)
4752 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00004753
Nate Begemanb9a47b82009-02-23 08:49:38 +00004754 // Calculate the shuffle mask for the second input, shuffle it, and
4755 // OR it with the first shuffled input.
4756 pshufbMask.clear();
4757 for (unsigned i = 0; i != 16; ++i) {
4758 int EltIdx = MaskVals[i];
4759 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004760 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004761 continue;
4762 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004763 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004764 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004765 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004766 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004767 MVT::v16i8, &pshufbMask[0], 16));
4768 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004769 }
Eric Christopherfd179292009-08-27 18:07:15 +00004770
Nate Begemanb9a47b82009-02-23 08:49:38 +00004771 // No SSSE3 - Calculate in place words and then fix all out of place words
4772 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
4773 // the 16 different words that comprise the two doublequadword input vectors.
Owen Anderson825b72b2009-08-11 20:47:22 +00004774 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
4775 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004776 SDValue NewV = V2Only ? V2 : V1;
4777 for (int i = 0; i != 8; ++i) {
4778 int Elt0 = MaskVals[i*2];
4779 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00004780
Nate Begemanb9a47b82009-02-23 08:49:38 +00004781 // This word of the result is all undef, skip it.
4782 if (Elt0 < 0 && Elt1 < 0)
4783 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004784
Nate Begemanb9a47b82009-02-23 08:49:38 +00004785 // This word of the result is already in the correct place, skip it.
4786 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
4787 continue;
4788 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
4789 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004790
Nate Begemanb9a47b82009-02-23 08:49:38 +00004791 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
4792 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
4793 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00004794
4795 // If Elt0 and Elt1 are defined, are consecutive, and can be load
4796 // using a single extract together, load it and store it.
4797 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004798 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004799 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00004800 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004801 DAG.getIntPtrConstant(i));
4802 continue;
4803 }
4804
Nate Begemanb9a47b82009-02-23 08:49:38 +00004805 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00004806 // source byte is not also odd, shift the extracted word left 8 bits
4807 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004808 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004809 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004810 DAG.getIntPtrConstant(Elt1 / 2));
4811 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004812 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004813 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004814 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004815 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
4816 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004817 }
4818 // If Elt0 is defined, extract it from the appropriate source. If the
4819 // source byte is not also even, shift the extracted word right 8 bits. If
4820 // Elt1 was also defined, OR the extracted values together before
4821 // inserting them in the result.
4822 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004823 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004824 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
4825 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004826 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004827 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004828 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004829 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
4830 DAG.getConstant(0x00FF, MVT::i16));
4831 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00004832 : InsElt0;
4833 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004834 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004835 DAG.getIntPtrConstant(i));
4836 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004837 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004838}
4839
Evan Cheng7a831ce2007-12-15 03:00:47 +00004840/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00004841/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00004842/// done when every pair / quad of shuffle mask elements point to elements in
4843/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00004844/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00004845static
Nate Begeman9008ca62009-04-27 18:41:29 +00004846SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00004847 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00004848 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00004849 SDValue V1 = SVOp->getOperand(0);
4850 SDValue V2 = SVOp->getOperand(1);
4851 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00004852 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00004853 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00004854 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004855 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004856 case MVT::v4f32: NewVT = MVT::v2f64; break;
4857 case MVT::v4i32: NewVT = MVT::v2i64; break;
4858 case MVT::v8i16: NewVT = MVT::v4i32; break;
4859 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004860 }
4861
Nate Begeman9008ca62009-04-27 18:41:29 +00004862 int Scale = NumElems / NewWidth;
4863 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00004864 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004865 int StartIdx = -1;
4866 for (int j = 0; j < Scale; ++j) {
4867 int EltIdx = SVOp->getMaskElt(i+j);
4868 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004869 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00004870 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00004871 StartIdx = EltIdx - (EltIdx % Scale);
4872 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00004873 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004874 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004875 if (StartIdx == -1)
4876 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00004877 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004878 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004879 }
4880
Dale Johannesenace16102009-02-03 19:33:06 +00004881 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V1);
4882 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00004883 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004884}
4885
Evan Chengd880b972008-05-09 21:53:03 +00004886/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004887///
Owen Andersone50ed302009-08-10 22:56:29 +00004888static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00004889 SDValue SrcOp, SelectionDAG &DAG,
4890 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004891 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004892 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00004893 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00004894 LD = dyn_cast<LoadSDNode>(SrcOp);
4895 if (!LD) {
4896 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
4897 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00004898 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
4899 if ((ExtVT.SimpleTy != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00004900 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4901 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00004902 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004903 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00004904 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Dale Johannesenace16102009-02-03 19:33:06 +00004905 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4906 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
4907 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
4908 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00004909 SrcOp.getOperand(0)
4910 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004911 }
4912 }
4913 }
4914
Dale Johannesenace16102009-02-03 19:33:06 +00004915 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4916 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004917 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00004918 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004919}
4920
Evan Chengace3c172008-07-22 21:13:36 +00004921/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
4922/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004923static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00004924LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
4925 SDValue V1 = SVOp->getOperand(0);
4926 SDValue V2 = SVOp->getOperand(1);
4927 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004928 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00004929
Evan Chengace3c172008-07-22 21:13:36 +00004930 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00004931 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00004932 SmallVector<int, 8> Mask1(4U, -1);
4933 SmallVector<int, 8> PermMask;
4934 SVOp->getMask(PermMask);
4935
Evan Chengace3c172008-07-22 21:13:36 +00004936 unsigned NumHi = 0;
4937 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00004938 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004939 int Idx = PermMask[i];
4940 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004941 Locs[i] = std::make_pair(-1, -1);
4942 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004943 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
4944 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004945 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00004946 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004947 NumLo++;
4948 } else {
4949 Locs[i] = std::make_pair(1, NumHi);
4950 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004951 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004952 NumHi++;
4953 }
4954 }
4955 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004956
Evan Chengace3c172008-07-22 21:13:36 +00004957 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004958 // If no more than two elements come from either vector. This can be
4959 // implemented with two shuffles. First shuffle gather the elements.
4960 // The second shuffle, which takes the first shuffle as both of its
4961 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00004962 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004963
Nate Begeman9008ca62009-04-27 18:41:29 +00004964 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00004965
Evan Chengace3c172008-07-22 21:13:36 +00004966 for (unsigned i = 0; i != 4; ++i) {
4967 if (Locs[i].first == -1)
4968 continue;
4969 else {
4970 unsigned Idx = (i < 2) ? 0 : 4;
4971 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004972 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004973 }
4974 }
4975
Nate Begeman9008ca62009-04-27 18:41:29 +00004976 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004977 } else if (NumLo == 3 || NumHi == 3) {
4978 // Otherwise, we must have three elements from one vector, call it X, and
4979 // one element from the other, call it Y. First, use a shufps to build an
4980 // intermediate vector with the one element from Y and the element from X
4981 // that will be in the same half in the final destination (the indexes don't
4982 // matter). Then, use a shufps to build the final vector, taking the half
4983 // containing the element from Y from the intermediate, and the other half
4984 // from X.
4985 if (NumHi == 3) {
4986 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00004987 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004988 std::swap(V1, V2);
4989 }
4990
4991 // Find the element from V2.
4992 unsigned HiIndex;
4993 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004994 int Val = PermMask[HiIndex];
4995 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004996 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004997 if (Val >= 4)
4998 break;
4999 }
5000
Nate Begeman9008ca62009-04-27 18:41:29 +00005001 Mask1[0] = PermMask[HiIndex];
5002 Mask1[1] = -1;
5003 Mask1[2] = PermMask[HiIndex^1];
5004 Mask1[3] = -1;
5005 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005006
5007 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005008 Mask1[0] = PermMask[0];
5009 Mask1[1] = PermMask[1];
5010 Mask1[2] = HiIndex & 1 ? 6 : 4;
5011 Mask1[3] = HiIndex & 1 ? 4 : 6;
5012 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005013 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005014 Mask1[0] = HiIndex & 1 ? 2 : 0;
5015 Mask1[1] = HiIndex & 1 ? 0 : 2;
5016 Mask1[2] = PermMask[2];
5017 Mask1[3] = PermMask[3];
5018 if (Mask1[2] >= 0)
5019 Mask1[2] += 4;
5020 if (Mask1[3] >= 0)
5021 Mask1[3] += 4;
5022 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005023 }
Evan Chengace3c172008-07-22 21:13:36 +00005024 }
5025
5026 // Break it into (shuffle shuffle_hi, shuffle_lo).
5027 Locs.clear();
Nate Begeman9008ca62009-04-27 18:41:29 +00005028 SmallVector<int,8> LoMask(4U, -1);
5029 SmallVector<int,8> HiMask(4U, -1);
5030
5031 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00005032 unsigned MaskIdx = 0;
5033 unsigned LoIdx = 0;
5034 unsigned HiIdx = 2;
5035 for (unsigned i = 0; i != 4; ++i) {
5036 if (i == 2) {
5037 MaskPtr = &HiMask;
5038 MaskIdx = 1;
5039 LoIdx = 0;
5040 HiIdx = 2;
5041 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005042 int Idx = PermMask[i];
5043 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005044 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00005045 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005046 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005047 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005048 LoIdx++;
5049 } else {
5050 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005051 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005052 HiIdx++;
5053 }
5054 }
5055
Nate Begeman9008ca62009-04-27 18:41:29 +00005056 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
5057 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
5058 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00005059 for (unsigned i = 0; i != 4; ++i) {
5060 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005061 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00005062 } else {
5063 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005064 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00005065 }
5066 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005067 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00005068}
5069
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005070static bool MayFoldVectorLoad(SDValue V) {
5071 if (V.hasOneUse() && V.getOpcode() == ISD::BIT_CONVERT)
5072 V = V.getOperand(0);
5073 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5074 V = V.getOperand(0);
5075 if (MayFoldLoad(V))
5076 return true;
5077 return false;
5078}
5079
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005080// FIXME: the version above should always be used. Since there's
5081// a bug where several vector shuffles can't be folded because the
5082// DAG is not updated during lowering and a node claims to have two
5083// uses while it only has one, use this version, and let isel match
5084// another instruction if the load really happens to have more than
5085// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00005086// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005087static bool RelaxedMayFoldVectorLoad(SDValue V) {
5088 if (V.hasOneUse() && V.getOpcode() == ISD::BIT_CONVERT)
5089 V = V.getOperand(0);
5090 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5091 V = V.getOperand(0);
5092 if (ISD::isNormalLoad(V.getNode()))
5093 return true;
5094 return false;
5095}
5096
5097/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
5098/// a vector extract, and if both can be later optimized into a single load.
5099/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
5100/// here because otherwise a target specific shuffle node is going to be
5101/// emitted for this shuffle, and the optimization not done.
5102/// FIXME: This is probably not the best approach, but fix the problem
5103/// until the right path is decided.
5104static
5105bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
5106 const TargetLowering &TLI) {
5107 EVT VT = V.getValueType();
5108 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
5109
5110 // Be sure that the vector shuffle is present in a pattern like this:
5111 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
5112 if (!V.hasOneUse())
5113 return false;
5114
5115 SDNode *N = *V.getNode()->use_begin();
5116 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5117 return false;
5118
5119 SDValue EltNo = N->getOperand(1);
5120 if (!isa<ConstantSDNode>(EltNo))
5121 return false;
5122
5123 // If the bit convert changed the number of elements, it is unsafe
5124 // to examine the mask.
5125 bool HasShuffleIntoBitcast = false;
5126 if (V.getOpcode() == ISD::BIT_CONVERT) {
5127 EVT SrcVT = V.getOperand(0).getValueType();
5128 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
5129 return false;
5130 V = V.getOperand(0);
5131 HasShuffleIntoBitcast = true;
5132 }
5133
5134 // Select the input vector, guarding against out of range extract vector.
5135 unsigned NumElems = VT.getVectorNumElements();
5136 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
5137 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
5138 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
5139
5140 // Skip one more bit_convert if necessary
5141 if (V.getOpcode() == ISD::BIT_CONVERT)
5142 V = V.getOperand(0);
5143
5144 if (ISD::isNormalLoad(V.getNode())) {
5145 // Is the original load suitable?
5146 LoadSDNode *LN0 = cast<LoadSDNode>(V);
5147
5148 // FIXME: avoid the multi-use bug that is preventing lots of
5149 // of foldings to be detected, this is still wrong of course, but
5150 // give the temporary desired behavior, and if it happens that
5151 // the load has real more uses, during isel it will not fold, and
5152 // will generate poor code.
5153 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
5154 return false;
5155
5156 if (!HasShuffleIntoBitcast)
5157 return true;
5158
5159 // If there's a bitcast before the shuffle, check if the load type and
5160 // alignment is valid.
5161 unsigned Align = LN0->getAlignment();
5162 unsigned NewAlign =
5163 TLI.getTargetData()->getABITypeAlignment(
5164 VT.getTypeForEVT(*DAG.getContext()));
5165
5166 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
5167 return false;
5168 }
5169
5170 return true;
5171}
5172
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005173static
Evan Cheng835580f2010-10-07 20:50:20 +00005174SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
5175 EVT VT = Op.getValueType();
5176
5177 // Canonizalize to v2f64.
5178 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, V1);
5179 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
5180 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
5181 V1, DAG));
5182}
5183
5184static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005185SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
5186 bool HasSSE2) {
5187 SDValue V1 = Op.getOperand(0);
5188 SDValue V2 = Op.getOperand(1);
5189 EVT VT = Op.getValueType();
5190
5191 assert(VT != MVT::v2i64 && "unsupported shuffle type");
5192
5193 if (HasSSE2 && VT == MVT::v2f64)
5194 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
5195
5196 // v4f32 or v4i32
5197 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V2, DAG);
5198}
5199
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005200static
5201SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
5202 SDValue V1 = Op.getOperand(0);
5203 SDValue V2 = Op.getOperand(1);
5204 EVT VT = Op.getValueType();
5205
5206 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
5207 "unsupported shuffle type");
5208
5209 if (V2.getOpcode() == ISD::UNDEF)
5210 V2 = V1;
5211
5212 // v4i32 or v4f32
5213 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
5214}
5215
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005216static
5217SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
5218 SDValue V1 = Op.getOperand(0);
5219 SDValue V2 = Op.getOperand(1);
5220 EVT VT = Op.getValueType();
5221 unsigned NumElems = VT.getVectorNumElements();
5222
5223 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
5224 // operand of these instructions is only memory, so check if there's a
5225 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
5226 // same masks.
5227 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005228
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00005229 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005230 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005231 CanFoldLoad = true;
5232
5233 // When V1 is a load, it can be folded later into a store in isel, example:
5234 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
5235 // turns into:
5236 // (MOVLPSmr addr:$src1, VR128:$src2)
5237 // So, recognize this potential and also use MOVLPS or MOVLPD
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005238 if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005239 CanFoldLoad = true;
5240
5241 if (CanFoldLoad) {
5242 if (HasSSE2 && NumElems == 2)
5243 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
5244
5245 if (NumElems == 4)
5246 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
5247 }
5248
5249 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5250 // movl and movlp will both match v2i64, but v2i64 is never matched by
5251 // movl earlier because we make it strict to avoid messing with the movlp load
5252 // folding logic (see the code above getMOVLP call). Match it here then,
5253 // this is horrible, but will stay like this until we move all shuffle
5254 // matching to x86 specific nodes. Note that for the 1st condition all
5255 // types are matched with movsd.
5256 if ((HasSSE2 && NumElems == 2) || !X86::isMOVLMask(SVOp))
5257 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5258 else if (HasSSE2)
5259 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5260
5261
5262 assert(VT != MVT::v4i32 && "unsupported shuffle type");
5263
5264 // Invert the operand order and use SHUFPS to match it.
5265 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V2, V1,
5266 X86::getShuffleSHUFImmediate(SVOp), DAG);
5267}
5268
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005269static inline unsigned getUNPCKLOpcode(EVT VT) {
5270 switch(VT.getSimpleVT().SimpleTy) {
5271 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
5272 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
5273 case MVT::v4f32: return X86ISD::UNPCKLPS;
5274 case MVT::v2f64: return X86ISD::UNPCKLPD;
5275 case MVT::v16i8: return X86ISD::PUNPCKLBW;
5276 case MVT::v8i16: return X86ISD::PUNPCKLWD;
5277 default:
5278 llvm_unreachable("Unknow type for unpckl");
5279 }
5280 return 0;
5281}
5282
5283static inline unsigned getUNPCKHOpcode(EVT VT) {
5284 switch(VT.getSimpleVT().SimpleTy) {
5285 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
5286 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
5287 case MVT::v4f32: return X86ISD::UNPCKHPS;
5288 case MVT::v2f64: return X86ISD::UNPCKHPD;
5289 case MVT::v16i8: return X86ISD::PUNPCKHBW;
5290 case MVT::v8i16: return X86ISD::PUNPCKHWD;
5291 default:
5292 llvm_unreachable("Unknow type for unpckh");
5293 }
5294 return 0;
5295}
5296
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005297static
5298SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005299 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005300 const X86Subtarget *Subtarget) {
5301 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5302 EVT VT = Op.getValueType();
5303 DebugLoc dl = Op.getDebugLoc();
5304 SDValue V1 = Op.getOperand(0);
5305 SDValue V2 = Op.getOperand(1);
5306
5307 if (isZeroShuffle(SVOp))
5308 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
5309
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005310 // Handle splat operations
5311 if (SVOp->isSplat()) {
5312 // Special case, this is the only place now where it's
5313 // allowed to return a vector_shuffle operation without
5314 // using a target specific node, because *hopefully* it
5315 // will be optimized away by the dag combiner.
5316 if (VT.getVectorNumElements() <= 4 &&
5317 CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
5318 return Op;
5319
5320 // Handle splats by matching through known masks
5321 if (VT.getVectorNumElements() <= 4)
5322 return SDValue();
5323
Evan Cheng835580f2010-10-07 20:50:20 +00005324 // Canonicalize all of the remaining to v4f32.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005325 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005326 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005327
5328 // If the shuffle can be profitably rewritten as a narrower shuffle, then
5329 // do it!
5330 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
5331 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5332 if (NewOp.getNode())
5333 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, NewOp);
5334 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
5335 // FIXME: Figure out a cleaner way to do this.
5336 // Try to make use of movq to zero out the top part.
5337 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
5338 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5339 if (NewOp.getNode()) {
5340 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
5341 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
5342 DAG, Subtarget, dl);
5343 }
5344 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
5345 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5346 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
5347 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
5348 DAG, Subtarget, dl);
5349 }
5350 }
5351 return SDValue();
5352}
5353
Dan Gohman475871a2008-07-27 21:46:04 +00005354SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005355X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00005356 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00005357 SDValue V1 = Op.getOperand(0);
5358 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005359 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005360 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00005361 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005362 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005363 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
5364 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00005365 bool V1IsSplat = false;
5366 bool V2IsSplat = false;
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005367 bool HasSSE2 = Subtarget->hasSSE2() || Subtarget->hasAVX();
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005368 bool HasSSE3 = Subtarget->hasSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005369 bool HasSSSE3 = Subtarget->hasSSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005370 MachineFunction &MF = DAG.getMachineFunction();
5371 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005372
Dale Johannesen0488fb62010-09-30 23:57:10 +00005373 // Shuffle operations on MMX not supported.
5374 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00005375 return Op;
5376
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005377 // Vector shuffle lowering takes 3 steps:
5378 //
5379 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
5380 // narrowing and commutation of operands should be handled.
5381 // 2) Matching of shuffles with known shuffle masks to x86 target specific
5382 // shuffle nodes.
5383 // 3) Rewriting of unmatched masks into new generic shuffle operations,
5384 // so the shuffle can be broken into other shuffles and the legalizer can
5385 // try the lowering again.
5386 //
5387 // The general ideia is that no vector_shuffle operation should be left to
5388 // be matched during isel, all of them must be converted to a target specific
5389 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00005390
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005391 // Normalize the input vectors. Here splats, zeroed vectors, profitable
5392 // narrowing and commutation of operands should be handled. The actual code
5393 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005394 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005395 if (NewOp.getNode())
5396 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00005397
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005398 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
5399 // unpckh_undef). Only use pshufd if speed is more important than size.
5400 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
5401 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5402 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
5403 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
5404 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5405 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00005406
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005407 if (X86::isMOVDDUPMask(SVOp) && HasSSE3 && V2IsUndef &&
Dale Johannesen0488fb62010-09-30 23:57:10 +00005408 RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00005409 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005410
Dale Johannesen0488fb62010-09-30 23:57:10 +00005411 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005412 return getMOVHighToLow(Op, dl, DAG);
5413
5414 // Use to match splats
5415 if (HasSSE2 && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
5416 (VT == MVT::v2f64 || VT == MVT::v2i64))
5417 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5418
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005419 if (X86::isPSHUFDMask(SVOp)) {
5420 // The actual implementation will match the mask in the if above and then
5421 // during isel it can match several different instructions, not only pshufd
5422 // as its name says, sad but true, emulate the behavior for now...
5423 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
5424 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
5425
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005426 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5427
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005428 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005429 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
5430
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005431 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005432 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V1,
5433 TargetMask, DAG);
5434
5435 if (VT == MVT::v4f32)
5436 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V1,
5437 TargetMask, DAG);
5438 }
Eric Christopherfd179292009-08-27 18:07:15 +00005439
Evan Chengf26ffe92008-05-29 08:22:04 +00005440 // Check if this can be converted into a logical shift.
5441 bool isLeft = false;
5442 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00005443 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00005444 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00005445 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00005446 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005447 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00005448 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005449 EVT EltVT = VT.getVectorElementType();
5450 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005451 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005452 }
Eric Christopherfd179292009-08-27 18:07:15 +00005453
Nate Begeman9008ca62009-04-27 18:41:29 +00005454 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005455 if (V1IsUndef)
5456 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00005457 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00005458 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005459 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005460 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005461 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5462
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005463 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005464 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5465 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00005466 }
Eric Christopherfd179292009-08-27 18:07:15 +00005467
Nate Begeman9008ca62009-04-27 18:41:29 +00005468 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00005469 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
5470 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005471
Dale Johannesen0488fb62010-09-30 23:57:10 +00005472 if (X86::isMOVHLPSMask(SVOp))
5473 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005474
Dale Johannesen0488fb62010-09-30 23:57:10 +00005475 if (X86::isMOVSHDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5476 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005477
Dale Johannesen0488fb62010-09-30 23:57:10 +00005478 if (X86::isMOVSLDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5479 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00005480
Dale Johannesen0488fb62010-09-30 23:57:10 +00005481 if (X86::isMOVLPMask(SVOp))
5482 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005483
Nate Begeman9008ca62009-04-27 18:41:29 +00005484 if (ShouldXformToMOVHLPS(SVOp) ||
5485 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
5486 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005487
Evan Chengf26ffe92008-05-29 08:22:04 +00005488 if (isShift) {
5489 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005490 EVT EltVT = VT.getVectorElementType();
5491 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005492 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005493 }
Eric Christopherfd179292009-08-27 18:07:15 +00005494
Evan Cheng9eca5e82006-10-25 21:49:50 +00005495 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00005496 // FIXME: This should also accept a bitcast of a splat? Be careful, not
5497 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00005498 V1IsSplat = isSplatVector(V1.getNode());
5499 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005500
Chris Lattner8a594482007-11-25 00:24:49 +00005501 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00005502 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005503 Op = CommuteVectorShuffle(SVOp, DAG);
5504 SVOp = cast<ShuffleVectorSDNode>(Op);
5505 V1 = SVOp->getOperand(0);
5506 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00005507 std::swap(V1IsSplat, V2IsSplat);
5508 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005509 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00005510 }
5511
Nate Begeman9008ca62009-04-27 18:41:29 +00005512 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
5513 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00005514 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00005515 return V1;
5516 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
5517 // the instruction selector will not match, so get a canonical MOVL with
5518 // swapped operands to undo the commute.
5519 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00005520 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005521
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005522 if (X86::isUNPCKLMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005523 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005524
5525 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005526 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00005527
Evan Cheng9bbbb982006-10-25 20:48:19 +00005528 if (V2IsSplat) {
5529 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005530 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00005531 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00005532 SDValue NewMask = NormalizeMask(SVOp, DAG);
5533 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
5534 if (NSVOp != SVOp) {
5535 if (X86::isUNPCKLMask(NSVOp, true)) {
5536 return NewMask;
5537 } else if (X86::isUNPCKHMask(NSVOp, true)) {
5538 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005539 }
5540 }
5541 }
5542
Evan Cheng9eca5e82006-10-25 21:49:50 +00005543 if (Commuted) {
5544 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00005545 // FIXME: this seems wrong.
5546 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
5547 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005548
5549 if (X86::isUNPCKLMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005550 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005551
5552 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005553 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005554 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005555
Nate Begeman9008ca62009-04-27 18:41:29 +00005556 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00005557 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00005558 return CommuteVectorShuffle(SVOp, DAG);
5559
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00005560 // The checks below are all present in isShuffleMaskLegal, but they are
5561 // inlined here right now to enable us to directly emit target specific
5562 // nodes, and remove one by one until they don't return Op anymore.
5563 SmallVector<int, 16> M;
5564 SVOp->getMask(M);
5565
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005566 if (isPALIGNRMask(M, VT, HasSSSE3))
5567 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
5568 X86::getShufflePALIGNRImmediate(SVOp),
5569 DAG);
5570
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00005571 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
5572 SVOp->getSplatIndex() == 0 && V2IsUndef) {
5573 if (VT == MVT::v2f64)
5574 return getTargetShuffleNode(X86ISD::UNPCKLPD, dl, VT, V1, V1, DAG);
5575 if (VT == MVT::v2i64)
5576 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
5577 }
5578
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00005579 if (isPSHUFHWMask(M, VT))
5580 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
5581 X86::getShufflePSHUFHWImmediate(SVOp),
5582 DAG);
5583
5584 if (isPSHUFLWMask(M, VT))
5585 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
5586 X86::getShufflePSHUFLWImmediate(SVOp),
5587 DAG);
5588
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00005589 if (isSHUFPMask(M, VT)) {
5590 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5591 if (VT == MVT::v4f32 || VT == MVT::v4i32)
5592 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V2,
5593 TargetMask, DAG);
5594 if (VT == MVT::v2f64 || VT == MVT::v2i64)
5595 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V2,
5596 TargetMask, DAG);
5597 }
5598
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005599 if (X86::isUNPCKL_v_undef_Mask(SVOp))
5600 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5601 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
5602 if (X86::isUNPCKH_v_undef_Mask(SVOp))
5603 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5604 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5605
Evan Cheng14b32e12007-12-11 01:46:18 +00005606 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00005607 if (VT == MVT::v8i16) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005608 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005609 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00005610 return NewOp;
5611 }
5612
Owen Anderson825b72b2009-08-11 20:47:22 +00005613 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005614 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005615 if (NewOp.getNode())
5616 return NewOp;
5617 }
Eric Christopherfd179292009-08-27 18:07:15 +00005618
Dale Johannesen0488fb62010-09-30 23:57:10 +00005619 // Handle all 4 wide cases with a number of shuffles.
5620 if (NumElems == 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005621 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005622
Dan Gohman475871a2008-07-27 21:46:04 +00005623 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005624}
5625
Dan Gohman475871a2008-07-27 21:46:04 +00005626SDValue
5627X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00005628 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005629 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005630 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005631 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005632 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005633 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005634 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005635 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005636 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005637 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00005638 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
5639 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
5640 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005641 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
5642 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Dale Johannesenace16102009-02-03 19:33:06 +00005643 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005644 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00005645 Op.getOperand(0)),
5646 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005647 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005648 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005649 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005650 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005651 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00005652 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00005653 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
5654 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005655 // result has a single use which is a store or a bitcast to i32. And in
5656 // the case of a store, it's not worth it if the index is a constant 0,
5657 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00005658 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00005659 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00005660 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005661 if ((User->getOpcode() != ISD::STORE ||
5662 (isa<ConstantSDNode>(Op.getOperand(1)) &&
5663 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman171c11e2008-04-16 02:32:24 +00005664 (User->getOpcode() != ISD::BIT_CONVERT ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005665 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00005666 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00005667 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
5668 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00005669 Op.getOperand(0)),
5670 Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005671 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Extract);
5672 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00005673 // ExtractPS works with constant index.
5674 if (isa<ConstantSDNode>(Op.getOperand(1)))
5675 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00005676 }
Dan Gohman475871a2008-07-27 21:46:04 +00005677 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005678}
5679
5680
Dan Gohman475871a2008-07-27 21:46:04 +00005681SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005682X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
5683 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005684 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00005685 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005686
Evan Cheng62a3f152008-03-24 21:52:23 +00005687 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005688 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005689 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00005690 return Res;
5691 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00005692
Owen Andersone50ed302009-08-10 22:56:29 +00005693 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005694 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005695 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005696 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005697 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005698 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005699 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005700 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
5701 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Scott Michelfdc40a02009-02-17 22:15:04 +00005702 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005703 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00005704 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005705 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00005706 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00005707 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00005708 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00005709 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00005710 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005711 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005712 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005713 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005714 if (Idx == 0)
5715 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00005716
Evan Cheng0db9fe62006-04-25 20:13:52 +00005717 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00005718 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00005719 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00005720 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00005721 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00005722 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00005723 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00005724 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00005725 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
5726 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
5727 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005728 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005729 if (Idx == 0)
5730 return Op;
5731
5732 // UNPCKHPD the element to the lowest double word, then movsd.
5733 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
5734 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00005735 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00005736 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00005737 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00005738 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00005739 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00005740 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005741 }
5742
Dan Gohman475871a2008-07-27 21:46:04 +00005743 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005744}
5745
Dan Gohman475871a2008-07-27 21:46:04 +00005746SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005747X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
5748 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005749 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00005750 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005751 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005752
Dan Gohman475871a2008-07-27 21:46:04 +00005753 SDValue N0 = Op.getOperand(0);
5754 SDValue N1 = Op.getOperand(1);
5755 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00005756
Dan Gohman8a55ce42009-09-23 21:02:20 +00005757 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00005758 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00005759 unsigned Opc;
5760 if (VT == MVT::v8i16)
5761 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00005762 else if (VT == MVT::v16i8)
5763 Opc = X86ISD::PINSRB;
5764 else
5765 Opc = X86ISD::PINSRB;
5766
Nate Begeman14d12ca2008-02-11 04:19:36 +00005767 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
5768 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00005769 if (N1.getValueType() != MVT::i32)
5770 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
5771 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005772 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00005773 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00005774 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00005775 // Bits [7:6] of the constant are the source select. This will always be
5776 // zero here. The DAG Combiner may combine an extract_elt index into these
5777 // bits. For example (insert (extract, 3), 2) could be matched by putting
5778 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00005779 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00005780 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00005781 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00005782 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005783 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00005784 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00005785 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00005786 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00005787 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00005788 // PINSR* works with constant index.
5789 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00005790 }
Dan Gohman475871a2008-07-27 21:46:04 +00005791 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005792}
5793
Dan Gohman475871a2008-07-27 21:46:04 +00005794SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005795X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005796 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00005797 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005798
5799 if (Subtarget->hasSSE41())
5800 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
5801
Dan Gohman8a55ce42009-09-23 21:02:20 +00005802 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00005803 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00005804
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005805 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005806 SDValue N0 = Op.getOperand(0);
5807 SDValue N1 = Op.getOperand(1);
5808 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00005809
Dan Gohman8a55ce42009-09-23 21:02:20 +00005810 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00005811 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
5812 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00005813 if (N1.getValueType() != MVT::i32)
5814 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
5815 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005816 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00005817 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005818 }
Dan Gohman475871a2008-07-27 21:46:04 +00005819 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005820}
5821
Dan Gohman475871a2008-07-27 21:46:04 +00005822SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005823X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005824 DebugLoc dl = Op.getDebugLoc();
Chris Lattnerf172ecd2010-07-04 23:07:25 +00005825
5826 if (Op.getValueType() == MVT::v1i64 &&
5827 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00005828 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00005829
Owen Anderson825b72b2009-08-11 20:47:22 +00005830 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00005831 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
5832 "Expected an SSE type!");
Dale Johannesenace16102009-02-03 19:33:06 +00005833 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00005834 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005835}
5836
Bill Wendling056292f2008-09-16 21:48:12 +00005837// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
5838// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
5839// one of the above mentioned nodes. It has to be wrapped because otherwise
5840// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
5841// be used to form addressing mode. These wrapped nodes will be selected
5842// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00005843SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005844X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005845 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00005846
Chris Lattner41621a22009-06-26 19:22:52 +00005847 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5848 // global base reg.
5849 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005850 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005851 CodeModel::Model M = getTargetMachine().getCodeModel();
5852
Chris Lattner4f066492009-07-11 20:29:19 +00005853 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005854 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005855 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005856 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005857 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005858 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005859 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005860
Evan Cheng1606e8e2009-03-13 07:51:59 +00005861 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00005862 CP->getAlignment(),
5863 CP->getOffset(), OpFlag);
5864 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00005865 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005866 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00005867 if (OpFlag) {
5868 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005869 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005870 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005871 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005872 }
5873
5874 return Result;
5875}
5876
Dan Gohmand858e902010-04-17 15:26:15 +00005877SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00005878 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00005879
Chris Lattner18c59872009-06-27 04:16:01 +00005880 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5881 // global base reg.
5882 unsigned char OpFlag = 0;
5883 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005884 CodeModel::Model M = getTargetMachine().getCodeModel();
5885
Chris Lattner4f066492009-07-11 20:29:19 +00005886 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005887 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005888 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005889 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005890 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005891 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005892 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005893
Chris Lattner18c59872009-06-27 04:16:01 +00005894 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
5895 OpFlag);
5896 DebugLoc DL = JT->getDebugLoc();
5897 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005898
Chris Lattner18c59872009-06-27 04:16:01 +00005899 // With PIC, the address is actually $g + Offset.
5900 if (OpFlag) {
5901 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5902 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005903 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00005904 Result);
5905 }
Eric Christopherfd179292009-08-27 18:07:15 +00005906
Chris Lattner18c59872009-06-27 04:16:01 +00005907 return Result;
5908}
5909
5910SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005911X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00005912 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00005913
Chris Lattner18c59872009-06-27 04:16:01 +00005914 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5915 // global base reg.
5916 unsigned char OpFlag = 0;
5917 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005918 CodeModel::Model M = getTargetMachine().getCodeModel();
5919
Chris Lattner4f066492009-07-11 20:29:19 +00005920 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005921 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005922 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005923 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005924 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005925 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005926 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005927
Chris Lattner18c59872009-06-27 04:16:01 +00005928 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00005929
Chris Lattner18c59872009-06-27 04:16:01 +00005930 DebugLoc DL = Op.getDebugLoc();
5931 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005932
5933
Chris Lattner18c59872009-06-27 04:16:01 +00005934 // With PIC, the address is actually $g + Offset.
5935 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00005936 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00005937 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5938 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005939 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00005940 Result);
5941 }
Eric Christopherfd179292009-08-27 18:07:15 +00005942
Chris Lattner18c59872009-06-27 04:16:01 +00005943 return Result;
5944}
5945
Dan Gohman475871a2008-07-27 21:46:04 +00005946SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005947X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00005948 // Create the TargetBlockAddressAddress node.
5949 unsigned char OpFlags =
5950 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00005951 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00005952 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00005953 DebugLoc dl = Op.getDebugLoc();
5954 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
5955 /*isTarget=*/true, OpFlags);
5956
Dan Gohmanf705adb2009-10-30 01:28:02 +00005957 if (Subtarget->isPICStyleRIPRel() &&
5958 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00005959 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
5960 else
5961 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00005962
Dan Gohman29cbade2009-11-20 23:18:13 +00005963 // With PIC, the address is actually $g + Offset.
5964 if (isGlobalRelativeToPICBase(OpFlags)) {
5965 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5966 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
5967 Result);
5968 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00005969
5970 return Result;
5971}
5972
5973SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00005974X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00005975 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00005976 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00005977 // Create the TargetGlobalAddress node, folding in the constant
5978 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00005979 unsigned char OpFlags =
5980 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005981 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00005982 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005983 if (OpFlags == X86II::MO_NO_FLAG &&
5984 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00005985 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00005986 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00005987 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005988 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00005989 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00005990 }
Eric Christopherfd179292009-08-27 18:07:15 +00005991
Chris Lattner4f066492009-07-11 20:29:19 +00005992 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005993 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00005994 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
5995 else
5996 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00005997
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005998 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00005999 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006000 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6001 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006002 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006003 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006004
Chris Lattner36c25012009-07-10 07:34:39 +00006005 // For globals that require a load from a stub to get the address, emit the
6006 // load.
6007 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00006008 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006009 MachinePointerInfo::getGOT(), false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006010
Dan Gohman6520e202008-10-18 02:06:02 +00006011 // If there was a non-zero offset that we didn't fold, create an explicit
6012 // addition for it.
6013 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006014 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00006015 DAG.getConstant(Offset, getPointerTy()));
6016
Evan Cheng0db9fe62006-04-25 20:13:52 +00006017 return Result;
6018}
6019
Evan Chengda43bcf2008-09-24 00:05:32 +00006020SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006021X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00006022 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00006023 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006024 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00006025}
6026
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006027static SDValue
6028GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00006029 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00006030 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006031 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Owen Anderson825b72b2009-08-11 20:47:22 +00006032 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006033 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006034 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006035 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006036 GA->getOffset(),
6037 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006038 if (InFlag) {
6039 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006040 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006041 } else {
6042 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006043 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006044 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006045
6046 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00006047 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006048
Rafael Espindola15f1b662009-04-24 12:59:40 +00006049 SDValue Flag = Chain.getValue(1);
6050 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006051}
6052
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006053// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006054static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006055LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006056 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00006057 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00006058 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
6059 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006060 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006061 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006062 InFlag = Chain.getValue(1);
6063
Chris Lattnerb903bed2009-06-26 21:20:29 +00006064 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006065}
6066
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006067// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006068static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006069LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006070 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006071 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
6072 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006073}
6074
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006075// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
6076// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00006077static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006078 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00006079 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006080 DebugLoc dl = GA->getDebugLoc();
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006081
6082 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
6083 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
6084 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00006085
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006086 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
6087 DAG.getIntPtrConstant(0),
6088 MachinePointerInfo(Ptr), false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00006089
Chris Lattnerb903bed2009-06-26 21:20:29 +00006090 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006091 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
6092 // initialexec.
6093 unsigned WrapperKind = X86ISD::Wrapper;
6094 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006095 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00006096 } else if (is64Bit) {
6097 assert(model == TLSModel::InitialExec);
6098 OperandFlags = X86II::MO_GOTTPOFF;
6099 WrapperKind = X86ISD::WrapperRIP;
6100 } else {
6101 assert(model == TLSModel::InitialExec);
6102 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00006103 }
Eric Christopherfd179292009-08-27 18:07:15 +00006104
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006105 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
6106 // exec)
Devang Patel0d881da2010-07-06 22:08:15 +00006107 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
6108 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006109 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006110 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006111
Rafael Espindola9a580232009-02-27 13:37:18 +00006112 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006113 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006114 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006115
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006116 // The address of the thread local variable is the add of the thread
6117 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00006118 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006119}
6120
Dan Gohman475871a2008-07-27 21:46:04 +00006121SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006122X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Eric Christopher30ef0e52010-06-03 04:07:48 +00006123
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006124 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00006125 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00006126
Eric Christopher30ef0e52010-06-03 04:07:48 +00006127 if (Subtarget->isTargetELF()) {
6128 // TODO: implement the "local dynamic" model
6129 // TODO: implement the "initial exec"model for pic executables
6130
6131 // If GV is an alias then use the aliasee for determining
6132 // thread-localness.
6133 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
6134 GV = GA->resolveAliasedGlobal(false);
6135
6136 TLSModel::Model model
6137 = getTLSModel(GV, getTargetMachine().getRelocationModel());
6138
6139 switch (model) {
6140 case TLSModel::GeneralDynamic:
6141 case TLSModel::LocalDynamic: // not implemented
6142 if (Subtarget->is64Bit())
6143 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
6144 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
6145
6146 case TLSModel::InitialExec:
6147 case TLSModel::LocalExec:
6148 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
6149 Subtarget->is64Bit());
6150 }
6151 } else if (Subtarget->isTargetDarwin()) {
6152 // Darwin only has one model of TLS. Lower to that.
6153 unsigned char OpFlag = 0;
6154 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
6155 X86ISD::WrapperRIP : X86ISD::Wrapper;
6156
6157 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6158 // global base reg.
6159 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
6160 !Subtarget->is64Bit();
6161 if (PIC32)
6162 OpFlag = X86II::MO_TLVP_PIC_BASE;
6163 else
6164 OpFlag = X86II::MO_TLVP;
Devang Patel0d881da2010-07-06 22:08:15 +00006165 DebugLoc DL = Op.getDebugLoc();
6166 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopher30ef0e52010-06-03 04:07:48 +00006167 getPointerTy(),
6168 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00006169 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
6170
6171 // With PIC32, the address is actually $g + Offset.
6172 if (PIC32)
6173 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6174 DAG.getNode(X86ISD::GlobalBaseReg,
6175 DebugLoc(), getPointerTy()),
6176 Offset);
6177
6178 // Lowering the machine isd will make sure everything is in the right
6179 // location.
6180 SDValue Args[] = { Offset };
6181 SDValue Chain = DAG.getNode(X86ISD::TLSCALL, DL, MVT::Other, Args, 1);
6182
6183 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
6184 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6185 MFI->setAdjustsStack(true);
Eric Christopherfd179292009-08-27 18:07:15 +00006186
Eric Christopher30ef0e52010-06-03 04:07:48 +00006187 // And our return value (tls address) is in the standard call return value
6188 // location.
6189 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
6190 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006191 }
Eric Christopher30ef0e52010-06-03 04:07:48 +00006192
6193 assert(false &&
6194 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00006195
Torok Edwinc23197a2009-07-14 16:55:14 +00006196 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00006197 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006198}
6199
Evan Cheng0db9fe62006-04-25 20:13:52 +00006200
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006201/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00006202/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00006203SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00006204 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00006205 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006206 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006207 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006208 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00006209 SDValue ShOpLo = Op.getOperand(0);
6210 SDValue ShOpHi = Op.getOperand(1);
6211 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00006212 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00006213 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00006214 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00006215
Dan Gohman475871a2008-07-27 21:46:04 +00006216 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006217 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006218 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
6219 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006220 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006221 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
6222 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006223 }
Evan Chenge3413162006-01-09 18:33:28 +00006224
Owen Anderson825b72b2009-08-11 20:47:22 +00006225 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
6226 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00006227 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00006228 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00006229
Dan Gohman475871a2008-07-27 21:46:04 +00006230 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00006231 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00006232 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
6233 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00006234
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006235 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006236 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6237 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006238 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006239 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6240 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006241 }
6242
Dan Gohman475871a2008-07-27 21:46:04 +00006243 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00006244 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006245}
Evan Chenga3195e82006-01-12 22:54:21 +00006246
Dan Gohmand858e902010-04-17 15:26:15 +00006247SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
6248 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006249 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00006250
Dale Johannesen0488fb62010-09-30 23:57:10 +00006251 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006252 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006253
Owen Anderson825b72b2009-08-11 20:47:22 +00006254 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00006255 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00006256
Eli Friedman36df4992009-05-27 00:47:34 +00006257 // These are really Legal; return the operand so the caller accepts it as
6258 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006259 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00006260 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00006261 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00006262 Subtarget->is64Bit()) {
6263 return Op;
6264 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006265
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006266 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006267 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006268 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00006269 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006270 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00006271 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00006272 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006273 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006274 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006275 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
6276}
Evan Cheng0db9fe62006-04-25 20:13:52 +00006277
Owen Andersone50ed302009-08-10 22:56:29 +00006278SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006279 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00006280 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006281 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00006282 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00006283 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00006284 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006285 if (useSSE)
Owen Anderson825b72b2009-08-11 20:47:22 +00006286 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00006287 else
Owen Anderson825b72b2009-08-11 20:47:22 +00006288 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Chris Lattner492a43e2010-09-22 01:28:21 +00006289
6290 unsigned ByteSize = SrcVT.getSizeInBits()/8;
6291
6292 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6293 MachineMemOperand *MMO =
6294 DAG.getMachineFunction()
6295 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6296 MachineMemOperand::MOLoad, ByteSize, ByteSize);
6297
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006298 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006299 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
6300 X86ISD::FILD, DL,
6301 Tys, Ops, array_lengthof(Ops),
6302 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006303
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006304 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006305 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00006306 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006307
6308 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
6309 // shouldn't be necessary except that RFP cannot be live across
6310 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006311 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006312 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
6313 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006314 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00006315 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006316 SDValue Ops[] = {
6317 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
6318 };
Chris Lattner492a43e2010-09-22 01:28:21 +00006319 MachineMemOperand *MMO =
6320 DAG.getMachineFunction()
6321 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006322 MachineMemOperand::MOStore, SSFISize, SSFISize);
Chris Lattner492a43e2010-09-22 01:28:21 +00006323
6324 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
6325 Ops, array_lengthof(Ops),
6326 Op.getValueType(), MMO);
6327 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006328 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006329 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006330 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006331
Evan Cheng0db9fe62006-04-25 20:13:52 +00006332 return Result;
6333}
6334
Bill Wendling8b8a6362009-01-17 03:56:04 +00006335// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006336SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
6337 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00006338 // This algorithm is not obvious. Here it is in C code, more or less:
6339 /*
6340 double uint64_to_double( uint32_t hi, uint32_t lo ) {
6341 static const __m128i exp = { 0x4330000045300000ULL, 0 };
6342 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00006343
Bill Wendling8b8a6362009-01-17 03:56:04 +00006344 // Copy ints to xmm registers.
6345 __m128i xh = _mm_cvtsi32_si128( hi );
6346 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00006347
Bill Wendling8b8a6362009-01-17 03:56:04 +00006348 // Combine into low half of a single xmm register.
6349 __m128i x = _mm_unpacklo_epi32( xh, xl );
6350 __m128d d;
6351 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00006352
Bill Wendling8b8a6362009-01-17 03:56:04 +00006353 // Merge in appropriate exponents to give the integer bits the right
6354 // magnitude.
6355 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00006356
Bill Wendling8b8a6362009-01-17 03:56:04 +00006357 // Subtract away the biases to deal with the IEEE-754 double precision
6358 // implicit 1.
6359 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00006360
Bill Wendling8b8a6362009-01-17 03:56:04 +00006361 // All conversions up to here are exact. The correctly rounded result is
6362 // calculated using the current rounding mode using the following
6363 // horizontal add.
6364 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
6365 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
6366 // store doesn't really need to be here (except
6367 // maybe to zero the other double)
6368 return sd;
6369 }
6370 */
Dale Johannesen040225f2008-10-21 23:07:49 +00006371
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006372 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00006373 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00006374
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006375 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00006376 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00006377 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
6378 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
6379 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
6380 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006381 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006382 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006383
Bill Wendling8b8a6362009-01-17 03:56:04 +00006384 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00006385 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006386 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00006387 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006388 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006389 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006390 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006391
Owen Anderson825b72b2009-08-11 20:47:22 +00006392 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6393 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006394 Op.getOperand(0),
6395 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006396 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6397 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006398 Op.getOperand(0),
6399 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006400 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
6401 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00006402 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006403 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006404 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
6405 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Unpck2);
6406 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00006407 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006408 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006409 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006410
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006411 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00006412 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00006413 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
6414 DAG.getUNDEF(MVT::v2f64), ShufMask);
6415 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
6416 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006417 DAG.getIntPtrConstant(0));
6418}
6419
Bill Wendling8b8a6362009-01-17 03:56:04 +00006420// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006421SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
6422 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006423 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006424 // FP constant to bias correct the final result.
6425 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00006426 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006427
6428 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00006429 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6430 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00006431 Op.getOperand(0),
6432 DAG.getIntPtrConstant(0)));
6433
Owen Anderson825b72b2009-08-11 20:47:22 +00006434 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
6435 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006436 DAG.getIntPtrConstant(0));
6437
6438 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006439 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
6440 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006441 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006442 MVT::v2f64, Load)),
6443 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006444 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006445 MVT::v2f64, Bias)));
6446 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
6447 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006448 DAG.getIntPtrConstant(0));
6449
6450 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006451 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006452
6453 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00006454 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00006455
Owen Anderson825b72b2009-08-11 20:47:22 +00006456 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006457 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00006458 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00006459 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006460 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00006461 }
6462
6463 // Handle final rounding.
6464 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00006465}
6466
Dan Gohmand858e902010-04-17 15:26:15 +00006467SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
6468 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00006469 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006470 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006471
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006472 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00006473 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
6474 // the optimization here.
6475 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00006476 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00006477
Owen Andersone50ed302009-08-10 22:56:29 +00006478 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006479 EVT DstVT = Op.getValueType();
6480 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006481 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006482 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006483 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00006484
6485 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00006486 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006487 if (SrcVT == MVT::i32) {
6488 SDValue WordOff = DAG.getConstant(4, getPointerTy());
6489 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
6490 getPointerTy(), StackSlot, WordOff);
6491 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006492 StackSlot, MachinePointerInfo(),
6493 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006494 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006495 OffsetSlot, MachinePointerInfo(),
6496 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006497 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
6498 return Fild;
6499 }
6500
6501 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
6502 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006503 StackSlot, MachinePointerInfo(),
6504 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006505 // For i64 source, we need to add the appropriate power of 2 if the input
6506 // was negative. This is the same as the optimization in
6507 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
6508 // we must be careful to do the computation in x87 extended precision, not
6509 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00006510 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6511 MachineMemOperand *MMO =
6512 DAG.getMachineFunction()
6513 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6514 MachineMemOperand::MOLoad, 8, 8);
6515
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006516 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
6517 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006518 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
6519 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006520
6521 APInt FF(32, 0x5F800000ULL);
6522
6523 // Check whether the sign bit is set.
6524 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
6525 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
6526 ISD::SETLT);
6527
6528 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
6529 SDValue FudgePtr = DAG.getConstantPool(
6530 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
6531 getPointerTy());
6532
6533 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
6534 SDValue Zero = DAG.getIntPtrConstant(0);
6535 SDValue Four = DAG.getIntPtrConstant(4);
6536 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
6537 Zero, Four);
6538 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
6539
6540 // Load the value out, extending it from f32 to f80.
6541 // FIXME: Avoid the extend by constructing the right constant pool?
Evan Chengbcc80172010-07-07 22:15:37 +00006542 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, MVT::f80, dl, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00006543 FudgePtr, MachinePointerInfo::getConstantPool(),
6544 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006545 // Extend everything to 80 bits to force it to be done on x87.
6546 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
6547 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00006548}
6549
Dan Gohman475871a2008-07-27 21:46:04 +00006550std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00006551FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00006552 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00006553
Owen Andersone50ed302009-08-10 22:56:29 +00006554 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00006555
6556 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006557 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
6558 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00006559 }
6560
Owen Anderson825b72b2009-08-11 20:47:22 +00006561 assert(DstTy.getSimpleVT() <= MVT::i64 &&
6562 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00006563 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00006564
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006565 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006566 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00006567 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00006568 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00006569 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00006570 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00006571 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00006572 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006573
Evan Cheng87c89352007-10-15 20:11:21 +00006574 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
6575 // stack slot.
6576 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00006577 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00006578 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006579 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00006580
Chris Lattner07290932010-09-22 01:05:16 +00006581
6582
Evan Cheng0db9fe62006-04-25 20:13:52 +00006583 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00006584 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006585 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00006586 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
6587 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
6588 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006589 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006590
Dan Gohman475871a2008-07-27 21:46:04 +00006591 SDValue Chain = DAG.getEntryNode();
6592 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00006593 EVT TheVT = Op.getOperand(0).getValueType();
6594 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006595 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00006596 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006597 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006598 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00006599 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00006600 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00006601 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00006602 };
Chris Lattner492a43e2010-09-22 01:28:21 +00006603
6604 MachineMemOperand *MMO =
6605 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6606 MachineMemOperand::MOLoad, MemSize, MemSize);
6607 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
6608 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006609 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00006610 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006611 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
6612 }
Chris Lattner07290932010-09-22 01:05:16 +00006613
6614 MachineMemOperand *MMO =
6615 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6616 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006617
Evan Cheng0db9fe62006-04-25 20:13:52 +00006618 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00006619 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00006620 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
6621 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00006622
Chris Lattner27a6c732007-11-24 07:07:01 +00006623 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006624}
6625
Dan Gohmand858e902010-04-17 15:26:15 +00006626SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
6627 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00006628 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006629 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006630
Eli Friedman948e95a2009-05-23 09:59:16 +00006631 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00006632 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00006633 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
6634 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00006635
Chris Lattner27a6c732007-11-24 07:07:01 +00006636 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006637 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00006638 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00006639}
6640
Dan Gohmand858e902010-04-17 15:26:15 +00006641SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
6642 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00006643 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
6644 SDValue FIST = Vals.first, StackSlot = Vals.second;
6645 assert(FIST.getNode() && "Unexpected failure");
6646
6647 // Load the result.
6648 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00006649 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006650}
6651
Dan Gohmand858e902010-04-17 15:26:15 +00006652SDValue X86TargetLowering::LowerFABS(SDValue Op,
6653 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00006654 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006655 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006656 EVT VT = Op.getValueType();
6657 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006658 if (VT.isVector())
6659 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006660 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00006661 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006662 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00006663 CV.push_back(C);
6664 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006665 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006666 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00006667 CV.push_back(C);
6668 CV.push_back(C);
6669 CV.push_back(C);
6670 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006671 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006672 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006673 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006674 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006675 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006676 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006677 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006678}
6679
Dan Gohmand858e902010-04-17 15:26:15 +00006680SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00006681 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006682 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006683 EVT VT = Op.getValueType();
6684 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00006685 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00006686 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006687 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00006688 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006689 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00006690 CV.push_back(C);
6691 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006692 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006693 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00006694 CV.push_back(C);
6695 CV.push_back(C);
6696 CV.push_back(C);
6697 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006698 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006699 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006700 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006701 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006702 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006703 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006704 if (VT.isVector()) {
Dale Johannesenace16102009-02-03 19:33:06 +00006705 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00006706 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
6707 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006708 Op.getOperand(0)),
Owen Anderson825b72b2009-08-11 20:47:22 +00006709 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00006710 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006711 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00006712 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006713}
6714
Dan Gohmand858e902010-04-17 15:26:15 +00006715SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00006716 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00006717 SDValue Op0 = Op.getOperand(0);
6718 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006719 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006720 EVT VT = Op.getValueType();
6721 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00006722
6723 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00006724 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006725 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00006726 SrcVT = VT;
6727 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00006728 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00006729 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006730 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00006731 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00006732 }
6733
6734 // At this point the operands and the result should have the same
6735 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00006736
Evan Cheng68c47cb2007-01-05 07:55:56 +00006737 // First get the sign bit of second operand.
6738 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00006739 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006740 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
6741 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00006742 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006743 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
6744 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6745 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6746 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00006747 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006748 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006749 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006750 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006751 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006752 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006753 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00006754
6755 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00006756 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006757 // Op0 is MVT::f32, Op1 is MVT::f64.
6758 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
6759 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
6760 DAG.getConstant(32, MVT::i32));
6761 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, SignBit);
6762 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00006763 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00006764 }
6765
Evan Cheng73d6cf12007-01-05 21:37:56 +00006766 // Clear first operand sign bit.
6767 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00006768 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006769 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
6770 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00006771 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006772 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
6773 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6774 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6775 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00006776 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006777 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006778 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006779 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006780 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006781 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006782 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00006783
6784 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00006785 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00006786}
6787
Dan Gohman076aee32009-03-04 19:44:21 +00006788/// Emit nodes that will be selected as "test Op0,Op0", or something
6789/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00006790SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00006791 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00006792 DebugLoc dl = Op.getDebugLoc();
6793
Dan Gohman31125812009-03-07 01:58:32 +00006794 // CF and OF aren't always set the way we want. Determine which
6795 // of these we need.
6796 bool NeedCF = false;
6797 bool NeedOF = false;
6798 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006799 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00006800 case X86::COND_A: case X86::COND_AE:
6801 case X86::COND_B: case X86::COND_BE:
6802 NeedCF = true;
6803 break;
6804 case X86::COND_G: case X86::COND_GE:
6805 case X86::COND_L: case X86::COND_LE:
6806 case X86::COND_O: case X86::COND_NO:
6807 NeedOF = true;
6808 break;
Dan Gohman31125812009-03-07 01:58:32 +00006809 }
6810
Dan Gohman076aee32009-03-04 19:44:21 +00006811 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00006812 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
6813 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006814 if (Op.getResNo() != 0 || NeedOF || NeedCF)
6815 // Emit a CMP with 0, which is the TEST pattern.
6816 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
6817 DAG.getConstant(0, Op.getValueType()));
6818
6819 unsigned Opcode = 0;
6820 unsigned NumOperands = 0;
6821 switch (Op.getNode()->getOpcode()) {
6822 case ISD::ADD:
6823 // Due to an isel shortcoming, be conservative if this add is likely to be
6824 // selected as part of a load-modify-store instruction. When the root node
6825 // in a match is a store, isel doesn't know how to remap non-chain non-flag
6826 // uses of other nodes in the match, such as the ADD in this case. This
6827 // leads to the ADD being left around and reselected, with the result being
6828 // two adds in the output. Alas, even if none our users are stores, that
6829 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
6830 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
6831 // climbing the DAG back to the root, and it doesn't seem to be worth the
6832 // effort.
6833 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00006834 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006835 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
6836 goto default_case;
6837
6838 if (ConstantSDNode *C =
6839 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
6840 // An add of one will be selected as an INC.
6841 if (C->getAPIntValue() == 1) {
6842 Opcode = X86ISD::INC;
6843 NumOperands = 1;
6844 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00006845 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006846
6847 // An add of negative one (subtract of one) will be selected as a DEC.
6848 if (C->getAPIntValue().isAllOnesValue()) {
6849 Opcode = X86ISD::DEC;
6850 NumOperands = 1;
6851 break;
6852 }
Dan Gohman076aee32009-03-04 19:44:21 +00006853 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006854
6855 // Otherwise use a regular EFLAGS-setting add.
6856 Opcode = X86ISD::ADD;
6857 NumOperands = 2;
6858 break;
6859 case ISD::AND: {
6860 // If the primary and result isn't used, don't bother using X86ISD::AND,
6861 // because a TEST instruction will be better.
6862 bool NonFlagUse = false;
6863 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
6864 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
6865 SDNode *User = *UI;
6866 unsigned UOpNo = UI.getOperandNo();
6867 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
6868 // Look pass truncate.
6869 UOpNo = User->use_begin().getOperandNo();
6870 User = *User->use_begin();
6871 }
6872
6873 if (User->getOpcode() != ISD::BRCOND &&
6874 User->getOpcode() != ISD::SETCC &&
6875 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
6876 NonFlagUse = true;
6877 break;
6878 }
Dan Gohman076aee32009-03-04 19:44:21 +00006879 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006880
6881 if (!NonFlagUse)
6882 break;
6883 }
6884 // FALL THROUGH
6885 case ISD::SUB:
6886 case ISD::OR:
6887 case ISD::XOR:
6888 // Due to the ISEL shortcoming noted above, be conservative if this op is
6889 // likely to be selected as part of a load-modify-store instruction.
6890 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
6891 UE = Op.getNode()->use_end(); UI != UE; ++UI)
6892 if (UI->getOpcode() == ISD::STORE)
6893 goto default_case;
6894
6895 // Otherwise use a regular EFLAGS-setting instruction.
6896 switch (Op.getNode()->getOpcode()) {
6897 default: llvm_unreachable("unexpected operator!");
6898 case ISD::SUB: Opcode = X86ISD::SUB; break;
6899 case ISD::OR: Opcode = X86ISD::OR; break;
6900 case ISD::XOR: Opcode = X86ISD::XOR; break;
6901 case ISD::AND: Opcode = X86ISD::AND; break;
6902 }
6903
6904 NumOperands = 2;
6905 break;
6906 case X86ISD::ADD:
6907 case X86ISD::SUB:
6908 case X86ISD::INC:
6909 case X86ISD::DEC:
6910 case X86ISD::OR:
6911 case X86ISD::XOR:
6912 case X86ISD::AND:
6913 return SDValue(Op.getNode(), 1);
6914 default:
6915 default_case:
6916 break;
Dan Gohman076aee32009-03-04 19:44:21 +00006917 }
6918
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006919 if (Opcode == 0)
6920 // Emit a CMP with 0, which is the TEST pattern.
6921 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
6922 DAG.getConstant(0, Op.getValueType()));
6923
6924 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
6925 SmallVector<SDValue, 4> Ops;
6926 for (unsigned i = 0; i != NumOperands; ++i)
6927 Ops.push_back(Op.getOperand(i));
6928
6929 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
6930 DAG.ReplaceAllUsesWith(Op, New);
6931 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00006932}
6933
6934/// Emit nodes that will be selected as "cmp Op0,Op1", or something
6935/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00006936SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00006937 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00006938 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
6939 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00006940 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00006941
6942 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00006943 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00006944}
6945
Evan Chengd40d03e2010-01-06 19:38:29 +00006946/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
6947/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00006948SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
6949 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00006950 SDValue Op0 = And.getOperand(0);
6951 SDValue Op1 = And.getOperand(1);
6952 if (Op0.getOpcode() == ISD::TRUNCATE)
6953 Op0 = Op0.getOperand(0);
6954 if (Op1.getOpcode() == ISD::TRUNCATE)
6955 Op1 = Op1.getOperand(0);
6956
Evan Chengd40d03e2010-01-06 19:38:29 +00006957 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00006958 if (Op1.getOpcode() == ISD::SHL)
6959 std::swap(Op0, Op1);
6960 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00006961 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
6962 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00006963 // If we looked past a truncate, check that it's only truncating away
6964 // known zeros.
6965 unsigned BitWidth = Op0.getValueSizeInBits();
6966 unsigned AndBitWidth = And.getValueSizeInBits();
6967 if (BitWidth > AndBitWidth) {
6968 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
6969 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
6970 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
6971 return SDValue();
6972 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00006973 LHS = Op1;
6974 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00006975 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00006976 } else if (Op1.getOpcode() == ISD::Constant) {
6977 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
6978 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00006979 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
6980 LHS = AndLHS.getOperand(0);
6981 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00006982 }
Evan Chengd40d03e2010-01-06 19:38:29 +00006983 }
Evan Cheng0488db92007-09-25 01:57:46 +00006984
Evan Chengd40d03e2010-01-06 19:38:29 +00006985 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00006986 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00006987 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00006988 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00006989 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00006990 // Also promote i16 to i32 for performance / code size reason.
6991 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00006992 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00006993 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00006994
Evan Chengd40d03e2010-01-06 19:38:29 +00006995 // If the operand types disagree, extend the shift amount to match. Since
6996 // BT ignores high bits (like shifts) we can use anyextend.
6997 if (LHS.getValueType() != RHS.getValueType())
6998 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00006999
Evan Chengd40d03e2010-01-06 19:38:29 +00007000 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
7001 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
7002 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7003 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00007004 }
7005
Evan Cheng54de3ea2010-01-05 06:52:31 +00007006 return SDValue();
7007}
7008
Dan Gohmand858e902010-04-17 15:26:15 +00007009SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng54de3ea2010-01-05 06:52:31 +00007010 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
7011 SDValue Op0 = Op.getOperand(0);
7012 SDValue Op1 = Op.getOperand(1);
7013 DebugLoc dl = Op.getDebugLoc();
7014 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
7015
7016 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00007017 // Lower (X & (1 << N)) == 0 to BT(X, N).
7018 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
7019 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
7020 if (Op0.getOpcode() == ISD::AND &&
7021 Op0.hasOneUse() &&
7022 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00007023 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007024 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
7025 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
7026 if (NewSetCC.getNode())
7027 return NewSetCC;
7028 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00007029
Evan Cheng2c755ba2010-02-27 07:36:59 +00007030 // Look for "(setcc) == / != 1" to avoid unncessary setcc.
7031 if (Op0.getOpcode() == X86ISD::SETCC &&
7032 Op1.getOpcode() == ISD::Constant &&
7033 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
7034 cast<ConstantSDNode>(Op1)->isNullValue()) &&
7035 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
7036 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
7037 bool Invert = (CC == ISD::SETNE) ^
7038 cast<ConstantSDNode>(Op1)->isNullValue();
7039 if (Invert)
7040 CCode = X86::GetOppositeBranchCondition(CCode);
7041 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7042 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
7043 }
7044
Evan Chenge5b51ac2010-04-17 06:13:15 +00007045 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00007046 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007047 if (X86CC == X86::COND_INVALID)
7048 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007049
Evan Cheng552f09a2010-04-26 19:06:11 +00007050 SDValue Cond = EmitCmp(Op0, Op1, X86CC, DAG);
Evan Chengad9c0a32009-12-15 00:53:42 +00007051
7052 // Use sbb x, x to materialize carry bit into a GPR.
Evan Cheng2e489c42009-12-16 00:53:11 +00007053 if (X86CC == X86::COND_B)
Evan Chengad9c0a32009-12-15 00:53:42 +00007054 return DAG.getNode(ISD::AND, dl, MVT::i8,
7055 DAG.getNode(X86ISD::SETCC_CARRY, dl, MVT::i8,
7056 DAG.getConstant(X86CC, MVT::i8), Cond),
7057 DAG.getConstant(1, MVT::i8));
Evan Chengad9c0a32009-12-15 00:53:42 +00007058
Owen Anderson825b72b2009-08-11 20:47:22 +00007059 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7060 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00007061}
7062
Dan Gohmand858e902010-04-17 15:26:15 +00007063SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007064 SDValue Cond;
7065 SDValue Op0 = Op.getOperand(0);
7066 SDValue Op1 = Op.getOperand(1);
7067 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00007068 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00007069 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
7070 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007071 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00007072
7073 if (isFP) {
7074 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00007075 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007076 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
7077 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00007078 bool Swap = false;
7079
7080 switch (SetCCOpcode) {
7081 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007082 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00007083 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00007084 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00007085 case ISD::SETGT: Swap = true; // Fallthrough
7086 case ISD::SETLT:
7087 case ISD::SETOLT: SSECC = 1; break;
7088 case ISD::SETOGE:
7089 case ISD::SETGE: Swap = true; // Fallthrough
7090 case ISD::SETLE:
7091 case ISD::SETOLE: SSECC = 2; break;
7092 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007093 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00007094 case ISD::SETNE: SSECC = 4; break;
7095 case ISD::SETULE: Swap = true;
7096 case ISD::SETUGE: SSECC = 5; break;
7097 case ISD::SETULT: Swap = true;
7098 case ISD::SETUGT: SSECC = 6; break;
7099 case ISD::SETO: SSECC = 7; break;
7100 }
7101 if (Swap)
7102 std::swap(Op0, Op1);
7103
Nate Begemanfb8ead02008-07-25 19:05:58 +00007104 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00007105 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00007106 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00007107 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007108 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
7109 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007110 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007111 }
7112 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00007113 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007114 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
7115 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007116 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007117 }
Torok Edwinc23197a2009-07-14 16:55:14 +00007118 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00007119 }
7120 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00007121 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00007122 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007123
Nate Begeman30a0de92008-07-17 16:51:19 +00007124 // We are handling one of the integer comparisons here. Since SSE only has
7125 // GT and EQ comparisons for integer, swapping operands and multiple
7126 // operations may be required for some comparisons.
7127 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
7128 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007129
Owen Anderson825b72b2009-08-11 20:47:22 +00007130 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00007131 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007132 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007133 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007134 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
7135 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00007136 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007137
Nate Begeman30a0de92008-07-17 16:51:19 +00007138 switch (SetCCOpcode) {
7139 default: break;
7140 case ISD::SETNE: Invert = true;
7141 case ISD::SETEQ: Opc = EQOpc; break;
7142 case ISD::SETLT: Swap = true;
7143 case ISD::SETGT: Opc = GTOpc; break;
7144 case ISD::SETGE: Swap = true;
7145 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
7146 case ISD::SETULT: Swap = true;
7147 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
7148 case ISD::SETUGE: Swap = true;
7149 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
7150 }
7151 if (Swap)
7152 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007153
Nate Begeman30a0de92008-07-17 16:51:19 +00007154 // Since SSE has no unsigned integer comparisons, we need to flip the sign
7155 // bits of the inputs before performing those operations.
7156 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00007157 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00007158 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
7159 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00007160 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00007161 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
7162 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00007163 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
7164 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00007165 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007166
Dale Johannesenace16102009-02-03 19:33:06 +00007167 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00007168
7169 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00007170 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00007171 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00007172
Nate Begeman30a0de92008-07-17 16:51:19 +00007173 return Result;
7174}
Evan Cheng0488db92007-09-25 01:57:46 +00007175
Evan Cheng370e5342008-12-03 08:38:43 +00007176// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00007177static bool isX86LogicalCmp(SDValue Op) {
7178 unsigned Opc = Op.getNode()->getOpcode();
7179 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
7180 return true;
7181 if (Op.getResNo() == 1 &&
7182 (Opc == X86ISD::ADD ||
7183 Opc == X86ISD::SUB ||
7184 Opc == X86ISD::SMUL ||
7185 Opc == X86ISD::UMUL ||
7186 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00007187 Opc == X86ISD::DEC ||
7188 Opc == X86ISD::OR ||
7189 Opc == X86ISD::XOR ||
7190 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00007191 return true;
7192
7193 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00007194}
7195
Dan Gohmand858e902010-04-17 15:26:15 +00007196SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007197 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007198 SDValue Cond = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007199 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007200 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00007201
Dan Gohman1a492952009-10-20 16:22:37 +00007202 if (Cond.getOpcode() == ISD::SETCC) {
7203 SDValue NewCond = LowerSETCC(Cond, DAG);
7204 if (NewCond.getNode())
7205 Cond = NewCond;
7206 }
Evan Cheng734503b2006-09-11 02:19:56 +00007207
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007208 // (select (x == 0), -1, 0) -> (sign_bit (x - 1))
7209 SDValue Op1 = Op.getOperand(1);
7210 SDValue Op2 = Op.getOperand(2);
7211 if (Cond.getOpcode() == X86ISD::SETCC &&
7212 cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue() == X86::COND_E) {
7213 SDValue Cmp = Cond.getOperand(1);
7214 if (Cmp.getOpcode() == X86ISD::CMP) {
7215 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op1);
7216 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
7217 ConstantSDNode *RHSC =
7218 dyn_cast<ConstantSDNode>(Cmp.getOperand(1).getNode());
7219 if (N1C && N1C->isAllOnesValue() &&
7220 N2C && N2C->isNullValue() &&
7221 RHSC && RHSC->isNullValue()) {
7222 SDValue CmpOp0 = Cmp.getOperand(0);
Chris Lattnerda0688e2010-03-14 18:44:35 +00007223 Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007224 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
7225 return DAG.getNode(X86ISD::SETCC_CARRY, dl, Op.getValueType(),
7226 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
7227 }
7228 }
7229 }
7230
Evan Chengad9c0a32009-12-15 00:53:42 +00007231 // Look pass (and (setcc_carry (cmp ...)), 1).
7232 if (Cond.getOpcode() == ISD::AND &&
7233 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7234 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
7235 if (C && C->getAPIntValue() == 1)
7236 Cond = Cond.getOperand(0);
7237 }
7238
Evan Cheng3f41d662007-10-08 22:16:29 +00007239 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7240 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007241 if (Cond.getOpcode() == X86ISD::SETCC ||
7242 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007243 CC = Cond.getOperand(0);
7244
Dan Gohman475871a2008-07-27 21:46:04 +00007245 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007246 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00007247 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00007248
Evan Cheng3f41d662007-10-08 22:16:29 +00007249 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007250 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00007251 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00007252 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00007253
Chris Lattnerd1980a52009-03-12 06:52:53 +00007254 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
7255 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00007256 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007257 addTest = false;
7258 }
7259 }
7260
7261 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007262 // Look pass the truncate.
7263 if (Cond.getOpcode() == ISD::TRUNCATE)
7264 Cond = Cond.getOperand(0);
7265
7266 // We know the result of AND is compared against zero. Try to match
7267 // it to BT.
7268 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
7269 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
7270 if (NewSetCC.getNode()) {
7271 CC = NewSetCC.getOperand(0);
7272 Cond = NewSetCC.getOperand(1);
7273 addTest = false;
7274 }
7275 }
7276 }
7277
7278 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007279 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007280 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007281 }
7282
Evan Cheng0488db92007-09-25 01:57:46 +00007283 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
7284 // condition is true.
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007285 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
7286 SDValue Ops[] = { Op2, Op1, CC, Cond };
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007287 return DAG.getNode(X86ISD::CMOV, dl, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00007288}
7289
Evan Cheng370e5342008-12-03 08:38:43 +00007290// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
7291// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
7292// from the AND / OR.
7293static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
7294 Opc = Op.getOpcode();
7295 if (Opc != ISD::OR && Opc != ISD::AND)
7296 return false;
7297 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7298 Op.getOperand(0).hasOneUse() &&
7299 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
7300 Op.getOperand(1).hasOneUse());
7301}
7302
Evan Cheng961d6d42009-02-02 08:19:07 +00007303// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
7304// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00007305static bool isXor1OfSetCC(SDValue Op) {
7306 if (Op.getOpcode() != ISD::XOR)
7307 return false;
7308 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7309 if (N1C && N1C->getAPIntValue() == 1) {
7310 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7311 Op.getOperand(0).hasOneUse();
7312 }
7313 return false;
7314}
7315
Dan Gohmand858e902010-04-17 15:26:15 +00007316SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007317 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007318 SDValue Chain = Op.getOperand(0);
7319 SDValue Cond = Op.getOperand(1);
7320 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007321 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007322 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00007323
Dan Gohman1a492952009-10-20 16:22:37 +00007324 if (Cond.getOpcode() == ISD::SETCC) {
7325 SDValue NewCond = LowerSETCC(Cond, DAG);
7326 if (NewCond.getNode())
7327 Cond = NewCond;
7328 }
Chris Lattnere55484e2008-12-25 05:34:37 +00007329#if 0
7330 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00007331 else if (Cond.getOpcode() == X86ISD::ADD ||
7332 Cond.getOpcode() == X86ISD::SUB ||
7333 Cond.getOpcode() == X86ISD::SMUL ||
7334 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00007335 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00007336#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00007337
Evan Chengad9c0a32009-12-15 00:53:42 +00007338 // Look pass (and (setcc_carry (cmp ...)), 1).
7339 if (Cond.getOpcode() == ISD::AND &&
7340 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7341 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
7342 if (C && C->getAPIntValue() == 1)
7343 Cond = Cond.getOperand(0);
7344 }
7345
Evan Cheng3f41d662007-10-08 22:16:29 +00007346 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7347 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007348 if (Cond.getOpcode() == X86ISD::SETCC ||
7349 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007350 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007351
Dan Gohman475871a2008-07-27 21:46:04 +00007352 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007353 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00007354 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00007355 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00007356 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007357 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00007358 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00007359 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007360 default: break;
7361 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00007362 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00007363 // These can only come from an arithmetic instruction with overflow,
7364 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007365 Cond = Cond.getNode()->getOperand(1);
7366 addTest = false;
7367 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007368 }
Evan Cheng0488db92007-09-25 01:57:46 +00007369 }
Evan Cheng370e5342008-12-03 08:38:43 +00007370 } else {
7371 unsigned CondOpc;
7372 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
7373 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00007374 if (CondOpc == ISD::OR) {
7375 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
7376 // two branches instead of an explicit OR instruction with a
7377 // separate test.
7378 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007379 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00007380 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007381 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007382 Chain, Dest, CC, Cmp);
7383 CC = Cond.getOperand(1).getOperand(0);
7384 Cond = Cmp;
7385 addTest = false;
7386 }
7387 } else { // ISD::AND
7388 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
7389 // two branches instead of an explicit AND instruction with a
7390 // separate test. However, we only do this if this block doesn't
7391 // have a fall-through edge, because this requires an explicit
7392 // jmp when the condition is false.
7393 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007394 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00007395 Op.getNode()->hasOneUse()) {
7396 X86::CondCode CCode =
7397 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7398 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007399 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00007400 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00007401 // Look for an unconditional branch following this conditional branch.
7402 // We need this because we need to reverse the successors in order
7403 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00007404 if (User->getOpcode() == ISD::BR) {
7405 SDValue FalseBB = User->getOperand(1);
7406 SDNode *NewBR =
7407 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00007408 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00007409 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00007410 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00007411
Dale Johannesene4d209d2009-02-03 20:21:25 +00007412 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007413 Chain, Dest, CC, Cmp);
7414 X86::CondCode CCode =
7415 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
7416 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007417 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00007418 Cond = Cmp;
7419 addTest = false;
7420 }
7421 }
Dan Gohman279c22e2008-10-21 03:29:32 +00007422 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00007423 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
7424 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
7425 // It should be transformed during dag combiner except when the condition
7426 // is set by a arithmetics with overflow node.
7427 X86::CondCode CCode =
7428 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7429 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007430 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00007431 Cond = Cond.getOperand(0).getOperand(1);
7432 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00007433 }
Evan Cheng0488db92007-09-25 01:57:46 +00007434 }
7435
7436 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007437 // Look pass the truncate.
7438 if (Cond.getOpcode() == ISD::TRUNCATE)
7439 Cond = Cond.getOperand(0);
7440
7441 // We know the result of AND is compared against zero. Try to match
7442 // it to BT.
7443 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
7444 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
7445 if (NewSetCC.getNode()) {
7446 CC = NewSetCC.getOperand(0);
7447 Cond = NewSetCC.getOperand(1);
7448 addTest = false;
7449 }
7450 }
7451 }
7452
7453 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007454 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007455 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007456 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00007457 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00007458 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00007459}
7460
Anton Korobeynikove060b532007-04-17 19:34:00 +00007461
7462// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
7463// Calls to _alloca is needed to probe the stack when allocating more than 4k
7464// bytes in one go. Touching the stack at 4K increments is necessary to ensure
7465// that the guard pages used by the OS virtual memory manager are allocated in
7466// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00007467SDValue
7468X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007469 SelectionDAG &DAG) const {
Anton Korobeynikove060b532007-04-17 19:34:00 +00007470 assert(Subtarget->isTargetCygMing() &&
7471 "This should be used only on Cygwin/Mingw targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007472 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007473
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007474 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00007475 SDValue Chain = Op.getOperand(0);
7476 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007477 // FIXME: Ensure alignment here
7478
Dan Gohman475871a2008-07-27 21:46:04 +00007479 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007480
Owen Anderson825b72b2009-08-11 20:47:22 +00007481 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007482
Dale Johannesendd64c412009-02-04 00:33:20 +00007483 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007484 Flag = Chain.getValue(1);
7485
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00007486 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007487
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00007488 Chain = DAG.getNode(X86ISD::MINGW_ALLOCA, dl, NodeTys, Chain, Flag);
7489 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007490
Dale Johannesendd64c412009-02-04 00:33:20 +00007491 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007492
Dan Gohman475871a2008-07-27 21:46:04 +00007493 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007494 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007495}
7496
Dan Gohmand858e902010-04-17 15:26:15 +00007497SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00007498 MachineFunction &MF = DAG.getMachineFunction();
7499 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
7500
Dan Gohman69de1932008-02-06 22:27:42 +00007501 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00007502 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00007503
Anton Korobeynikove7beda12010-10-03 22:52:07 +00007504 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00007505 // vastart just stores the address of the VarArgsFrameIndex slot into the
7506 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00007507 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
7508 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007509 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
7510 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007511 }
7512
7513 // __va_list_tag:
7514 // gp_offset (0 - 6 * 8)
7515 // fp_offset (48 - 48 + 8 * 16)
7516 // overflow_arg_area (point to parameters coming in memory).
7517 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00007518 SmallVector<SDValue, 8> MemOps;
7519 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00007520 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00007521 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00007522 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
7523 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007524 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007525 MemOps.push_back(Store);
7526
7527 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00007528 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007529 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00007530 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00007531 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
7532 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007533 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007534 MemOps.push_back(Store);
7535
7536 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00007537 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007538 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00007539 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
7540 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007541 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
7542 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00007543 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007544 MemOps.push_back(Store);
7545
7546 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00007547 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007548 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00007549 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
7550 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007551 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
7552 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007553 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00007554 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00007555 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00007556}
7557
Dan Gohmand858e902010-04-17 15:26:15 +00007558SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman9018e832008-05-10 01:26:14 +00007559 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
7560 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman9018e832008-05-10 01:26:14 +00007561
Chris Lattner75361b62010-04-07 22:58:41 +00007562 report_fatal_error("VAArgInst is not yet implemented for x86-64!");
Dan Gohman475871a2008-07-27 21:46:04 +00007563 return SDValue();
Dan Gohman9018e832008-05-10 01:26:14 +00007564}
7565
Dan Gohmand858e902010-04-17 15:26:15 +00007566SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00007567 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00007568 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00007569 SDValue Chain = Op.getOperand(0);
7570 SDValue DstPtr = Op.getOperand(1);
7571 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00007572 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
7573 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00007574 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00007575
Chris Lattnere72f2022010-09-21 05:40:29 +00007576 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00007577 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Chris Lattnere72f2022010-09-21 05:40:29 +00007578 false,
7579 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00007580}
7581
Dan Gohman475871a2008-07-27 21:46:04 +00007582SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007583X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007584 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007585 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007586 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00007587 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00007588 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00007589 case Intrinsic::x86_sse_comieq_ss:
7590 case Intrinsic::x86_sse_comilt_ss:
7591 case Intrinsic::x86_sse_comile_ss:
7592 case Intrinsic::x86_sse_comigt_ss:
7593 case Intrinsic::x86_sse_comige_ss:
7594 case Intrinsic::x86_sse_comineq_ss:
7595 case Intrinsic::x86_sse_ucomieq_ss:
7596 case Intrinsic::x86_sse_ucomilt_ss:
7597 case Intrinsic::x86_sse_ucomile_ss:
7598 case Intrinsic::x86_sse_ucomigt_ss:
7599 case Intrinsic::x86_sse_ucomige_ss:
7600 case Intrinsic::x86_sse_ucomineq_ss:
7601 case Intrinsic::x86_sse2_comieq_sd:
7602 case Intrinsic::x86_sse2_comilt_sd:
7603 case Intrinsic::x86_sse2_comile_sd:
7604 case Intrinsic::x86_sse2_comigt_sd:
7605 case Intrinsic::x86_sse2_comige_sd:
7606 case Intrinsic::x86_sse2_comineq_sd:
7607 case Intrinsic::x86_sse2_ucomieq_sd:
7608 case Intrinsic::x86_sse2_ucomilt_sd:
7609 case Intrinsic::x86_sse2_ucomile_sd:
7610 case Intrinsic::x86_sse2_ucomigt_sd:
7611 case Intrinsic::x86_sse2_ucomige_sd:
7612 case Intrinsic::x86_sse2_ucomineq_sd: {
7613 unsigned Opc = 0;
7614 ISD::CondCode CC = ISD::SETCC_INVALID;
7615 switch (IntNo) {
7616 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007617 case Intrinsic::x86_sse_comieq_ss:
7618 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007619 Opc = X86ISD::COMI;
7620 CC = ISD::SETEQ;
7621 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00007622 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007623 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007624 Opc = X86ISD::COMI;
7625 CC = ISD::SETLT;
7626 break;
7627 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007628 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007629 Opc = X86ISD::COMI;
7630 CC = ISD::SETLE;
7631 break;
7632 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007633 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007634 Opc = X86ISD::COMI;
7635 CC = ISD::SETGT;
7636 break;
7637 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007638 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007639 Opc = X86ISD::COMI;
7640 CC = ISD::SETGE;
7641 break;
7642 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007643 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007644 Opc = X86ISD::COMI;
7645 CC = ISD::SETNE;
7646 break;
7647 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007648 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007649 Opc = X86ISD::UCOMI;
7650 CC = ISD::SETEQ;
7651 break;
7652 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007653 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007654 Opc = X86ISD::UCOMI;
7655 CC = ISD::SETLT;
7656 break;
7657 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007658 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007659 Opc = X86ISD::UCOMI;
7660 CC = ISD::SETLE;
7661 break;
7662 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007663 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007664 Opc = X86ISD::UCOMI;
7665 CC = ISD::SETGT;
7666 break;
7667 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007668 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007669 Opc = X86ISD::UCOMI;
7670 CC = ISD::SETGE;
7671 break;
7672 case Intrinsic::x86_sse_ucomineq_ss:
7673 case Intrinsic::x86_sse2_ucomineq_sd:
7674 Opc = X86ISD::UCOMI;
7675 CC = ISD::SETNE;
7676 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00007677 }
Evan Cheng734503b2006-09-11 02:19:56 +00007678
Dan Gohman475871a2008-07-27 21:46:04 +00007679 SDValue LHS = Op.getOperand(1);
7680 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00007681 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007682 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007683 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
7684 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7685 DAG.getConstant(X86CC, MVT::i8), Cond);
7686 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00007687 }
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007688 // ptest and testp intrinsics. The intrinsic these come from are designed to
7689 // return an integer value, not just an instruction so lower it to the ptest
7690 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00007691 case Intrinsic::x86_sse41_ptestz:
7692 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007693 case Intrinsic::x86_sse41_ptestnzc:
7694 case Intrinsic::x86_avx_ptestz_256:
7695 case Intrinsic::x86_avx_ptestc_256:
7696 case Intrinsic::x86_avx_ptestnzc_256:
7697 case Intrinsic::x86_avx_vtestz_ps:
7698 case Intrinsic::x86_avx_vtestc_ps:
7699 case Intrinsic::x86_avx_vtestnzc_ps:
7700 case Intrinsic::x86_avx_vtestz_pd:
7701 case Intrinsic::x86_avx_vtestc_pd:
7702 case Intrinsic::x86_avx_vtestnzc_pd:
7703 case Intrinsic::x86_avx_vtestz_ps_256:
7704 case Intrinsic::x86_avx_vtestc_ps_256:
7705 case Intrinsic::x86_avx_vtestnzc_ps_256:
7706 case Intrinsic::x86_avx_vtestz_pd_256:
7707 case Intrinsic::x86_avx_vtestc_pd_256:
7708 case Intrinsic::x86_avx_vtestnzc_pd_256: {
7709 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00007710 unsigned X86CC = 0;
7711 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00007712 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007713 case Intrinsic::x86_avx_vtestz_ps:
7714 case Intrinsic::x86_avx_vtestz_pd:
7715 case Intrinsic::x86_avx_vtestz_ps_256:
7716 case Intrinsic::x86_avx_vtestz_pd_256:
7717 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00007718 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007719 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00007720 // ZF = 1
7721 X86CC = X86::COND_E;
7722 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007723 case Intrinsic::x86_avx_vtestc_ps:
7724 case Intrinsic::x86_avx_vtestc_pd:
7725 case Intrinsic::x86_avx_vtestc_ps_256:
7726 case Intrinsic::x86_avx_vtestc_pd_256:
7727 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00007728 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007729 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00007730 // CF = 1
7731 X86CC = X86::COND_B;
7732 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007733 case Intrinsic::x86_avx_vtestnzc_ps:
7734 case Intrinsic::x86_avx_vtestnzc_pd:
7735 case Intrinsic::x86_avx_vtestnzc_ps_256:
7736 case Intrinsic::x86_avx_vtestnzc_pd_256:
7737 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00007738 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007739 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00007740 // ZF and CF = 0
7741 X86CC = X86::COND_A;
7742 break;
7743 }
Eric Christopherfd179292009-08-27 18:07:15 +00007744
Eric Christopher71c67532009-07-29 00:28:05 +00007745 SDValue LHS = Op.getOperand(1);
7746 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007747 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
7748 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00007749 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
7750 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
7751 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00007752 }
Evan Cheng5759f972008-05-04 09:15:50 +00007753
7754 // Fix vector shift instructions where the last operand is a non-immediate
7755 // i32 value.
7756 case Intrinsic::x86_sse2_pslli_w:
7757 case Intrinsic::x86_sse2_pslli_d:
7758 case Intrinsic::x86_sse2_pslli_q:
7759 case Intrinsic::x86_sse2_psrli_w:
7760 case Intrinsic::x86_sse2_psrli_d:
7761 case Intrinsic::x86_sse2_psrli_q:
7762 case Intrinsic::x86_sse2_psrai_w:
7763 case Intrinsic::x86_sse2_psrai_d:
7764 case Intrinsic::x86_mmx_pslli_w:
7765 case Intrinsic::x86_mmx_pslli_d:
7766 case Intrinsic::x86_mmx_pslli_q:
7767 case Intrinsic::x86_mmx_psrli_w:
7768 case Intrinsic::x86_mmx_psrli_d:
7769 case Intrinsic::x86_mmx_psrli_q:
7770 case Intrinsic::x86_mmx_psrai_w:
7771 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00007772 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00007773 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00007774 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00007775
7776 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00007777 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00007778 switch (IntNo) {
7779 case Intrinsic::x86_sse2_pslli_w:
7780 NewIntNo = Intrinsic::x86_sse2_psll_w;
7781 break;
7782 case Intrinsic::x86_sse2_pslli_d:
7783 NewIntNo = Intrinsic::x86_sse2_psll_d;
7784 break;
7785 case Intrinsic::x86_sse2_pslli_q:
7786 NewIntNo = Intrinsic::x86_sse2_psll_q;
7787 break;
7788 case Intrinsic::x86_sse2_psrli_w:
7789 NewIntNo = Intrinsic::x86_sse2_psrl_w;
7790 break;
7791 case Intrinsic::x86_sse2_psrli_d:
7792 NewIntNo = Intrinsic::x86_sse2_psrl_d;
7793 break;
7794 case Intrinsic::x86_sse2_psrli_q:
7795 NewIntNo = Intrinsic::x86_sse2_psrl_q;
7796 break;
7797 case Intrinsic::x86_sse2_psrai_w:
7798 NewIntNo = Intrinsic::x86_sse2_psra_w;
7799 break;
7800 case Intrinsic::x86_sse2_psrai_d:
7801 NewIntNo = Intrinsic::x86_sse2_psra_d;
7802 break;
7803 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00007804 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00007805 switch (IntNo) {
7806 case Intrinsic::x86_mmx_pslli_w:
7807 NewIntNo = Intrinsic::x86_mmx_psll_w;
7808 break;
7809 case Intrinsic::x86_mmx_pslli_d:
7810 NewIntNo = Intrinsic::x86_mmx_psll_d;
7811 break;
7812 case Intrinsic::x86_mmx_pslli_q:
7813 NewIntNo = Intrinsic::x86_mmx_psll_q;
7814 break;
7815 case Intrinsic::x86_mmx_psrli_w:
7816 NewIntNo = Intrinsic::x86_mmx_psrl_w;
7817 break;
7818 case Intrinsic::x86_mmx_psrli_d:
7819 NewIntNo = Intrinsic::x86_mmx_psrl_d;
7820 break;
7821 case Intrinsic::x86_mmx_psrli_q:
7822 NewIntNo = Intrinsic::x86_mmx_psrl_q;
7823 break;
7824 case Intrinsic::x86_mmx_psrai_w:
7825 NewIntNo = Intrinsic::x86_mmx_psra_w;
7826 break;
7827 case Intrinsic::x86_mmx_psrai_d:
7828 NewIntNo = Intrinsic::x86_mmx_psra_d;
7829 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00007830 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00007831 }
7832 break;
7833 }
7834 }
Mon P Wangefa42202009-09-03 19:56:25 +00007835
7836 // The vector shift intrinsics with scalars uses 32b shift amounts but
7837 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
7838 // to be zero.
7839 SDValue ShOps[4];
7840 ShOps[0] = ShAmt;
7841 ShOps[1] = DAG.getConstant(0, MVT::i32);
7842 if (ShAmtVT == MVT::v4i32) {
7843 ShOps[2] = DAG.getUNDEF(MVT::i32);
7844 ShOps[3] = DAG.getUNDEF(MVT::i32);
7845 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
7846 } else {
7847 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00007848// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00007849 }
7850
Owen Andersone50ed302009-08-10 22:56:29 +00007851 EVT VT = Op.getValueType();
Mon P Wangefa42202009-09-03 19:56:25 +00007852 ShAmt = DAG.getNode(ISD::BIT_CONVERT, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007853 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007854 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00007855 Op.getOperand(1), ShAmt);
7856 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00007857 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007858}
Evan Cheng72261582005-12-20 06:22:03 +00007859
Dan Gohmand858e902010-04-17 15:26:15 +00007860SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
7861 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00007862 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7863 MFI->setReturnAddressIsTaken(true);
7864
Bill Wendling64e87322009-01-16 19:25:27 +00007865 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007866 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00007867
7868 if (Depth > 0) {
7869 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
7870 SDValue Offset =
7871 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00007872 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007873 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00007874 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007875 FrameAddr, Offset),
Chris Lattner51abfe42010-09-21 06:02:19 +00007876 MachinePointerInfo(), false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00007877 }
7878
7879 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00007880 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00007881 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007882 RetAddrFI, MachinePointerInfo(), false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00007883}
7884
Dan Gohmand858e902010-04-17 15:26:15 +00007885SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00007886 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7887 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00007888
Owen Andersone50ed302009-08-10 22:56:29 +00007889 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007890 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00007891 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
7892 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00007893 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00007894 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00007895 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
7896 MachinePointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +00007897 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00007898 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00007899}
7900
Dan Gohman475871a2008-07-27 21:46:04 +00007901SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007902 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00007903 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007904}
7905
Dan Gohmand858e902010-04-17 15:26:15 +00007906SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007907 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00007908 SDValue Chain = Op.getOperand(0);
7909 SDValue Offset = Op.getOperand(1);
7910 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007911 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007912
Dan Gohmand8816272010-08-11 18:14:00 +00007913 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
7914 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
7915 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007916 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007917
Dan Gohmand8816272010-08-11 18:14:00 +00007918 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
7919 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007920 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00007921 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
7922 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00007923 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007924 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007925
Dale Johannesene4d209d2009-02-03 20:21:25 +00007926 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007927 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007928 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007929}
7930
Dan Gohman475871a2008-07-27 21:46:04 +00007931SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007932 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007933 SDValue Root = Op.getOperand(0);
7934 SDValue Trmp = Op.getOperand(1); // trampoline
7935 SDValue FPtr = Op.getOperand(2); // nested function
7936 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007937 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00007938
Dan Gohman69de1932008-02-06 22:27:42 +00007939 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00007940
7941 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00007942 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00007943
7944 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00007945 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
7946 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00007947
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00007948 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
7949 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00007950
7951 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
7952
7953 // Load the pointer to the nested function into R11.
7954 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00007955 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00007956 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007957 Addr, MachinePointerInfo(TrmpAddr),
7958 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007959
Owen Anderson825b72b2009-08-11 20:47:22 +00007960 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7961 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00007962 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
7963 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00007964 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00007965
7966 // Load the 'nest' parameter value into R10.
7967 // R10 is specified in X86CallingConv.td
7968 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00007969 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7970 DAG.getConstant(10, MVT::i64));
7971 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007972 Addr, MachinePointerInfo(TrmpAddr, 10),
7973 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007974
Owen Anderson825b72b2009-08-11 20:47:22 +00007975 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7976 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00007977 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
7978 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00007979 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00007980
7981 // Jump to the nested function.
7982 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00007983 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7984 DAG.getConstant(20, MVT::i64));
7985 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007986 Addr, MachinePointerInfo(TrmpAddr, 20),
7987 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007988
7989 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00007990 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7991 DAG.getConstant(22, MVT::i64));
7992 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00007993 MachinePointerInfo(TrmpAddr, 22),
7994 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007995
Dan Gohman475871a2008-07-27 21:46:04 +00007996 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00007997 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007998 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007999 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00008000 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00008001 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00008002 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00008003 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008004
8005 switch (CC) {
8006 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008007 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008008 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008009 case CallingConv::X86_StdCall: {
8010 // Pass 'nest' parameter in ECX.
8011 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008012 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008013
8014 // Check that ECX wasn't needed by an 'inreg' parameter.
8015 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00008016 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008017
Chris Lattner58d74912008-03-12 17:45:29 +00008018 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00008019 unsigned InRegCount = 0;
8020 unsigned Idx = 1;
8021
8022 for (FunctionType::param_iterator I = FTy->param_begin(),
8023 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00008024 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00008025 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008026 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008027
8028 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00008029 report_fatal_error("Nest register in use - reduce number of inreg"
8030 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008031 }
8032 }
8033 break;
8034 }
8035 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00008036 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00008037 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008038 // Pass 'nest' parameter in EAX.
8039 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008040 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008041 break;
8042 }
8043
Dan Gohman475871a2008-07-27 21:46:04 +00008044 SDValue OutChains[4];
8045 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008046
Owen Anderson825b72b2009-08-11 20:47:22 +00008047 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8048 DAG.getConstant(10, MVT::i32));
8049 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008050
Chris Lattnera62fe662010-02-05 19:20:30 +00008051 // This is storing the opcode for MOV32ri.
8052 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008053 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00008054 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008055 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008056 Trmp, MachinePointerInfo(TrmpAddr),
8057 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008058
Owen Anderson825b72b2009-08-11 20:47:22 +00008059 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8060 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008061 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
8062 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00008063 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008064
Chris Lattnera62fe662010-02-05 19:20:30 +00008065 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00008066 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8067 DAG.getConstant(5, MVT::i32));
8068 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008069 MachinePointerInfo(TrmpAddr, 5),
8070 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008071
Owen Anderson825b72b2009-08-11 20:47:22 +00008072 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8073 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008074 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
8075 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00008076 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008077
Dan Gohman475871a2008-07-27 21:46:04 +00008078 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008079 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008080 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008081 }
8082}
8083
Dan Gohmand858e902010-04-17 15:26:15 +00008084SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
8085 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008086 /*
8087 The rounding mode is in bits 11:10 of FPSR, and has the following
8088 settings:
8089 00 Round to nearest
8090 01 Round to -inf
8091 10 Round to +inf
8092 11 Round to 0
8093
8094 FLT_ROUNDS, on the other hand, expects the following:
8095 -1 Undefined
8096 0 Round to 0
8097 1 Round to nearest
8098 2 Round to +inf
8099 3 Round to -inf
8100
8101 To perform the conversion, we do:
8102 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
8103 */
8104
8105 MachineFunction &MF = DAG.getMachineFunction();
8106 const TargetMachine &TM = MF.getTarget();
8107 const TargetFrameInfo &TFI = *TM.getFrameInfo();
8108 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00008109 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00008110 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008111
8112 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00008113 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008114 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008115
Chris Lattner2156b792010-09-22 01:11:26 +00008116
8117 MachineMemOperand *MMO =
8118 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8119 MachineMemOperand::MOStore, 2, 2);
8120
8121 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
8122 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
8123 DAG.getVTList(MVT::Other),
8124 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008125
8126 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00008127 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Chris Lattner51abfe42010-09-21 06:02:19 +00008128 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008129
8130 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00008131 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00008132 DAG.getNode(ISD::SRL, DL, MVT::i16,
8133 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008134 CWD, DAG.getConstant(0x800, MVT::i16)),
8135 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00008136 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00008137 DAG.getNode(ISD::SRL, DL, MVT::i16,
8138 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008139 CWD, DAG.getConstant(0x400, MVT::i16)),
8140 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008141
Dan Gohman475871a2008-07-27 21:46:04 +00008142 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00008143 DAG.getNode(ISD::AND, DL, MVT::i16,
8144 DAG.getNode(ISD::ADD, DL, MVT::i16,
8145 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00008146 DAG.getConstant(1, MVT::i16)),
8147 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008148
8149
Duncan Sands83ec4b62008-06-06 12:08:01 +00008150 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00008151 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008152}
8153
Dan Gohmand858e902010-04-17 15:26:15 +00008154SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008155 EVT VT = Op.getValueType();
8156 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008157 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008158 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008159
8160 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008161 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00008162 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00008163 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008164 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008165 }
Evan Cheng18efe262007-12-14 02:13:44 +00008166
Evan Cheng152804e2007-12-14 08:30:15 +00008167 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008168 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008169 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008170
8171 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008172 SDValue Ops[] = {
8173 Op,
8174 DAG.getConstant(NumBits+NumBits-1, OpVT),
8175 DAG.getConstant(X86::COND_E, MVT::i8),
8176 Op.getValue(1)
8177 };
8178 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008179
8180 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00008181 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00008182
Owen Anderson825b72b2009-08-11 20:47:22 +00008183 if (VT == MVT::i8)
8184 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008185 return Op;
8186}
8187
Dan Gohmand858e902010-04-17 15:26:15 +00008188SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008189 EVT VT = Op.getValueType();
8190 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008191 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008192 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008193
8194 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008195 if (VT == MVT::i8) {
8196 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008197 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008198 }
Evan Cheng152804e2007-12-14 08:30:15 +00008199
8200 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008201 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008202 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008203
8204 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008205 SDValue Ops[] = {
8206 Op,
8207 DAG.getConstant(NumBits, OpVT),
8208 DAG.getConstant(X86::COND_E, MVT::i8),
8209 Op.getValue(1)
8210 };
8211 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008212
Owen Anderson825b72b2009-08-11 20:47:22 +00008213 if (VT == MVT::i8)
8214 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008215 return Op;
8216}
8217
Dan Gohmand858e902010-04-17 15:26:15 +00008218SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008219 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00008220 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008221 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00008222
Mon P Wangaf9b9522008-12-18 21:42:19 +00008223 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
8224 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
8225 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
8226 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
8227 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
8228 //
8229 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
8230 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
8231 // return AloBlo + AloBhi + AhiBlo;
8232
8233 SDValue A = Op.getOperand(0);
8234 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008235
Dale Johannesene4d209d2009-02-03 20:21:25 +00008236 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008237 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8238 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008239 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008240 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8241 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008242 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008243 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008244 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008245 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008246 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008247 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008248 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008249 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008250 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008251 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008252 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8253 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008254 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008255 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8256 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008257 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
8258 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008259 return Res;
8260}
8261
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008262SDValue X86TargetLowering::LowerSHL(SDValue Op, SelectionDAG &DAG) const {
8263 EVT VT = Op.getValueType();
8264 DebugLoc dl = Op.getDebugLoc();
8265 SDValue R = Op.getOperand(0);
8266
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008267 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008268
Nate Begeman51409212010-07-28 00:21:48 +00008269 assert(Subtarget->hasSSE41() && "Cannot lower SHL without SSE4.1 or later");
8270
8271 if (VT == MVT::v4i32) {
8272 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8273 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8274 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
8275
8276 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
8277
8278 std::vector<Constant*> CV(4, CI);
8279 Constant *C = ConstantVector::get(CV);
8280 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8281 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008282 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008283 false, false, 16);
8284
8285 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
8286 Op = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, Op);
8287 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
8288 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
8289 }
8290 if (VT == MVT::v16i8) {
8291 // a = a << 5;
8292 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8293 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8294 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
8295
8296 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
8297 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
8298
8299 std::vector<Constant*> CVM1(16, CM1);
8300 std::vector<Constant*> CVM2(16, CM2);
8301 Constant *C = ConstantVector::get(CVM1);
8302 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8303 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008304 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008305 false, false, 16);
8306
8307 // r = pblendv(r, psllw(r & (char16)15, 4), a);
8308 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8309 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8310 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8311 DAG.getConstant(4, MVT::i32));
8312 R = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8313 DAG.getConstant(Intrinsic::x86_sse41_pblendvb, MVT::i32),
8314 R, M, Op);
8315 // a += a
8316 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
8317
8318 C = ConstantVector::get(CVM2);
8319 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8320 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008321 MachinePointerInfo::getConstantPool(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008322 false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +00008323
8324 // r = pblendv(r, psllw(r & (char16)63, 2), a);
8325 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8326 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8327 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8328 DAG.getConstant(2, MVT::i32));
8329 R = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8330 DAG.getConstant(Intrinsic::x86_sse41_pblendvb, MVT::i32),
8331 R, M, Op);
8332 // a += a
8333 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
8334
8335 // return pblendv(r, r+r, a);
8336 R = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8337 DAG.getConstant(Intrinsic::x86_sse41_pblendvb, MVT::i32),
8338 R, DAG.getNode(ISD::ADD, dl, VT, R, R), Op);
8339 return R;
8340 }
8341 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008342}
Mon P Wangaf9b9522008-12-18 21:42:19 +00008343
Dan Gohmand858e902010-04-17 15:26:15 +00008344SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +00008345 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
8346 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00008347 // looks for this combo and may remove the "setcc" instruction if the "setcc"
8348 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00008349 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00008350 SDValue LHS = N->getOperand(0);
8351 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00008352 unsigned BaseOp = 0;
8353 unsigned Cond = 0;
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008354 DebugLoc dl = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00008355
8356 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008357 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00008358 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00008359 // A subtract of one will be selected as a INC. Note that INC doesn't
8360 // set CF, so we can't do this for UADDO.
8361 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
8362 if (C->getAPIntValue() == 1) {
8363 BaseOp = X86ISD::INC;
8364 Cond = X86::COND_O;
8365 break;
8366 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008367 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00008368 Cond = X86::COND_O;
8369 break;
8370 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008371 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00008372 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008373 break;
8374 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00008375 // A subtract of one will be selected as a DEC. Note that DEC doesn't
8376 // set CF, so we can't do this for USUBO.
8377 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
8378 if (C->getAPIntValue() == 1) {
8379 BaseOp = X86ISD::DEC;
8380 Cond = X86::COND_O;
8381 break;
8382 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008383 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00008384 Cond = X86::COND_O;
8385 break;
8386 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008387 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00008388 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008389 break;
8390 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00008391 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00008392 Cond = X86::COND_O;
8393 break;
8394 case ISD::UMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00008395 BaseOp = X86ISD::UMUL;
Dan Gohman653456c2009-01-07 00:15:08 +00008396 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008397 break;
8398 }
Bill Wendling3fafd932008-11-26 22:37:40 +00008399
Bill Wendling61edeb52008-12-02 01:06:39 +00008400 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008401 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008402 SDValue Sum = DAG.getNode(BaseOp, dl, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00008403
Bill Wendling61edeb52008-12-02 01:06:39 +00008404 SDValue SetCC =
Dale Johannesene4d209d2009-02-03 20:21:25 +00008405 DAG.getNode(X86ISD::SETCC, dl, N->getValueType(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00008406 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00008407
Bill Wendling61edeb52008-12-02 01:06:39 +00008408 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
8409 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00008410}
8411
Eric Christopher9a9d2752010-07-22 02:48:34 +00008412SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
8413 DebugLoc dl = Op.getDebugLoc();
8414
Eric Christopherb6729dc2010-08-04 23:03:04 +00008415 if (!Subtarget->hasSSE2()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +00008416 SDValue Chain = Op.getOperand(0);
8417 SDValue Zero = DAG.getConstant(0,
Eric Christopherb6729dc2010-08-04 23:03:04 +00008418 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +00008419 SDValue Ops[] = {
8420 DAG.getRegister(X86::ESP, MVT::i32), // Base
8421 DAG.getTargetConstant(1, MVT::i8), // Scale
8422 DAG.getRegister(0, MVT::i32), // Index
8423 DAG.getTargetConstant(0, MVT::i32), // Disp
8424 DAG.getRegister(0, MVT::i32), // Segment.
8425 Zero,
8426 Chain
8427 };
8428 SDNode *Res =
8429 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
8430 array_lengthof(Ops));
8431 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +00008432 }
Eric Christopher9a9d2752010-07-22 02:48:34 +00008433
8434 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +00008435 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +00008436 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Chris Lattner132929a2010-08-14 17:26:09 +00008437
8438 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
8439 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
8440 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
8441 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
8442
8443 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
8444 if (!Op1 && !Op2 && !Op3 && Op4)
8445 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
8446
8447 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
8448 if (Op1 && !Op2 && !Op3 && !Op4)
8449 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
8450
8451 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
8452 // (MFENCE)>;
8453 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +00008454}
8455
Dan Gohmand858e902010-04-17 15:26:15 +00008456SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008457 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008458 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00008459 unsigned Reg = 0;
8460 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00008461 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008462 default:
8463 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008464 case MVT::i8: Reg = X86::AL; size = 1; break;
8465 case MVT::i16: Reg = X86::AX; size = 2; break;
8466 case MVT::i32: Reg = X86::EAX; size = 4; break;
8467 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00008468 assert(Subtarget->is64Bit() && "Node not type legal!");
8469 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00008470 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008471 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008472 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00008473 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00008474 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00008475 Op.getOperand(1),
8476 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00008477 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00008478 cpIn.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00008479 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008480 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
8481 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
8482 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +00008483 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008484 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00008485 return cpOut;
8486}
8487
Duncan Sands1607f052008-12-01 11:39:25 +00008488SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008489 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +00008490 assert(Subtarget->is64Bit() && "Result not type legalized?");
Owen Anderson825b72b2009-08-11 20:47:22 +00008491 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00008492 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008493 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00008494 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008495 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
8496 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00008497 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00008498 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
8499 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00008500 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00008501 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00008502 rdx.getValue(1)
8503 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008504 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008505}
8506
Dale Johannesen7d07b482010-05-21 00:52:33 +00008507SDValue X86TargetLowering::LowerBIT_CONVERT(SDValue Op,
8508 SelectionDAG &DAG) const {
8509 EVT SrcVT = Op.getOperand(0).getValueType();
8510 EVT DstVT = Op.getValueType();
8511 assert((Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
8512 Subtarget->hasMMX() && !DisableMMX) &&
8513 "Unexpected custom BIT_CONVERT");
8514 assert((DstVT == MVT::i64 ||
8515 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
8516 "Unexpected custom BIT_CONVERT");
8517 // i64 <=> MMX conversions are Legal.
8518 if (SrcVT==MVT::i64 && DstVT.isVector())
8519 return Op;
8520 if (DstVT==MVT::i64 && SrcVT.isVector())
8521 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +00008522 // MMX <=> MMX conversions are Legal.
8523 if (SrcVT.isVector() && DstVT.isVector())
8524 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +00008525 // All other conversions need to be expanded.
8526 return SDValue();
8527}
Dan Gohmand858e902010-04-17 15:26:15 +00008528SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +00008529 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00008530 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008531 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008532 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00008533 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008534 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008535 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00008536 Node->getOperand(0),
8537 Node->getOperand(1), negOp,
8538 cast<AtomicSDNode>(Node)->getSrcValue(),
8539 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00008540}
8541
Evan Cheng0db9fe62006-04-25 20:13:52 +00008542/// LowerOperation - Provide custom lowering hooks for some operations.
8543///
Dan Gohmand858e902010-04-17 15:26:15 +00008544SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00008545 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008546 default: llvm_unreachable("Should not custom lower this!");
Eric Christopher9a9d2752010-07-22 02:48:34 +00008547 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008548 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
8549 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008550 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00008551 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008552 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
8553 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
8554 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
8555 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
8556 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
8557 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00008558 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00008559 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00008560 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008561 case ISD::SHL_PARTS:
8562 case ISD::SRA_PARTS:
8563 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
8564 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008565 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008566 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00008567 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008568 case ISD::FABS: return LowerFABS(Op, DAG);
8569 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008570 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00008571 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00008572 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00008573 case ISD::SELECT: return LowerSELECT(Op, DAG);
8574 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008575 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008576 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00008577 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00008578 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008579 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00008580 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
8581 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008582 case ISD::FRAME_TO_ARGS_OFFSET:
8583 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008584 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008585 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008586 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00008587 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00008588 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
8589 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008590 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008591 case ISD::SHL: return LowerSHL(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00008592 case ISD::SADDO:
8593 case ISD::UADDO:
8594 case ISD::SSUBO:
8595 case ISD::USUBO:
8596 case ISD::SMULO:
8597 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00008598 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Dale Johannesen7d07b482010-05-21 00:52:33 +00008599 case ISD::BIT_CONVERT: return LowerBIT_CONVERT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008600 }
Chris Lattner27a6c732007-11-24 07:07:01 +00008601}
8602
Duncan Sands1607f052008-12-01 11:39:25 +00008603void X86TargetLowering::
8604ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00008605 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008606 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008607 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00008608 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00008609
8610 SDValue Chain = Node->getOperand(0);
8611 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008612 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00008613 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00008614 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00008615 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00008616 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00008617 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00008618 SDValue Result =
8619 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
8620 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00008621 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00008622 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00008623 Results.push_back(Result.getValue(2));
8624}
8625
Duncan Sands126d9072008-07-04 11:47:58 +00008626/// ReplaceNodeResults - Replace a node with an illegal result type
8627/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00008628void X86TargetLowering::ReplaceNodeResults(SDNode *N,
8629 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00008630 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00008631 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00008632 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00008633 default:
Duncan Sands1607f052008-12-01 11:39:25 +00008634 assert(false && "Do not know how to custom type legalize this operation!");
8635 return;
8636 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00008637 std::pair<SDValue,SDValue> Vals =
8638 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00008639 SDValue FIST = Vals.first, StackSlot = Vals.second;
8640 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00008641 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00008642 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +00008643 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
8644 MachinePointerInfo(), false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00008645 }
8646 return;
8647 }
8648 case ISD::READCYCLECOUNTER: {
Owen Anderson825b72b2009-08-11 20:47:22 +00008649 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00008650 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008651 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008652 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00008653 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008654 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00008655 eax.getValue(2));
8656 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
8657 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00008658 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00008659 Results.push_back(edx.getValue(1));
8660 return;
8661 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008662 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00008663 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008664 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00008665 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00008666 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
8667 DAG.getConstant(0, MVT::i32));
8668 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
8669 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00008670 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
8671 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00008672 cpInL.getValue(1));
8673 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00008674 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
8675 DAG.getConstant(0, MVT::i32));
8676 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
8677 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00008678 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00008679 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00008680 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00008681 swapInL.getValue(1));
8682 SDValue Ops[] = { swapInH.getValue(0),
8683 N->getOperand(1),
8684 swapInH.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00008685 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008686 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, dl, Tys, Ops, 3);
Dale Johannesendd64c412009-02-04 00:33:20 +00008687 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00008688 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00008689 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00008690 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00008691 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00008692 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00008693 Results.push_back(cpOutH.getValue(1));
8694 return;
8695 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008696 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00008697 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
8698 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008699 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00008700 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
8701 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008702 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00008703 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
8704 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008705 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00008706 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
8707 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008708 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00008709 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
8710 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008711 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00008712 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
8713 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008714 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00008715 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
8716 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00008717 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008718}
8719
Evan Cheng72261582005-12-20 06:22:03 +00008720const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
8721 switch (Opcode) {
8722 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00008723 case X86ISD::BSF: return "X86ISD::BSF";
8724 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00008725 case X86ISD::SHLD: return "X86ISD::SHLD";
8726 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00008727 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00008728 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00008729 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00008730 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00008731 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00008732 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00008733 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
8734 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
8735 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00008736 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00008737 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00008738 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00008739 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00008740 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00008741 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00008742 case X86ISD::COMI: return "X86ISD::COMI";
8743 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00008744 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00008745 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Evan Cheng72261582005-12-20 06:22:03 +00008746 case X86ISD::CMOV: return "X86ISD::CMOV";
8747 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00008748 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00008749 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
8750 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00008751 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00008752 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00008753 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00008754 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00008755 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00008756 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
8757 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00008758 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00008759 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Evan Cheng8ca29322006-11-10 21:43:37 +00008760 case X86ISD::FMAX: return "X86ISD::FMAX";
8761 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00008762 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
8763 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00008764 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +00008765 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008766 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00008767 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008768 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00008769 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
8770 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008771 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
8772 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
8773 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
8774 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
8775 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
8776 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00008777 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
8778 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00008779 case X86ISD::VSHL: return "X86ISD::VSHL";
8780 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00008781 case X86ISD::CMPPD: return "X86ISD::CMPPD";
8782 case X86ISD::CMPPS: return "X86ISD::CMPPS";
8783 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
8784 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
8785 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
8786 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
8787 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
8788 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
8789 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
8790 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008791 case X86ISD::ADD: return "X86ISD::ADD";
8792 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingd350e022008-12-12 21:15:41 +00008793 case X86ISD::SMUL: return "X86ISD::SMUL";
8794 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00008795 case X86ISD::INC: return "X86ISD::INC";
8796 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00008797 case X86ISD::OR: return "X86ISD::OR";
8798 case X86ISD::XOR: return "X86ISD::XOR";
8799 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00008800 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00008801 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008802 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00008803 case X86ISD::PALIGN: return "X86ISD::PALIGN";
8804 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
8805 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
8806 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
8807 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
8808 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
8809 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
8810 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
8811 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00008812 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00008813 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00008814 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00008815 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
8816 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00008817 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
8818 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
8819 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
8820 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
8821 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
8822 case X86ISD::MOVSD: return "X86ISD::MOVSD";
8823 case X86ISD::MOVSS: return "X86ISD::MOVSS";
8824 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
8825 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
8826 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
8827 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
8828 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
8829 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
8830 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
8831 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
8832 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
8833 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
8834 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
8835 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +00008836 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008837 case X86ISD::MINGW_ALLOCA: return "X86ISD::MINGW_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00008838 }
8839}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008840
Chris Lattnerc9addb72007-03-30 23:15:24 +00008841// isLegalAddressingMode - Return true if the addressing mode represented
8842// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00008843bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00008844 const Type *Ty) const {
8845 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008846 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +00008847 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00008848
Chris Lattnerc9addb72007-03-30 23:15:24 +00008849 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008850 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00008851 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00008852
Chris Lattnerc9addb72007-03-30 23:15:24 +00008853 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00008854 unsigned GVFlags =
8855 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008856
Chris Lattnerdfed4132009-07-10 07:38:24 +00008857 // If a reference to this global requires an extra load, we can't fold it.
8858 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00008859 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008860
Chris Lattnerdfed4132009-07-10 07:38:24 +00008861 // If BaseGV requires a register for the PIC base, we cannot also have a
8862 // BaseReg specified.
8863 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00008864 return false;
Evan Cheng52787842007-08-01 23:46:47 +00008865
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008866 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +00008867 if ((M != CodeModel::Small || R != Reloc::Static) &&
8868 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008869 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00008870 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008871
Chris Lattnerc9addb72007-03-30 23:15:24 +00008872 switch (AM.Scale) {
8873 case 0:
8874 case 1:
8875 case 2:
8876 case 4:
8877 case 8:
8878 // These scales always work.
8879 break;
8880 case 3:
8881 case 5:
8882 case 9:
8883 // These scales are formed with basereg+scalereg. Only accept if there is
8884 // no basereg yet.
8885 if (AM.HasBaseReg)
8886 return false;
8887 break;
8888 default: // Other stuff never works.
8889 return false;
8890 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008891
Chris Lattnerc9addb72007-03-30 23:15:24 +00008892 return true;
8893}
8894
8895
Evan Cheng2bd122c2007-10-26 01:56:11 +00008896bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00008897 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00008898 return false;
Evan Chenge127a732007-10-29 07:57:50 +00008899 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
8900 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00008901 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00008902 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00008903 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00008904}
8905
Owen Andersone50ed302009-08-10 22:56:29 +00008906bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008907 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00008908 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008909 unsigned NumBits1 = VT1.getSizeInBits();
8910 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00008911 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00008912 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00008913 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00008914}
Evan Cheng2bd122c2007-10-26 01:56:11 +00008915
Dan Gohman97121ba2009-04-08 00:15:30 +00008916bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00008917 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00008918 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00008919}
8920
Owen Andersone50ed302009-08-10 22:56:29 +00008921bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00008922 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00008923 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00008924}
8925
Owen Andersone50ed302009-08-10 22:56:29 +00008926bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00008927 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00008928 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00008929}
8930
Evan Cheng60c07e12006-07-05 22:17:51 +00008931/// isShuffleMaskLegal - Targets can use this to indicate that they only
8932/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
8933/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
8934/// are assumed to be legal.
8935bool
Eric Christopherfd179292009-08-27 18:07:15 +00008936X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00008937 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +00008938 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +00008939 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +00008940 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +00008941
Nate Begemana09008b2009-10-19 02:17:23 +00008942 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00008943 return (VT.getVectorNumElements() == 2 ||
8944 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
8945 isMOVLMask(M, VT) ||
8946 isSHUFPMask(M, VT) ||
8947 isPSHUFDMask(M, VT) ||
8948 isPSHUFHWMask(M, VT) ||
8949 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00008950 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00008951 isUNPCKLMask(M, VT) ||
8952 isUNPCKHMask(M, VT) ||
8953 isUNPCKL_v_undef_Mask(M, VT) ||
8954 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00008955}
8956
Dan Gohman7d8143f2008-04-09 20:09:42 +00008957bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00008958X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00008959 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00008960 unsigned NumElts = VT.getVectorNumElements();
8961 // FIXME: This collection of masks seems suspect.
8962 if (NumElts == 2)
8963 return true;
8964 if (NumElts == 4 && VT.getSizeInBits() == 128) {
8965 return (isMOVLMask(Mask, VT) ||
8966 isCommutedMOVLMask(Mask, VT, true) ||
8967 isSHUFPMask(Mask, VT) ||
8968 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00008969 }
8970 return false;
8971}
8972
8973//===----------------------------------------------------------------------===//
8974// X86 Scheduler Hooks
8975//===----------------------------------------------------------------------===//
8976
Mon P Wang63307c32008-05-05 19:05:59 +00008977// private utility function
8978MachineBasicBlock *
8979X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
8980 MachineBasicBlock *MBB,
8981 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008982 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008983 unsigned LoadOpc,
8984 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008985 unsigned notOpc,
8986 unsigned EAXreg,
8987 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00008988 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00008989 // For the atomic bitwise operator, we generate
8990 // thisMBB:
8991 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00008992 // ld t1 = [bitinstr.addr]
8993 // op t2 = t1, [bitinstr.val]
8994 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00008995 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
8996 // bz newMBB
8997 // fallthrough -->nextMBB
8998 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8999 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009000 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009001 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009002
Mon P Wang63307c32008-05-05 19:05:59 +00009003 /// First build the CFG
9004 MachineFunction *F = MBB->getParent();
9005 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009006 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9007 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9008 F->insert(MBBIter, newMBB);
9009 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009010
Dan Gohman14152b42010-07-06 20:24:04 +00009011 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9012 nextMBB->splice(nextMBB->begin(), thisMBB,
9013 llvm::next(MachineBasicBlock::iterator(bInstr)),
9014 thisMBB->end());
9015 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009016
Mon P Wang63307c32008-05-05 19:05:59 +00009017 // Update thisMBB to fall through to newMBB
9018 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009019
Mon P Wang63307c32008-05-05 19:05:59 +00009020 // newMBB jumps to itself and fall through to nextMBB
9021 newMBB->addSuccessor(nextMBB);
9022 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009023
Mon P Wang63307c32008-05-05 19:05:59 +00009024 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009025 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009026 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00009027 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009028 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009029 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009030 int numArgs = bInstr->getNumOperands() - 1;
9031 for (int i=0; i < numArgs; ++i)
9032 argOpers[i] = &bInstr->getOperand(i+1);
9033
9034 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009035 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009036 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009037
Dale Johannesen140be2d2008-08-19 18:47:28 +00009038 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009039 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009040 for (int i=0; i <= lastAddrIndx; ++i)
9041 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009042
Dale Johannesen140be2d2008-08-19 18:47:28 +00009043 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009044 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009045 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009046 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009047 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009048 tt = t1;
9049
Dale Johannesen140be2d2008-08-19 18:47:28 +00009050 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00009051 assert((argOpers[valArgIndx]->isReg() ||
9052 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009053 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00009054 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009055 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009056 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009057 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009058 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00009059 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009060
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009061 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00009062 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009063
Dale Johannesene4d209d2009-02-03 20:21:25 +00009064 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00009065 for (int i=0; i <= lastAddrIndx; ++i)
9066 (*MIB).addOperand(*argOpers[i]);
9067 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00009068 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009069 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9070 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00009071
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009072 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00009073 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009074
Mon P Wang63307c32008-05-05 19:05:59 +00009075 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009076 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009077
Dan Gohman14152b42010-07-06 20:24:04 +00009078 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009079 return nextMBB;
9080}
9081
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00009082// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00009083MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009084X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
9085 MachineBasicBlock *MBB,
9086 unsigned regOpcL,
9087 unsigned regOpcH,
9088 unsigned immOpcL,
9089 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009090 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009091 // For the atomic bitwise operator, we generate
9092 // thisMBB (instructions are in pairs, except cmpxchg8b)
9093 // ld t1,t2 = [bitinstr.addr]
9094 // newMBB:
9095 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
9096 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00009097 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009098 // mov ECX, EBX <- t5, t6
9099 // mov EAX, EDX <- t1, t2
9100 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
9101 // mov t3, t4 <- EAX, EDX
9102 // bz newMBB
9103 // result in out1, out2
9104 // fallthrough -->nextMBB
9105
9106 const TargetRegisterClass *RC = X86::GR32RegisterClass;
9107 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009108 const unsigned NotOpc = X86::NOT32r;
9109 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9110 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9111 MachineFunction::iterator MBBIter = MBB;
9112 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009113
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009114 /// First build the CFG
9115 MachineFunction *F = MBB->getParent();
9116 MachineBasicBlock *thisMBB = MBB;
9117 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9118 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9119 F->insert(MBBIter, newMBB);
9120 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009121
Dan Gohman14152b42010-07-06 20:24:04 +00009122 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9123 nextMBB->splice(nextMBB->begin(), thisMBB,
9124 llvm::next(MachineBasicBlock::iterator(bInstr)),
9125 thisMBB->end());
9126 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009127
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009128 // Update thisMBB to fall through to newMBB
9129 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009130
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009131 // newMBB jumps to itself and fall through to nextMBB
9132 newMBB->addSuccessor(nextMBB);
9133 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009134
Dale Johannesene4d209d2009-02-03 20:21:25 +00009135 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009136 // Insert instructions into newMBB based on incoming instruction
9137 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009138 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009139 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009140 MachineOperand& dest1Oper = bInstr->getOperand(0);
9141 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009142 MachineOperand* argOpers[2 + X86::AddrNumOperands];
9143 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009144 argOpers[i] = &bInstr->getOperand(i+2);
9145
Dan Gohman71ea4e52010-05-14 21:01:44 +00009146 // We use some of the operands multiple times, so conservatively just
9147 // clear any kill flags that might be present.
9148 if (argOpers[i]->isReg() && argOpers[i]->isUse())
9149 argOpers[i]->setIsKill(false);
9150 }
9151
Evan Chengad5b52f2010-01-08 19:14:57 +00009152 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009153 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00009154
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009155 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009156 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009157 for (int i=0; i <= lastAddrIndx; ++i)
9158 (*MIB).addOperand(*argOpers[i]);
9159 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009160 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00009161 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00009162 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009163 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00009164 MachineOperand newOp3 = *(argOpers[3]);
9165 if (newOp3.isImm())
9166 newOp3.setImm(newOp3.getImm()+4);
9167 else
9168 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009169 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00009170 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009171
9172 // t3/4 are defined later, at the bottom of the loop
9173 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
9174 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009175 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009176 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009177 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009178 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
9179
Evan Cheng306b4ca2010-01-08 23:41:50 +00009180 // The subsequent operations should be using the destination registers of
9181 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00009182 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009183 t1 = F->getRegInfo().createVirtualRegister(RC);
9184 t2 = F->getRegInfo().createVirtualRegister(RC);
9185 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
9186 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009187 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009188 t1 = dest1Oper.getReg();
9189 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009190 }
9191
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009192 int valArgIndx = lastAddrIndx + 1;
9193 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00009194 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009195 "invalid operand");
9196 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
9197 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009198 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009199 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009200 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009201 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00009202 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009203 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009204 (*MIB).addOperand(*argOpers[valArgIndx]);
9205 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009206 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009207 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009208 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009209 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009210 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009211 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009212 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00009213 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009214 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009215 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009216
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009217 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009218 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009219 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009220 MIB.addReg(t2);
9221
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009222 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009223 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009224 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009225 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00009226
Dale Johannesene4d209d2009-02-03 20:21:25 +00009227 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009228 for (int i=0; i <= lastAddrIndx; ++i)
9229 (*MIB).addOperand(*argOpers[i]);
9230
9231 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009232 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9233 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009234
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009235 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009236 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009237 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009238 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009239
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009240 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009241 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009242
Dan Gohman14152b42010-07-06 20:24:04 +00009243 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009244 return nextMBB;
9245}
9246
9247// private utility function
9248MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00009249X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
9250 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009251 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009252 // For the atomic min/max operator, we generate
9253 // thisMBB:
9254 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009255 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00009256 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00009257 // cmp t1, t2
9258 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00009259 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009260 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9261 // bz newMBB
9262 // fallthrough -->nextMBB
9263 //
9264 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9265 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009266 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009267 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009268
Mon P Wang63307c32008-05-05 19:05:59 +00009269 /// First build the CFG
9270 MachineFunction *F = MBB->getParent();
9271 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009272 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9273 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9274 F->insert(MBBIter, newMBB);
9275 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009276
Dan Gohman14152b42010-07-06 20:24:04 +00009277 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9278 nextMBB->splice(nextMBB->begin(), thisMBB,
9279 llvm::next(MachineBasicBlock::iterator(mInstr)),
9280 thisMBB->end());
9281 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009282
Mon P Wang63307c32008-05-05 19:05:59 +00009283 // Update thisMBB to fall through to newMBB
9284 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009285
Mon P Wang63307c32008-05-05 19:05:59 +00009286 // newMBB jumps to newMBB and fall through to nextMBB
9287 newMBB->addSuccessor(nextMBB);
9288 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009289
Dale Johannesene4d209d2009-02-03 20:21:25 +00009290 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009291 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009292 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009293 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00009294 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009295 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009296 int numArgs = mInstr->getNumOperands() - 1;
9297 for (int i=0; i < numArgs; ++i)
9298 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009299
Mon P Wang63307c32008-05-05 19:05:59 +00009300 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009301 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009302 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009303
Mon P Wangab3e7472008-05-05 22:56:23 +00009304 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009305 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009306 for (int i=0; i <= lastAddrIndx; ++i)
9307 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00009308
Mon P Wang63307c32008-05-05 19:05:59 +00009309 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00009310 assert((argOpers[valArgIndx]->isReg() ||
9311 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009312 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00009313
9314 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00009315 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009316 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00009317 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009318 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009319 (*MIB).addOperand(*argOpers[valArgIndx]);
9320
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009321 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00009322 MIB.addReg(t1);
9323
Dale Johannesene4d209d2009-02-03 20:21:25 +00009324 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00009325 MIB.addReg(t1);
9326 MIB.addReg(t2);
9327
9328 // Generate movc
9329 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009330 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00009331 MIB.addReg(t2);
9332 MIB.addReg(t1);
9333
9334 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00009335 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00009336 for (int i=0; i <= lastAddrIndx; ++i)
9337 (*MIB).addOperand(*argOpers[i]);
9338 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00009339 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009340 (*MIB).setMemRefs(mInstr->memoperands_begin(),
9341 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +00009342
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009343 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00009344 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009345
Mon P Wang63307c32008-05-05 19:05:59 +00009346 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009347 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009348
Dan Gohman14152b42010-07-06 20:24:04 +00009349 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009350 return nextMBB;
9351}
9352
Eric Christopherf83a5de2009-08-27 18:08:16 +00009353// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009354// or XMM0_V32I8 in AVX all of this code can be replaced with that
9355// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +00009356MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +00009357X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +00009358 unsigned numArgs, bool memArg) const {
Eric Christopherb120ab42009-08-18 22:50:32 +00009359
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009360 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
9361 "Target must have SSE4.2 or AVX features enabled");
9362
Eric Christopherb120ab42009-08-18 22:50:32 +00009363 DebugLoc dl = MI->getDebugLoc();
9364 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9365
9366 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009367
9368 if (!Subtarget->hasAVX()) {
9369 if (memArg)
9370 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
9371 else
9372 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
9373 } else {
9374 if (memArg)
9375 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
9376 else
9377 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
9378 }
Eric Christopherb120ab42009-08-18 22:50:32 +00009379
9380 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(Opc));
9381
9382 for (unsigned i = 0; i < numArgs; ++i) {
9383 MachineOperand &Op = MI->getOperand(i+1);
9384
9385 if (!(Op.isReg() && Op.isImplicit()))
9386 MIB.addOperand(Op);
9387 }
9388
9389 BuildMI(BB, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
9390 .addReg(X86::XMM0);
9391
Dan Gohman14152b42010-07-06 20:24:04 +00009392 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +00009393
9394 return BB;
9395}
9396
9397MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +00009398X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
9399 MachineInstr *MI,
9400 MachineBasicBlock *MBB) const {
9401 // Emit code to save XMM registers to the stack. The ABI says that the
9402 // number of registers to save is given in %al, so it's theoretically
9403 // possible to do an indirect jump trick to avoid saving all of them,
9404 // however this code takes a simpler approach and just executes all
9405 // of the stores if %al is non-zero. It's less code, and it's probably
9406 // easier on the hardware branch predictor, and stores aren't all that
9407 // expensive anyway.
9408
9409 // Create the new basic blocks. One block contains all the XMM stores,
9410 // and one block is the final destination regardless of whether any
9411 // stores were performed.
9412 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9413 MachineFunction *F = MBB->getParent();
9414 MachineFunction::iterator MBBIter = MBB;
9415 ++MBBIter;
9416 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
9417 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
9418 F->insert(MBBIter, XMMSaveMBB);
9419 F->insert(MBBIter, EndMBB);
9420
Dan Gohman14152b42010-07-06 20:24:04 +00009421 // Transfer the remainder of MBB and its successor edges to EndMBB.
9422 EndMBB->splice(EndMBB->begin(), MBB,
9423 llvm::next(MachineBasicBlock::iterator(MI)),
9424 MBB->end());
9425 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
9426
Dan Gohmand6708ea2009-08-15 01:38:56 +00009427 // The original block will now fall through to the XMM save block.
9428 MBB->addSuccessor(XMMSaveMBB);
9429 // The XMMSaveMBB will fall through to the end block.
9430 XMMSaveMBB->addSuccessor(EndMBB);
9431
9432 // Now add the instructions.
9433 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9434 DebugLoc DL = MI->getDebugLoc();
9435
9436 unsigned CountReg = MI->getOperand(0).getReg();
9437 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
9438 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
9439
9440 if (!Subtarget->isTargetWin64()) {
9441 // If %al is 0, branch around the XMM save block.
9442 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009443 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +00009444 MBB->addSuccessor(EndMBB);
9445 }
9446
9447 // In the XMM save block, save all the XMM argument registers.
9448 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
9449 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +00009450 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +00009451 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +00009452 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +00009453 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +00009454 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +00009455 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
9456 .addFrameIndex(RegSaveFrameIndex)
9457 .addImm(/*Scale=*/1)
9458 .addReg(/*IndexReg=*/0)
9459 .addImm(/*Disp=*/Offset)
9460 .addReg(/*Segment=*/0)
9461 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +00009462 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +00009463 }
9464
Dan Gohman14152b42010-07-06 20:24:04 +00009465 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +00009466
9467 return EndMBB;
9468}
Mon P Wang63307c32008-05-05 19:05:59 +00009469
Evan Cheng60c07e12006-07-05 22:17:51 +00009470MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +00009471X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00009472 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +00009473 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9474 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +00009475
Chris Lattner52600972009-09-02 05:57:00 +00009476 // To "insert" a SELECT_CC instruction, we actually have to insert the
9477 // diamond control-flow pattern. The incoming instruction knows the
9478 // destination vreg to set, the condition code register to branch on, the
9479 // true/false values to select between, and a branch opcode to use.
9480 const BasicBlock *LLVM_BB = BB->getBasicBlock();
9481 MachineFunction::iterator It = BB;
9482 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +00009483
Chris Lattner52600972009-09-02 05:57:00 +00009484 // thisMBB:
9485 // ...
9486 // TrueVal = ...
9487 // cmpTY ccX, r1, r2
9488 // bCC copy1MBB
9489 // fallthrough --> copy0MBB
9490 MachineBasicBlock *thisMBB = BB;
9491 MachineFunction *F = BB->getParent();
9492 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
9493 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +00009494 F->insert(It, copy0MBB);
9495 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +00009496
Bill Wendling730c07e2010-06-25 20:48:10 +00009497 // If the EFLAGS register isn't dead in the terminator, then claim that it's
9498 // live into the sink and copy blocks.
9499 const MachineFunction *MF = BB->getParent();
9500 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
9501 BitVector ReservedRegs = TRI->getReservedRegs(*MF);
Bill Wendling730c07e2010-06-25 20:48:10 +00009502
Dan Gohman14152b42010-07-06 20:24:04 +00009503 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
9504 const MachineOperand &MO = MI->getOperand(I);
9505 if (!MO.isReg() || !MO.isUse() || MO.isKill()) continue;
Bill Wendling730c07e2010-06-25 20:48:10 +00009506 unsigned Reg = MO.getReg();
9507 if (Reg != X86::EFLAGS) continue;
9508 copy0MBB->addLiveIn(Reg);
9509 sinkMBB->addLiveIn(Reg);
9510 }
9511
Dan Gohman14152b42010-07-06 20:24:04 +00009512 // Transfer the remainder of BB and its successor edges to sinkMBB.
9513 sinkMBB->splice(sinkMBB->begin(), BB,
9514 llvm::next(MachineBasicBlock::iterator(MI)),
9515 BB->end());
9516 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
9517
9518 // Add the true and fallthrough blocks as its successors.
9519 BB->addSuccessor(copy0MBB);
9520 BB->addSuccessor(sinkMBB);
9521
9522 // Create the conditional branch instruction.
9523 unsigned Opc =
9524 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
9525 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
9526
Chris Lattner52600972009-09-02 05:57:00 +00009527 // copy0MBB:
9528 // %FalseValue = ...
9529 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +00009530 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00009531
Chris Lattner52600972009-09-02 05:57:00 +00009532 // sinkMBB:
9533 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
9534 // ...
Dan Gohman14152b42010-07-06 20:24:04 +00009535 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
9536 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +00009537 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
9538 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
9539
Dan Gohman14152b42010-07-06 20:24:04 +00009540 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +00009541 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +00009542}
9543
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00009544MachineBasicBlock *
9545X86TargetLowering::EmitLoweredMingwAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00009546 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00009547 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9548 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00009549
9550 // The lowering is pretty easy: we're just emitting the call to _alloca. The
9551 // non-trivial part is impdef of ESP.
9552 // FIXME: The code should be tweaked as soon as we'll try to do codegen for
9553 // mingw-w64.
9554
Dan Gohman14152b42010-07-06 20:24:04 +00009555 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00009556 .addExternalSymbol("_alloca")
9557 .addReg(X86::EAX, RegState::Implicit)
9558 .addReg(X86::ESP, RegState::Implicit)
9559 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
Anton Korobeynikov9f7f83b2010-08-25 07:50:11 +00009560 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
9561 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00009562
Dan Gohman14152b42010-07-06 20:24:04 +00009563 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00009564 return BB;
9565}
Chris Lattner52600972009-09-02 05:57:00 +00009566
9567MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +00009568X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
9569 MachineBasicBlock *BB) const {
9570 // This is pretty easy. We're taking the value that we received from
9571 // our load from the relocation, sticking it in either RDI (x86-64)
9572 // or EAX and doing an indirect call. The return value will then
9573 // be in the normal return register.
Eric Christopher54415362010-06-08 22:04:25 +00009574 const X86InstrInfo *TII
9575 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +00009576 DebugLoc DL = MI->getDebugLoc();
9577 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +00009578
9579 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +00009580 assert(MI->getOperand(3).isGlobal() && "This should be a global");
9581
Eric Christopher30ef0e52010-06-03 04:07:48 +00009582 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +00009583 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
9584 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +00009585 .addReg(X86::RIP)
9586 .addImm(0).addReg(0)
9587 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
9588 MI->getOperand(3).getTargetFlags())
9589 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +00009590 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +00009591 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +00009592 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +00009593 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
9594 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +00009595 .addReg(0)
9596 .addImm(0).addReg(0)
9597 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
9598 MI->getOperand(3).getTargetFlags())
9599 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +00009600 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +00009601 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +00009602 } else {
Dan Gohman14152b42010-07-06 20:24:04 +00009603 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
9604 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +00009605 .addReg(TII->getGlobalBaseReg(F))
9606 .addImm(0).addReg(0)
9607 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
9608 MI->getOperand(3).getTargetFlags())
9609 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +00009610 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +00009611 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +00009612 }
9613
Dan Gohman14152b42010-07-06 20:24:04 +00009614 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +00009615 return BB;
9616}
9617
9618MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00009619X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00009620 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00009621 switch (MI->getOpcode()) {
9622 default: assert(false && "Unexpected instr type to insert");
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00009623 case X86::MINGW_ALLOCA:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00009624 return EmitLoweredMingwAlloca(MI, BB);
Eric Christopher30ef0e52010-06-03 04:07:48 +00009625 case X86::TLSCall_32:
9626 case X86::TLSCall_64:
9627 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +00009628 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +00009629 case X86::CMOV_FR32:
9630 case X86::CMOV_FR64:
9631 case X86::CMOV_V4F32:
9632 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +00009633 case X86::CMOV_V2I64:
Chris Lattner314a1132010-03-14 18:31:44 +00009634 case X86::CMOV_GR16:
9635 case X86::CMOV_GR32:
9636 case X86::CMOV_RFP32:
9637 case X86::CMOV_RFP64:
9638 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00009639 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +00009640
Dale Johannesen849f2142007-07-03 00:53:03 +00009641 case X86::FP32_TO_INT16_IN_MEM:
9642 case X86::FP32_TO_INT32_IN_MEM:
9643 case X86::FP32_TO_INT64_IN_MEM:
9644 case X86::FP64_TO_INT16_IN_MEM:
9645 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00009646 case X86::FP64_TO_INT64_IN_MEM:
9647 case X86::FP80_TO_INT16_IN_MEM:
9648 case X86::FP80_TO_INT32_IN_MEM:
9649 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +00009650 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9651 DebugLoc DL = MI->getDebugLoc();
9652
Evan Cheng60c07e12006-07-05 22:17:51 +00009653 // Change the floating point control register to use "round towards zero"
9654 // mode when truncating to an integer value.
9655 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +00009656 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +00009657 addFrameReference(BuildMI(*BB, MI, DL,
9658 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00009659
9660 // Load the old value of the high byte of the control word...
9661 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +00009662 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +00009663 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009664 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00009665
9666 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +00009667 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00009668 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00009669
9670 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +00009671 addFrameReference(BuildMI(*BB, MI, DL,
9672 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00009673
9674 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +00009675 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00009676 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00009677
9678 // Get the X86 opcode to use.
9679 unsigned Opc;
9680 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009681 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00009682 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
9683 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
9684 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
9685 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
9686 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
9687 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00009688 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
9689 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
9690 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00009691 }
9692
9693 X86AddressMode AM;
9694 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +00009695 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00009696 AM.BaseType = X86AddressMode::RegBase;
9697 AM.Base.Reg = Op.getReg();
9698 } else {
9699 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +00009700 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +00009701 }
9702 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +00009703 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00009704 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00009705 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +00009706 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00009707 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00009708 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +00009709 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00009710 AM.GV = Op.getGlobal();
9711 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00009712 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00009713 }
Dan Gohman14152b42010-07-06 20:24:04 +00009714 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009715 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00009716
9717 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +00009718 addFrameReference(BuildMI(*BB, MI, DL,
9719 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00009720
Dan Gohman14152b42010-07-06 20:24:04 +00009721 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00009722 return BB;
9723 }
Eric Christopherb120ab42009-08-18 22:50:32 +00009724 // String/text processing lowering.
9725 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009726 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +00009727 return EmitPCMP(MI, BB, 3, false /* in-mem */);
9728 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009729 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +00009730 return EmitPCMP(MI, BB, 3, true /* in-mem */);
9731 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009732 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +00009733 return EmitPCMP(MI, BB, 5, false /* in mem */);
9734 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009735 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +00009736 return EmitPCMP(MI, BB, 5, true /* in mem */);
9737
9738 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +00009739 case X86::ATOMAND32:
9740 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00009741 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009742 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009743 X86::NOT32r, X86::EAX,
9744 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00009745 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +00009746 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
9747 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009748 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009749 X86::NOT32r, X86::EAX,
9750 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00009751 case X86::ATOMXOR32:
9752 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00009753 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009754 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009755 X86::NOT32r, X86::EAX,
9756 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009757 case X86::ATOMNAND32:
9758 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009759 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009760 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009761 X86::NOT32r, X86::EAX,
9762 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +00009763 case X86::ATOMMIN32:
9764 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
9765 case X86::ATOMMAX32:
9766 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
9767 case X86::ATOMUMIN32:
9768 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
9769 case X86::ATOMUMAX32:
9770 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +00009771
9772 case X86::ATOMAND16:
9773 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
9774 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009775 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009776 X86::NOT16r, X86::AX,
9777 X86::GR16RegisterClass);
9778 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +00009779 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009780 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009781 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009782 X86::NOT16r, X86::AX,
9783 X86::GR16RegisterClass);
9784 case X86::ATOMXOR16:
9785 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
9786 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009787 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009788 X86::NOT16r, X86::AX,
9789 X86::GR16RegisterClass);
9790 case X86::ATOMNAND16:
9791 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
9792 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009793 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009794 X86::NOT16r, X86::AX,
9795 X86::GR16RegisterClass, true);
9796 case X86::ATOMMIN16:
9797 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
9798 case X86::ATOMMAX16:
9799 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
9800 case X86::ATOMUMIN16:
9801 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
9802 case X86::ATOMUMAX16:
9803 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
9804
9805 case X86::ATOMAND8:
9806 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
9807 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009808 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009809 X86::NOT8r, X86::AL,
9810 X86::GR8RegisterClass);
9811 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +00009812 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009813 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009814 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009815 X86::NOT8r, X86::AL,
9816 X86::GR8RegisterClass);
9817 case X86::ATOMXOR8:
9818 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
9819 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009820 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009821 X86::NOT8r, X86::AL,
9822 X86::GR8RegisterClass);
9823 case X86::ATOMNAND8:
9824 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
9825 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009826 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009827 X86::NOT8r, X86::AL,
9828 X86::GR8RegisterClass, true);
9829 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009830 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +00009831 case X86::ATOMAND64:
9832 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00009833 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009834 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +00009835 X86::NOT64r, X86::RAX,
9836 X86::GR64RegisterClass);
9837 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +00009838 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
9839 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009840 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +00009841 X86::NOT64r, X86::RAX,
9842 X86::GR64RegisterClass);
9843 case X86::ATOMXOR64:
9844 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00009845 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009846 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +00009847 X86::NOT64r, X86::RAX,
9848 X86::GR64RegisterClass);
9849 case X86::ATOMNAND64:
9850 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
9851 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009852 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +00009853 X86::NOT64r, X86::RAX,
9854 X86::GR64RegisterClass, true);
9855 case X86::ATOMMIN64:
9856 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
9857 case X86::ATOMMAX64:
9858 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
9859 case X86::ATOMUMIN64:
9860 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
9861 case X86::ATOMUMAX64:
9862 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009863
9864 // This group does 64-bit operations on a 32-bit host.
9865 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00009866 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009867 X86::AND32rr, X86::AND32rr,
9868 X86::AND32ri, X86::AND32ri,
9869 false);
9870 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00009871 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009872 X86::OR32rr, X86::OR32rr,
9873 X86::OR32ri, X86::OR32ri,
9874 false);
9875 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00009876 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009877 X86::XOR32rr, X86::XOR32rr,
9878 X86::XOR32ri, X86::XOR32ri,
9879 false);
9880 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00009881 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009882 X86::AND32rr, X86::AND32rr,
9883 X86::AND32ri, X86::AND32ri,
9884 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009885 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00009886 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009887 X86::ADD32rr, X86::ADC32rr,
9888 X86::ADD32ri, X86::ADC32ri,
9889 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009890 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00009891 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009892 X86::SUB32rr, X86::SBB32rr,
9893 X86::SUB32ri, X86::SBB32ri,
9894 false);
Dale Johannesen880ae362008-10-03 22:25:52 +00009895 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00009896 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +00009897 X86::MOV32rr, X86::MOV32rr,
9898 X86::MOV32ri, X86::MOV32ri,
9899 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +00009900 case X86::VASTART_SAVE_XMM_REGS:
9901 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +00009902 }
9903}
9904
9905//===----------------------------------------------------------------------===//
9906// X86 Optimization Hooks
9907//===----------------------------------------------------------------------===//
9908
Dan Gohman475871a2008-07-27 21:46:04 +00009909void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00009910 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00009911 APInt &KnownZero,
9912 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00009913 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00009914 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009915 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00009916 assert((Opc >= ISD::BUILTIN_OP_END ||
9917 Opc == ISD::INTRINSIC_WO_CHAIN ||
9918 Opc == ISD::INTRINSIC_W_CHAIN ||
9919 Opc == ISD::INTRINSIC_VOID) &&
9920 "Should use MaskedValueIsZero if you don't know whether Op"
9921 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009922
Dan Gohmanf4f92f52008-02-13 23:07:24 +00009923 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009924 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00009925 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +00009926 case X86ISD::ADD:
9927 case X86ISD::SUB:
9928 case X86ISD::SMUL:
9929 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +00009930 case X86ISD::INC:
9931 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +00009932 case X86ISD::OR:
9933 case X86ISD::XOR:
9934 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +00009935 // These nodes' second result is a boolean.
9936 if (Op.getResNo() == 0)
9937 break;
9938 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009939 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00009940 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
9941 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +00009942 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009943 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009944}
Chris Lattner259e97c2006-01-31 19:43:35 +00009945
Owen Andersonbc146b02010-09-21 20:42:50 +00009946unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
9947 unsigned Depth) const {
9948 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
9949 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
9950 return Op.getValueType().getScalarType().getSizeInBits();
9951
9952 // Fallback case.
9953 return 1;
9954}
9955
Evan Cheng206ee9d2006-07-07 08:33:52 +00009956/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +00009957/// node is a GlobalAddress + offset.
9958bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +00009959 const GlobalValue* &GA,
9960 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +00009961 if (N->getOpcode() == X86ISD::Wrapper) {
9962 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00009963 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00009964 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +00009965 return true;
9966 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00009967 }
Evan Chengad4196b2008-05-12 19:56:52 +00009968 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +00009969}
9970
Evan Cheng206ee9d2006-07-07 08:33:52 +00009971/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
9972/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
9973/// if the load addresses are consecutive, non-overlapping, and in the right
Nate Begemanfdea31a2010-03-24 20:49:50 +00009974/// order.
Dan Gohman475871a2008-07-27 21:46:04 +00009975static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00009976 const TargetLowering &TLI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009977 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00009978 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +00009979
Eli Friedman7a5e5552009-06-07 06:52:44 +00009980 if (VT.getSizeInBits() != 128)
9981 return SDValue();
9982
Nate Begemanfdea31a2010-03-24 20:49:50 +00009983 SmallVector<SDValue, 16> Elts;
9984 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00009985 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00009986
Nate Begemanfdea31a2010-03-24 20:49:50 +00009987 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00009988}
Evan Chengd880b972008-05-09 21:53:03 +00009989
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +00009990/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
9991/// generation and convert it from being a bunch of shuffles and extracts
9992/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +00009993static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
9994 const TargetLowering &TLI) {
9995 SDValue InputVector = N->getOperand(0);
9996
9997 // Only operate on vectors of 4 elements, where the alternative shuffling
9998 // gets to be more expensive.
9999 if (InputVector.getValueType() != MVT::v4i32)
10000 return SDValue();
10001
10002 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
10003 // single use which is a sign-extend or zero-extend, and all elements are
10004 // used.
10005 SmallVector<SDNode *, 4> Uses;
10006 unsigned ExtractedElements = 0;
10007 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
10008 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
10009 if (UI.getUse().getResNo() != InputVector.getResNo())
10010 return SDValue();
10011
10012 SDNode *Extract = *UI;
10013 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
10014 return SDValue();
10015
10016 if (Extract->getValueType(0) != MVT::i32)
10017 return SDValue();
10018 if (!Extract->hasOneUse())
10019 return SDValue();
10020 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
10021 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
10022 return SDValue();
10023 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
10024 return SDValue();
10025
10026 // Record which element was extracted.
10027 ExtractedElements |=
10028 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
10029
10030 Uses.push_back(Extract);
10031 }
10032
10033 // If not all the elements were used, this may not be worthwhile.
10034 if (ExtractedElements != 15)
10035 return SDValue();
10036
10037 // Ok, we've now decided to do the transformation.
10038 DebugLoc dl = InputVector.getDebugLoc();
10039
10040 // Store the value to a temporary stack slot.
10041 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010042 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
10043 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010044
10045 // Replace each use (extract) with a load of the appropriate element.
10046 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
10047 UE = Uses.end(); UI != UE; ++UI) {
10048 SDNode *Extract = *UI;
10049
10050 // Compute the element's address.
10051 SDValue Idx = Extract->getOperand(1);
10052 unsigned EltSize =
10053 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
10054 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
10055 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
10056
Eric Christopher90eb4022010-07-22 00:26:08 +000010057 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(),
Chris Lattner51abfe42010-09-21 06:02:19 +000010058 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010059
10060 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000010061 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000010062 ScalarAddr, MachinePointerInfo(),
10063 false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010064
10065 // Replace the exact with the load.
10066 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
10067 }
10068
10069 // The replacement was made in place; don't return anything.
10070 return SDValue();
10071}
10072
Chris Lattner83e6c992006-10-04 06:57:07 +000010073/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000010074static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000010075 const X86Subtarget *Subtarget) {
10076 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000010077 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000010078 // Get the LHS/RHS of the select.
10079 SDValue LHS = N->getOperand(1);
10080 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +000010081
Dan Gohman670e5392009-09-21 18:03:22 +000010082 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000010083 // instructions match the semantics of the common C idiom x<y?x:y but not
10084 // x<=y?x:y, because of how they handle negative zero (which can be
10085 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +000010086 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +000010087 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +000010088 Cond.getOpcode() == ISD::SETCC) {
10089 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010090
Chris Lattner47b4ce82009-03-11 05:48:52 +000010091 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000010092 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000010093 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
10094 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000010095 switch (CC) {
10096 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000010097 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000010098 // Converting this to a min would handle NaNs incorrectly, and swapping
10099 // the operands would cause it to handle comparisons between positive
10100 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010101 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000010102 if (!UnsafeFPMath &&
10103 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
10104 break;
10105 std::swap(LHS, RHS);
10106 }
Dan Gohman670e5392009-09-21 18:03:22 +000010107 Opcode = X86ISD::FMIN;
10108 break;
10109 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000010110 // Converting this to a min would handle comparisons between positive
10111 // and negative zero incorrectly.
10112 if (!UnsafeFPMath &&
10113 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
10114 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010115 Opcode = X86ISD::FMIN;
10116 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000010117 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000010118 // Converting this to a min would handle both negative zeros and NaNs
10119 // incorrectly, but we can swap the operands to fix both.
10120 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010121 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010122 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000010123 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010124 Opcode = X86ISD::FMIN;
10125 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010126
Dan Gohman670e5392009-09-21 18:03:22 +000010127 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010128 // Converting this to a max would handle comparisons between positive
10129 // and negative zero incorrectly.
10130 if (!UnsafeFPMath &&
10131 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
10132 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010133 Opcode = X86ISD::FMAX;
10134 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000010135 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000010136 // Converting this to a max would handle NaNs incorrectly, and swapping
10137 // the operands would cause it to handle comparisons between positive
10138 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010139 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000010140 if (!UnsafeFPMath &&
10141 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
10142 break;
10143 std::swap(LHS, RHS);
10144 }
Dan Gohman670e5392009-09-21 18:03:22 +000010145 Opcode = X86ISD::FMAX;
10146 break;
10147 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010148 // Converting this to a max would handle both negative zeros and NaNs
10149 // incorrectly, but we can swap the operands to fix both.
10150 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010151 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010152 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010153 case ISD::SETGE:
10154 Opcode = X86ISD::FMAX;
10155 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000010156 }
Dan Gohman670e5392009-09-21 18:03:22 +000010157 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000010158 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
10159 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000010160 switch (CC) {
10161 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000010162 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010163 // Converting this to a min would handle comparisons between positive
10164 // and negative zero incorrectly, and swapping the operands would
10165 // cause it to handle NaNs incorrectly.
10166 if (!UnsafeFPMath &&
10167 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000010168 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000010169 break;
10170 std::swap(LHS, RHS);
10171 }
Dan Gohman670e5392009-09-21 18:03:22 +000010172 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000010173 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010174 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000010175 // Converting this to a min would handle NaNs incorrectly.
10176 if (!UnsafeFPMath &&
10177 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
10178 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010179 Opcode = X86ISD::FMIN;
10180 break;
10181 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010182 // Converting this to a min would handle both negative zeros and NaNs
10183 // incorrectly, but we can swap the operands to fix both.
10184 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010185 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010186 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010187 case ISD::SETGE:
10188 Opcode = X86ISD::FMIN;
10189 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010190
Dan Gohman670e5392009-09-21 18:03:22 +000010191 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000010192 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010193 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000010194 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010195 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000010196 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010197 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000010198 // Converting this to a max would handle comparisons between positive
10199 // and negative zero incorrectly, and swapping the operands would
10200 // cause it to handle NaNs incorrectly.
10201 if (!UnsafeFPMath &&
10202 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000010203 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000010204 break;
10205 std::swap(LHS, RHS);
10206 }
Dan Gohman670e5392009-09-21 18:03:22 +000010207 Opcode = X86ISD::FMAX;
10208 break;
10209 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000010210 // Converting this to a max would handle both negative zeros and NaNs
10211 // incorrectly, but we can swap the operands to fix both.
10212 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010213 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010214 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000010215 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010216 Opcode = X86ISD::FMAX;
10217 break;
10218 }
Chris Lattner83e6c992006-10-04 06:57:07 +000010219 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010220
Chris Lattner47b4ce82009-03-11 05:48:52 +000010221 if (Opcode)
10222 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000010223 }
Eric Christopherfd179292009-08-27 18:07:15 +000010224
Chris Lattnerd1980a52009-03-12 06:52:53 +000010225 // If this is a select between two integer constants, try to do some
10226 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000010227 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
10228 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000010229 // Don't do this for crazy integer types.
10230 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
10231 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000010232 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000010233 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000010234
Chris Lattnercee56e72009-03-13 05:53:31 +000010235 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000010236 // Efficiently invertible.
10237 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
10238 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
10239 isa<ConstantSDNode>(Cond.getOperand(1))))) {
10240 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000010241 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000010242 }
Eric Christopherfd179292009-08-27 18:07:15 +000010243
Chris Lattnerd1980a52009-03-12 06:52:53 +000010244 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000010245 if (FalseC->getAPIntValue() == 0 &&
10246 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000010247 if (NeedsCondInvert) // Invert the condition if needed.
10248 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
10249 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010250
Chris Lattnerd1980a52009-03-12 06:52:53 +000010251 // Zero extend the condition if needed.
10252 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010253
Chris Lattnercee56e72009-03-13 05:53:31 +000010254 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000010255 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000010256 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000010257 }
Eric Christopherfd179292009-08-27 18:07:15 +000010258
Chris Lattner97a29a52009-03-13 05:22:11 +000010259 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000010260 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000010261 if (NeedsCondInvert) // Invert the condition if needed.
10262 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
10263 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010264
Chris Lattner97a29a52009-03-13 05:22:11 +000010265 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000010266 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
10267 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000010268 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000010269 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000010270 }
Eric Christopherfd179292009-08-27 18:07:15 +000010271
Chris Lattnercee56e72009-03-13 05:53:31 +000010272 // Optimize cases that will turn into an LEA instruction. This requires
10273 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000010274 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000010275 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000010276 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000010277
Chris Lattnercee56e72009-03-13 05:53:31 +000010278 bool isFastMultiplier = false;
10279 if (Diff < 10) {
10280 switch ((unsigned char)Diff) {
10281 default: break;
10282 case 1: // result = add base, cond
10283 case 2: // result = lea base( , cond*2)
10284 case 3: // result = lea base(cond, cond*2)
10285 case 4: // result = lea base( , cond*4)
10286 case 5: // result = lea base(cond, cond*4)
10287 case 8: // result = lea base( , cond*8)
10288 case 9: // result = lea base(cond, cond*8)
10289 isFastMultiplier = true;
10290 break;
10291 }
10292 }
Eric Christopherfd179292009-08-27 18:07:15 +000010293
Chris Lattnercee56e72009-03-13 05:53:31 +000010294 if (isFastMultiplier) {
10295 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
10296 if (NeedsCondInvert) // Invert the condition if needed.
10297 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
10298 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010299
Chris Lattnercee56e72009-03-13 05:53:31 +000010300 // Zero extend the condition if needed.
10301 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
10302 Cond);
10303 // Scale the condition by the difference.
10304 if (Diff != 1)
10305 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
10306 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010307
Chris Lattnercee56e72009-03-13 05:53:31 +000010308 // Add the base if non-zero.
10309 if (FalseC->getAPIntValue() != 0)
10310 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
10311 SDValue(FalseC, 0));
10312 return Cond;
10313 }
Eric Christopherfd179292009-08-27 18:07:15 +000010314 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000010315 }
10316 }
Eric Christopherfd179292009-08-27 18:07:15 +000010317
Dan Gohman475871a2008-07-27 21:46:04 +000010318 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000010319}
10320
Chris Lattnerd1980a52009-03-12 06:52:53 +000010321/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
10322static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
10323 TargetLowering::DAGCombinerInfo &DCI) {
10324 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000010325
Chris Lattnerd1980a52009-03-12 06:52:53 +000010326 // If the flag operand isn't dead, don't touch this CMOV.
10327 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
10328 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000010329
Chris Lattnerd1980a52009-03-12 06:52:53 +000010330 // If this is a select between two integer constants, try to do some
10331 // optimizations. Note that the operands are ordered the opposite of SELECT
10332 // operands.
10333 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
10334 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
10335 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
10336 // larger than FalseC (the false value).
10337 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
Eric Christopherfd179292009-08-27 18:07:15 +000010338
Chris Lattnerd1980a52009-03-12 06:52:53 +000010339 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
10340 CC = X86::GetOppositeBranchCondition(CC);
10341 std::swap(TrueC, FalseC);
10342 }
Eric Christopherfd179292009-08-27 18:07:15 +000010343
Chris Lattnerd1980a52009-03-12 06:52:53 +000010344 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000010345 // This is efficient for any integer data type (including i8/i16) and
10346 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000010347 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
10348 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000010349 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10350 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010351
Chris Lattnerd1980a52009-03-12 06:52:53 +000010352 // Zero extend the condition if needed.
10353 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010354
Chris Lattnerd1980a52009-03-12 06:52:53 +000010355 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
10356 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000010357 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000010358 if (N->getNumValues() == 2) // Dead flag value?
10359 return DCI.CombineTo(N, Cond, SDValue());
10360 return Cond;
10361 }
Eric Christopherfd179292009-08-27 18:07:15 +000010362
Chris Lattnercee56e72009-03-13 05:53:31 +000010363 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
10364 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000010365 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
10366 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000010367 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10368 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010369
Chris Lattner97a29a52009-03-13 05:22:11 +000010370 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000010371 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
10372 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000010373 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
10374 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000010375
Chris Lattner97a29a52009-03-13 05:22:11 +000010376 if (N->getNumValues() == 2) // Dead flag value?
10377 return DCI.CombineTo(N, Cond, SDValue());
10378 return Cond;
10379 }
Eric Christopherfd179292009-08-27 18:07:15 +000010380
Chris Lattnercee56e72009-03-13 05:53:31 +000010381 // Optimize cases that will turn into an LEA instruction. This requires
10382 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000010383 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000010384 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000010385 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000010386
Chris Lattnercee56e72009-03-13 05:53:31 +000010387 bool isFastMultiplier = false;
10388 if (Diff < 10) {
10389 switch ((unsigned char)Diff) {
10390 default: break;
10391 case 1: // result = add base, cond
10392 case 2: // result = lea base( , cond*2)
10393 case 3: // result = lea base(cond, cond*2)
10394 case 4: // result = lea base( , cond*4)
10395 case 5: // result = lea base(cond, cond*4)
10396 case 8: // result = lea base( , cond*8)
10397 case 9: // result = lea base(cond, cond*8)
10398 isFastMultiplier = true;
10399 break;
10400 }
10401 }
Eric Christopherfd179292009-08-27 18:07:15 +000010402
Chris Lattnercee56e72009-03-13 05:53:31 +000010403 if (isFastMultiplier) {
10404 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
10405 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000010406 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10407 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000010408 // Zero extend the condition if needed.
10409 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
10410 Cond);
10411 // Scale the condition by the difference.
10412 if (Diff != 1)
10413 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
10414 DAG.getConstant(Diff, Cond.getValueType()));
10415
10416 // Add the base if non-zero.
10417 if (FalseC->getAPIntValue() != 0)
10418 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
10419 SDValue(FalseC, 0));
10420 if (N->getNumValues() == 2) // Dead flag value?
10421 return DCI.CombineTo(N, Cond, SDValue());
10422 return Cond;
10423 }
Eric Christopherfd179292009-08-27 18:07:15 +000010424 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000010425 }
10426 }
10427 return SDValue();
10428}
10429
10430
Evan Cheng0b0cd912009-03-28 05:57:29 +000010431/// PerformMulCombine - Optimize a single multiply with constant into two
10432/// in order to implement it with two cheaper instructions, e.g.
10433/// LEA + SHL, LEA + LEA.
10434static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
10435 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000010436 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
10437 return SDValue();
10438
Owen Andersone50ed302009-08-10 22:56:29 +000010439 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010440 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000010441 return SDValue();
10442
10443 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
10444 if (!C)
10445 return SDValue();
10446 uint64_t MulAmt = C->getZExtValue();
10447 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
10448 return SDValue();
10449
10450 uint64_t MulAmt1 = 0;
10451 uint64_t MulAmt2 = 0;
10452 if ((MulAmt % 9) == 0) {
10453 MulAmt1 = 9;
10454 MulAmt2 = MulAmt / 9;
10455 } else if ((MulAmt % 5) == 0) {
10456 MulAmt1 = 5;
10457 MulAmt2 = MulAmt / 5;
10458 } else if ((MulAmt % 3) == 0) {
10459 MulAmt1 = 3;
10460 MulAmt2 = MulAmt / 3;
10461 }
10462 if (MulAmt2 &&
10463 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
10464 DebugLoc DL = N->getDebugLoc();
10465
10466 if (isPowerOf2_64(MulAmt2) &&
10467 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
10468 // If second multiplifer is pow2, issue it first. We want the multiply by
10469 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
10470 // is an add.
10471 std::swap(MulAmt1, MulAmt2);
10472
10473 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000010474 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000010475 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000010476 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000010477 else
Evan Cheng73f24c92009-03-30 21:36:47 +000010478 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000010479 DAG.getConstant(MulAmt1, VT));
10480
Eric Christopherfd179292009-08-27 18:07:15 +000010481 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000010482 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000010483 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000010484 else
Evan Cheng73f24c92009-03-30 21:36:47 +000010485 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000010486 DAG.getConstant(MulAmt2, VT));
10487
10488 // Do not add new nodes to DAG combiner worklist.
10489 DCI.CombineTo(N, NewMul, false);
10490 }
10491 return SDValue();
10492}
10493
Evan Chengad9c0a32009-12-15 00:53:42 +000010494static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
10495 SDValue N0 = N->getOperand(0);
10496 SDValue N1 = N->getOperand(1);
10497 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
10498 EVT VT = N0.getValueType();
10499
10500 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
10501 // since the result of setcc_c is all zero's or all ones.
10502 if (N1C && N0.getOpcode() == ISD::AND &&
10503 N0.getOperand(1).getOpcode() == ISD::Constant) {
10504 SDValue N00 = N0.getOperand(0);
10505 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
10506 ((N00.getOpcode() == ISD::ANY_EXTEND ||
10507 N00.getOpcode() == ISD::ZERO_EXTEND) &&
10508 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
10509 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
10510 APInt ShAmt = N1C->getAPIntValue();
10511 Mask = Mask.shl(ShAmt);
10512 if (Mask != 0)
10513 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
10514 N00, DAG.getConstant(Mask, VT));
10515 }
10516 }
10517
10518 return SDValue();
10519}
Evan Cheng0b0cd912009-03-28 05:57:29 +000010520
Nate Begeman740ab032009-01-26 00:52:55 +000010521/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
10522/// when possible.
10523static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
10524 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000010525 EVT VT = N->getValueType(0);
10526 if (!VT.isVector() && VT.isInteger() &&
10527 N->getOpcode() == ISD::SHL)
10528 return PerformSHLCombine(N, DAG);
10529
Nate Begeman740ab032009-01-26 00:52:55 +000010530 // On X86 with SSE2 support, we can transform this to a vector shift if
10531 // all elements are shifted by the same amount. We can't do this in legalize
10532 // because the a constant vector is typically transformed to a constant pool
10533 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +000010534 if (!Subtarget->hasSSE2())
10535 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000010536
Owen Anderson825b72b2009-08-11 20:47:22 +000010537 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +000010538 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000010539
Mon P Wang3becd092009-01-28 08:12:05 +000010540 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000010541 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000010542 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000010543 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000010544 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
10545 unsigned NumElts = VT.getVectorNumElements();
10546 unsigned i = 0;
10547 for (; i != NumElts; ++i) {
10548 SDValue Arg = ShAmtOp.getOperand(i);
10549 if (Arg.getOpcode() == ISD::UNDEF) continue;
10550 BaseShAmt = Arg;
10551 break;
10552 }
10553 for (; i != NumElts; ++i) {
10554 SDValue Arg = ShAmtOp.getOperand(i);
10555 if (Arg.getOpcode() == ISD::UNDEF) continue;
10556 if (Arg != BaseShAmt) {
10557 return SDValue();
10558 }
10559 }
10560 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000010561 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000010562 SDValue InVec = ShAmtOp.getOperand(0);
10563 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
10564 unsigned NumElts = InVec.getValueType().getVectorNumElements();
10565 unsigned i = 0;
10566 for (; i != NumElts; ++i) {
10567 SDValue Arg = InVec.getOperand(i);
10568 if (Arg.getOpcode() == ISD::UNDEF) continue;
10569 BaseShAmt = Arg;
10570 break;
10571 }
10572 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
10573 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000010574 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000010575 if (C->getZExtValue() == SplatIdx)
10576 BaseShAmt = InVec.getOperand(1);
10577 }
10578 }
10579 if (BaseShAmt.getNode() == 0)
10580 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
10581 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000010582 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000010583 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000010584
Mon P Wangefa42202009-09-03 19:56:25 +000010585 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000010586 if (EltVT.bitsGT(MVT::i32))
10587 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
10588 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000010589 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000010590
Nate Begemanc2fd67f2009-01-26 03:15:31 +000010591 // The shift amount is identical so we can do a vector shift.
10592 SDValue ValOp = N->getOperand(0);
10593 switch (N->getOpcode()) {
10594 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000010595 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000010596 break;
10597 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000010598 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000010599 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010600 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000010601 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000010602 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000010603 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010604 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000010605 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000010606 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000010607 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010608 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000010609 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000010610 break;
10611 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000010612 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000010613 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010614 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000010615 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000010616 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000010617 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010618 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000010619 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000010620 break;
10621 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000010622 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000010623 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010624 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000010625 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000010626 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000010627 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010628 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000010629 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000010630 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000010631 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010632 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000010633 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000010634 break;
Nate Begeman740ab032009-01-26 00:52:55 +000010635 }
10636 return SDValue();
10637}
10638
Evan Cheng760d1942010-01-04 21:22:48 +000010639static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000010640 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000010641 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000010642 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000010643 return SDValue();
10644
Evan Cheng760d1942010-01-04 21:22:48 +000010645 EVT VT = N->getValueType(0);
Evan Cheng8b1190a2010-04-28 01:18:01 +000010646 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
Evan Cheng760d1942010-01-04 21:22:48 +000010647 return SDValue();
10648
10649 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
10650 SDValue N0 = N->getOperand(0);
10651 SDValue N1 = N->getOperand(1);
10652 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
10653 std::swap(N0, N1);
10654 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
10655 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000010656 if (!N0.hasOneUse() || !N1.hasOneUse())
10657 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000010658
10659 SDValue ShAmt0 = N0.getOperand(1);
10660 if (ShAmt0.getValueType() != MVT::i8)
10661 return SDValue();
10662 SDValue ShAmt1 = N1.getOperand(1);
10663 if (ShAmt1.getValueType() != MVT::i8)
10664 return SDValue();
10665 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
10666 ShAmt0 = ShAmt0.getOperand(0);
10667 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
10668 ShAmt1 = ShAmt1.getOperand(0);
10669
10670 DebugLoc DL = N->getDebugLoc();
10671 unsigned Opc = X86ISD::SHLD;
10672 SDValue Op0 = N0.getOperand(0);
10673 SDValue Op1 = N1.getOperand(0);
10674 if (ShAmt0.getOpcode() == ISD::SUB) {
10675 Opc = X86ISD::SHRD;
10676 std::swap(Op0, Op1);
10677 std::swap(ShAmt0, ShAmt1);
10678 }
10679
Evan Cheng8b1190a2010-04-28 01:18:01 +000010680 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000010681 if (ShAmt1.getOpcode() == ISD::SUB) {
10682 SDValue Sum = ShAmt1.getOperand(0);
10683 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000010684 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
10685 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
10686 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
10687 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000010688 return DAG.getNode(Opc, DL, VT,
10689 Op0, Op1,
10690 DAG.getNode(ISD::TRUNCATE, DL,
10691 MVT::i8, ShAmt0));
10692 }
10693 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
10694 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
10695 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000010696 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000010697 return DAG.getNode(Opc, DL, VT,
10698 N0.getOperand(0), N1.getOperand(0),
10699 DAG.getNode(ISD::TRUNCATE, DL,
10700 MVT::i8, ShAmt0));
10701 }
10702
10703 return SDValue();
10704}
10705
Chris Lattner149a4e52008-02-22 02:09:43 +000010706/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000010707static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000010708 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +000010709 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
10710 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000010711 // A preferable solution to the general problem is to figure out the right
10712 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000010713
10714 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +000010715 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +000010716 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +000010717 if (VT.getSizeInBits() != 64)
10718 return SDValue();
10719
Devang Patel578efa92009-06-05 21:57:13 +000010720 const Function *F = DAG.getMachineFunction().getFunction();
10721 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000010722 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +000010723 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000010724 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000010725 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000010726 isa<LoadSDNode>(St->getValue()) &&
10727 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
10728 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000010729 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000010730 LoadSDNode *Ld = 0;
10731 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000010732 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000010733 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000010734 // Must be a store of a load. We currently handle two cases: the load
10735 // is a direct child, and it's under an intervening TokenFactor. It is
10736 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000010737 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000010738 Ld = cast<LoadSDNode>(St->getChain());
10739 else if (St->getValue().hasOneUse() &&
10740 ChainVal->getOpcode() == ISD::TokenFactor) {
10741 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000010742 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000010743 TokenFactorIndex = i;
10744 Ld = cast<LoadSDNode>(St->getValue());
10745 } else
10746 Ops.push_back(ChainVal->getOperand(i));
10747 }
10748 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000010749
Evan Cheng536e6672009-03-12 05:59:15 +000010750 if (!Ld || !ISD::isNormalLoad(Ld))
10751 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000010752
Evan Cheng536e6672009-03-12 05:59:15 +000010753 // If this is not the MMX case, i.e. we are just turning i64 load/store
10754 // into f64 load/store, avoid the transformation if there are multiple
10755 // uses of the loaded value.
10756 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
10757 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000010758
Evan Cheng536e6672009-03-12 05:59:15 +000010759 DebugLoc LdDL = Ld->getDebugLoc();
10760 DebugLoc StDL = N->getDebugLoc();
10761 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
10762 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
10763 // pair instead.
10764 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000010765 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000010766 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
10767 Ld->getPointerInfo(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000010768 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000010769 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000010770 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000010771 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000010772 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000010773 Ops.size());
10774 }
Evan Cheng536e6672009-03-12 05:59:15 +000010775 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000010776 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000010777 St->isVolatile(), St->isNonTemporal(),
10778 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000010779 }
Evan Cheng536e6672009-03-12 05:59:15 +000010780
10781 // Otherwise, lower to two pairs of 32-bit loads / stores.
10782 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000010783 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
10784 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000010785
Owen Anderson825b72b2009-08-11 20:47:22 +000010786 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000010787 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000010788 Ld->isVolatile(), Ld->isNonTemporal(),
10789 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000010790 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000010791 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000010792 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000010793 MinAlign(Ld->getAlignment(), 4));
10794
10795 SDValue NewChain = LoLd.getValue(1);
10796 if (TokenFactorIndex != -1) {
10797 Ops.push_back(LoLd);
10798 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000010799 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000010800 Ops.size());
10801 }
10802
10803 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000010804 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
10805 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000010806
10807 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000010808 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000010809 St->isVolatile(), St->isNonTemporal(),
10810 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000010811 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000010812 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000010813 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000010814 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000010815 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000010816 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000010817 }
Dan Gohman475871a2008-07-27 21:46:04 +000010818 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000010819}
10820
Chris Lattner6cf73262008-01-25 06:14:17 +000010821/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
10822/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000010823static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000010824 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
10825 // F[X]OR(0.0, x) -> x
10826 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000010827 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
10828 if (C->getValueAPF().isPosZero())
10829 return N->getOperand(1);
10830 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
10831 if (C->getValueAPF().isPosZero())
10832 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000010833 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000010834}
10835
10836/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000010837static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000010838 // FAND(0.0, x) -> 0.0
10839 // FAND(x, 0.0) -> 0.0
10840 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
10841 if (C->getValueAPF().isPosZero())
10842 return N->getOperand(0);
10843 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
10844 if (C->getValueAPF().isPosZero())
10845 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000010846 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000010847}
10848
Dan Gohmane5af2d32009-01-29 01:59:02 +000010849static SDValue PerformBTCombine(SDNode *N,
10850 SelectionDAG &DAG,
10851 TargetLowering::DAGCombinerInfo &DCI) {
10852 // BT ignores high bits in the bit index operand.
10853 SDValue Op1 = N->getOperand(1);
10854 if (Op1.hasOneUse()) {
10855 unsigned BitWidth = Op1.getValueSizeInBits();
10856 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
10857 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000010858 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
10859 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000010860 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000010861 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
10862 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
10863 DCI.CommitTargetLoweringOpt(TLO);
10864 }
10865 return SDValue();
10866}
Chris Lattner83e6c992006-10-04 06:57:07 +000010867
Eli Friedman7a5e5552009-06-07 06:52:44 +000010868static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
10869 SDValue Op = N->getOperand(0);
10870 if (Op.getOpcode() == ISD::BIT_CONVERT)
10871 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000010872 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000010873 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000010874 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000010875 OpVT.getVectorElementType().getSizeInBits()) {
10876 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT, Op);
10877 }
10878 return SDValue();
10879}
10880
Evan Cheng2e489c42009-12-16 00:53:11 +000010881static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
10882 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
10883 // (and (i32 x86isd::setcc_carry), 1)
10884 // This eliminates the zext. This transformation is necessary because
10885 // ISD::SETCC is always legalized to i8.
10886 DebugLoc dl = N->getDebugLoc();
10887 SDValue N0 = N->getOperand(0);
10888 EVT VT = N->getValueType(0);
10889 if (N0.getOpcode() == ISD::AND &&
10890 N0.hasOneUse() &&
10891 N0.getOperand(0).hasOneUse()) {
10892 SDValue N00 = N0.getOperand(0);
10893 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
10894 return SDValue();
10895 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
10896 if (!C || C->getZExtValue() != 1)
10897 return SDValue();
10898 return DAG.getNode(ISD::AND, dl, VT,
10899 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
10900 N00.getOperand(0), N00.getOperand(1)),
10901 DAG.getConstant(1, VT));
10902 }
10903
10904 return SDValue();
10905}
10906
Dan Gohman475871a2008-07-27 21:46:04 +000010907SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000010908 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000010909 SelectionDAG &DAG = DCI.DAG;
10910 switch (N->getOpcode()) {
10911 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010912 case ISD::EXTRACT_VECTOR_ELT:
10913 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +000010914 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000010915 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000010916 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000010917 case ISD::SHL:
10918 case ISD::SRA:
10919 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000010920 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000010921 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000010922 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000010923 case X86ISD::FOR: return PerformFORCombine(N, DAG);
10924 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000010925 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000010926 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000010927 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000010928 case X86ISD::SHUFPS: // Handle all target specific shuffles
10929 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000010930 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000010931 case X86ISD::PUNPCKHBW:
10932 case X86ISD::PUNPCKHWD:
10933 case X86ISD::PUNPCKHDQ:
10934 case X86ISD::PUNPCKHQDQ:
10935 case X86ISD::UNPCKHPS:
10936 case X86ISD::UNPCKHPD:
10937 case X86ISD::PUNPCKLBW:
10938 case X86ISD::PUNPCKLWD:
10939 case X86ISD::PUNPCKLDQ:
10940 case X86ISD::PUNPCKLQDQ:
10941 case X86ISD::UNPCKLPS:
10942 case X86ISD::UNPCKLPD:
10943 case X86ISD::MOVHLPS:
10944 case X86ISD::MOVLHPS:
10945 case X86ISD::PSHUFD:
10946 case X86ISD::PSHUFHW:
10947 case X86ISD::PSHUFLW:
10948 case X86ISD::MOVSS:
10949 case X86ISD::MOVSD:
10950 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
Evan Cheng206ee9d2006-07-07 08:33:52 +000010951 }
10952
Dan Gohman475871a2008-07-27 21:46:04 +000010953 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000010954}
10955
Evan Chenge5b51ac2010-04-17 06:13:15 +000010956/// isTypeDesirableForOp - Return true if the target has native support for
10957/// the specified value type and it is 'desirable' to use the type for the
10958/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
10959/// instruction encodings are longer and some i16 instructions are slow.
10960bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
10961 if (!isTypeLegal(VT))
10962 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000010963 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000010964 return true;
10965
10966 switch (Opc) {
10967 default:
10968 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000010969 case ISD::LOAD:
10970 case ISD::SIGN_EXTEND:
10971 case ISD::ZERO_EXTEND:
10972 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000010973 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000010974 case ISD::SRL:
10975 case ISD::SUB:
10976 case ISD::ADD:
10977 case ISD::MUL:
10978 case ISD::AND:
10979 case ISD::OR:
10980 case ISD::XOR:
10981 return false;
10982 }
10983}
10984
10985/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000010986/// beneficial for dag combiner to promote the specified node. If true, it
10987/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000010988bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000010989 EVT VT = Op.getValueType();
10990 if (VT != MVT::i16)
10991 return false;
10992
Evan Cheng4c26e932010-04-19 19:29:22 +000010993 bool Promote = false;
10994 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000010995 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000010996 default: break;
10997 case ISD::LOAD: {
10998 LoadSDNode *LD = cast<LoadSDNode>(Op);
10999 // If the non-extending load has a single use and it's not live out, then it
11000 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000011001 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
11002 Op.hasOneUse()*/) {
11003 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
11004 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
11005 // The only case where we'd want to promote LOAD (rather then it being
11006 // promoted as an operand is when it's only use is liveout.
11007 if (UI->getOpcode() != ISD::CopyToReg)
11008 return false;
11009 }
11010 }
Evan Cheng4c26e932010-04-19 19:29:22 +000011011 Promote = true;
11012 break;
11013 }
11014 case ISD::SIGN_EXTEND:
11015 case ISD::ZERO_EXTEND:
11016 case ISD::ANY_EXTEND:
11017 Promote = true;
11018 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000011019 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000011020 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000011021 SDValue N0 = Op.getOperand(0);
11022 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000011023 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000011024 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000011025 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000011026 break;
11027 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000011028 case ISD::ADD:
11029 case ISD::MUL:
11030 case ISD::AND:
11031 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000011032 case ISD::XOR:
11033 Commute = true;
11034 // fallthrough
11035 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000011036 SDValue N0 = Op.getOperand(0);
11037 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000011038 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000011039 return false;
11040 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000011041 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000011042 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000011043 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000011044 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000011045 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000011046 }
11047 }
11048
11049 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000011050 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000011051}
11052
Evan Cheng60c07e12006-07-05 22:17:51 +000011053//===----------------------------------------------------------------------===//
11054// X86 Inline Assembly Support
11055//===----------------------------------------------------------------------===//
11056
Chris Lattnerb8105652009-07-20 17:51:36 +000011057static bool LowerToBSwap(CallInst *CI) {
11058 // FIXME: this should verify that we are targetting a 486 or better. If not,
11059 // we will turn this bswap into something that will be lowered to logical ops
11060 // instead of emitting the bswap asm. For now, we don't support 486 or lower
11061 // so don't worry about this.
Eric Christopherfd179292009-08-27 18:07:15 +000011062
Chris Lattnerb8105652009-07-20 17:51:36 +000011063 // Verify this is a simple bswap.
Gabor Greife1c2b9c2010-06-30 13:03:37 +000011064 if (CI->getNumArgOperands() != 1 ||
Gabor Greif1cfe44a2010-06-26 11:51:52 +000011065 CI->getType() != CI->getArgOperand(0)->getType() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011066 !CI->getType()->isIntegerTy())
Chris Lattnerb8105652009-07-20 17:51:36 +000011067 return false;
Eric Christopherfd179292009-08-27 18:07:15 +000011068
Chris Lattnerb8105652009-07-20 17:51:36 +000011069 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
11070 if (!Ty || Ty->getBitWidth() % 16 != 0)
11071 return false;
Eric Christopherfd179292009-08-27 18:07:15 +000011072
Chris Lattnerb8105652009-07-20 17:51:36 +000011073 // Okay, we can do this xform, do so now.
11074 const Type *Tys[] = { Ty };
11075 Module *M = CI->getParent()->getParent()->getParent();
11076 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
Eric Christopherfd179292009-08-27 18:07:15 +000011077
Gabor Greif1cfe44a2010-06-26 11:51:52 +000011078 Value *Op = CI->getArgOperand(0);
Chris Lattnerb8105652009-07-20 17:51:36 +000011079 Op = CallInst::Create(Int, Op, CI->getName(), CI);
Eric Christopherfd179292009-08-27 18:07:15 +000011080
Chris Lattnerb8105652009-07-20 17:51:36 +000011081 CI->replaceAllUsesWith(Op);
11082 CI->eraseFromParent();
11083 return true;
11084}
11085
11086bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
11087 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
11088 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
11089
11090 std::string AsmStr = IA->getAsmString();
11091
11092 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000011093 SmallVector<StringRef, 4> AsmPieces;
Chris Lattnerb8105652009-07-20 17:51:36 +000011094 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
11095
11096 switch (AsmPieces.size()) {
11097 default: return false;
11098 case 1:
11099 AsmStr = AsmPieces[0];
11100 AsmPieces.clear();
11101 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
11102
11103 // bswap $0
11104 if (AsmPieces.size() == 2 &&
11105 (AsmPieces[0] == "bswap" ||
11106 AsmPieces[0] == "bswapq" ||
11107 AsmPieces[0] == "bswapl") &&
11108 (AsmPieces[1] == "$0" ||
11109 AsmPieces[1] == "${0:q}")) {
11110 // No need to check constraints, nothing other than the equivalent of
11111 // "=r,0" would be valid here.
11112 return LowerToBSwap(CI);
11113 }
11114 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011115 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000011116 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000011117 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000011118 AsmPieces[1] == "$$8," &&
11119 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000011120 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
11121 AsmPieces.clear();
Benjamin Kramer018cbd52010-03-12 13:54:59 +000011122 const std::string &Constraints = IA->getConstraintString();
11123 SplitString(StringRef(Constraints).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000011124 std::sort(AsmPieces.begin(), AsmPieces.end());
11125 if (AsmPieces.size() == 4 &&
11126 AsmPieces[0] == "~{cc}" &&
11127 AsmPieces[1] == "~{dirflag}" &&
11128 AsmPieces[2] == "~{flags}" &&
11129 AsmPieces[3] == "~{fpsr}") {
11130 return LowerToBSwap(CI);
11131 }
Chris Lattnerb8105652009-07-20 17:51:36 +000011132 }
11133 break;
11134 case 3:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011135 if (CI->getType()->isIntegerTy(64) &&
Owen Anderson1d0be152009-08-13 21:58:54 +000011136 Constraints.size() >= 2 &&
Chris Lattnerb8105652009-07-20 17:51:36 +000011137 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
11138 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
11139 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramerd4f19592010-01-11 18:03:24 +000011140 SmallVector<StringRef, 4> Words;
Chris Lattnerb8105652009-07-20 17:51:36 +000011141 SplitString(AsmPieces[0], Words, " \t");
11142 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
11143 Words.clear();
11144 SplitString(AsmPieces[1], Words, " \t");
11145 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
11146 Words.clear();
11147 SplitString(AsmPieces[2], Words, " \t,");
11148 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
11149 Words[2] == "%edx") {
11150 return LowerToBSwap(CI);
11151 }
11152 }
11153 }
11154 }
11155 break;
11156 }
11157 return false;
11158}
11159
11160
11161
Chris Lattnerf4dff842006-07-11 02:54:03 +000011162/// getConstraintType - Given a constraint letter, return the type of
11163/// constraint it is for this target.
11164X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000011165X86TargetLowering::getConstraintType(const std::string &Constraint) const {
11166 if (Constraint.size() == 1) {
11167 switch (Constraint[0]) {
11168 case 'A':
Dale Johannesen330169f2008-11-13 21:52:36 +000011169 return C_Register;
Chris Lattnerfce84ac2008-03-11 19:06:29 +000011170 case 'f':
Chris Lattner4234f572007-03-25 02:14:49 +000011171 case 'r':
11172 case 'R':
11173 case 'l':
11174 case 'q':
11175 case 'Q':
11176 case 'x':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000011177 case 'y':
Chris Lattner4234f572007-03-25 02:14:49 +000011178 case 'Y':
11179 return C_RegisterClass;
Dale Johannesen78e3e522009-02-12 20:58:09 +000011180 case 'e':
11181 case 'Z':
11182 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000011183 default:
11184 break;
11185 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000011186 }
Chris Lattner4234f572007-03-25 02:14:49 +000011187 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000011188}
11189
John Thompsoneac6e1d2010-09-13 18:15:37 +000011190/// Examine constraint type and operand type and determine a weight value,
11191/// where: -1 = invalid match, and 0 = so-so match to 3 = good match.
11192/// This object must already have been set up with the operand type
11193/// and the current alternative constraint selected.
11194int X86TargetLowering::getSingleConstraintMatchWeight(
11195 AsmOperandInfo &info, const char *constraint) const {
11196 int weight = -1;
11197 Value *CallOperandVal = info.CallOperandVal;
11198 // If we don't have a value, we can't do a match,
11199 // but allow it at the lowest weight.
11200 if (CallOperandVal == NULL)
11201 return 0;
11202 // Look at the constraint type.
11203 switch (*constraint) {
11204 default:
11205 return TargetLowering::getSingleConstraintMatchWeight(info, constraint);
11206 break;
11207 case 'I':
11208 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
11209 if (C->getZExtValue() <= 31)
11210 weight = 3;
11211 }
11212 break;
11213 // etc.
11214 }
11215 return weight;
11216}
11217
Dale Johannesenba2a0b92008-01-29 02:21:21 +000011218/// LowerXConstraint - try to replace an X constraint, which matches anything,
11219/// with another that has more specific requirements based on the type of the
11220/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000011221const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000011222LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000011223 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
11224 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000011225 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesenba2a0b92008-01-29 02:21:21 +000011226 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +000011227 return "Y";
11228 if (Subtarget->hasSSE1())
11229 return "x";
11230 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011231
Chris Lattner5e764232008-04-26 23:02:14 +000011232 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000011233}
11234
Chris Lattner48884cd2007-08-25 00:47:38 +000011235/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
11236/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000011237void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +000011238 char Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000011239 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000011240 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000011241 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000011242
Chris Lattner22aaf1d2006-10-31 20:13:11 +000011243 switch (Constraint) {
11244 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000011245 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000011246 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000011247 if (C->getZExtValue() <= 31) {
11248 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000011249 break;
11250 }
Devang Patel84f7fd22007-03-17 00:13:28 +000011251 }
Chris Lattner48884cd2007-08-25 00:47:38 +000011252 return;
Evan Cheng364091e2008-09-22 23:57:37 +000011253 case 'J':
11254 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000011255 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000011256 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
11257 break;
11258 }
11259 }
11260 return;
11261 case 'K':
11262 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000011263 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000011264 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
11265 break;
11266 }
11267 }
11268 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000011269 case 'N':
11270 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000011271 if (C->getZExtValue() <= 255) {
11272 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000011273 break;
11274 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000011275 }
Chris Lattner48884cd2007-08-25 00:47:38 +000011276 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000011277 case 'e': {
11278 // 32-bit signed value
11279 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000011280 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
11281 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000011282 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000011283 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000011284 break;
11285 }
11286 // FIXME gcc accepts some relocatable values here too, but only in certain
11287 // memory models; it's complicated.
11288 }
11289 return;
11290 }
11291 case 'Z': {
11292 // 32-bit unsigned value
11293 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000011294 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
11295 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000011296 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
11297 break;
11298 }
11299 }
11300 // FIXME gcc accepts some relocatable values here too, but only in certain
11301 // memory models; it's complicated.
11302 return;
11303 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000011304 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000011305 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000011306 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000011307 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000011308 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000011309 break;
11310 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011311
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000011312 // In any sort of PIC mode addresses need to be computed at runtime by
11313 // adding in a register or some sort of table lookup. These can't
11314 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000011315 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000011316 return;
11317
Chris Lattnerdc43a882007-05-03 16:52:29 +000011318 // If we are in non-pic codegen mode, we allow the address of a global (with
11319 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000011320 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000011321 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000011322
Chris Lattner49921962009-05-08 18:23:14 +000011323 // Match either (GA), (GA+C), (GA+C1+C2), etc.
11324 while (1) {
11325 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
11326 Offset += GA->getOffset();
11327 break;
11328 } else if (Op.getOpcode() == ISD::ADD) {
11329 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
11330 Offset += C->getZExtValue();
11331 Op = Op.getOperand(0);
11332 continue;
11333 }
11334 } else if (Op.getOpcode() == ISD::SUB) {
11335 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
11336 Offset += -C->getZExtValue();
11337 Op = Op.getOperand(0);
11338 continue;
11339 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000011340 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000011341
Chris Lattner49921962009-05-08 18:23:14 +000011342 // Otherwise, this isn't something we can handle, reject it.
11343 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000011344 }
Eric Christopherfd179292009-08-27 18:07:15 +000011345
Dan Gohman46510a72010-04-15 01:51:59 +000011346 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000011347 // If we require an extra load to get this address, as in PIC mode, we
11348 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000011349 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
11350 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000011351 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000011352
Devang Patel0d881da2010-07-06 22:08:15 +000011353 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
11354 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000011355 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000011356 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000011357 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011358
Gabor Greifba36cb52008-08-28 21:40:38 +000011359 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000011360 Ops.push_back(Result);
11361 return;
11362 }
Dale Johannesen1784d162010-06-25 21:55:36 +000011363 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000011364}
11365
Chris Lattner259e97c2006-01-31 19:43:35 +000011366std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +000011367getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000011368 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +000011369 if (Constraint.size() == 1) {
11370 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +000011371 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +000011372 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +000011373 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
11374 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011375 if (VT == MVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +000011376 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
11377 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
11378 X86::R10D,X86::R11D,X86::R12D,
11379 X86::R13D,X86::R14D,X86::R15D,
11380 X86::EBP, X86::ESP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011381 else if (VT == MVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +000011382 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
11383 X86::SI, X86::DI, X86::R8W,X86::R9W,
11384 X86::R10W,X86::R11W,X86::R12W,
11385 X86::R13W,X86::R14W,X86::R15W,
11386 X86::BP, X86::SP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011387 else if (VT == MVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +000011388 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
11389 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
11390 X86::R10B,X86::R11B,X86::R12B,
11391 X86::R13B,X86::R14B,X86::R15B,
11392 X86::BPL, X86::SPL, 0);
11393
Owen Anderson825b72b2009-08-11 20:47:22 +000011394 else if (VT == MVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +000011395 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
11396 X86::RSI, X86::RDI, X86::R8, X86::R9,
11397 X86::R10, X86::R11, X86::R12,
11398 X86::R13, X86::R14, X86::R15,
11399 X86::RBP, X86::RSP, 0);
11400
11401 break;
11402 }
Eric Christopherfd179292009-08-27 18:07:15 +000011403 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +000011404 case 'Q': // Q_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000011405 if (VT == MVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000011406 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011407 else if (VT == MVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000011408 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011409 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +000011410 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011411 else if (VT == MVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +000011412 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
11413 break;
Chris Lattner259e97c2006-01-31 19:43:35 +000011414 }
11415 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011416
Chris Lattner1efa40f2006-02-22 00:56:39 +000011417 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +000011418}
Chris Lattnerf76d1802006-07-31 23:26:50 +000011419
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011420std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000011421X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000011422 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000011423 // First, see if this is a constraint that directly corresponds to an LLVM
11424 // register class.
11425 if (Constraint.size() == 1) {
11426 // GCC Constraint Letters
11427 switch (Constraint[0]) {
11428 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000011429 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000011430 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000011431 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +000011432 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000011433 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000011434 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000011435 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000011436 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000011437 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000011438 case 'R': // LEGACY_REGS
11439 if (VT == MVT::i8)
11440 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
11441 if (VT == MVT::i16)
11442 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
11443 if (VT == MVT::i32 || !Subtarget->is64Bit())
11444 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
11445 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000011446 case 'f': // FP Stack registers.
11447 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
11448 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000011449 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000011450 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000011451 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000011452 return std::make_pair(0U, X86::RFP64RegisterClass);
11453 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000011454 case 'y': // MMX_REGS if MMX allowed.
11455 if (!Subtarget->hasMMX()) break;
11456 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000011457 case 'Y': // SSE_REGS if SSE2 allowed
11458 if (!Subtarget->hasSSE2()) break;
11459 // FALL THROUGH.
11460 case 'x': // SSE_REGS if SSE1 allowed
11461 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000011462
Owen Anderson825b72b2009-08-11 20:47:22 +000011463 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000011464 default: break;
11465 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000011466 case MVT::f32:
11467 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000011468 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000011469 case MVT::f64:
11470 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000011471 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000011472 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000011473 case MVT::v16i8:
11474 case MVT::v8i16:
11475 case MVT::v4i32:
11476 case MVT::v2i64:
11477 case MVT::v4f32:
11478 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000011479 return std::make_pair(0U, X86::VR128RegisterClass);
11480 }
Chris Lattnerad043e82007-04-09 05:11:28 +000011481 break;
11482 }
11483 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011484
Chris Lattnerf76d1802006-07-31 23:26:50 +000011485 // Use the default implementation in TargetLowering to convert the register
11486 // constraint into a member of a register class.
11487 std::pair<unsigned, const TargetRegisterClass*> Res;
11488 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000011489
11490 // Not found as a standard register?
11491 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000011492 // Map st(0) -> st(7) -> ST0
11493 if (Constraint.size() == 7 && Constraint[0] == '{' &&
11494 tolower(Constraint[1]) == 's' &&
11495 tolower(Constraint[2]) == 't' &&
11496 Constraint[3] == '(' &&
11497 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
11498 Constraint[5] == ')' &&
11499 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000011500
Chris Lattner56d77c72009-09-13 22:41:48 +000011501 Res.first = X86::ST0+Constraint[4]-'0';
11502 Res.second = X86::RFP80RegisterClass;
11503 return Res;
11504 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000011505
Chris Lattner56d77c72009-09-13 22:41:48 +000011506 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000011507 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000011508 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000011509 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000011510 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000011511 }
Chris Lattner56d77c72009-09-13 22:41:48 +000011512
11513 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000011514 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000011515 Res.first = X86::EFLAGS;
11516 Res.second = X86::CCRRegisterClass;
11517 return Res;
11518 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000011519
Dale Johannesen330169f2008-11-13 21:52:36 +000011520 // 'A' means EAX + EDX.
11521 if (Constraint == "A") {
11522 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000011523 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000011524 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000011525 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000011526 return Res;
11527 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011528
Chris Lattnerf76d1802006-07-31 23:26:50 +000011529 // Otherwise, check to see if this is a register class of the wrong value
11530 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
11531 // turn into {ax},{dx}.
11532 if (Res.second->hasType(VT))
11533 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011534
Chris Lattnerf76d1802006-07-31 23:26:50 +000011535 // All of the single-register GCC register classes map their values onto
11536 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
11537 // really want an 8-bit or 32-bit register, map to the appropriate register
11538 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000011539 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011540 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000011541 unsigned DestReg = 0;
11542 switch (Res.first) {
11543 default: break;
11544 case X86::AX: DestReg = X86::AL; break;
11545 case X86::DX: DestReg = X86::DL; break;
11546 case X86::CX: DestReg = X86::CL; break;
11547 case X86::BX: DestReg = X86::BL; break;
11548 }
11549 if (DestReg) {
11550 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000011551 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000011552 }
Owen Anderson825b72b2009-08-11 20:47:22 +000011553 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000011554 unsigned DestReg = 0;
11555 switch (Res.first) {
11556 default: break;
11557 case X86::AX: DestReg = X86::EAX; break;
11558 case X86::DX: DestReg = X86::EDX; break;
11559 case X86::CX: DestReg = X86::ECX; break;
11560 case X86::BX: DestReg = X86::EBX; break;
11561 case X86::SI: DestReg = X86::ESI; break;
11562 case X86::DI: DestReg = X86::EDI; break;
11563 case X86::BP: DestReg = X86::EBP; break;
11564 case X86::SP: DestReg = X86::ESP; break;
11565 }
11566 if (DestReg) {
11567 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000011568 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000011569 }
Owen Anderson825b72b2009-08-11 20:47:22 +000011570 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000011571 unsigned DestReg = 0;
11572 switch (Res.first) {
11573 default: break;
11574 case X86::AX: DestReg = X86::RAX; break;
11575 case X86::DX: DestReg = X86::RDX; break;
11576 case X86::CX: DestReg = X86::RCX; break;
11577 case X86::BX: DestReg = X86::RBX; break;
11578 case X86::SI: DestReg = X86::RSI; break;
11579 case X86::DI: DestReg = X86::RDI; break;
11580 case X86::BP: DestReg = X86::RBP; break;
11581 case X86::SP: DestReg = X86::RSP; break;
11582 }
11583 if (DestReg) {
11584 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000011585 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000011586 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000011587 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000011588 } else if (Res.second == X86::FR32RegisterClass ||
11589 Res.second == X86::FR64RegisterClass ||
11590 Res.second == X86::VR128RegisterClass) {
11591 // Handle references to XMM physical registers that got mapped into the
11592 // wrong class. This can happen with constraints like {xmm0} where the
11593 // target independent register mapper will just pick the first match it can
11594 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000011595 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000011596 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000011597 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000011598 Res.second = X86::FR64RegisterClass;
11599 else if (X86::VR128RegisterClass->hasType(VT))
11600 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000011601 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011602
Chris Lattnerf76d1802006-07-31 23:26:50 +000011603 return Res;
11604}