blob: 55daf769cffc899e36610d8d97c286a104f80eb6 [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 Johannesene39859a2010-05-21 18:40:15 +0000221 // Without SSE, i64->f64 goes through memory; i64->MMX is Legal.
222 if (Subtarget->hasMMX() && !DisableMMX)
223 setOperationAction(ISD::BIT_CONVERT , MVT::i64 , Custom);
224 else
225 setOperationAction(ISD::BIT_CONVERT , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000226 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000227 }
Chris Lattner21f66852005-12-23 05:15:23 +0000228
Dan Gohmanb00ee212008-02-18 19:34:53 +0000229 // Scalar integer divide and remainder are lowered to use operations that
230 // produce two results, to match the available instructions. This exposes
231 // the two-result form to trivial CSE, which is able to combine x/y and x%y
232 // into a single instruction.
233 //
234 // Scalar integer multiply-high is also lowered to use two-result
235 // operations, to match the available instructions. However, plain multiply
236 // (low) operations are left as Legal, as there are single-result
237 // instructions for this in x86. Using the two-result multiply instructions
238 // when both high and low results are needed must be arranged by dagcombine.
Owen Anderson825b72b2009-08-11 20:47:22 +0000239 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
240 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
241 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
242 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
243 setOperationAction(ISD::SREM , MVT::i8 , Expand);
244 setOperationAction(ISD::UREM , MVT::i8 , Expand);
245 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
246 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
247 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
248 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
249 setOperationAction(ISD::SREM , MVT::i16 , Expand);
250 setOperationAction(ISD::UREM , MVT::i16 , Expand);
251 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
252 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
253 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
254 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
255 setOperationAction(ISD::SREM , MVT::i32 , Expand);
256 setOperationAction(ISD::UREM , MVT::i32 , Expand);
257 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
258 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
259 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
260 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
261 setOperationAction(ISD::SREM , MVT::i64 , Expand);
262 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohmana37c9f72007-09-25 18:23:27 +0000263
Owen Anderson825b72b2009-08-11 20:47:22 +0000264 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
265 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
266 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
267 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000268 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000269 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
270 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
271 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
272 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
273 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
274 setOperationAction(ISD::FREM , MVT::f32 , Expand);
275 setOperationAction(ISD::FREM , MVT::f64 , Expand);
276 setOperationAction(ISD::FREM , MVT::f80 , Expand);
277 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000278
Owen Anderson825b72b2009-08-11 20:47:22 +0000279 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
280 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
281 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
282 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000283 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
284 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000285 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
286 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
287 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000288 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000289 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
290 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
291 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000292 }
293
Owen Anderson825b72b2009-08-11 20:47:22 +0000294 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
295 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000296
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000297 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000298 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000299 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000300 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000301 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000302 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
303 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
304 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
305 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
306 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000307 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000308 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
309 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
310 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
311 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000312 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000313 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
314 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000315 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000316 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000317
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000318 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000319 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
320 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
321 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
322 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000323 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000324 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
325 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000326 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000327 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000328 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
329 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
330 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
331 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000332 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000333 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000334 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000335 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
336 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
337 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000338 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000339 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
340 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
341 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000342 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000343
Evan Chengd2cde682008-03-10 19:38:10 +0000344 if (Subtarget->hasSSE1())
Owen Anderson825b72b2009-08-11 20:47:22 +0000345 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000346
Eric Christopher9a9d2752010-07-22 02:48:34 +0000347 // We may not have a libcall for MEMBARRIER so we should lower this.
348 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
349
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000350 // On X86 and X86-64, atomic operations are lowered to locked instructions.
351 // Locked instructions, in turn, have implicit fence semantics (all memory
352 // operations are flushed before issuing the locked instruction, and they
353 // are not buffered), so we can fold away the common pattern of
354 // fence-atomic-fence.
355 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000356
Mon P Wang63307c32008-05-05 19:05:59 +0000357 // Expand certain atomics
Owen Anderson825b72b2009-08-11 20:47:22 +0000358 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Custom);
359 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Custom);
360 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
361 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Bill Wendling5bf1b4e2008-08-20 00:28:16 +0000362
Owen Anderson825b72b2009-08-11 20:47:22 +0000363 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Custom);
364 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Custom);
365 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
366 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000367
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000368 if (!Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000369 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
370 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
371 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
372 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
373 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
374 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
375 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000376 }
377
Evan Cheng3c992d22006-03-07 02:02:57 +0000378 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000379 if (!Subtarget->isTargetDarwin() &&
380 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000381 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000382 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000383 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000384
Owen Anderson825b72b2009-08-11 20:47:22 +0000385 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
386 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
387 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
388 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000389 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000390 setExceptionPointerRegister(X86::RAX);
391 setExceptionSelectorRegister(X86::RDX);
392 } else {
393 setExceptionPointerRegister(X86::EAX);
394 setExceptionSelectorRegister(X86::EDX);
395 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000396 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
397 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000398
Owen Anderson825b72b2009-08-11 20:47:22 +0000399 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000400
Owen Anderson825b72b2009-08-11 20:47:22 +0000401 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000402
Nate Begemanacc398c2006-01-25 18:21:52 +0000403 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000404 setOperationAction(ISD::VASTART , MVT::Other, Custom);
405 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000406 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000407 setOperationAction(ISD::VAARG , MVT::Other, Custom);
408 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000409 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000410 setOperationAction(ISD::VAARG , MVT::Other, Expand);
411 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000412 }
Evan Chengae642192007-03-02 23:16:35 +0000413
Owen Anderson825b72b2009-08-11 20:47:22 +0000414 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
415 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000416 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000417 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000418 if (Subtarget->isTargetCygMing())
Owen Anderson825b72b2009-08-11 20:47:22 +0000419 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000420 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000421 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000422
Evan Chengc7ce29b2009-02-13 22:36:38 +0000423 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000424 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000425 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000426 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
427 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000428
Evan Cheng223547a2006-01-31 22:28:30 +0000429 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000430 setOperationAction(ISD::FABS , MVT::f64, Custom);
431 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000432
433 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000434 setOperationAction(ISD::FNEG , MVT::f64, Custom);
435 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000436
Evan Cheng68c47cb2007-01-05 07:55:56 +0000437 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000438 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
439 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000440
Evan Chengd25e9e82006-02-02 00:28:23 +0000441 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000442 setOperationAction(ISD::FSIN , MVT::f64, Expand);
443 setOperationAction(ISD::FCOS , MVT::f64, Expand);
444 setOperationAction(ISD::FSIN , MVT::f32, Expand);
445 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000446
Chris Lattnera54aa942006-01-29 06:26:08 +0000447 // Expand FP immediates into loads from the stack, except for the special
448 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000449 addLegalFPImmediate(APFloat(+0.0)); // xorpd
450 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000451 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000452 // Use SSE for f32, x87 for f64.
453 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000454 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
455 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000456
457 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000458 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000459
460 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000461 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000462
Owen Anderson825b72b2009-08-11 20:47:22 +0000463 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000464
465 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000466 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
467 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000468
469 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000470 setOperationAction(ISD::FSIN , MVT::f32, Expand);
471 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000472
Nate Begemane1795842008-02-14 08:57:00 +0000473 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000474 addLegalFPImmediate(APFloat(+0.0f)); // xorps
475 addLegalFPImmediate(APFloat(+0.0)); // FLD0
476 addLegalFPImmediate(APFloat(+1.0)); // FLD1
477 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
478 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
479
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000480 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000481 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
482 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000483 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000484 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000485 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000486 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000487 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
488 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000489
Owen Anderson825b72b2009-08-11 20:47:22 +0000490 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
491 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
492 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
493 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000494
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000495 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000496 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
497 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000498 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000499 addLegalFPImmediate(APFloat(+0.0)); // FLD0
500 addLegalFPImmediate(APFloat(+1.0)); // FLD1
501 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
502 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000503 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
504 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
505 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
506 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000507 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000508
Dale Johannesen59a58732007-08-05 18:49:15 +0000509 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000510 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000511 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
512 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
513 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000514 {
515 bool ignored;
516 APFloat TmpFlt(+0.0);
517 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
518 &ignored);
519 addLegalFPImmediate(TmpFlt); // FLD0
520 TmpFlt.changeSign();
521 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
522 APFloat TmpFlt2(+1.0);
523 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
524 &ignored);
525 addLegalFPImmediate(TmpFlt2); // FLD1
526 TmpFlt2.changeSign();
527 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
528 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000529
Evan Chengc7ce29b2009-02-13 22:36:38 +0000530 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000531 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
532 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000533 }
Dale Johannesen2f429012007-09-26 21:10:55 +0000534 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000535
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000536 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000537 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
538 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
539 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000540
Owen Anderson825b72b2009-08-11 20:47:22 +0000541 setOperationAction(ISD::FLOG, MVT::f80, Expand);
542 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
543 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
544 setOperationAction(ISD::FEXP, MVT::f80, Expand);
545 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000546
Mon P Wangf007a8b2008-11-06 05:31:54 +0000547 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000548 // (for widening) or expand (for scalarization). Then we will selectively
549 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000550 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
551 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
552 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
553 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
554 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
555 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
556 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
557 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
558 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
559 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
560 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
561 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
562 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
563 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
564 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
565 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
566 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
567 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
568 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
569 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
570 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
571 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
572 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
573 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
574 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
575 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
576 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
577 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
578 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
579 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
580 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
581 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
582 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
583 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
584 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
585 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
586 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
587 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
588 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
589 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
590 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
591 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
592 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
593 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
594 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
595 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
596 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
597 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
598 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
599 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000600 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000601 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
602 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
603 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
604 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
605 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
606 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
607 setTruncStoreAction((MVT::SimpleValueType)VT,
608 (MVT::SimpleValueType)InnerVT, Expand);
609 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
610 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
611 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000612 }
613
Evan Chengc7ce29b2009-02-13 22:36:38 +0000614 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
615 // with -msoft-float, disable use of MMX as well.
Evan Cheng92722532009-03-26 23:06:32 +0000616 if (!UseSoftFloat && !DisableMMX && Subtarget->hasMMX()) {
Bill Wendlingd8dd5752010-09-07 20:03:56 +0000617 addRegisterClass(MVT::x86mmx, X86::VR64RegisterClass, false);
618
619 // FIXME: Remove the rest of this stuff.
Dale Johannesen76090172010-04-20 22:34:09 +0000620 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass, false);
621 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass, false);
622 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass, false);
Chris Lattnere35d9842010-07-04 22:57:10 +0000623
Dale Johannesen76090172010-04-20 22:34:09 +0000624 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass, false);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000625
Owen Anderson825b72b2009-08-11 20:47:22 +0000626 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
627 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
628 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
629 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000630
Owen Anderson825b72b2009-08-11 20:47:22 +0000631 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
632 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
633 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
634 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000635
Owen Anderson825b72b2009-08-11 20:47:22 +0000636 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
637 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
Bill Wendling74027e92007-03-15 21:24:36 +0000638
Owen Anderson825b72b2009-08-11 20:47:22 +0000639 setOperationAction(ISD::AND, MVT::v8i8, Promote);
640 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
641 setOperationAction(ISD::AND, MVT::v4i16, Promote);
642 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
643 setOperationAction(ISD::AND, MVT::v2i32, Promote);
644 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
645 setOperationAction(ISD::AND, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000646
Owen Anderson825b72b2009-08-11 20:47:22 +0000647 setOperationAction(ISD::OR, MVT::v8i8, Promote);
648 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
649 setOperationAction(ISD::OR, MVT::v4i16, Promote);
650 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
651 setOperationAction(ISD::OR, MVT::v2i32, Promote);
652 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
653 setOperationAction(ISD::OR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000654
Owen Anderson825b72b2009-08-11 20:47:22 +0000655 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
656 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
657 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
658 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
659 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
660 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
661 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000662
Owen Anderson825b72b2009-08-11 20:47:22 +0000663 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
664 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
665 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
666 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
667 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
668 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
Owen Anderson825b72b2009-08-11 20:47:22 +0000669 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000670
Owen Anderson825b72b2009-08-11 20:47:22 +0000671 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
672 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
673 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000674 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
Bill Wendlinga348c562007-03-22 18:42:45 +0000675
Owen Anderson825b72b2009-08-11 20:47:22 +0000676 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
677 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
678 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
679 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000680
Owen Anderson825b72b2009-08-11 20:47:22 +0000681 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
682 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
683 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Bill Wendling3180e202008-07-20 02:32:23 +0000684
Owen Anderson825b72b2009-08-11 20:47:22 +0000685 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
Mon P Wang9e5ecb82008-12-12 01:25:51 +0000686
Owen Anderson825b72b2009-08-11 20:47:22 +0000687 setOperationAction(ISD::SELECT, MVT::v8i8, Promote);
688 setOperationAction(ISD::SELECT, MVT::v4i16, Promote);
689 setOperationAction(ISD::SELECT, MVT::v2i32, Promote);
690 setOperationAction(ISD::SELECT, MVT::v1i64, Custom);
691 setOperationAction(ISD::VSETCC, MVT::v8i8, Custom);
692 setOperationAction(ISD::VSETCC, MVT::v4i16, Custom);
693 setOperationAction(ISD::VSETCC, MVT::v2i32, Custom);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000694
695 if (!X86ScalarSSEf64 && Subtarget->is64Bit()) {
696 setOperationAction(ISD::BIT_CONVERT, MVT::v8i8, Custom);
697 setOperationAction(ISD::BIT_CONVERT, MVT::v4i16, Custom);
698 setOperationAction(ISD::BIT_CONVERT, MVT::v2i32, Custom);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000699 setOperationAction(ISD::BIT_CONVERT, MVT::v1i64, Custom);
700 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000701 }
702
Evan Cheng92722532009-03-26 23:06:32 +0000703 if (!UseSoftFloat && Subtarget->hasSSE1()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000704 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000705
Owen Anderson825b72b2009-08-11 20:47:22 +0000706 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
707 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
708 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
709 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
710 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
711 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
712 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
713 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
714 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
715 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
716 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
717 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000718 }
719
Evan Cheng92722532009-03-26 23:06:32 +0000720 if (!UseSoftFloat && Subtarget->hasSSE2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000721 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000722
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000723 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
724 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000725 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
726 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
727 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
728 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000729
Owen Anderson825b72b2009-08-11 20:47:22 +0000730 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
731 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
732 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
733 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
734 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
735 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
736 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
737 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
738 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
739 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
740 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
741 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
742 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
743 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
744 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
745 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000746
Owen Anderson825b72b2009-08-11 20:47:22 +0000747 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
748 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
749 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
750 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000751
Owen Anderson825b72b2009-08-11 20:47:22 +0000752 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
753 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
754 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
755 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
756 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000757
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000758 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
759 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
760 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
761 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
762 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
763
Evan Cheng2c3ae372006-04-12 21:21:57 +0000764 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000765 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
766 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000767 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000768 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000769 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000770 // Do not attempt to custom lower non-128-bit vectors
771 if (!VT.is128BitVector())
772 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000773 setOperationAction(ISD::BUILD_VECTOR,
774 VT.getSimpleVT().SimpleTy, Custom);
775 setOperationAction(ISD::VECTOR_SHUFFLE,
776 VT.getSimpleVT().SimpleTy, Custom);
777 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
778 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000779 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000780
Owen Anderson825b72b2009-08-11 20:47:22 +0000781 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
782 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
783 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
784 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
785 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
786 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000787
Nate Begemancdd1eec2008-02-12 22:51:28 +0000788 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000789 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
790 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000791 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000792
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000793 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000794 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
795 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000796 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000797
798 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000799 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000800 continue;
Eric Christopher4bd24c22010-03-30 01:04:59 +0000801
Owen Andersond6662ad2009-08-10 20:46:15 +0000802 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000803 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000804 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000805 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000806 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000807 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000808 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000809 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000810 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000811 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000812 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000813
Owen Anderson825b72b2009-08-11 20:47:22 +0000814 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000815
Evan Cheng2c3ae372006-04-12 21:21:57 +0000816 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000817 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
818 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
819 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
820 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000821
Owen Anderson825b72b2009-08-11 20:47:22 +0000822 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
823 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Eli Friedman23ef1052009-06-06 03:57:58 +0000824 if (!DisableMMX && Subtarget->hasMMX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000825 setOperationAction(ISD::FP_TO_SINT, MVT::v2i32, Custom);
826 setOperationAction(ISD::SINT_TO_FP, MVT::v2i32, Custom);
Eli Friedman23ef1052009-06-06 03:57:58 +0000827 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000828 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000829
Nate Begeman14d12ca2008-02-11 04:19:36 +0000830 if (Subtarget->hasSSE41()) {
Dale Johannesen54feef22010-05-27 20:12:41 +0000831 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
832 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
833 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
834 setOperationAction(ISD::FRINT, MVT::f32, Legal);
835 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
836 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
837 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
838 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
839 setOperationAction(ISD::FRINT, MVT::f64, Legal);
840 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
841
Nate Begeman14d12ca2008-02-11 04:19:36 +0000842 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000843 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000844
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000845 // Can turn SHL into an integer multiply.
846 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
Nate Begeman51409212010-07-28 00:21:48 +0000847 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000848
Nate Begeman14d12ca2008-02-11 04:19:36 +0000849 // i8 and i16 vectors are custom , because the source register and source
850 // source memory operand types are not the same width. f32 vectors are
851 // custom since the immediate controlling the insert encodes additional
852 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000853 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
854 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
855 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
856 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000857
Owen Anderson825b72b2009-08-11 20:47:22 +0000858 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
859 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
860 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
861 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000862
863 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000864 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
865 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000866 }
867 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000868
Nate Begeman30a0de92008-07-17 16:51:19 +0000869 if (Subtarget->hasSSE42()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000870 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
Nate Begeman30a0de92008-07-17 16:51:19 +0000871 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000872
David Greene9b9838d2009-06-29 16:47:10 +0000873 if (!UseSoftFloat && Subtarget->hasAVX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000874 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
875 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
876 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
877 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
Bruno Cardoso Lopes405f11b2010-08-10 01:43:16 +0000878 addRegisterClass(MVT::v32i8, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000879
Owen Anderson825b72b2009-08-11 20:47:22 +0000880 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
881 setOperationAction(ISD::LOAD, MVT::v8i32, Legal);
882 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
883 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
884 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
885 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
886 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
887 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
888 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
889 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +0000890 setOperationAction(ISD::BUILD_VECTOR, MVT::v8f32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000891 //setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Custom);
892 //setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8f32, Custom);
893 //setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
894 //setOperationAction(ISD::VSETCC, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000895
896 // Operations to consider commented out -v16i16 v32i8
Owen Anderson825b72b2009-08-11 20:47:22 +0000897 //setOperationAction(ISD::ADD, MVT::v16i16, Legal);
898 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
899 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
900 //setOperationAction(ISD::SUB, MVT::v32i8, Legal);
901 //setOperationAction(ISD::SUB, MVT::v16i16, Legal);
902 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
903 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
904 //setOperationAction(ISD::MUL, MVT::v16i16, Legal);
905 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
906 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
907 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
908 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
909 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
910 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000911
Owen Anderson825b72b2009-08-11 20:47:22 +0000912 setOperationAction(ISD::VSETCC, MVT::v4f64, Custom);
913 // setOperationAction(ISD::VSETCC, MVT::v32i8, Custom);
914 // setOperationAction(ISD::VSETCC, MVT::v16i16, Custom);
915 setOperationAction(ISD::VSETCC, MVT::v8i32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000916
Owen Anderson825b72b2009-08-11 20:47:22 +0000917 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v32i8, Custom);
918 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i16, Custom);
919 // setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i16, Custom);
920 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i32, Custom);
921 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000922
Owen Anderson825b72b2009-08-11 20:47:22 +0000923 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f64, Custom);
924 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i64, Custom);
925 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f64, Custom);
926 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i64, Custom);
927 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f64, Custom);
928 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000929
930#if 0
931 // Not sure we want to do this since there are no 256-bit integer
932 // operations in AVX
933
934 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
935 // This includes 256-bit vectors
Owen Anderson825b72b2009-08-11 20:47:22 +0000936 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; ++i) {
937 EVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000938
939 // Do not attempt to custom lower non-power-of-2 vectors
940 if (!isPowerOf2_32(VT.getVectorNumElements()))
941 continue;
942
943 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
944 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
945 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
946 }
947
948 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000949 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i64, Custom);
950 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i64, Custom);
Eric Christopherfd179292009-08-27 18:07:15 +0000951 }
David Greene9b9838d2009-06-29 16:47:10 +0000952#endif
953
954#if 0
955 // Not sure we want to do this since there are no 256-bit integer
956 // operations in AVX
957
958 // Promote v32i8, v16i16, v8i32 load, select, and, or, xor to v4i64.
959 // Including 256-bit vectors
Owen Anderson825b72b2009-08-11 20:47:22 +0000960 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; i++) {
961 EVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000962
963 if (!VT.is256BitVector()) {
964 continue;
965 }
966 setOperationAction(ISD::AND, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000967 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000968 setOperationAction(ISD::OR, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000969 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000970 setOperationAction(ISD::XOR, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000971 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000972 setOperationAction(ISD::LOAD, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000973 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000974 setOperationAction(ISD::SELECT, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000975 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000976 }
977
Owen Anderson825b72b2009-08-11 20:47:22 +0000978 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
David Greene9b9838d2009-06-29 16:47:10 +0000979#endif
980 }
981
Evan Cheng6be2c582006-04-05 23:38:46 +0000982 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000983 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +0000984
Bill Wendling74c37652008-12-09 22:08:41 +0000985 // Add/Sub/Mul with overflow operations are custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000986 setOperationAction(ISD::SADDO, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000987 setOperationAction(ISD::UADDO, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000988 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000989 setOperationAction(ISD::USUBO, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000990 setOperationAction(ISD::SMULO, MVT::i32, Custom);
Dan Gohman71c62a22010-06-02 19:13:40 +0000991
Eli Friedman962f5492010-06-02 19:35:46 +0000992 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
993 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +0000994 //
Eli Friedman962f5492010-06-02 19:35:46 +0000995 // FIXME: We really should do custom legalization for addition and
996 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
997 // than generic legalization for 64-bit multiplication-with-overflow, though.
Eli Friedmana993f0a2010-06-02 00:27:18 +0000998 if (Subtarget->is64Bit()) {
999 setOperationAction(ISD::SADDO, MVT::i64, Custom);
1000 setOperationAction(ISD::UADDO, MVT::i64, Custom);
1001 setOperationAction(ISD::SSUBO, MVT::i64, Custom);
1002 setOperationAction(ISD::USUBO, MVT::i64, Custom);
1003 setOperationAction(ISD::SMULO, MVT::i64, Custom);
1004 }
Bill Wendling41ea7e72008-11-24 19:21:46 +00001005
Evan Chengd54f2d52009-03-31 19:38:51 +00001006 if (!Subtarget->is64Bit()) {
1007 // These libcalls are not available in 32-bit.
1008 setLibcallName(RTLIB::SHL_I128, 0);
1009 setLibcallName(RTLIB::SRL_I128, 0);
1010 setLibcallName(RTLIB::SRA_I128, 0);
1011 }
1012
Evan Cheng206ee9d2006-07-07 08:33:52 +00001013 // We have target-specific dag combine patterns for the following nodes:
1014 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001015 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +00001016 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +00001017 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001018 setTargetDAGCombine(ISD::SHL);
1019 setTargetDAGCombine(ISD::SRA);
1020 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001021 setTargetDAGCombine(ISD::OR);
Chris Lattner149a4e52008-02-22 02:09:43 +00001022 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001023 setTargetDAGCombine(ISD::ZERO_EXTEND);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001024 if (Subtarget->is64Bit())
1025 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001026
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001027 computeRegisterProperties();
1028
Evan Cheng87ed7162006-02-14 08:25:08 +00001029 // FIXME: These should be based on subtarget info. Plus, the values should
1030 // be smaller when we are in optimizing for size mode.
Dan Gohman87060f52008-06-30 21:00:56 +00001031 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng255f20f2010-04-01 06:04:33 +00001032 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Dan Gohman87060f52008-06-30 21:00:56 +00001033 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Evan Chengfb8075d2008-02-28 00:43:03 +00001034 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001035 benefitFromCodePlacementOpt = true;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001036}
1037
Scott Michel5b8f82e2008-03-10 15:42:14 +00001038
Owen Anderson825b72b2009-08-11 20:47:22 +00001039MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
1040 return MVT::i8;
Scott Michel5b8f82e2008-03-10 15:42:14 +00001041}
1042
1043
Evan Cheng29286502008-01-23 23:17:41 +00001044/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1045/// the desired ByVal argument alignment.
1046static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
1047 if (MaxAlign == 16)
1048 return;
1049 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
1050 if (VTy->getBitWidth() == 128)
1051 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +00001052 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1053 unsigned EltAlign = 0;
1054 getMaxByValAlign(ATy->getElementType(), EltAlign);
1055 if (EltAlign > MaxAlign)
1056 MaxAlign = EltAlign;
1057 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1058 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1059 unsigned EltAlign = 0;
1060 getMaxByValAlign(STy->getElementType(i), EltAlign);
1061 if (EltAlign > MaxAlign)
1062 MaxAlign = EltAlign;
1063 if (MaxAlign == 16)
1064 break;
1065 }
1066 }
1067 return;
1068}
1069
1070/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1071/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001072/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1073/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +00001074unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001075 if (Subtarget->is64Bit()) {
1076 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001077 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001078 if (TyAlign > 8)
1079 return TyAlign;
1080 return 8;
1081 }
1082
Evan Cheng29286502008-01-23 23:17:41 +00001083 unsigned Align = 4;
Dale Johannesen0c191872008-02-08 19:48:20 +00001084 if (Subtarget->hasSSE1())
1085 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001086 return Align;
1087}
Chris Lattner2b02a442007-02-25 08:29:00 +00001088
Evan Chengf0df0312008-05-15 08:39:06 +00001089/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001090/// and store operations as a result of memset, memcpy, and memmove
1091/// lowering. If DstAlign is zero that means it's safe to destination
1092/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1093/// means there isn't a need to check it against alignment requirement,
1094/// probably because the source does not need to be loaded. If
1095/// 'NonScalarIntSafe' is true, that means it's safe to return a
1096/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1097/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1098/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001099/// It returns EVT::Other if the type should be determined using generic
1100/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001101EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001102X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1103 unsigned DstAlign, unsigned SrcAlign,
Evan Chengf28f8bc2010-04-02 19:36:14 +00001104 bool NonScalarIntSafe,
Evan Chengc3b0c342010-04-08 07:37:57 +00001105 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001106 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001107 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1108 // linux. This is because the stack realignment code can't handle certain
1109 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001110 const Function *F = MF.getFunction();
Evan Chengf28f8bc2010-04-02 19:36:14 +00001111 if (NonScalarIntSafe &&
1112 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001113 if (Size >= 16 &&
1114 (Subtarget->isUnalignedMemAccessFast() ||
Chandler Carruthae1d41c2010-04-02 01:31:24 +00001115 ((DstAlign == 0 || DstAlign >= 16) &&
1116 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001117 Subtarget->getStackAlignment() >= 16) {
1118 if (Subtarget->hasSSE2())
1119 return MVT::v4i32;
Evan Chengf28f8bc2010-04-02 19:36:14 +00001120 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001121 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001122 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001123 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001124 Subtarget->getStackAlignment() >= 8 &&
Evan Chengc3b0c342010-04-08 07:37:57 +00001125 Subtarget->hasSSE2()) {
1126 // Do not use f64 to lower memcpy if source is string constant. It's
1127 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001128 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001129 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001130 }
Evan Chengf0df0312008-05-15 08:39:06 +00001131 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001132 return MVT::i64;
1133 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001134}
1135
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001136/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1137/// current function. The returned value is a member of the
1138/// MachineJumpTableInfo::JTEntryKind enum.
1139unsigned X86TargetLowering::getJumpTableEncoding() const {
1140 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1141 // symbol.
1142 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1143 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001144 return MachineJumpTableInfo::EK_Custom32;
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001145
1146 // Otherwise, use the normal jump table encoding heuristics.
1147 return TargetLowering::getJumpTableEncoding();
1148}
1149
Chris Lattner589c6f62010-01-26 06:28:43 +00001150/// getPICBaseSymbol - Return the X86-32 PIC base.
1151MCSymbol *
1152X86TargetLowering::getPICBaseSymbol(const MachineFunction *MF,
1153 MCContext &Ctx) const {
1154 const MCAsmInfo &MAI = *getTargetMachine().getMCAsmInfo();
Chris Lattner9b97a732010-03-30 18:10:53 +00001155 return Ctx.GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix())+
1156 Twine(MF->getFunctionNumber())+"$pb");
Chris Lattner589c6f62010-01-26 06:28:43 +00001157}
1158
1159
Chris Lattnerc64daab2010-01-26 05:02:42 +00001160const MCExpr *
1161X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1162 const MachineBasicBlock *MBB,
1163 unsigned uid,MCContext &Ctx) const{
1164 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1165 Subtarget->isPICStyleGOT());
1166 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1167 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001168 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1169 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001170}
1171
Evan Chengcc415862007-11-09 01:32:10 +00001172/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1173/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001174SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001175 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001176 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001177 // This doesn't have DebugLoc associated with it, but is not really the
1178 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001179 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001180 return Table;
1181}
1182
Chris Lattner589c6f62010-01-26 06:28:43 +00001183/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1184/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1185/// MCExpr.
1186const MCExpr *X86TargetLowering::
1187getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1188 MCContext &Ctx) const {
1189 // X86-64 uses RIP relative addressing based on the jump table label.
1190 if (Subtarget->isPICStyleRIPRel())
1191 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1192
1193 // Otherwise, the reference is relative to the PIC base.
1194 return MCSymbolRefExpr::Create(getPICBaseSymbol(MF, Ctx), Ctx);
1195}
1196
Bill Wendlingb4202b82009-07-01 18:50:55 +00001197/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +00001198unsigned X86TargetLowering::getFunctionAlignment(const Function *F) const {
Dan Gohman25103a22009-08-18 00:20:06 +00001199 return F->hasFnAttr(Attribute::OptimizeForSize) ? 0 : 4;
Bill Wendling20c568f2009-06-30 22:38:32 +00001200}
1201
Evan Chengdee81012010-07-26 21:50:05 +00001202std::pair<const TargetRegisterClass*, uint8_t>
1203X86TargetLowering::findRepresentativeClass(EVT VT) const{
1204 const TargetRegisterClass *RRC = 0;
1205 uint8_t Cost = 1;
1206 switch (VT.getSimpleVT().SimpleTy) {
1207 default:
1208 return TargetLowering::findRepresentativeClass(VT);
1209 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1210 RRC = (Subtarget->is64Bit()
1211 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1212 break;
1213 case MVT::v8i8: case MVT::v4i16:
1214 case MVT::v2i32: case MVT::v1i64:
1215 RRC = X86::VR64RegisterClass;
1216 break;
1217 case MVT::f32: case MVT::f64:
1218 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1219 case MVT::v4f32: case MVT::v2f64:
1220 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1221 case MVT::v4f64:
1222 RRC = X86::VR128RegisterClass;
1223 break;
1224 }
1225 return std::make_pair(RRC, Cost);
1226}
1227
Evan Cheng70017e42010-07-24 00:39:05 +00001228unsigned
1229X86TargetLowering::getRegPressureLimit(const TargetRegisterClass *RC,
1230 MachineFunction &MF) const {
1231 unsigned FPDiff = RegInfo->hasFP(MF) ? 1 : 0;
1232 switch (RC->getID()) {
1233 default:
1234 return 0;
1235 case X86::GR32RegClassID:
1236 return 4 - FPDiff;
1237 case X86::GR64RegClassID:
1238 return 8 - FPDiff;
1239 case X86::VR128RegClassID:
1240 return Subtarget->is64Bit() ? 10 : 4;
1241 case X86::VR64RegClassID:
1242 return 4;
1243 }
1244}
1245
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001246bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1247 unsigned &Offset) const {
1248 if (!Subtarget->isTargetLinux())
1249 return false;
1250
1251 if (Subtarget->is64Bit()) {
1252 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1253 Offset = 0x28;
1254 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1255 AddressSpace = 256;
1256 else
1257 AddressSpace = 257;
1258 } else {
1259 // %gs:0x14 on i386
1260 Offset = 0x14;
1261 AddressSpace = 256;
1262 }
1263 return true;
1264}
1265
1266
Chris Lattner2b02a442007-02-25 08:29:00 +00001267//===----------------------------------------------------------------------===//
1268// Return Value Calling Convention Implementation
1269//===----------------------------------------------------------------------===//
1270
Chris Lattner59ed56b2007-02-28 04:55:35 +00001271#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001272
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001273bool
1274X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001275 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001276 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001277 SmallVector<CCValAssign, 16> RVLocs;
1278 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001279 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001280 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001281}
1282
Dan Gohman98ca4f22009-08-05 01:29:28 +00001283SDValue
1284X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001285 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001286 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001287 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001288 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001289 MachineFunction &MF = DAG.getMachineFunction();
1290 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001291
Chris Lattner9774c912007-02-27 05:28:59 +00001292 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001293 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1294 RVLocs, *DAG.getContext());
1295 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001296
Evan Chengdcea1632010-02-04 02:40:39 +00001297 // Add the regs to the liveout set for the function.
1298 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1299 for (unsigned i = 0; i != RVLocs.size(); ++i)
1300 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1301 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001302
Dan Gohman475871a2008-07-27 21:46:04 +00001303 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001304
Dan Gohman475871a2008-07-27 21:46:04 +00001305 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001306 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1307 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001308 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1309 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001310
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001311 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001312 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1313 CCValAssign &VA = RVLocs[i];
1314 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001315 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001316 EVT ValVT = ValToCopy.getValueType();
1317
Dale Johannesenc4510512010-09-24 19:05:48 +00001318 // If this is x86-64, and we disabled SSE, we can't return FP values,
1319 // or SSE or MMX vectors.
1320 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1321 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
1322 (Subtarget->is64Bit() && !Subtarget->hasSSE1())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001323 report_fatal_error("SSE register return with SSE disabled");
1324 }
1325 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1326 // llvm-gcc has never done it right and no one has noticed, so this
1327 // should be OK for now.
1328 if (ValVT == MVT::f64 &&
Chris Lattner83069682010-08-26 05:51:22 +00001329 (Subtarget->is64Bit() && !Subtarget->hasSSE2()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001330 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001331
Chris Lattner447ff682008-03-11 03:23:40 +00001332 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1333 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001334 if (VA.getLocReg() == X86::ST0 ||
1335 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001336 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1337 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001338 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001339 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001340 RetOps.push_back(ValToCopy);
1341 // Don't emit a copytoreg.
1342 continue;
1343 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001344
Evan Cheng242b38b2009-02-23 09:03:22 +00001345 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1346 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001347 if (Subtarget->is64Bit()) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001348 if (ValVT.isVector() && ValVT.getSizeInBits() == 64) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001349 ValToCopy = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001350 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Eric Christopher90eb4022010-07-22 00:26:08 +00001351 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1352 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001353
1354 // If we don't have SSE2 available, convert to v4f32 so the generated
1355 // register is legal.
1356 if (!Subtarget->hasSSE2())
1357 ValToCopy = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32,ValToCopy);
1358 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001359 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001360 }
Chris Lattner97a2a562010-08-26 05:24:29 +00001361
Dale Johannesendd64c412009-02-04 00:33:20 +00001362 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001363 Flag = Chain.getValue(1);
1364 }
Dan Gohman61a92132008-04-21 23:59:07 +00001365
1366 // The x86-64 ABI for returning structs by value requires that we copy
1367 // the sret argument into %rax for the return. We saved the argument into
1368 // a virtual register in the entry block, so now we copy the value out
1369 // and into %rax.
1370 if (Subtarget->is64Bit() &&
1371 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1372 MachineFunction &MF = DAG.getMachineFunction();
1373 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1374 unsigned Reg = FuncInfo->getSRetReturnReg();
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001375 assert(Reg &&
1376 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001377 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001378
Dale Johannesendd64c412009-02-04 00:33:20 +00001379 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001380 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001381
1382 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001383 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001384 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001385
Chris Lattner447ff682008-03-11 03:23:40 +00001386 RetOps[0] = Chain; // Update chain.
1387
1388 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001389 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001390 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001391
1392 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001393 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001394}
1395
Dan Gohman98ca4f22009-08-05 01:29:28 +00001396/// LowerCallResult - Lower the result values of a call into the
1397/// appropriate copies out of appropriate physical registers.
1398///
1399SDValue
1400X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001401 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001402 const SmallVectorImpl<ISD::InputArg> &Ins,
1403 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001404 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001405
Chris Lattnere32bbf62007-02-28 07:09:55 +00001406 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001407 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001408 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001409 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001410 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001411 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001412
Chris Lattner3085e152007-02-25 08:59:22 +00001413 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001414 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001415 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001416 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001417
Torok Edwin3f142c32009-02-01 18:15:56 +00001418 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001419 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Dan Gohman98ca4f22009-08-05 01:29:28 +00001420 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001421 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001422 }
1423
Evan Cheng79fb3b42009-02-20 20:43:02 +00001424 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001425
1426 // If this is a call to a function that returns an fp value on the floating
1427 // point stack, we must guarantee the the value is popped from the stack, so
1428 // a CopyFromReg is not good enough - the copy instruction may be eliminated
1429 // if the return value is not used. We use the FpGET_ST0 instructions
1430 // instead.
1431 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1432 // If we prefer to use the value in xmm registers, copy it out as f80 and
1433 // use a truncate to move it from fp stack reg to xmm reg.
1434 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
1435 bool isST0 = VA.getLocReg() == X86::ST0;
1436 unsigned Opc = 0;
1437 if (CopyVT == MVT::f32) Opc = isST0 ? X86::FpGET_ST0_32:X86::FpGET_ST1_32;
1438 if (CopyVT == MVT::f64) Opc = isST0 ? X86::FpGET_ST0_64:X86::FpGET_ST1_64;
1439 if (CopyVT == MVT::f80) Opc = isST0 ? X86::FpGET_ST0_80:X86::FpGET_ST1_80;
1440 SDValue Ops[] = { Chain, InFlag };
1441 Chain = SDValue(DAG.getMachineNode(Opc, dl, CopyVT, MVT::Other, MVT::Flag,
1442 Ops, 2), 1);
1443 Val = Chain.getValue(0);
1444
1445 // Round the f80 to the right size, which also moves it to the appropriate
1446 // xmm register.
1447 if (CopyVT != VA.getValVT())
1448 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1449 // This truncation won't change the value.
1450 DAG.getIntPtrConstant(1));
1451 } else if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001452 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1453 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1454 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001455 MVT::v2i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001456 Val = Chain.getValue(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001457 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1458 Val, DAG.getConstant(0, MVT::i64));
Evan Cheng242b38b2009-02-23 09:03:22 +00001459 } else {
1460 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001461 MVT::i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001462 Val = Chain.getValue(0);
1463 }
Evan Cheng79fb3b42009-02-20 20:43:02 +00001464 Val = DAG.getNode(ISD::BIT_CONVERT, dl, CopyVT, Val);
1465 } else {
1466 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1467 CopyVT, InFlag).getValue(1);
1468 Val = Chain.getValue(0);
1469 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001470 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001471 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001472 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001473
Dan Gohman98ca4f22009-08-05 01:29:28 +00001474 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001475}
1476
1477
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001478//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001479// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001480//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001481// StdCall calling convention seems to be standard for many Windows' API
1482// routines and around. It differs from C calling convention just a little:
1483// callee should clean up the stack, not caller. Symbols should be also
1484// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001485// For info on fast calling convention see Fast Calling Convention (tail call)
1486// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001487
Dan Gohman98ca4f22009-08-05 01:29:28 +00001488/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001489/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001490static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1491 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001492 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001493
Dan Gohman98ca4f22009-08-05 01:29:28 +00001494 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001495}
1496
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001497/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001498/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001499static bool
1500ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1501 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001502 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001503
Dan Gohman98ca4f22009-08-05 01:29:28 +00001504 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001505}
1506
Dan Gohman095cc292008-09-13 01:54:27 +00001507/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1508/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001509CCAssignFn *X86TargetLowering::CCAssignFnForNode(CallingConv::ID CC) const {
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001510 if (Subtarget->is64Bit()) {
Chris Lattner29689432010-03-11 00:22:57 +00001511 if (CC == CallingConv::GHC)
1512 return CC_X86_64_GHC;
1513 else if (Subtarget->isTargetWin64())
Anton Korobeynikov8f88cb02008-03-22 20:37:30 +00001514 return CC_X86_Win64_C;
Evan Chenge9ac9e62008-09-07 09:07:23 +00001515 else
1516 return CC_X86_64_C;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001517 }
1518
Gordon Henriksen86737662008-01-05 16:56:59 +00001519 if (CC == CallingConv::X86_FastCall)
1520 return CC_X86_32_FastCall;
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001521 else if (CC == CallingConv::X86_ThisCall)
1522 return CC_X86_32_ThisCall;
Evan Chengb188dd92008-09-10 18:25:29 +00001523 else if (CC == CallingConv::Fast)
1524 return CC_X86_32_FastCC;
Chris Lattner29689432010-03-11 00:22:57 +00001525 else if (CC == CallingConv::GHC)
1526 return CC_X86_32_GHC;
Gordon Henriksen86737662008-01-05 16:56:59 +00001527 else
1528 return CC_X86_32_C;
1529}
1530
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001531/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1532/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001533/// the specific parameter attribute. The copy will be passed as a byval
1534/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001535static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001536CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001537 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1538 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001539 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
1540
Dale Johannesendd64c412009-02-04 00:33:20 +00001541 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001542 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001543 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001544}
1545
Chris Lattner29689432010-03-11 00:22:57 +00001546/// IsTailCallConvention - Return true if the calling convention is one that
1547/// supports tail call optimization.
1548static bool IsTailCallConvention(CallingConv::ID CC) {
1549 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1550}
1551
Evan Cheng0c439eb2010-01-27 00:07:07 +00001552/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1553/// a tailcall target by changing its ABI.
1554static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001555 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001556}
1557
Dan Gohman98ca4f22009-08-05 01:29:28 +00001558SDValue
1559X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001560 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001561 const SmallVectorImpl<ISD::InputArg> &Ins,
1562 DebugLoc dl, SelectionDAG &DAG,
1563 const CCValAssign &VA,
1564 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001565 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001566 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001567 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001568 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001569 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001570 EVT ValVT;
1571
1572 // If value is passed by pointer we have address passed instead of the value
1573 // itself.
1574 if (VA.getLocInfo() == CCValAssign::Indirect)
1575 ValVT = VA.getLocVT();
1576 else
1577 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001578
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001579 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001580 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001581 // In case of tail call optimization mark all arguments mutable. Since they
1582 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001583 if (Flags.isByVal()) {
1584 int FI = MFI->CreateFixedObject(Flags.getByValSize(),
Evan Chenged2ae132010-07-03 00:40:23 +00001585 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001586 return DAG.getFrameIndex(FI, getPointerTy());
1587 } else {
1588 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001589 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001590 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1591 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001592 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001593 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001594 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001595}
1596
Dan Gohman475871a2008-07-27 21:46:04 +00001597SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001598X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001599 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001600 bool isVarArg,
1601 const SmallVectorImpl<ISD::InputArg> &Ins,
1602 DebugLoc dl,
1603 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001604 SmallVectorImpl<SDValue> &InVals)
1605 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001606 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001607 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001608
Gordon Henriksen86737662008-01-05 16:56:59 +00001609 const Function* Fn = MF.getFunction();
1610 if (Fn->hasExternalLinkage() &&
1611 Subtarget->isTargetCygMing() &&
1612 Fn->getName() == "main")
1613 FuncInfo->setForceFramePointer(true);
1614
Evan Cheng1bc78042006-04-26 01:20:17 +00001615 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001616 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001617 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001618
Chris Lattner29689432010-03-11 00:22:57 +00001619 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1620 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001621
Chris Lattner638402b2007-02-28 07:00:42 +00001622 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001623 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001624 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1625 ArgLocs, *DAG.getContext());
1626 CCInfo.AnalyzeFormalArguments(Ins, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001627
Chris Lattnerf39f7712007-02-28 05:46:49 +00001628 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001629 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001630 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1631 CCValAssign &VA = ArgLocs[i];
1632 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1633 // places.
1634 assert(VA.getValNo() != LastVal &&
1635 "Don't support value assigned to multiple locs yet");
1636 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001637
Chris Lattnerf39f7712007-02-28 05:46:49 +00001638 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001639 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001640 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001641 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001642 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001643 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001644 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001645 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001646 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001647 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001648 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001649 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1650 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001651 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001652 RC = X86::VR128RegisterClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001653 else if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
1654 RC = X86::VR64RegisterClass;
1655 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001656 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001657
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001658 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001659 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001660
Chris Lattnerf39f7712007-02-28 05:46:49 +00001661 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1662 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1663 // right size.
1664 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001665 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001666 DAG.getValueType(VA.getValVT()));
1667 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001668 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001669 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001670 else if (VA.getLocInfo() == CCValAssign::BCvt)
Anton Korobeynikov6dde14b2009-08-03 08:14:14 +00001671 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001672
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001673 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001674 // Handle MMX values passed in XMM regs.
1675 if (RegVT.isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001676 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1677 ArgValue, DAG.getConstant(0, MVT::i64));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001678 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1679 } else
1680 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001681 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001682 } else {
1683 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001684 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001685 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001686
1687 // If value is passed via pointer - do a load.
1688 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001689 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
1690 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001691
Dan Gohman98ca4f22009-08-05 01:29:28 +00001692 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001693 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001694
Dan Gohman61a92132008-04-21 23:59:07 +00001695 // The x86-64 ABI for returning structs by value requires that we copy
1696 // the sret argument into %rax for the return. Save the argument into
1697 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001698 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001699 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1700 unsigned Reg = FuncInfo->getSRetReturnReg();
1701 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001702 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001703 FuncInfo->setSRetReturnReg(Reg);
1704 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001705 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001706 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001707 }
1708
Chris Lattnerf39f7712007-02-28 05:46:49 +00001709 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001710 // Align stack specially for tail calls.
1711 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001712 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001713
Evan Cheng1bc78042006-04-26 01:20:17 +00001714 // If the function takes variable number of arguments, make a frame index for
1715 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001716 if (isVarArg) {
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001717 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1718 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001719 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001720 }
1721 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001722 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1723
1724 // FIXME: We should really autogenerate these arrays
1725 static const unsigned GPR64ArgRegsWin64[] = {
1726 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001727 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001728 static const unsigned XMMArgRegsWin64[] = {
1729 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1730 };
1731 static const unsigned GPR64ArgRegs64Bit[] = {
1732 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1733 };
1734 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001735 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1736 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1737 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001738 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1739
1740 if (IsWin64) {
1741 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1742 GPR64ArgRegs = GPR64ArgRegsWin64;
1743 XMMArgRegs = XMMArgRegsWin64;
1744 } else {
1745 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1746 GPR64ArgRegs = GPR64ArgRegs64Bit;
1747 XMMArgRegs = XMMArgRegs64Bit;
1748 }
1749 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1750 TotalNumIntRegs);
1751 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1752 TotalNumXMMRegs);
1753
Devang Patel578efa92009-06-05 21:57:13 +00001754 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Evan Chengc7ce29b2009-02-13 22:36:38 +00001755 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001756 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001757 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001758 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001759 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00001760 // Kernel mode asks for SSE to be disabled, so don't push them
1761 // on the stack.
1762 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001763
Gordon Henriksen86737662008-01-05 16:56:59 +00001764 // For X86-64, if there are vararg parameters that are passed via
1765 // registers, then we must store them to their spots on the stack so they
1766 // may be loaded by deferencing the result of va_next.
Dan Gohman1e93df62010-04-17 14:41:14 +00001767 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1768 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1769 FuncInfo->setRegSaveFrameIndex(
1770 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
1771 false));
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001772
Gordon Henriksen86737662008-01-05 16:56:59 +00001773 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001774 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001775 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1776 getPointerTy());
1777 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001778 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001779 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1780 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001781 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
1782 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001783 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001784 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001785 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001786 MachinePointerInfo::getFixedStack(
1787 FuncInfo->getRegSaveFrameIndex(), Offset),
1788 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001789 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001790 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001791 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001792
Dan Gohmanface41a2009-08-16 21:24:25 +00001793 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1794 // Now store the XMM (fp + vector) parameter registers.
1795 SmallVector<SDValue, 11> SaveXMMOps;
1796 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001797
Dan Gohmanface41a2009-08-16 21:24:25 +00001798 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
1799 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1800 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001801
Dan Gohman1e93df62010-04-17 14:41:14 +00001802 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1803 FuncInfo->getRegSaveFrameIndex()));
1804 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1805 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001806
Dan Gohmanface41a2009-08-16 21:24:25 +00001807 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
1808 unsigned VReg = MF.addLiveIn(XMMArgRegs[NumXMMRegs],
1809 X86::VR128RegisterClass);
1810 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1811 SaveXMMOps.push_back(Val);
1812 }
1813 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1814 MVT::Other,
1815 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001816 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001817
1818 if (!MemOps.empty())
1819 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1820 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001821 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001822 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001823
Gordon Henriksen86737662008-01-05 16:56:59 +00001824 // Some CCs need callee pop.
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001825 if (Subtarget->IsCalleePop(isVarArg, CallConv)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001826 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001827 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001828 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001829 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001830 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001831 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001832 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001833
Gordon Henriksen86737662008-01-05 16:56:59 +00001834 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001835 // RegSaveFrameIndex is X86-64 only.
1836 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001837 if (CallConv == CallingConv::X86_FastCall ||
1838 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001839 // fastcc functions can't have varargs.
1840 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001841 }
Evan Cheng25caf632006-05-23 21:06:34 +00001842
Dan Gohman98ca4f22009-08-05 01:29:28 +00001843 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001844}
1845
Dan Gohman475871a2008-07-27 21:46:04 +00001846SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001847X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1848 SDValue StackPtr, SDValue Arg,
1849 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001850 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001851 ISD::ArgFlagsTy Flags) const {
Anton Korobeynikovc7c62bb2010-09-02 22:31:32 +00001852 const unsigned FirstStackArgOffset = (Subtarget->isTargetWin64() ? 32 : 0);
1853 unsigned LocMemOffset = FirstStackArgOffset + VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001854 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001855 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001856 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00001857 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001858
1859 return DAG.getStore(Chain, dl, Arg, PtrOff,
1860 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00001861 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001862}
1863
Bill Wendling64e87322009-01-16 19:25:27 +00001864/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001865/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001866SDValue
1867X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001868 SDValue &OutRetAddr, SDValue Chain,
1869 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001870 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001871 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001872 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001873 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001874
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001875 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00001876 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
1877 false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001878 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001879}
1880
1881/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1882/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001883static SDValue
1884EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001885 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001886 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001887 // Store the return address to the appropriate stack slot.
1888 if (!FPDiff) return Chain;
1889 // Calculate the new stack slot for the return address.
1890 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001891 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00001892 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001893 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001894 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001895 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00001896 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00001897 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001898 return Chain;
1899}
1900
Dan Gohman98ca4f22009-08-05 01:29:28 +00001901SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001902X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001903 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001904 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001905 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001906 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001907 const SmallVectorImpl<ISD::InputArg> &Ins,
1908 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001909 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001910 MachineFunction &MF = DAG.getMachineFunction();
1911 bool Is64Bit = Subtarget->is64Bit();
1912 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00001913 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001914
Evan Cheng5f941932010-02-05 02:21:12 +00001915 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001916 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00001917 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1918 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001919 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00001920
1921 // Sibcalls are automatically detected tailcalls which do not require
1922 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00001923 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00001924 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00001925
1926 if (isTailCall)
1927 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00001928 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00001929
Chris Lattner29689432010-03-11 00:22:57 +00001930 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1931 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001932
Chris Lattner638402b2007-02-28 07:00:42 +00001933 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001934 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001935 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1936 ArgLocs, *DAG.getContext());
1937 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001938
Chris Lattner423c5f42007-02-28 05:31:48 +00001939 // Get a count of how many bytes are to be pushed on the stack.
1940 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00001941 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00001942 // This is a sibcall. The memory operands are available in caller's
1943 // own caller's stack.
1944 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00001945 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00001946 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001947
Gordon Henriksen86737662008-01-05 16:56:59 +00001948 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00001949 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001950 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00001951 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00001952 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1953 FPDiff = NumBytesCallerPushed - NumBytes;
1954
1955 // Set the delta of movement of the returnaddr stackslot.
1956 // But only set if delta is greater than previous delta.
1957 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1958 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1959 }
1960
Evan Chengf22f9b32010-02-06 03:28:46 +00001961 if (!IsSibcall)
1962 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001963
Dan Gohman475871a2008-07-27 21:46:04 +00001964 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001965 // Load return adress for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00001966 if (isTailCall && FPDiff)
1967 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
1968 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001969
Dan Gohman475871a2008-07-27 21:46:04 +00001970 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1971 SmallVector<SDValue, 8> MemOpChains;
1972 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001973
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001974 // Walk the register/memloc assignments, inserting copies/loads. In the case
1975 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00001976 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1977 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001978 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001979 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001980 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00001981 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00001982
Chris Lattner423c5f42007-02-28 05:31:48 +00001983 // Promote the value if needed.
1984 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001985 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00001986 case CCValAssign::Full: break;
1987 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001988 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001989 break;
1990 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001991 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001992 break;
1993 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001994 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
1995 // Special case: passing MMX values in XMM registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001996 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
1997 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
1998 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001999 } else
2000 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2001 break;
2002 case CCValAssign::BCvt:
2003 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002004 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002005 case CCValAssign::Indirect: {
2006 // Store the argument.
2007 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002008 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002009 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002010 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002011 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002012 Arg = SpillSlot;
2013 break;
2014 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002015 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002016
Chris Lattner423c5f42007-02-28 05:31:48 +00002017 if (VA.isRegLoc()) {
2018 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002019 if (isVarArg && Subtarget->isTargetWin64()) {
2020 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2021 // shadow reg if callee is a varargs function.
2022 unsigned ShadowReg = 0;
2023 switch (VA.getLocReg()) {
2024 case X86::XMM0: ShadowReg = X86::RCX; break;
2025 case X86::XMM1: ShadowReg = X86::RDX; break;
2026 case X86::XMM2: ShadowReg = X86::R8; break;
2027 case X86::XMM3: ShadowReg = X86::R9; break;
2028 }
2029 if (ShadowReg)
2030 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
2031 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002032 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002033 assert(VA.isMemLoc());
2034 if (StackPtr.getNode() == 0)
2035 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2036 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2037 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002038 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002039 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002040
Evan Cheng32fe1032006-05-25 00:59:30 +00002041 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002042 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002043 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002044
Evan Cheng347d5f72006-04-28 21:29:37 +00002045 // Build a sequence of copy-to-reg nodes chained together with token chain
2046 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002047 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002048 // Tail call byval lowering might overwrite argument registers so in case of
2049 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002050 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002051 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002052 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002053 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002054 InFlag = Chain.getValue(1);
2055 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002056
Chris Lattner88e1fd52009-07-09 04:24:46 +00002057 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002058 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2059 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002060 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002061 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2062 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002063 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002064 InFlag);
2065 InFlag = Chain.getValue(1);
2066 } else {
2067 // If we are tail calling and generating PIC/GOT style code load the
2068 // address of the callee into ECX. The value in ecx is used as target of
2069 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2070 // for tail calls on PIC/GOT architectures. Normally we would just put the
2071 // address of GOT into ebx and then call target@PLT. But for tail calls
2072 // ebx would be restored (since ebx is callee saved) before jumping to the
2073 // target@PLT.
2074
2075 // Note: The actual moving to ECX is done further down.
2076 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2077 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2078 !G->getGlobal()->hasProtectedVisibility())
2079 Callee = LowerGlobalAddress(Callee, DAG);
2080 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002081 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002082 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002083 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002084
Nate Begemanc8ea6732010-07-21 20:49:52 +00002085 if (Is64Bit && isVarArg && !Subtarget->isTargetWin64()) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002086 // From AMD64 ABI document:
2087 // For calls that may call functions that use varargs or stdargs
2088 // (prototype-less calls or calls to functions containing ellipsis (...) in
2089 // the declaration) %al is used as hidden argument to specify the number
2090 // of SSE registers used. The contents of %al do not need to match exactly
2091 // the number of registers, but must be an ubound on the number of SSE
2092 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002093
Gordon Henriksen86737662008-01-05 16:56:59 +00002094 // Count the number of XMM registers allocated.
2095 static const unsigned XMMArgRegs[] = {
2096 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2097 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2098 };
2099 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Scott Michelfdc40a02009-02-17 22:15:04 +00002100 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002101 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002102
Dale Johannesendd64c412009-02-04 00:33:20 +00002103 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002104 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002105 InFlag = Chain.getValue(1);
2106 }
2107
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002108
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002109 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002110 if (isTailCall) {
2111 // Force all the incoming stack arguments to be loaded from the stack
2112 // before any new outgoing arguments are stored to the stack, because the
2113 // outgoing stack slots may alias the incoming argument stack slots, and
2114 // the alias isn't otherwise explicit. This is slightly more conservative
2115 // than necessary, because it means that each store effectively depends
2116 // on every argument instead of just those arguments it would clobber.
2117 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2118
Dan Gohman475871a2008-07-27 21:46:04 +00002119 SmallVector<SDValue, 8> MemOpChains2;
2120 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002121 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002122 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002123 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002124 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002125 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2126 CCValAssign &VA = ArgLocs[i];
2127 if (VA.isRegLoc())
2128 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002129 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002130 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002131 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002132 // Create frame index.
2133 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002134 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002135 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002136 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002137
Duncan Sands276dcbd2008-03-21 09:14:45 +00002138 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002139 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002140 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002141 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002142 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002143 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002144 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002145
Dan Gohman98ca4f22009-08-05 01:29:28 +00002146 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2147 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002148 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002149 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002150 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002151 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002152 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002153 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002154 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002155 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002156 }
2157 }
2158
2159 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002160 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002161 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002162
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002163 // Copy arguments to their registers.
2164 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002165 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002166 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002167 InFlag = Chain.getValue(1);
2168 }
Dan Gohman475871a2008-07-27 21:46:04 +00002169 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002170
Gordon Henriksen86737662008-01-05 16:56:59 +00002171 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002172 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002173 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002174 }
2175
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002176 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2177 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2178 // In the 64-bit large code model, we have to make all calls
2179 // through a register, since the call instruction's 32-bit
2180 // pc-relative offset may not be large enough to hold the whole
2181 // address.
2182 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002183 // If the callee is a GlobalAddress node (quite common, every direct call
2184 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2185 // it.
2186
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002187 // We should use extra load for direct calls to dllimported functions in
2188 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002189 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002190 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002191 unsigned char OpFlags = 0;
Eric Christopherfd179292009-08-27 18:07:15 +00002192
Chris Lattner48a7d022009-07-09 05:02:21 +00002193 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2194 // external symbols most go through the PLT in PIC mode. If the symbol
2195 // has hidden or protected visibility, or if it is static or local, then
2196 // we don't need to use the PLT - we can directly call it.
2197 if (Subtarget->isTargetELF() &&
2198 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002199 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002200 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002201 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002202 (GV->isDeclaration() || GV->isWeakForLinker()) &&
2203 Subtarget->getDarwinVers() < 9) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002204 // PC-relative references to external symbols should go through $stub,
2205 // unless we're building with the leopard linker or later, which
2206 // automatically synthesizes these stubs.
2207 OpFlags = X86II::MO_DARWIN_STUB;
2208 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002209
Devang Patel0d881da2010-07-06 22:08:15 +00002210 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002211 G->getOffset(), OpFlags);
2212 }
Bill Wendling056292f2008-09-16 21:48:12 +00002213 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002214 unsigned char OpFlags = 0;
2215
2216 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to external
2217 // symbols should go through the PLT.
2218 if (Subtarget->isTargetELF() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002219 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002220 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002221 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002222 Subtarget->getDarwinVers() < 9) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002223 // PC-relative references to external symbols should go through $stub,
2224 // unless we're building with the leopard linker or later, which
2225 // automatically synthesizes these stubs.
2226 OpFlags = X86II::MO_DARWIN_STUB;
2227 }
Eric Christopherfd179292009-08-27 18:07:15 +00002228
Chris Lattner48a7d022009-07-09 05:02:21 +00002229 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2230 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002231 }
2232
Chris Lattnerd96d0722007-02-25 06:40:16 +00002233 // Returns a chain & a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +00002234 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00002235 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002236
Evan Chengf22f9b32010-02-06 03:28:46 +00002237 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002238 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2239 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002240 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002241 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002242
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002243 Ops.push_back(Chain);
2244 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002245
Dan Gohman98ca4f22009-08-05 01:29:28 +00002246 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002247 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002248
Gordon Henriksen86737662008-01-05 16:56:59 +00002249 // Add argument registers to the end of the list so that they are known live
2250 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002251 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2252 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2253 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002254
Evan Cheng586ccac2008-03-18 23:36:35 +00002255 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002256 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002257 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2258
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002259 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
2260 if (Is64Bit && isVarArg && !Subtarget->isTargetWin64())
Owen Anderson825b72b2009-08-11 20:47:22 +00002261 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002262
Gabor Greifba36cb52008-08-28 21:40:38 +00002263 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002264 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002265
Dan Gohman98ca4f22009-08-05 01:29:28 +00002266 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002267 // We used to do:
2268 //// If this is the first return lowered for this function, add the regs
2269 //// to the liveout set for the function.
2270 // This isn't right, although it's probably harmless on x86; liveouts
2271 // should be computed from returns not tail calls. Consider a void
2272 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002273 return DAG.getNode(X86ISD::TC_RETURN, dl,
2274 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002275 }
2276
Dale Johannesenace16102009-02-03 19:33:06 +00002277 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002278 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002279
Chris Lattner2d297092006-05-23 18:50:38 +00002280 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002281 unsigned NumBytesForCalleeToPush;
Dan Gohman4d3d6e12010-05-27 18:43:40 +00002282 if (Subtarget->IsCalleePop(isVarArg, CallConv))
Gordon Henriksen86737662008-01-05 16:56:59 +00002283 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002284 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002285 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002286 // pops the hidden struct pointer, so we have to push it back.
2287 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002288 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002289 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002290 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002291
Gordon Henriksenae636f82008-01-03 16:47:34 +00002292 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002293 if (!IsSibcall) {
2294 Chain = DAG.getCALLSEQ_END(Chain,
2295 DAG.getIntPtrConstant(NumBytes, true),
2296 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2297 true),
2298 InFlag);
2299 InFlag = Chain.getValue(1);
2300 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002301
Chris Lattner3085e152007-02-25 08:59:22 +00002302 // Handle result values, copying them out of physregs into vregs that we
2303 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002304 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2305 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002306}
2307
Evan Cheng25ab6902006-09-08 06:48:29 +00002308
2309//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002310// Fast Calling Convention (tail call) implementation
2311//===----------------------------------------------------------------------===//
2312
2313// Like std call, callee cleans arguments, convention except that ECX is
2314// reserved for storing the tail called function address. Only 2 registers are
2315// free for argument passing (inreg). Tail call optimization is performed
2316// provided:
2317// * tailcallopt is enabled
2318// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002319// On X86_64 architecture with GOT-style position independent code only local
2320// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002321// To keep the stack aligned according to platform abi the function
2322// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2323// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002324// If a tail called function callee has more arguments than the caller the
2325// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002326// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002327// original REtADDR, but before the saved framepointer or the spilled registers
2328// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2329// stack layout:
2330// arg1
2331// arg2
2332// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002333// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002334// move area ]
2335// (possible EBP)
2336// ESI
2337// EDI
2338// local1 ..
2339
2340/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2341/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002342unsigned
2343X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2344 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002345 MachineFunction &MF = DAG.getMachineFunction();
2346 const TargetMachine &TM = MF.getTarget();
2347 const TargetFrameInfo &TFI = *TM.getFrameInfo();
2348 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002349 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002350 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002351 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002352 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2353 // Number smaller than 12 so just add the difference.
2354 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2355 } else {
2356 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002357 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002358 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002359 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002360 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002361}
2362
Evan Cheng5f941932010-02-05 02:21:12 +00002363/// MatchingStackOffset - Return true if the given stack call argument is
2364/// already available in the same position (relatively) of the caller's
2365/// incoming argument stack.
2366static
2367bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2368 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2369 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002370 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2371 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002372 if (Arg.getOpcode() == ISD::CopyFromReg) {
2373 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
2374 if (!VR || TargetRegisterInfo::isPhysicalRegister(VR))
2375 return false;
2376 MachineInstr *Def = MRI->getVRegDef(VR);
2377 if (!Def)
2378 return false;
2379 if (!Flags.isByVal()) {
2380 if (!TII->isLoadFromStackSlot(Def, FI))
2381 return false;
2382 } else {
2383 unsigned Opcode = Def->getOpcode();
2384 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2385 Def->getOperand(1).isFI()) {
2386 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002387 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002388 } else
2389 return false;
2390 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002391 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2392 if (Flags.isByVal())
2393 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002394 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002395 // define @foo(%struct.X* %A) {
2396 // tail call @bar(%struct.X* byval %A)
2397 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002398 return false;
2399 SDValue Ptr = Ld->getBasePtr();
2400 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2401 if (!FINode)
2402 return false;
2403 FI = FINode->getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002404 } else
2405 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002406
Evan Cheng4cae1332010-03-05 08:38:04 +00002407 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002408 if (!MFI->isFixedObjectIndex(FI))
2409 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002410 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002411}
2412
Dan Gohman98ca4f22009-08-05 01:29:28 +00002413/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2414/// for tail call optimization. Targets which want to do tail call
2415/// optimization should implement this function.
2416bool
2417X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002418 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002419 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002420 bool isCalleeStructRet,
2421 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002422 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002423 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002424 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002425 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002426 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002427 CalleeCC != CallingConv::C)
2428 return false;
2429
Evan Cheng7096ae42010-01-29 06:45:59 +00002430 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002431 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002432 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002433 CallingConv::ID CallerCC = CallerF->getCallingConv();
2434 bool CCMatch = CallerCC == CalleeCC;
2435
Dan Gohman1797ed52010-02-08 20:27:50 +00002436 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002437 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002438 return true;
2439 return false;
2440 }
2441
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002442 // Look for obvious safe cases to perform tail call optimization that do not
2443 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002444
Evan Cheng2c12cb42010-03-26 16:26:03 +00002445 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2446 // emit a special epilogue.
2447 if (RegInfo->needsStackRealignment(MF))
2448 return false;
2449
Eric Christopher90eb4022010-07-22 00:26:08 +00002450 // Do not sibcall optimize vararg calls unless the call site is not passing
2451 // any arguments.
Evan Cheng3c262ee2010-03-26 02:13:13 +00002452 if (isVarArg && !Outs.empty())
Evan Cheng843bd692010-01-31 06:44:49 +00002453 return false;
2454
Evan Chenga375d472010-03-15 18:54:48 +00002455 // Also avoid sibcall optimization if either caller or callee uses struct
2456 // return semantics.
2457 if (isCalleeStructRet || isCallerStructRet)
2458 return false;
2459
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002460 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2461 // Therefore if it's not used by the call it is not safe to optimize this into
2462 // a sibcall.
2463 bool Unused = false;
2464 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2465 if (!Ins[i].Used) {
2466 Unused = true;
2467 break;
2468 }
2469 }
2470 if (Unused) {
2471 SmallVector<CCValAssign, 16> RVLocs;
2472 CCState CCInfo(CalleeCC, false, getTargetMachine(),
2473 RVLocs, *DAG.getContext());
2474 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002475 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002476 CCValAssign &VA = RVLocs[i];
2477 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2478 return false;
2479 }
2480 }
2481
Evan Cheng13617962010-04-30 01:12:32 +00002482 // If the calling conventions do not match, then we'd better make sure the
2483 // results are returned in the same way as what the caller expects.
2484 if (!CCMatch) {
2485 SmallVector<CCValAssign, 16> RVLocs1;
2486 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
2487 RVLocs1, *DAG.getContext());
2488 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2489
2490 SmallVector<CCValAssign, 16> RVLocs2;
2491 CCState CCInfo2(CallerCC, false, getTargetMachine(),
2492 RVLocs2, *DAG.getContext());
2493 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2494
2495 if (RVLocs1.size() != RVLocs2.size())
2496 return false;
2497 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2498 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2499 return false;
2500 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2501 return false;
2502 if (RVLocs1[i].isRegLoc()) {
2503 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2504 return false;
2505 } else {
2506 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2507 return false;
2508 }
2509 }
2510 }
2511
Evan Chenga6bff982010-01-30 01:22:00 +00002512 // If the callee takes no arguments then go on to check the results of the
2513 // call.
2514 if (!Outs.empty()) {
2515 // Check if stack adjustment is needed. For now, do not do this if any
2516 // argument is passed on the stack.
2517 SmallVector<CCValAssign, 16> ArgLocs;
2518 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
2519 ArgLocs, *DAG.getContext());
2520 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CalleeCC));
Evan Chengb2c92902010-02-02 02:22:50 +00002521 if (CCInfo.getNextStackOffset()) {
2522 MachineFunction &MF = DAG.getMachineFunction();
2523 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2524 return false;
2525 if (Subtarget->isTargetWin64())
2526 // Win64 ABI has additional complications.
2527 return false;
2528
2529 // Check if the arguments are already laid out in the right way as
2530 // the caller's fixed stack objects.
2531 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002532 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2533 const X86InstrInfo *TII =
2534 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002535 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2536 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002537 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002538 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002539 if (VA.getLocInfo() == CCValAssign::Indirect)
2540 return false;
2541 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002542 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2543 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002544 return false;
2545 }
2546 }
2547 }
Evan Cheng9c044672010-05-29 01:35:22 +00002548
2549 // If the tailcall address may be in a register, then make sure it's
2550 // possible to register allocate for it. In 32-bit, the call address can
2551 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002552 // callee-saved registers are restored. These happen to be the same
2553 // registers used to pass 'inreg' arguments so watch out for those.
2554 if (!Subtarget->is64Bit() &&
2555 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002556 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002557 unsigned NumInRegs = 0;
2558 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2559 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002560 if (!VA.isRegLoc())
2561 continue;
2562 unsigned Reg = VA.getLocReg();
2563 switch (Reg) {
2564 default: break;
2565 case X86::EAX: case X86::EDX: case X86::ECX:
2566 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002567 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002568 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002569 }
2570 }
2571 }
Evan Chenga6bff982010-01-30 01:22:00 +00002572 }
Evan Chengb1712452010-01-27 06:25:16 +00002573
Evan Cheng86809cc2010-02-03 03:28:02 +00002574 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002575}
2576
Dan Gohman3df24e62008-09-03 23:12:08 +00002577FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002578X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2579 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002580}
2581
2582
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002583//===----------------------------------------------------------------------===//
2584// Other Lowering Hooks
2585//===----------------------------------------------------------------------===//
2586
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002587static bool MayFoldLoad(SDValue Op) {
2588 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2589}
2590
2591static bool MayFoldIntoStore(SDValue Op) {
2592 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2593}
2594
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002595static bool isTargetShuffle(unsigned Opcode) {
2596 switch(Opcode) {
2597 default: return false;
2598 case X86ISD::PSHUFD:
2599 case X86ISD::PSHUFHW:
2600 case X86ISD::PSHUFLW:
2601 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002602 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002603 case X86ISD::SHUFPS:
2604 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002605 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002606 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002607 case X86ISD::MOVLPS:
2608 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002609 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002610 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002611 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002612 case X86ISD::MOVSS:
2613 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002614 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002615 case X86ISD::UNPCKLPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002616 case X86ISD::PUNPCKLWD:
2617 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002618 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002619 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002620 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002621 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002622 case X86ISD::PUNPCKHWD:
2623 case X86ISD::PUNPCKHBW:
2624 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002625 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002626 return true;
2627 }
2628 return false;
2629}
2630
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002631static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002632 SDValue V1, SelectionDAG &DAG) {
2633 switch(Opc) {
2634 default: llvm_unreachable("Unknown x86 shuffle node");
2635 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002636 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002637 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002638 return DAG.getNode(Opc, dl, VT, V1);
2639 }
2640
2641 return SDValue();
2642}
2643
2644static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002645 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002646 switch(Opc) {
2647 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002648 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002649 case X86ISD::PSHUFHW:
2650 case X86ISD::PSHUFLW:
2651 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2652 }
2653
2654 return SDValue();
2655}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002656
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002657static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2658 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2659 switch(Opc) {
2660 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002661 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002662 case X86ISD::SHUFPD:
2663 case X86ISD::SHUFPS:
2664 return DAG.getNode(Opc, dl, VT, V1, V2,
2665 DAG.getConstant(TargetMask, MVT::i8));
2666 }
2667 return SDValue();
2668}
2669
2670static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2671 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2672 switch(Opc) {
2673 default: llvm_unreachable("Unknown x86 shuffle node");
2674 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002675 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002676 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002677 case X86ISD::MOVLPS:
2678 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002679 case X86ISD::MOVSS:
2680 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002681 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002682 case X86ISD::UNPCKLPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002683 case X86ISD::PUNPCKLWD:
2684 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002685 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002686 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002687 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002688 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002689 case X86ISD::PUNPCKHWD:
2690 case X86ISD::PUNPCKHBW:
2691 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002692 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002693 return DAG.getNode(Opc, dl, VT, V1, V2);
2694 }
2695 return SDValue();
2696}
2697
Dan Gohmand858e902010-04-17 15:26:15 +00002698SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002699 MachineFunction &MF = DAG.getMachineFunction();
2700 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2701 int ReturnAddrIndex = FuncInfo->getRAIndex();
2702
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002703 if (ReturnAddrIndex == 0) {
2704 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002705 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002706 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002707 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002708 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002709 }
2710
Evan Cheng25ab6902006-09-08 06:48:29 +00002711 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002712}
2713
2714
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002715bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2716 bool hasSymbolicDisplacement) {
2717 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002718 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002719 return false;
2720
2721 // If we don't have a symbolic displacement - we don't have any extra
2722 // restrictions.
2723 if (!hasSymbolicDisplacement)
2724 return true;
2725
2726 // FIXME: Some tweaks might be needed for medium code model.
2727 if (M != CodeModel::Small && M != CodeModel::Kernel)
2728 return false;
2729
2730 // For small code model we assume that latest object is 16MB before end of 31
2731 // bits boundary. We may also accept pretty large negative constants knowing
2732 // that all objects are in the positive half of address space.
2733 if (M == CodeModel::Small && Offset < 16*1024*1024)
2734 return true;
2735
2736 // For kernel code model we know that all object resist in the negative half
2737 // of 32bits address space. We may not accept negative offsets, since they may
2738 // be just off and we may accept pretty large positive ones.
2739 if (M == CodeModel::Kernel && Offset > 0)
2740 return true;
2741
2742 return false;
2743}
2744
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002745/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2746/// specific condition code, returning the condition code and the LHS/RHS of the
2747/// comparison to make.
2748static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2749 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002750 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002751 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2752 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2753 // X > -1 -> X == 0, jump !sign.
2754 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002755 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002756 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2757 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002758 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002759 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002760 // X < 1 -> X <= 0
2761 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002762 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002763 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002764 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002765
Evan Chengd9558e02006-01-06 00:43:03 +00002766 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002767 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002768 case ISD::SETEQ: return X86::COND_E;
2769 case ISD::SETGT: return X86::COND_G;
2770 case ISD::SETGE: return X86::COND_GE;
2771 case ISD::SETLT: return X86::COND_L;
2772 case ISD::SETLE: return X86::COND_LE;
2773 case ISD::SETNE: return X86::COND_NE;
2774 case ISD::SETULT: return X86::COND_B;
2775 case ISD::SETUGT: return X86::COND_A;
2776 case ISD::SETULE: return X86::COND_BE;
2777 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002778 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002779 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002780
Chris Lattner4c78e022008-12-23 23:42:27 +00002781 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002782
Chris Lattner4c78e022008-12-23 23:42:27 +00002783 // If LHS is a foldable load, but RHS is not, flip the condition.
2784 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2785 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2786 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2787 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002788 }
2789
Chris Lattner4c78e022008-12-23 23:42:27 +00002790 switch (SetCCOpcode) {
2791 default: break;
2792 case ISD::SETOLT:
2793 case ISD::SETOLE:
2794 case ISD::SETUGT:
2795 case ISD::SETUGE:
2796 std::swap(LHS, RHS);
2797 break;
2798 }
2799
2800 // On a floating point condition, the flags are set as follows:
2801 // ZF PF CF op
2802 // 0 | 0 | 0 | X > Y
2803 // 0 | 0 | 1 | X < Y
2804 // 1 | 0 | 0 | X == Y
2805 // 1 | 1 | 1 | unordered
2806 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002807 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002808 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002809 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002810 case ISD::SETOLT: // flipped
2811 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002812 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002813 case ISD::SETOLE: // flipped
2814 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002815 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002816 case ISD::SETUGT: // flipped
2817 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002818 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002819 case ISD::SETUGE: // flipped
2820 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002821 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002822 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002823 case ISD::SETNE: return X86::COND_NE;
2824 case ISD::SETUO: return X86::COND_P;
2825 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002826 case ISD::SETOEQ:
2827 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002828 }
Evan Chengd9558e02006-01-06 00:43:03 +00002829}
2830
Evan Cheng4a460802006-01-11 00:33:36 +00002831/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2832/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002833/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002834static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002835 switch (X86CC) {
2836 default:
2837 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002838 case X86::COND_B:
2839 case X86::COND_BE:
2840 case X86::COND_E:
2841 case X86::COND_P:
2842 case X86::COND_A:
2843 case X86::COND_AE:
2844 case X86::COND_NE:
2845 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002846 return true;
2847 }
2848}
2849
Evan Chengeb2f9692009-10-27 19:56:55 +00002850/// isFPImmLegal - Returns true if the target can instruction select the
2851/// specified FP immediate natively. If false, the legalizer will
2852/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002853bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00002854 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
2855 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
2856 return true;
2857 }
2858 return false;
2859}
2860
Nate Begeman9008ca62009-04-27 18:41:29 +00002861/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2862/// the specified range (L, H].
2863static bool isUndefOrInRange(int Val, int Low, int Hi) {
2864 return (Val < 0) || (Val >= Low && Val < Hi);
2865}
2866
2867/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2868/// specified value.
2869static bool isUndefOrEqual(int Val, int CmpVal) {
2870 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002871 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002872 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002873}
2874
Nate Begeman9008ca62009-04-27 18:41:29 +00002875/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2876/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2877/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00002878static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002879 if (VT == MVT::v4f32 || VT == MVT::v4i32 || VT == MVT::v4i16)
Nate Begeman9008ca62009-04-27 18:41:29 +00002880 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002881 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00002882 return (Mask[0] < 2 && Mask[1] < 2);
2883 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002884}
2885
Nate Begeman9008ca62009-04-27 18:41:29 +00002886bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002887 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002888 N->getMask(M);
2889 return ::isPSHUFDMask(M, N->getValueType(0));
2890}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002891
Nate Begeman9008ca62009-04-27 18:41:29 +00002892/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2893/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00002894static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002895 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002896 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002897
Nate Begeman9008ca62009-04-27 18:41:29 +00002898 // Lower quadword copied in order or undef.
2899 for (int i = 0; i != 4; ++i)
2900 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002901 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002902
Evan Cheng506d3df2006-03-29 23:07:14 +00002903 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002904 for (int i = 4; i != 8; ++i)
2905 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002906 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002907
Evan Cheng506d3df2006-03-29 23:07:14 +00002908 return true;
2909}
2910
Nate Begeman9008ca62009-04-27 18:41:29 +00002911bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002912 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002913 N->getMask(M);
2914 return ::isPSHUFHWMask(M, N->getValueType(0));
2915}
Evan Cheng506d3df2006-03-29 23:07:14 +00002916
Nate Begeman9008ca62009-04-27 18:41:29 +00002917/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2918/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00002919static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002920 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002921 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002922
Rafael Espindola15684b22009-04-24 12:40:33 +00002923 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00002924 for (int i = 4; i != 8; ++i)
2925 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00002926 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002927
Rafael Espindola15684b22009-04-24 12:40:33 +00002928 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002929 for (int i = 0; i != 4; ++i)
2930 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00002931 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002932
Rafael Espindola15684b22009-04-24 12:40:33 +00002933 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002934}
2935
Nate Begeman9008ca62009-04-27 18:41:29 +00002936bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002937 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002938 N->getMask(M);
2939 return ::isPSHUFLWMask(M, N->getValueType(0));
2940}
2941
Nate Begemana09008b2009-10-19 02:17:23 +00002942/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
2943/// is suitable for input to PALIGNR.
2944static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
2945 bool hasSSSE3) {
2946 int i, e = VT.getVectorNumElements();
2947
2948 // Do not handle v2i64 / v2f64 shuffles with palignr.
2949 if (e < 4 || !hasSSSE3)
2950 return false;
2951
2952 for (i = 0; i != e; ++i)
2953 if (Mask[i] >= 0)
2954 break;
2955
2956 // All undef, not a palignr.
2957 if (i == e)
2958 return false;
2959
2960 // Determine if it's ok to perform a palignr with only the LHS, since we
2961 // don't have access to the actual shuffle elements to see if RHS is undef.
2962 bool Unary = Mask[i] < (int)e;
2963 bool NeedsUnary = false;
2964
2965 int s = Mask[i] - i;
2966
2967 // Check the rest of the elements to see if they are consecutive.
2968 for (++i; i != e; ++i) {
2969 int m = Mask[i];
2970 if (m < 0)
2971 continue;
2972
2973 Unary = Unary && (m < (int)e);
2974 NeedsUnary = NeedsUnary || (m < s);
2975
2976 if (NeedsUnary && !Unary)
2977 return false;
2978 if (Unary && m != ((s+i) & (e-1)))
2979 return false;
2980 if (!Unary && m != (s+i))
2981 return false;
2982 }
2983 return true;
2984}
2985
2986bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
2987 SmallVector<int, 8> M;
2988 N->getMask(M);
2989 return ::isPALIGNRMask(M, N->getValueType(0), true);
2990}
2991
Evan Cheng14aed5e2006-03-24 01:18:28 +00002992/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2993/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00002994static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002995 int NumElems = VT.getVectorNumElements();
2996 if (NumElems != 2 && NumElems != 4)
2997 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002998
Nate Begeman9008ca62009-04-27 18:41:29 +00002999 int Half = NumElems / 2;
3000 for (int i = 0; i < Half; ++i)
3001 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003002 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003003 for (int i = Half; i < NumElems; ++i)
3004 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003005 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003006
Evan Cheng14aed5e2006-03-24 01:18:28 +00003007 return true;
3008}
3009
Nate Begeman9008ca62009-04-27 18:41:29 +00003010bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
3011 SmallVector<int, 8> M;
3012 N->getMask(M);
3013 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003014}
3015
Evan Cheng213d2cf2007-05-17 18:45:50 +00003016/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00003017/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
3018/// half elements to come from vector 1 (which would equal the dest.) and
3019/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00003020static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003021 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003022
3023 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003024 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003025
Nate Begeman9008ca62009-04-27 18:41:29 +00003026 int Half = NumElems / 2;
3027 for (int i = 0; i < Half; ++i)
3028 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003029 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003030 for (int i = Half; i < NumElems; ++i)
3031 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003032 return false;
3033 return true;
3034}
3035
Nate Begeman9008ca62009-04-27 18:41:29 +00003036static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
3037 SmallVector<int, 8> M;
3038 N->getMask(M);
3039 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003040}
3041
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003042/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3043/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00003044bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
3045 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003046 return false;
3047
Evan Cheng2064a2b2006-03-28 06:50:32 +00003048 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003049 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3050 isUndefOrEqual(N->getMaskElt(1), 7) &&
3051 isUndefOrEqual(N->getMaskElt(2), 2) &&
3052 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003053}
3054
Nate Begeman0b10b912009-11-07 23:17:15 +00003055/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3056/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3057/// <2, 3, 2, 3>
3058bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
3059 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3060
3061 if (NumElems != 4)
3062 return false;
3063
3064 return isUndefOrEqual(N->getMaskElt(0), 2) &&
3065 isUndefOrEqual(N->getMaskElt(1), 3) &&
3066 isUndefOrEqual(N->getMaskElt(2), 2) &&
3067 isUndefOrEqual(N->getMaskElt(3), 3);
3068}
3069
Evan Cheng5ced1d82006-04-06 23:23:56 +00003070/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3071/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003072bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3073 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003074
Evan Cheng5ced1d82006-04-06 23:23:56 +00003075 if (NumElems != 2 && NumElems != 4)
3076 return false;
3077
Evan Chengc5cdff22006-04-07 21:53:05 +00003078 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003079 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003080 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003081
Evan Chengc5cdff22006-04-07 21:53:05 +00003082 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003083 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003084 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003085
3086 return true;
3087}
3088
Nate Begeman0b10b912009-11-07 23:17:15 +00003089/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3090/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3091bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003092 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003093
Evan Cheng5ced1d82006-04-06 23:23:56 +00003094 if (NumElems != 2 && NumElems != 4)
3095 return false;
3096
Evan Chengc5cdff22006-04-07 21:53:05 +00003097 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003098 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003099 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003100
Nate Begeman9008ca62009-04-27 18:41:29 +00003101 for (unsigned i = 0; i < NumElems/2; ++i)
3102 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003103 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003104
3105 return true;
3106}
3107
Evan Cheng0038e592006-03-28 00:39:58 +00003108/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3109/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003110static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003111 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003112 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003113 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00003114 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003115
Nate Begeman9008ca62009-04-27 18:41:29 +00003116 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3117 int BitI = Mask[i];
3118 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00003119 if (!isUndefOrEqual(BitI, j))
3120 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003121 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003122 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003123 return false;
3124 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003125 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003126 return false;
3127 }
Evan Cheng0038e592006-03-28 00:39:58 +00003128 }
Evan Cheng0038e592006-03-28 00:39:58 +00003129 return true;
3130}
3131
Nate Begeman9008ca62009-04-27 18:41:29 +00003132bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3133 SmallVector<int, 8> M;
3134 N->getMask(M);
3135 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003136}
3137
Evan Cheng4fcb9222006-03-28 02:43:26 +00003138/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3139/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003140static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003141 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003142 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003143 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003144 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003145
Nate Begeman9008ca62009-04-27 18:41:29 +00003146 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3147 int BitI = Mask[i];
3148 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00003149 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00003150 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003151 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00003152 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003153 return false;
3154 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003155 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003156 return false;
3157 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003158 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003159 return true;
3160}
3161
Nate Begeman9008ca62009-04-27 18:41:29 +00003162bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3163 SmallVector<int, 8> M;
3164 N->getMask(M);
3165 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003166}
3167
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003168/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3169/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3170/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003171static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003172 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003173 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003174 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003175
Nate Begeman9008ca62009-04-27 18:41:29 +00003176 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
3177 int BitI = Mask[i];
3178 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00003179 if (!isUndefOrEqual(BitI, j))
3180 return false;
3181 if (!isUndefOrEqual(BitI1, j))
3182 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003183 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003184 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003185}
3186
Nate Begeman9008ca62009-04-27 18:41:29 +00003187bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3188 SmallVector<int, 8> M;
3189 N->getMask(M);
3190 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3191}
3192
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003193/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3194/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3195/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003196static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003197 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003198 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3199 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003200
Nate Begeman9008ca62009-04-27 18:41:29 +00003201 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3202 int BitI = Mask[i];
3203 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003204 if (!isUndefOrEqual(BitI, j))
3205 return false;
3206 if (!isUndefOrEqual(BitI1, j))
3207 return false;
3208 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003209 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003210}
3211
Nate Begeman9008ca62009-04-27 18:41:29 +00003212bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3213 SmallVector<int, 8> M;
3214 N->getMask(M);
3215 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3216}
3217
Evan Cheng017dcc62006-04-21 01:05:10 +00003218/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3219/// specifies a shuffle of elements that is suitable for input to MOVSS,
3220/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003221static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003222 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003223 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003224
3225 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003226
Nate Begeman9008ca62009-04-27 18:41:29 +00003227 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003228 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003229
Nate Begeman9008ca62009-04-27 18:41:29 +00003230 for (int i = 1; i < NumElts; ++i)
3231 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003232 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003233
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003234 return true;
3235}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003236
Nate Begeman9008ca62009-04-27 18:41:29 +00003237bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3238 SmallVector<int, 8> M;
3239 N->getMask(M);
3240 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003241}
3242
Evan Cheng017dcc62006-04-21 01:05:10 +00003243/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3244/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003245/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003246static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003247 bool V2IsSplat = false, bool V2IsUndef = false) {
3248 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003249 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003250 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003251
Nate Begeman9008ca62009-04-27 18:41:29 +00003252 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003253 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003254
Nate Begeman9008ca62009-04-27 18:41:29 +00003255 for (int i = 1; i < NumOps; ++i)
3256 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3257 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3258 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003259 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003260
Evan Cheng39623da2006-04-20 08:58:49 +00003261 return true;
3262}
3263
Nate Begeman9008ca62009-04-27 18:41:29 +00003264static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003265 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003266 SmallVector<int, 8> M;
3267 N->getMask(M);
3268 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003269}
3270
Evan Chengd9539472006-04-14 21:59:03 +00003271/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3272/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003273bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
3274 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003275 return false;
3276
3277 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00003278 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003279 int Elt = N->getMaskElt(i);
3280 if (Elt >= 0 && Elt != 1)
3281 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00003282 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003283
3284 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003285 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003286 int Elt = N->getMaskElt(i);
3287 if (Elt >= 0 && Elt != 3)
3288 return false;
3289 if (Elt == 3)
3290 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003291 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003292 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00003293 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003294 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003295}
3296
3297/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3298/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003299bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
3300 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003301 return false;
3302
3303 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00003304 for (unsigned i = 0; i < 2; ++i)
3305 if (N->getMaskElt(i) > 0)
3306 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003307
3308 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003309 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003310 int Elt = N->getMaskElt(i);
3311 if (Elt >= 0 && Elt != 2)
3312 return false;
3313 if (Elt == 2)
3314 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003315 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003316 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003317 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003318}
3319
Evan Cheng0b457f02008-09-25 20:50:48 +00003320/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3321/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003322bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3323 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003324
Nate Begeman9008ca62009-04-27 18:41:29 +00003325 for (int i = 0; i < e; ++i)
3326 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003327 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003328 for (int i = 0; i < e; ++i)
3329 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003330 return false;
3331 return true;
3332}
3333
Evan Cheng63d33002006-03-22 08:01:21 +00003334/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003335/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003336unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003337 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3338 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3339
Evan Chengb9df0ca2006-03-22 02:53:00 +00003340 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3341 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003342 for (int i = 0; i < NumOperands; ++i) {
3343 int Val = SVOp->getMaskElt(NumOperands-i-1);
3344 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003345 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003346 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003347 if (i != NumOperands - 1)
3348 Mask <<= Shift;
3349 }
Evan Cheng63d33002006-03-22 08:01:21 +00003350 return Mask;
3351}
3352
Evan Cheng506d3df2006-03-29 23:07:14 +00003353/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003354/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003355unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003356 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003357 unsigned Mask = 0;
3358 // 8 nodes, but we only care about the last 4.
3359 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003360 int Val = SVOp->getMaskElt(i);
3361 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003362 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003363 if (i != 4)
3364 Mask <<= 2;
3365 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003366 return Mask;
3367}
3368
3369/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003370/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003371unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003372 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003373 unsigned Mask = 0;
3374 // 8 nodes, but we only care about the first 4.
3375 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003376 int Val = SVOp->getMaskElt(i);
3377 if (Val >= 0)
3378 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003379 if (i != 0)
3380 Mask <<= 2;
3381 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003382 return Mask;
3383}
3384
Nate Begemana09008b2009-10-19 02:17:23 +00003385/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3386/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3387unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3388 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3389 EVT VVT = N->getValueType(0);
3390 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3391 int Val = 0;
3392
3393 unsigned i, e;
3394 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3395 Val = SVOp->getMaskElt(i);
3396 if (Val >= 0)
3397 break;
3398 }
3399 return (Val - i) * EltSize;
3400}
3401
Evan Cheng37b73872009-07-30 08:33:02 +00003402/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3403/// constant +0.0.
3404bool X86::isZeroNode(SDValue Elt) {
3405 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00003406 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00003407 (isa<ConstantFPSDNode>(Elt) &&
3408 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3409}
3410
Nate Begeman9008ca62009-04-27 18:41:29 +00003411/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3412/// their permute mask.
3413static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3414 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003415 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003416 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003417 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003418
Nate Begeman5a5ca152009-04-29 05:20:52 +00003419 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003420 int idx = SVOp->getMaskElt(i);
3421 if (idx < 0)
3422 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003423 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003424 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003425 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003426 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003427 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003428 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3429 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003430}
3431
Evan Cheng779ccea2007-12-07 21:30:01 +00003432/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3433/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003434static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003435 unsigned NumElems = VT.getVectorNumElements();
3436 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003437 int idx = Mask[i];
3438 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003439 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003440 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003441 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003442 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003443 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003444 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003445}
3446
Evan Cheng533a0aa2006-04-19 20:35:22 +00003447/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3448/// match movhlps. The lower half elements should come from upper half of
3449/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003450/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003451static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3452 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003453 return false;
3454 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003455 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003456 return false;
3457 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003458 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003459 return false;
3460 return true;
3461}
3462
Evan Cheng5ced1d82006-04-06 23:23:56 +00003463/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003464/// is promoted to a vector. It also returns the LoadSDNode by reference if
3465/// required.
3466static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003467 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3468 return false;
3469 N = N->getOperand(0).getNode();
3470 if (!ISD::isNON_EXTLoad(N))
3471 return false;
3472 if (LD)
3473 *LD = cast<LoadSDNode>(N);
3474 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003475}
3476
Evan Cheng533a0aa2006-04-19 20:35:22 +00003477/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3478/// match movlp{s|d}. The lower half elements should come from lower half of
3479/// V1 (and in order), and the upper half elements should come from the upper
3480/// half of V2 (and in order). And since V1 will become the source of the
3481/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003482static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3483 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003484 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003485 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003486 // Is V2 is a vector load, don't do this transformation. We will try to use
3487 // load folding shufps op.
3488 if (ISD::isNON_EXTLoad(V2))
3489 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003490
Nate Begeman5a5ca152009-04-29 05:20:52 +00003491 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003492
Evan Cheng533a0aa2006-04-19 20:35:22 +00003493 if (NumElems != 2 && NumElems != 4)
3494 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003495 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003496 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003497 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003498 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003499 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003500 return false;
3501 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003502}
3503
Evan Cheng39623da2006-04-20 08:58:49 +00003504/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3505/// all the same.
3506static bool isSplatVector(SDNode *N) {
3507 if (N->getOpcode() != ISD::BUILD_VECTOR)
3508 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003509
Dan Gohman475871a2008-07-27 21:46:04 +00003510 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003511 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3512 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003513 return false;
3514 return true;
3515}
3516
Evan Cheng213d2cf2007-05-17 18:45:50 +00003517/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003518/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003519/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003520static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003521 SDValue V1 = N->getOperand(0);
3522 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003523 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3524 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003525 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003526 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003527 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003528 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3529 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003530 if (Opc != ISD::BUILD_VECTOR ||
3531 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003532 return false;
3533 } else if (Idx >= 0) {
3534 unsigned Opc = V1.getOpcode();
3535 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3536 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003537 if (Opc != ISD::BUILD_VECTOR ||
3538 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003539 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003540 }
3541 }
3542 return true;
3543}
3544
3545/// getZeroVector - Returns a vector of specified type with all zero elements.
3546///
Owen Andersone50ed302009-08-10 22:56:29 +00003547static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003548 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003549 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003550
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003551 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted
3552 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003553 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003554 if (VT.getSizeInBits() == 64) { // MMX
Owen Anderson825b72b2009-08-11 20:47:22 +00003555 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3556 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003557 } else if (VT.getSizeInBits() == 128) {
3558 if (HasSSE2) { // SSE2
3559 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3560 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
3561 } else { // SSE1
3562 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3563 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
3564 }
3565 } else if (VT.getSizeInBits() == 256) { // AVX
3566 // 256-bit logic and arithmetic instructions in AVX are
3567 // all floating-point, no support for integer ops. Default
3568 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00003569 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003570 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
3571 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00003572 }
Dale Johannesenace16102009-02-03 19:33:06 +00003573 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003574}
3575
Chris Lattner8a594482007-11-25 00:24:49 +00003576/// getOnesVector - Returns a vector of specified type with all bits set.
3577///
Owen Andersone50ed302009-08-10 22:56:29 +00003578static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003579 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003580
Chris Lattner8a594482007-11-25 00:24:49 +00003581 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3582 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003583 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003584 SDValue Vec;
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003585 if (VT.getSizeInBits() == 64) // MMX
Owen Anderson825b72b2009-08-11 20:47:22 +00003586 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003587 else // SSE
Owen Anderson825b72b2009-08-11 20:47:22 +00003588 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Dale Johannesenace16102009-02-03 19:33:06 +00003589 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003590}
3591
3592
Evan Cheng39623da2006-04-20 08:58:49 +00003593/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3594/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003595static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003596 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003597 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003598
Evan Cheng39623da2006-04-20 08:58:49 +00003599 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003600 SmallVector<int, 8> MaskVec;
3601 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003602
Nate Begeman5a5ca152009-04-29 05:20:52 +00003603 for (unsigned i = 0; i != NumElems; ++i) {
3604 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003605 MaskVec[i] = NumElems;
3606 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003607 }
Evan Cheng39623da2006-04-20 08:58:49 +00003608 }
Evan Cheng39623da2006-04-20 08:58:49 +00003609 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003610 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3611 SVOp->getOperand(1), &MaskVec[0]);
3612 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003613}
3614
Evan Cheng017dcc62006-04-21 01:05:10 +00003615/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3616/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003617static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003618 SDValue V2) {
3619 unsigned NumElems = VT.getVectorNumElements();
3620 SmallVector<int, 8> Mask;
3621 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003622 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003623 Mask.push_back(i);
3624 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003625}
3626
Nate Begeman9008ca62009-04-27 18:41:29 +00003627/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003628static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003629 SDValue V2) {
3630 unsigned NumElems = VT.getVectorNumElements();
3631 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003632 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003633 Mask.push_back(i);
3634 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003635 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003636 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003637}
3638
Nate Begeman9008ca62009-04-27 18:41:29 +00003639/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003640static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003641 SDValue V2) {
3642 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003643 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003644 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003645 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003646 Mask.push_back(i + Half);
3647 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003648 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003649 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003650}
3651
Bruno Cardoso Lopesbb0a9482010-08-13 17:50:47 +00003652/// PromoteSplat - Promote a splat of v4i32, v8i16 or v16i8 to v4f32.
3653static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003654 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003655 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003656 DebugLoc dl = SV->getDebugLoc();
3657 SDValue V1 = SV->getOperand(0);
3658 int NumElems = VT.getVectorNumElements();
3659 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003660
Nate Begeman9008ca62009-04-27 18:41:29 +00003661 // unpack elements to the correct location
3662 while (NumElems > 4) {
3663 if (EltNo < NumElems/2) {
3664 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3665 } else {
3666 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3667 EltNo -= NumElems/2;
3668 }
3669 NumElems >>= 1;
3670 }
Eric Christopherfd179292009-08-27 18:07:15 +00003671
Nate Begeman9008ca62009-04-27 18:41:29 +00003672 // Perform the splat.
3673 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Dale Johannesenace16102009-02-03 19:33:06 +00003674 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003675 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
3676 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003677}
3678
Evan Chengba05f722006-04-21 23:03:30 +00003679/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003680/// vector of zero or undef vector. This produces a shuffle where the low
3681/// element of V2 is swizzled into the zero/undef vector, landing at element
3682/// Idx. This produces a shuffle mask like 4,1,2,3 (idx=0) or 0,1,2,4 (idx=3).
Dan Gohman475871a2008-07-27 21:46:04 +00003683static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003684 bool isZero, bool HasSSE2,
3685 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003686 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003687 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003688 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3689 unsigned NumElems = VT.getVectorNumElements();
3690 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003691 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003692 // If this is the insertion idx, put the low elt of V2 here.
3693 MaskVec.push_back(i == Idx ? NumElems : i);
3694 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003695}
3696
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003697/// getShuffleScalarElt - Returns the scalar element that will make up the ith
3698/// element of the result of the vector shuffle.
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003699SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
3700 unsigned Depth) {
3701 if (Depth == 6)
3702 return SDValue(); // Limit search depth.
3703
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003704 SDValue V = SDValue(N, 0);
3705 EVT VT = V.getValueType();
3706 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003707
3708 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
3709 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
3710 Index = SV->getMaskElt(Index);
3711
3712 if (Index < 0)
3713 return DAG.getUNDEF(VT.getVectorElementType());
3714
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003715 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003716 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003717 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003718 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003719
3720 // Recurse into target specific vector shuffles to find scalars.
3721 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003722 int NumElems = VT.getVectorNumElements();
3723 SmallVector<unsigned, 16> ShuffleMask;
3724 SDValue ImmN;
3725
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003726 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003727 case X86ISD::SHUFPS:
3728 case X86ISD::SHUFPD:
3729 ImmN = N->getOperand(N->getNumOperands()-1);
3730 DecodeSHUFPSMask(NumElems,
3731 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3732 ShuffleMask);
3733 break;
3734 case X86ISD::PUNPCKHBW:
3735 case X86ISD::PUNPCKHWD:
3736 case X86ISD::PUNPCKHDQ:
3737 case X86ISD::PUNPCKHQDQ:
3738 DecodePUNPCKHMask(NumElems, ShuffleMask);
3739 break;
3740 case X86ISD::UNPCKHPS:
3741 case X86ISD::UNPCKHPD:
3742 DecodeUNPCKHPMask(NumElems, ShuffleMask);
3743 break;
3744 case X86ISD::PUNPCKLBW:
3745 case X86ISD::PUNPCKLWD:
3746 case X86ISD::PUNPCKLDQ:
3747 case X86ISD::PUNPCKLQDQ:
3748 DecodePUNPCKLMask(NumElems, ShuffleMask);
3749 break;
3750 case X86ISD::UNPCKLPS:
3751 case X86ISD::UNPCKLPD:
3752 DecodeUNPCKLPMask(NumElems, ShuffleMask);
3753 break;
3754 case X86ISD::MOVHLPS:
3755 DecodeMOVHLPSMask(NumElems, ShuffleMask);
3756 break;
3757 case X86ISD::MOVLHPS:
3758 DecodeMOVLHPSMask(NumElems, ShuffleMask);
3759 break;
3760 case X86ISD::PSHUFD:
3761 ImmN = N->getOperand(N->getNumOperands()-1);
3762 DecodePSHUFMask(NumElems,
3763 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3764 ShuffleMask);
3765 break;
3766 case X86ISD::PSHUFHW:
3767 ImmN = N->getOperand(N->getNumOperands()-1);
3768 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3769 ShuffleMask);
3770 break;
3771 case X86ISD::PSHUFLW:
3772 ImmN = N->getOperand(N->getNumOperands()-1);
3773 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3774 ShuffleMask);
3775 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003776 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00003777 case X86ISD::MOVSD: {
3778 // The index 0 always comes from the first element of the second source,
3779 // this is why MOVSS and MOVSD are used in the first place. The other
3780 // elements come from the other positions of the first source vector.
3781 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003782 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
3783 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00003784 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003785 default:
3786 assert("not implemented for target shuffle node");
3787 return SDValue();
3788 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003789
3790 Index = ShuffleMask[Index];
3791 if (Index < 0)
3792 return DAG.getUNDEF(VT.getVectorElementType());
3793
3794 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
3795 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
3796 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003797 }
3798
3799 // Actual nodes that may contain scalar elements
3800 if (Opcode == ISD::BIT_CONVERT) {
3801 V = V.getOperand(0);
3802 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003803 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003804
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003805 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003806 return SDValue();
3807 }
3808
3809 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
3810 return (Index == 0) ? V.getOperand(0)
3811 : DAG.getUNDEF(VT.getVectorElementType());
3812
3813 if (V.getOpcode() == ISD::BUILD_VECTOR)
3814 return V.getOperand(Index);
3815
3816 return SDValue();
3817}
3818
3819/// getNumOfConsecutiveZeros - Return the number of elements of a vector
3820/// shuffle operation which come from a consecutively from a zero. The
3821/// search can start in two diferent directions, from left or right.
3822static
3823unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
3824 bool ZerosFromLeft, SelectionDAG &DAG) {
3825 int i = 0;
3826
3827 while (i < NumElems) {
3828 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003829 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003830 if (!(Elt.getNode() &&
3831 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
3832 break;
3833 ++i;
3834 }
3835
3836 return i;
3837}
3838
3839/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
3840/// MaskE correspond consecutively to elements from one of the vector operands,
3841/// starting from its index OpIdx. Also tell OpNum which source vector operand.
3842static
3843bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
3844 int OpIdx, int NumElems, unsigned &OpNum) {
3845 bool SeenV1 = false;
3846 bool SeenV2 = false;
3847
3848 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
3849 int Idx = SVOp->getMaskElt(i);
3850 // Ignore undef indicies
3851 if (Idx < 0)
3852 continue;
3853
3854 if (Idx < NumElems)
3855 SeenV1 = true;
3856 else
3857 SeenV2 = true;
3858
3859 // Only accept consecutive elements from the same vector
3860 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
3861 return false;
3862 }
3863
3864 OpNum = SeenV1 ? 0 : 1;
3865 return true;
3866}
3867
3868/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
3869/// logical left shift of a vector.
3870static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
3871 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
3872 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
3873 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
3874 false /* check zeros from right */, DAG);
3875 unsigned OpSrc;
3876
3877 if (!NumZeros)
3878 return false;
3879
3880 // Considering the elements in the mask that are not consecutive zeros,
3881 // check if they consecutively come from only one of the source vectors.
3882 //
3883 // V1 = {X, A, B, C} 0
3884 // \ \ \ /
3885 // vector_shuffle V1, V2 <1, 2, 3, X>
3886 //
3887 if (!isShuffleMaskConsecutive(SVOp,
3888 0, // Mask Start Index
3889 NumElems-NumZeros-1, // Mask End Index
3890 NumZeros, // Where to start looking in the src vector
3891 NumElems, // Number of elements in vector
3892 OpSrc)) // Which source operand ?
3893 return false;
3894
3895 isLeft = false;
3896 ShAmt = NumZeros;
3897 ShVal = SVOp->getOperand(OpSrc);
3898 return true;
3899}
3900
3901/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
3902/// logical left shift of a vector.
3903static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
3904 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
3905 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
3906 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
3907 true /* check zeros from left */, DAG);
3908 unsigned OpSrc;
3909
3910 if (!NumZeros)
3911 return false;
3912
3913 // Considering the elements in the mask that are not consecutive zeros,
3914 // check if they consecutively come from only one of the source vectors.
3915 //
3916 // 0 { A, B, X, X } = V2
3917 // / \ / /
3918 // vector_shuffle V1, V2 <X, X, 4, 5>
3919 //
3920 if (!isShuffleMaskConsecutive(SVOp,
3921 NumZeros, // Mask Start Index
3922 NumElems-1, // Mask End Index
3923 0, // Where to start looking in the src vector
3924 NumElems, // Number of elements in vector
3925 OpSrc)) // Which source operand ?
3926 return false;
3927
3928 isLeft = true;
3929 ShAmt = NumZeros;
3930 ShVal = SVOp->getOperand(OpSrc);
3931 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00003932}
3933
3934/// isVectorShift - Returns true if the shuffle can be implemented as a
3935/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003936static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00003937 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003938 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
3939 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
3940 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00003941
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003942 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00003943}
3944
Evan Chengc78d3b42006-04-24 18:01:45 +00003945/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3946///
Dan Gohman475871a2008-07-27 21:46:04 +00003947static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003948 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00003949 SelectionDAG &DAG,
3950 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003951 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00003952 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003953
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003954 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003955 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003956 bool First = true;
3957 for (unsigned i = 0; i < 16; ++i) {
3958 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3959 if (ThisIsNonZero && First) {
3960 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003961 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003962 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003963 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003964 First = false;
3965 }
3966
3967 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00003968 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003969 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3970 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003971 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003972 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00003973 }
3974 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003975 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
3976 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
3977 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00003978 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003979 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00003980 } else
3981 ThisElt = LastElt;
3982
Gabor Greifba36cb52008-08-28 21:40:38 +00003983 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00003984 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00003985 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00003986 }
3987 }
3988
Owen Anderson825b72b2009-08-11 20:47:22 +00003989 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00003990}
3991
Bill Wendlinga348c562007-03-22 18:42:45 +00003992/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003993///
Dan Gohman475871a2008-07-27 21:46:04 +00003994static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00003995 unsigned NumNonZero, unsigned NumZero,
3996 SelectionDAG &DAG,
3997 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003998 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00003999 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004000
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004001 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004002 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004003 bool First = true;
4004 for (unsigned i = 0; i < 8; ++i) {
4005 bool isNonZero = (NonZeros & (1 << i)) != 0;
4006 if (isNonZero) {
4007 if (First) {
4008 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004009 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004010 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004011 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004012 First = false;
4013 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004014 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004015 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004016 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004017 }
4018 }
4019
4020 return V;
4021}
4022
Evan Chengf26ffe92008-05-29 08:22:04 +00004023/// getVShift - Return a vector logical shift node.
4024///
Owen Andersone50ed302009-08-10 22:56:29 +00004025static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004026 unsigned NumBits, SelectionDAG &DAG,
4027 const TargetLowering &TLI, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004028 bool isMMX = VT.getSizeInBits() == 64;
Owen Anderson825b72b2009-08-11 20:47:22 +00004029 EVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004030 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Dale Johannesenace16102009-02-03 19:33:06 +00004031 SrcOp = DAG.getNode(ISD::BIT_CONVERT, dl, ShVT, SrcOp);
4032 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4033 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00004034 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00004035}
4036
Dan Gohman475871a2008-07-27 21:46:04 +00004037SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004038X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004039 SelectionDAG &DAG) const {
Evan Chengc3630942009-12-09 21:00:30 +00004040
4041 // Check if the scalar load can be widened into a vector load. And if
4042 // the address is "base + cst" see if the cst can be "absorbed" into
4043 // the shuffle mask.
4044 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4045 SDValue Ptr = LD->getBasePtr();
4046 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4047 return SDValue();
4048 EVT PVT = LD->getValueType(0);
4049 if (PVT != MVT::i32 && PVT != MVT::f32)
4050 return SDValue();
4051
4052 int FI = -1;
4053 int64_t Offset = 0;
4054 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4055 FI = FINode->getIndex();
4056 Offset = 0;
4057 } else if (Ptr.getOpcode() == ISD::ADD &&
4058 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
4059 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4060 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4061 Offset = Ptr.getConstantOperandVal(1);
4062 Ptr = Ptr.getOperand(0);
4063 } else {
4064 return SDValue();
4065 }
4066
4067 SDValue Chain = LD->getChain();
4068 // Make sure the stack object alignment is at least 16.
4069 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
4070 if (DAG.InferPtrAlignment(Ptr) < 16) {
4071 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004072 // Can't change the alignment. FIXME: It's possible to compute
4073 // the exact stack offset and reference FI + adjust offset instead.
4074 // If someone *really* cares about this. That's the way to implement it.
4075 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004076 } else {
4077 MFI->setObjectAlignment(FI, 16);
4078 }
4079 }
4080
4081 // (Offset % 16) must be multiple of 4. Then address is then
4082 // Ptr + (Offset & ~15).
4083 if (Offset < 0)
4084 return SDValue();
4085 if ((Offset % 16) & 3)
4086 return SDValue();
4087 int64_t StartOffset = Offset & ~15;
4088 if (StartOffset)
4089 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4090 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4091
4092 int EltNo = (Offset - StartOffset) >> 2;
4093 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
4094 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
Chris Lattner51abfe42010-09-21 06:02:19 +00004095 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,
4096 LD->getPointerInfo().getWithOffset(StartOffset),
David Greene67c9d422010-02-15 16:53:33 +00004097 false, false, 0);
Evan Chengc3630942009-12-09 21:00:30 +00004098 // Canonicalize it to a v4i32 shuffle.
4099 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32, V1);
4100 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4101 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
Chris Lattner51abfe42010-09-21 06:02:19 +00004102 DAG.getUNDEF(MVT::v4i32),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004103 }
4104
4105 return SDValue();
4106}
4107
Nate Begeman1449f292010-03-24 22:19:06 +00004108/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4109/// vector of type 'VT', see if the elements can be replaced by a single large
4110/// load which has the same value as a build_vector whose operands are 'elts'.
4111///
4112/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
4113///
4114/// FIXME: we'd also like to handle the case where the last elements are zero
4115/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4116/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004117static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004118 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004119 EVT EltVT = VT.getVectorElementType();
4120 unsigned NumElems = Elts.size();
4121
Nate Begemanfdea31a2010-03-24 20:49:50 +00004122 LoadSDNode *LDBase = NULL;
4123 unsigned LastLoadedElt = -1U;
Nate Begeman1449f292010-03-24 22:19:06 +00004124
4125 // For each element in the initializer, see if we've found a load or an undef.
4126 // If we don't find an initial load element, or later load elements are
4127 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004128 for (unsigned i = 0; i < NumElems; ++i) {
4129 SDValue Elt = Elts[i];
4130
4131 if (!Elt.getNode() ||
4132 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4133 return SDValue();
4134 if (!LDBase) {
4135 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4136 return SDValue();
4137 LDBase = cast<LoadSDNode>(Elt.getNode());
4138 LastLoadedElt = i;
4139 continue;
4140 }
4141 if (Elt.getOpcode() == ISD::UNDEF)
4142 continue;
4143
4144 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4145 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4146 return SDValue();
4147 LastLoadedElt = i;
4148 }
Nate Begeman1449f292010-03-24 22:19:06 +00004149
4150 // If we have found an entire vector of loads and undefs, then return a large
4151 // load of the entire vector width starting at the base pointer. If we found
4152 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004153 if (LastLoadedElt == NumElems - 1) {
4154 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004155 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004156 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004157 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004158 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004159 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004160 LDBase->isVolatile(), LDBase->isNonTemporal(),
4161 LDBase->getAlignment());
4162 } else if (NumElems == 4 && LastLoadedElt == 1) {
4163 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4164 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Chris Lattner88641552010-09-22 00:34:38 +00004165 SDValue ResNode = DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys,
4166 Ops, 2, MVT::i32,
4167 LDBase->getMemOperand());
4168 return DAG.getNode(ISD::BIT_CONVERT, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00004169 }
4170 return SDValue();
4171}
4172
Evan Chengc3630942009-12-09 21:00:30 +00004173SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004174X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004175 DebugLoc dl = Op.getDebugLoc();
Chris Lattner6e80e442010-08-28 17:15:43 +00004176 // All zero's are handled with pxor in SSE2 and above, xorps in SSE1.
4177 // All one's are handled with pcmpeqd. In AVX, zero's are handled with
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004178 // vpxor in 128-bit and xor{pd,ps} in 256-bit, but no 256 version of pcmpeqd
4179 // is present, so AllOnes is ignored.
4180 if (ISD::isBuildVectorAllZeros(Op.getNode()) ||
4181 (Op.getValueType().getSizeInBits() != 256 &&
4182 ISD::isBuildVectorAllOnes(Op.getNode()))) {
Chris Lattner8a594482007-11-25 00:24:49 +00004183 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
4184 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
4185 // eliminated on x86-32 hosts.
Owen Anderson825b72b2009-08-11 20:47:22 +00004186 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
Chris Lattner8a594482007-11-25 00:24:49 +00004187 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004188
Gabor Greifba36cb52008-08-28 21:40:38 +00004189 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004190 return getOnesVector(Op.getValueType(), DAG, dl);
4191 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00004192 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004193
Owen Andersone50ed302009-08-10 22:56:29 +00004194 EVT VT = Op.getValueType();
4195 EVT ExtVT = VT.getVectorElementType();
4196 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004197
4198 unsigned NumElems = Op.getNumOperands();
4199 unsigned NumZero = 0;
4200 unsigned NumNonZero = 0;
4201 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004202 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004203 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004204 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004205 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00004206 if (Elt.getOpcode() == ISD::UNDEF)
4207 continue;
4208 Values.insert(Elt);
4209 if (Elt.getOpcode() != ISD::Constant &&
4210 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004211 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00004212 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00004213 NumZero++;
4214 else {
4215 NonZeros |= (1 << i);
4216 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004217 }
4218 }
4219
Chris Lattner97a2a562010-08-26 05:24:29 +00004220 // All undef vector. Return an UNDEF. All zero vectors were handled above.
4221 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00004222 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004223
Chris Lattner67f453a2008-03-09 05:42:06 +00004224 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00004225 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004226 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00004227 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00004228
Chris Lattner62098042008-03-09 01:05:04 +00004229 // If this is an insertion of an i64 value on x86-32, and if the top bits of
4230 // the value are obviously zero, truncate the value to i32 and do the
4231 // insertion that way. Only do this if the value is non-constant or if the
4232 // value is a constant being inserted into element 0. It is cheaper to do
4233 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00004234 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00004235 (!IsAllConstants || Idx == 0)) {
4236 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
4237 // Handle MMX and SSE both.
Owen Anderson825b72b2009-08-11 20:47:22 +00004238 EVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
4239 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Scott Michelfdc40a02009-02-17 22:15:04 +00004240
Chris Lattner62098042008-03-09 01:05:04 +00004241 // Truncate the value (which may itself be a constant) to i32, and
4242 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00004243 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00004244 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00004245 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4246 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00004247
Chris Lattner62098042008-03-09 01:05:04 +00004248 // Now we have our 32-bit value zero extended in the low element of
4249 // a vector. If Idx != 0, swizzle it into place.
4250 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004251 SmallVector<int, 4> Mask;
4252 Mask.push_back(Idx);
4253 for (unsigned i = 1; i != VecElts; ++i)
4254 Mask.push_back(i);
4255 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00004256 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00004257 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004258 }
Dale Johannesenace16102009-02-03 19:33:06 +00004259 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00004260 }
4261 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004262
Chris Lattner19f79692008-03-08 22:59:52 +00004263 // If we have a constant or non-constant insertion into the low element of
4264 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
4265 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00004266 // depending on what the source datatype is.
4267 if (Idx == 0) {
4268 if (NumZero == 0) {
4269 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00004270 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
4271 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00004272 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
4273 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
4274 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
4275 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00004276 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
4277 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
4278 EVT MiddleVT = VT.getSizeInBits() == 64 ? MVT::v2i32 : MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00004279 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
4280 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4281 Subtarget->hasSSE2(), DAG);
4282 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Item);
4283 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004284 }
Evan Chengf26ffe92008-05-29 08:22:04 +00004285
4286 // Is it a vector logical left shift?
4287 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00004288 X86::isZeroNode(Op.getOperand(0)) &&
4289 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004290 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00004291 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004292 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004293 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00004294 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004295 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004296
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004297 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00004298 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004299
Chris Lattner19f79692008-03-08 22:59:52 +00004300 // Otherwise, if this is a vector with i32 or f32 elements, and the element
4301 // is a non-constant being inserted into an element other than the low one,
4302 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
4303 // movd/movss) to move this into the low element, then shuffle it into
4304 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004305 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00004306 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00004307
Evan Cheng0db9fe62006-04-25 20:13:52 +00004308 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00004309 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
4310 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00004311 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004312 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00004313 MaskVec.push_back(i == Idx ? 0 : 1);
4314 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004315 }
4316 }
4317
Chris Lattner67f453a2008-03-09 05:42:06 +00004318 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00004319 if (Values.size() == 1) {
4320 if (EVTBits == 32) {
4321 // Instead of a shuffle like this:
4322 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
4323 // Check if it's possible to issue this instead.
4324 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
4325 unsigned Idx = CountTrailingZeros_32(NonZeros);
4326 SDValue Item = Op.getOperand(Idx);
4327 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
4328 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
4329 }
Dan Gohman475871a2008-07-27 21:46:04 +00004330 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004331 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004332
Dan Gohmana3941172007-07-24 22:55:08 +00004333 // A vector full of immediates; various special cases are already
4334 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004335 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00004336 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00004337
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00004338 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004339 if (EVTBits == 64) {
4340 if (NumNonZero == 1) {
4341 // One half is zero or undef.
4342 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00004343 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00004344 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00004345 return getShuffleVectorZeroOrUndef(V2, Idx, true,
4346 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00004347 }
Dan Gohman475871a2008-07-27 21:46:04 +00004348 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00004349 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004350
4351 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00004352 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004353 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00004354 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004355 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004356 }
4357
Bill Wendling826f36f2007-03-28 00:57:11 +00004358 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00004359 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00004360 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004361 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004362 }
4363
4364 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004365 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00004366 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004367 if (NumElems == 4 && NumZero > 0) {
4368 for (unsigned i = 0; i < 4; ++i) {
4369 bool isZero = !(NonZeros & (1 << i));
4370 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00004371 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004372 else
Dale Johannesenace16102009-02-03 19:33:06 +00004373 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004374 }
4375
4376 for (unsigned i = 0; i < 2; ++i) {
4377 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
4378 default: break;
4379 case 0:
4380 V[i] = V[i*2]; // Must be a zero vector.
4381 break;
4382 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00004383 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004384 break;
4385 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00004386 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004387 break;
4388 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00004389 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004390 break;
4391 }
4392 }
4393
Nate Begeman9008ca62009-04-27 18:41:29 +00004394 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004395 bool Reverse = (NonZeros & 0x3) == 2;
4396 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004397 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004398 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
4399 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004400 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
4401 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004402 }
4403
Nate Begemanfdea31a2010-03-24 20:49:50 +00004404 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
4405 // Check for a build vector of consecutive loads.
4406 for (unsigned i = 0; i < NumElems; ++i)
4407 V[i] = Op.getOperand(i);
4408
4409 // Check for elements which are consecutive loads.
4410 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
4411 if (LD.getNode())
4412 return LD;
4413
Chris Lattner24faf612010-08-28 17:59:08 +00004414 // For SSE 4.1, use insertps to put the high elements into the low element.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004415 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00004416 SDValue Result;
4417 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
4418 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
4419 else
4420 Result = DAG.getUNDEF(VT);
4421
4422 for (unsigned i = 1; i < NumElems; ++i) {
4423 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
4424 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00004425 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00004426 }
4427 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00004428 }
Nate Begemanfdea31a2010-03-24 20:49:50 +00004429
Chris Lattner6e80e442010-08-28 17:15:43 +00004430 // Otherwise, expand into a number of unpckl*, start by extending each of
4431 // our (non-undef) elements to the full vector width with the element in the
4432 // bottom slot of the vector (which generates no code for SSE).
4433 for (unsigned i = 0; i < NumElems; ++i) {
4434 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
4435 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
4436 else
4437 V[i] = DAG.getUNDEF(VT);
4438 }
4439
4440 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004441 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
4442 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
4443 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00004444 unsigned EltStride = NumElems >> 1;
4445 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00004446 for (unsigned i = 0; i < EltStride; ++i) {
4447 // If V[i+EltStride] is undef and this is the first round of mixing,
4448 // then it is safe to just drop this shuffle: V[i] is already in the
4449 // right place, the one element (since it's the first round) being
4450 // inserted as undef can be dropped. This isn't safe for successive
4451 // rounds because they will permute elements within both vectors.
4452 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
4453 EltStride == NumElems/2)
4454 continue;
4455
Chris Lattner6e80e442010-08-28 17:15:43 +00004456 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00004457 }
Chris Lattner6e80e442010-08-28 17:15:43 +00004458 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004459 }
4460 return V[0];
4461 }
Dan Gohman475871a2008-07-27 21:46:04 +00004462 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004463}
4464
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004465SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004466X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004467 // We support concatenate two MMX registers and place them in a MMX
4468 // register. This is better than doing a stack convert.
4469 DebugLoc dl = Op.getDebugLoc();
4470 EVT ResVT = Op.getValueType();
4471 assert(Op.getNumOperands() == 2);
4472 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
4473 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
4474 int Mask[2];
4475 SDValue InVec = DAG.getNode(ISD::BIT_CONVERT,dl, MVT::v1i64, Op.getOperand(0));
4476 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4477 InVec = Op.getOperand(1);
4478 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4479 unsigned NumElts = ResVT.getVectorNumElements();
4480 VecOp = DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
4481 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
4482 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
4483 } else {
4484 InVec = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v1i64, InVec);
4485 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4486 Mask[0] = 0; Mask[1] = 2;
4487 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
4488 }
4489 return DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
4490}
4491
Nate Begemanb9a47b82009-02-23 08:49:38 +00004492// v8i16 shuffles - Prefer shuffles in the following order:
4493// 1. [all] pshuflw, pshufhw, optional move
4494// 2. [ssse3] 1 x pshufb
4495// 3. [ssse3] 2 x pshufb + 1 x por
4496// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004497SDValue
4498X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
4499 SelectionDAG &DAG) const {
4500 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00004501 SDValue V1 = SVOp->getOperand(0);
4502 SDValue V2 = SVOp->getOperand(1);
4503 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004504 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00004505
Nate Begemanb9a47b82009-02-23 08:49:38 +00004506 // Determine if more than 1 of the words in each of the low and high quadwords
4507 // of the result come from the same quadword of one of the two inputs. Undef
4508 // mask values count as coming from any quadword, for better codegen.
4509 SmallVector<unsigned, 4> LoQuad(4);
4510 SmallVector<unsigned, 4> HiQuad(4);
4511 BitVector InputQuads(4);
4512 for (unsigned i = 0; i < 8; ++i) {
4513 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00004514 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004515 MaskVals.push_back(EltIdx);
4516 if (EltIdx < 0) {
4517 ++Quad[0];
4518 ++Quad[1];
4519 ++Quad[2];
4520 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00004521 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004522 }
4523 ++Quad[EltIdx / 4];
4524 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00004525 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004526
Nate Begemanb9a47b82009-02-23 08:49:38 +00004527 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004528 unsigned MaxQuad = 1;
4529 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004530 if (LoQuad[i] > MaxQuad) {
4531 BestLoQuad = i;
4532 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004533 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004534 }
4535
Nate Begemanb9a47b82009-02-23 08:49:38 +00004536 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004537 MaxQuad = 1;
4538 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004539 if (HiQuad[i] > MaxQuad) {
4540 BestHiQuad = i;
4541 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004542 }
4543 }
4544
Nate Begemanb9a47b82009-02-23 08:49:38 +00004545 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00004546 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00004547 // single pshufb instruction is necessary. If There are more than 2 input
4548 // quads, disable the next transformation since it does not help SSSE3.
4549 bool V1Used = InputQuads[0] || InputQuads[1];
4550 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004551 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004552 if (InputQuads.count() == 2 && V1Used && V2Used) {
4553 BestLoQuad = InputQuads.find_first();
4554 BestHiQuad = InputQuads.find_next(BestLoQuad);
4555 }
4556 if (InputQuads.count() > 2) {
4557 BestLoQuad = -1;
4558 BestHiQuad = -1;
4559 }
4560 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004561
Nate Begemanb9a47b82009-02-23 08:49:38 +00004562 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
4563 // the shuffle mask. If a quad is scored as -1, that means that it contains
4564 // words from all 4 input quadwords.
4565 SDValue NewV;
4566 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004567 SmallVector<int, 8> MaskV;
4568 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
4569 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00004570 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004571 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V1),
4572 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V2), &MaskV[0]);
4573 NewV = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004574
Nate Begemanb9a47b82009-02-23 08:49:38 +00004575 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
4576 // source words for the shuffle, to aid later transformations.
4577 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00004578 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00004579 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004580 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00004581 if (idx != (int)i)
4582 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004583 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00004584 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004585 AllWordsInNewV = false;
4586 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00004587 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004588
Nate Begemanb9a47b82009-02-23 08:49:38 +00004589 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
4590 if (AllWordsInNewV) {
4591 for (int i = 0; i != 8; ++i) {
4592 int idx = MaskVals[i];
4593 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004594 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004595 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004596 if ((idx != i) && idx < 4)
4597 pshufhw = false;
4598 if ((idx != i) && idx > 3)
4599 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00004600 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00004601 V1 = NewV;
4602 V2Used = false;
4603 BestLoQuad = 0;
4604 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004605 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004606
Nate Begemanb9a47b82009-02-23 08:49:38 +00004607 // If we've eliminated the use of V2, and the new mask is a pshuflw or
4608 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00004609 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004610 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
4611 unsigned TargetMask = 0;
4612 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00004613 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004614 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
4615 X86::getShufflePSHUFLWImmediate(NewV.getNode());
4616 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004617 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00004618 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004619 }
Eric Christopherfd179292009-08-27 18:07:15 +00004620
Nate Begemanb9a47b82009-02-23 08:49:38 +00004621 // If we have SSSE3, and all words of the result are from 1 input vector,
4622 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
4623 // is present, fall back to case 4.
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004624 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004625 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004626
Nate Begemanb9a47b82009-02-23 08:49:38 +00004627 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00004628 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00004629 // mask, and elements that come from V1 in the V2 mask, so that the two
4630 // results can be OR'd together.
4631 bool TwoInputs = V1Used && V2Used;
4632 for (unsigned i = 0; i != 8; ++i) {
4633 int EltIdx = MaskVals[i] * 2;
4634 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004635 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4636 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004637 continue;
4638 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004639 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
4640 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004641 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004642 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004643 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004644 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004645 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004646 if (!TwoInputs)
Owen Anderson825b72b2009-08-11 20:47:22 +00004647 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004648
Nate Begemanb9a47b82009-02-23 08:49:38 +00004649 // Calculate the shuffle mask for the second input, shuffle it, and
4650 // OR it with the first shuffled input.
4651 pshufbMask.clear();
4652 for (unsigned i = 0; i != 8; ++i) {
4653 int EltIdx = MaskVals[i] * 2;
4654 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004655 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4656 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004657 continue;
4658 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004659 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
4660 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004661 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004662 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00004663 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004664 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004665 MVT::v16i8, &pshufbMask[0], 16));
4666 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
4667 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004668 }
4669
4670 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
4671 // and update MaskVals with new element order.
4672 BitVector InOrder(8);
4673 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004674 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004675 for (int i = 0; i != 4; ++i) {
4676 int idx = MaskVals[i];
4677 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004678 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004679 InOrder.set(i);
4680 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004681 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004682 InOrder.set(i);
4683 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004684 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004685 }
4686 }
4687 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004688 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00004689 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004690 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004691
4692 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4693 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
4694 NewV.getOperand(0),
4695 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
4696 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004697 }
Eric Christopherfd179292009-08-27 18:07:15 +00004698
Nate Begemanb9a47b82009-02-23 08:49:38 +00004699 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
4700 // and update MaskVals with the new element order.
4701 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004702 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004703 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004704 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004705 for (unsigned i = 4; i != 8; ++i) {
4706 int idx = MaskVals[i];
4707 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004708 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004709 InOrder.set(i);
4710 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004711 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004712 InOrder.set(i);
4713 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004714 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004715 }
4716 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004717 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004718 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004719
4720 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4721 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
4722 NewV.getOperand(0),
4723 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
4724 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004725 }
Eric Christopherfd179292009-08-27 18:07:15 +00004726
Nate Begemanb9a47b82009-02-23 08:49:38 +00004727 // In case BestHi & BestLo were both -1, which means each quadword has a word
4728 // from each of the four input quadwords, calculate the InOrder bitvector now
4729 // before falling through to the insert/extract cleanup.
4730 if (BestLoQuad == -1 && BestHiQuad == -1) {
4731 NewV = V1;
4732 for (int i = 0; i != 8; ++i)
4733 if (MaskVals[i] < 0 || MaskVals[i] == i)
4734 InOrder.set(i);
4735 }
Eric Christopherfd179292009-08-27 18:07:15 +00004736
Nate Begemanb9a47b82009-02-23 08:49:38 +00004737 // The other elements are put in the right place using pextrw and pinsrw.
4738 for (unsigned i = 0; i != 8; ++i) {
4739 if (InOrder[i])
4740 continue;
4741 int EltIdx = MaskVals[i];
4742 if (EltIdx < 0)
4743 continue;
4744 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004745 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004746 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00004747 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004748 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004749 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004750 DAG.getIntPtrConstant(i));
4751 }
4752 return NewV;
4753}
4754
4755// v16i8 shuffles - Prefer shuffles in the following order:
4756// 1. [ssse3] 1 x pshufb
4757// 2. [ssse3] 2 x pshufb + 1 x por
4758// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
4759static
Nate Begeman9008ca62009-04-27 18:41:29 +00004760SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00004761 SelectionDAG &DAG,
4762 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004763 SDValue V1 = SVOp->getOperand(0);
4764 SDValue V2 = SVOp->getOperand(1);
4765 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004766 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00004767 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00004768
Nate Begemanb9a47b82009-02-23 08:49:38 +00004769 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00004770 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00004771 // present, fall back to case 3.
4772 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
4773 bool V1Only = true;
4774 bool V2Only = true;
4775 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004776 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00004777 if (EltIdx < 0)
4778 continue;
4779 if (EltIdx < 16)
4780 V2Only = false;
4781 else
4782 V1Only = false;
4783 }
Eric Christopherfd179292009-08-27 18:07:15 +00004784
Nate Begemanb9a47b82009-02-23 08:49:38 +00004785 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
4786 if (TLI.getSubtarget()->hasSSSE3()) {
4787 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004788
Nate Begemanb9a47b82009-02-23 08:49:38 +00004789 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00004790 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004791 //
4792 // Otherwise, we have elements from both input vectors, and must zero out
4793 // elements that come from V2 in the first mask, and V1 in the second mask
4794 // so that we can OR them together.
4795 bool TwoInputs = !(V1Only || V2Only);
4796 for (unsigned i = 0; i != 16; ++i) {
4797 int EltIdx = MaskVals[i];
4798 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004799 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004800 continue;
4801 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004802 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004803 }
4804 // If all the elements are from V2, assign it to V1 and return after
4805 // building the first pshufb.
4806 if (V2Only)
4807 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00004808 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004809 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004810 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004811 if (!TwoInputs)
4812 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00004813
Nate Begemanb9a47b82009-02-23 08:49:38 +00004814 // Calculate the shuffle mask for the second input, shuffle it, and
4815 // OR it with the first shuffled input.
4816 pshufbMask.clear();
4817 for (unsigned i = 0; i != 16; ++i) {
4818 int EltIdx = MaskVals[i];
4819 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004820 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004821 continue;
4822 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004823 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004824 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004825 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004826 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004827 MVT::v16i8, &pshufbMask[0], 16));
4828 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004829 }
Eric Christopherfd179292009-08-27 18:07:15 +00004830
Nate Begemanb9a47b82009-02-23 08:49:38 +00004831 // No SSSE3 - Calculate in place words and then fix all out of place words
4832 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
4833 // the 16 different words that comprise the two doublequadword input vectors.
Owen Anderson825b72b2009-08-11 20:47:22 +00004834 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
4835 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004836 SDValue NewV = V2Only ? V2 : V1;
4837 for (int i = 0; i != 8; ++i) {
4838 int Elt0 = MaskVals[i*2];
4839 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00004840
Nate Begemanb9a47b82009-02-23 08:49:38 +00004841 // This word of the result is all undef, skip it.
4842 if (Elt0 < 0 && Elt1 < 0)
4843 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004844
Nate Begemanb9a47b82009-02-23 08:49:38 +00004845 // This word of the result is already in the correct place, skip it.
4846 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
4847 continue;
4848 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
4849 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004850
Nate Begemanb9a47b82009-02-23 08:49:38 +00004851 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
4852 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
4853 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00004854
4855 // If Elt0 and Elt1 are defined, are consecutive, and can be load
4856 // using a single extract together, load it and store it.
4857 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004858 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004859 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00004860 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004861 DAG.getIntPtrConstant(i));
4862 continue;
4863 }
4864
Nate Begemanb9a47b82009-02-23 08:49:38 +00004865 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00004866 // source byte is not also odd, shift the extracted word left 8 bits
4867 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004868 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004869 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004870 DAG.getIntPtrConstant(Elt1 / 2));
4871 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004872 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004873 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004874 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004875 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
4876 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004877 }
4878 // If Elt0 is defined, extract it from the appropriate source. If the
4879 // source byte is not also even, shift the extracted word right 8 bits. If
4880 // Elt1 was also defined, OR the extracted values together before
4881 // inserting them in the result.
4882 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004883 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004884 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
4885 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004886 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004887 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004888 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004889 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
4890 DAG.getConstant(0x00FF, MVT::i16));
4891 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00004892 : InsElt0;
4893 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004894 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004895 DAG.getIntPtrConstant(i));
4896 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004897 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004898}
4899
Evan Cheng7a831ce2007-12-15 03:00:47 +00004900/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00004901/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00004902/// done when every pair / quad of shuffle mask elements point to elements in
4903/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00004904/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00004905static
Nate Begeman9008ca62009-04-27 18:41:29 +00004906SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00004907 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00004908 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00004909 SDValue V1 = SVOp->getOperand(0);
4910 SDValue V2 = SVOp->getOperand(1);
4911 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00004912 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00004913 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00004914 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004915 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004916 case MVT::v4f32: NewVT = MVT::v2f64; break;
4917 case MVT::v4i32: NewVT = MVT::v2i64; break;
4918 case MVT::v8i16: NewVT = MVT::v4i32; break;
4919 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004920 }
4921
Nate Begeman9008ca62009-04-27 18:41:29 +00004922 int Scale = NumElems / NewWidth;
4923 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00004924 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004925 int StartIdx = -1;
4926 for (int j = 0; j < Scale; ++j) {
4927 int EltIdx = SVOp->getMaskElt(i+j);
4928 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004929 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00004930 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00004931 StartIdx = EltIdx - (EltIdx % Scale);
4932 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00004933 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004934 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004935 if (StartIdx == -1)
4936 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00004937 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004938 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004939 }
4940
Dale Johannesenace16102009-02-03 19:33:06 +00004941 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V1);
4942 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00004943 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004944}
4945
Evan Chengd880b972008-05-09 21:53:03 +00004946/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004947///
Owen Andersone50ed302009-08-10 22:56:29 +00004948static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00004949 SDValue SrcOp, SelectionDAG &DAG,
4950 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004951 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004952 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00004953 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00004954 LD = dyn_cast<LoadSDNode>(SrcOp);
4955 if (!LD) {
4956 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
4957 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00004958 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
4959 if ((ExtVT.SimpleTy != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00004960 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4961 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00004962 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004963 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00004964 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Dale Johannesenace16102009-02-03 19:33:06 +00004965 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4966 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
4967 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
4968 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00004969 SrcOp.getOperand(0)
4970 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004971 }
4972 }
4973 }
4974
Dale Johannesenace16102009-02-03 19:33:06 +00004975 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4976 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004977 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00004978 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004979}
4980
Evan Chengace3c172008-07-22 21:13:36 +00004981/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
4982/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004983static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00004984LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
4985 SDValue V1 = SVOp->getOperand(0);
4986 SDValue V2 = SVOp->getOperand(1);
4987 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004988 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00004989
Evan Chengace3c172008-07-22 21:13:36 +00004990 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00004991 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00004992 SmallVector<int, 8> Mask1(4U, -1);
4993 SmallVector<int, 8> PermMask;
4994 SVOp->getMask(PermMask);
4995
Evan Chengace3c172008-07-22 21:13:36 +00004996 unsigned NumHi = 0;
4997 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00004998 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004999 int Idx = PermMask[i];
5000 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005001 Locs[i] = std::make_pair(-1, -1);
5002 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005003 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
5004 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005005 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00005006 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005007 NumLo++;
5008 } else {
5009 Locs[i] = std::make_pair(1, NumHi);
5010 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005011 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005012 NumHi++;
5013 }
5014 }
5015 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005016
Evan Chengace3c172008-07-22 21:13:36 +00005017 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005018 // If no more than two elements come from either vector. This can be
5019 // implemented with two shuffles. First shuffle gather the elements.
5020 // The second shuffle, which takes the first shuffle as both of its
5021 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00005022 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005023
Nate Begeman9008ca62009-04-27 18:41:29 +00005024 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00005025
Evan Chengace3c172008-07-22 21:13:36 +00005026 for (unsigned i = 0; i != 4; ++i) {
5027 if (Locs[i].first == -1)
5028 continue;
5029 else {
5030 unsigned Idx = (i < 2) ? 0 : 4;
5031 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005032 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005033 }
5034 }
5035
Nate Begeman9008ca62009-04-27 18:41:29 +00005036 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005037 } else if (NumLo == 3 || NumHi == 3) {
5038 // Otherwise, we must have three elements from one vector, call it X, and
5039 // one element from the other, call it Y. First, use a shufps to build an
5040 // intermediate vector with the one element from Y and the element from X
5041 // that will be in the same half in the final destination (the indexes don't
5042 // matter). Then, use a shufps to build the final vector, taking the half
5043 // containing the element from Y from the intermediate, and the other half
5044 // from X.
5045 if (NumHi == 3) {
5046 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00005047 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005048 std::swap(V1, V2);
5049 }
5050
5051 // Find the element from V2.
5052 unsigned HiIndex;
5053 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005054 int Val = PermMask[HiIndex];
5055 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005056 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005057 if (Val >= 4)
5058 break;
5059 }
5060
Nate Begeman9008ca62009-04-27 18:41:29 +00005061 Mask1[0] = PermMask[HiIndex];
5062 Mask1[1] = -1;
5063 Mask1[2] = PermMask[HiIndex^1];
5064 Mask1[3] = -1;
5065 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005066
5067 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005068 Mask1[0] = PermMask[0];
5069 Mask1[1] = PermMask[1];
5070 Mask1[2] = HiIndex & 1 ? 6 : 4;
5071 Mask1[3] = HiIndex & 1 ? 4 : 6;
5072 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005073 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005074 Mask1[0] = HiIndex & 1 ? 2 : 0;
5075 Mask1[1] = HiIndex & 1 ? 0 : 2;
5076 Mask1[2] = PermMask[2];
5077 Mask1[3] = PermMask[3];
5078 if (Mask1[2] >= 0)
5079 Mask1[2] += 4;
5080 if (Mask1[3] >= 0)
5081 Mask1[3] += 4;
5082 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005083 }
Evan Chengace3c172008-07-22 21:13:36 +00005084 }
5085
5086 // Break it into (shuffle shuffle_hi, shuffle_lo).
5087 Locs.clear();
Nate Begeman9008ca62009-04-27 18:41:29 +00005088 SmallVector<int,8> LoMask(4U, -1);
5089 SmallVector<int,8> HiMask(4U, -1);
5090
5091 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00005092 unsigned MaskIdx = 0;
5093 unsigned LoIdx = 0;
5094 unsigned HiIdx = 2;
5095 for (unsigned i = 0; i != 4; ++i) {
5096 if (i == 2) {
5097 MaskPtr = &HiMask;
5098 MaskIdx = 1;
5099 LoIdx = 0;
5100 HiIdx = 2;
5101 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005102 int Idx = PermMask[i];
5103 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005104 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00005105 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005106 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005107 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005108 LoIdx++;
5109 } else {
5110 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005111 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005112 HiIdx++;
5113 }
5114 }
5115
Nate Begeman9008ca62009-04-27 18:41:29 +00005116 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
5117 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
5118 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00005119 for (unsigned i = 0; i != 4; ++i) {
5120 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005121 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00005122 } else {
5123 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005124 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00005125 }
5126 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005127 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00005128}
5129
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005130static bool MayFoldVectorLoad(SDValue V) {
5131 if (V.hasOneUse() && V.getOpcode() == ISD::BIT_CONVERT)
5132 V = V.getOperand(0);
5133 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5134 V = V.getOperand(0);
5135 if (MayFoldLoad(V))
5136 return true;
5137 return false;
5138}
5139
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005140// FIXME: the version above should always be used. Since there's
5141// a bug where several vector shuffles can't be folded because the
5142// DAG is not updated during lowering and a node claims to have two
5143// uses while it only has one, use this version, and let isel match
5144// another instruction if the load really happens to have more than
5145// one use. Remove this version after this bug get fixed.
5146static bool RelaxedMayFoldVectorLoad(SDValue V) {
5147 if (V.hasOneUse() && V.getOpcode() == ISD::BIT_CONVERT)
5148 V = V.getOperand(0);
5149 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5150 V = V.getOperand(0);
5151 if (ISD::isNormalLoad(V.getNode()))
5152 return true;
5153 return false;
5154}
5155
5156/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
5157/// a vector extract, and if both can be later optimized into a single load.
5158/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
5159/// here because otherwise a target specific shuffle node is going to be
5160/// emitted for this shuffle, and the optimization not done.
5161/// FIXME: This is probably not the best approach, but fix the problem
5162/// until the right path is decided.
5163static
5164bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
5165 const TargetLowering &TLI) {
5166 EVT VT = V.getValueType();
5167 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
5168
5169 // Be sure that the vector shuffle is present in a pattern like this:
5170 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
5171 if (!V.hasOneUse())
5172 return false;
5173
5174 SDNode *N = *V.getNode()->use_begin();
5175 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5176 return false;
5177
5178 SDValue EltNo = N->getOperand(1);
5179 if (!isa<ConstantSDNode>(EltNo))
5180 return false;
5181
5182 // If the bit convert changed the number of elements, it is unsafe
5183 // to examine the mask.
5184 bool HasShuffleIntoBitcast = false;
5185 if (V.getOpcode() == ISD::BIT_CONVERT) {
5186 EVT SrcVT = V.getOperand(0).getValueType();
5187 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
5188 return false;
5189 V = V.getOperand(0);
5190 HasShuffleIntoBitcast = true;
5191 }
5192
5193 // Select the input vector, guarding against out of range extract vector.
5194 unsigned NumElems = VT.getVectorNumElements();
5195 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
5196 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
5197 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
5198
5199 // Skip one more bit_convert if necessary
5200 if (V.getOpcode() == ISD::BIT_CONVERT)
5201 V = V.getOperand(0);
5202
5203 if (ISD::isNormalLoad(V.getNode())) {
5204 // Is the original load suitable?
5205 LoadSDNode *LN0 = cast<LoadSDNode>(V);
5206
5207 // FIXME: avoid the multi-use bug that is preventing lots of
5208 // of foldings to be detected, this is still wrong of course, but
5209 // give the temporary desired behavior, and if it happens that
5210 // the load has real more uses, during isel it will not fold, and
5211 // will generate poor code.
5212 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
5213 return false;
5214
5215 if (!HasShuffleIntoBitcast)
5216 return true;
5217
5218 // If there's a bitcast before the shuffle, check if the load type and
5219 // alignment is valid.
5220 unsigned Align = LN0->getAlignment();
5221 unsigned NewAlign =
5222 TLI.getTargetData()->getABITypeAlignment(
5223 VT.getTypeForEVT(*DAG.getContext()));
5224
5225 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
5226 return false;
5227 }
5228
5229 return true;
5230}
5231
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005232static
5233SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
5234 bool HasSSE2) {
5235 SDValue V1 = Op.getOperand(0);
5236 SDValue V2 = Op.getOperand(1);
5237 EVT VT = Op.getValueType();
5238
5239 assert(VT != MVT::v2i64 && "unsupported shuffle type");
5240
5241 if (HasSSE2 && VT == MVT::v2f64)
5242 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
5243
5244 // v4f32 or v4i32
5245 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V2, DAG);
5246}
5247
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005248static
5249SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
5250 SDValue V1 = Op.getOperand(0);
5251 SDValue V2 = Op.getOperand(1);
5252 EVT VT = Op.getValueType();
5253
5254 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
5255 "unsupported shuffle type");
5256
5257 if (V2.getOpcode() == ISD::UNDEF)
5258 V2 = V1;
5259
5260 // v4i32 or v4f32
5261 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
5262}
5263
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005264static
5265SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
5266 SDValue V1 = Op.getOperand(0);
5267 SDValue V2 = Op.getOperand(1);
5268 EVT VT = Op.getValueType();
5269 unsigned NumElems = VT.getVectorNumElements();
5270
5271 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
5272 // operand of these instructions is only memory, so check if there's a
5273 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
5274 // same masks.
5275 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005276
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00005277 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005278 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005279 CanFoldLoad = true;
5280
5281 // When V1 is a load, it can be folded later into a store in isel, example:
5282 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
5283 // turns into:
5284 // (MOVLPSmr addr:$src1, VR128:$src2)
5285 // So, recognize this potential and also use MOVLPS or MOVLPD
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005286 if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005287 CanFoldLoad = true;
5288
5289 if (CanFoldLoad) {
5290 if (HasSSE2 && NumElems == 2)
5291 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
5292
5293 if (NumElems == 4)
5294 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
5295 }
5296
5297 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5298 // movl and movlp will both match v2i64, but v2i64 is never matched by
5299 // movl earlier because we make it strict to avoid messing with the movlp load
5300 // folding logic (see the code above getMOVLP call). Match it here then,
5301 // this is horrible, but will stay like this until we move all shuffle
5302 // matching to x86 specific nodes. Note that for the 1st condition all
5303 // types are matched with movsd.
5304 if ((HasSSE2 && NumElems == 2) || !X86::isMOVLMask(SVOp))
5305 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5306 else if (HasSSE2)
5307 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5308
5309
5310 assert(VT != MVT::v4i32 && "unsupported shuffle type");
5311
5312 // Invert the operand order and use SHUFPS to match it.
5313 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V2, V1,
5314 X86::getShuffleSHUFImmediate(SVOp), DAG);
5315}
5316
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005317static inline unsigned getUNPCKLOpcode(EVT VT) {
5318 switch(VT.getSimpleVT().SimpleTy) {
5319 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
5320 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
5321 case MVT::v4f32: return X86ISD::UNPCKLPS;
5322 case MVT::v2f64: return X86ISD::UNPCKLPD;
5323 case MVT::v16i8: return X86ISD::PUNPCKLBW;
5324 case MVT::v8i16: return X86ISD::PUNPCKLWD;
5325 default:
5326 llvm_unreachable("Unknow type for unpckl");
5327 }
5328 return 0;
5329}
5330
5331static inline unsigned getUNPCKHOpcode(EVT VT) {
5332 switch(VT.getSimpleVT().SimpleTy) {
5333 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
5334 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
5335 case MVT::v4f32: return X86ISD::UNPCKHPS;
5336 case MVT::v2f64: return X86ISD::UNPCKHPD;
5337 case MVT::v16i8: return X86ISD::PUNPCKHBW;
5338 case MVT::v8i16: return X86ISD::PUNPCKHWD;
5339 default:
5340 llvm_unreachable("Unknow type for unpckh");
5341 }
5342 return 0;
5343}
5344
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005345static
5346SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005347 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005348 const X86Subtarget *Subtarget) {
5349 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5350 EVT VT = Op.getValueType();
5351 DebugLoc dl = Op.getDebugLoc();
5352 SDValue V1 = Op.getOperand(0);
5353 SDValue V2 = Op.getOperand(1);
5354
5355 if (isZeroShuffle(SVOp))
5356 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
5357
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005358 // Handle splat operations
5359 if (SVOp->isSplat()) {
5360 // Special case, this is the only place now where it's
5361 // allowed to return a vector_shuffle operation without
5362 // using a target specific node, because *hopefully* it
5363 // will be optimized away by the dag combiner.
5364 if (VT.getVectorNumElements() <= 4 &&
5365 CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
5366 return Op;
5367
5368 // Handle splats by matching through known masks
5369 if (VT.getVectorNumElements() <= 4)
5370 return SDValue();
5371
5372 // Canonize all of the remaining to v4f32.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005373 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005374 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005375
5376 // If the shuffle can be profitably rewritten as a narrower shuffle, then
5377 // do it!
5378 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
5379 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5380 if (NewOp.getNode())
5381 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, NewOp);
5382 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
5383 // FIXME: Figure out a cleaner way to do this.
5384 // Try to make use of movq to zero out the top part.
5385 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
5386 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5387 if (NewOp.getNode()) {
5388 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
5389 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
5390 DAG, Subtarget, dl);
5391 }
5392 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
5393 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5394 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
5395 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
5396 DAG, Subtarget, dl);
5397 }
5398 }
5399 return SDValue();
5400}
5401
Dan Gohman475871a2008-07-27 21:46:04 +00005402SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005403X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00005404 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00005405 SDValue V1 = Op.getOperand(0);
5406 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005407 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005408 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00005409 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005410 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005411 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
5412 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00005413 bool V1IsSplat = false;
5414 bool V2IsSplat = false;
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005415 bool HasSSE2 = Subtarget->hasSSE2() || Subtarget->hasAVX();
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005416 bool HasSSE3 = Subtarget->hasSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005417 bool HasSSSE3 = Subtarget->hasSSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005418 MachineFunction &MF = DAG.getMachineFunction();
5419 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005420
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00005421 // FIXME: this is somehow handled during isel by MMX pattern fragments. Remove
5422 // the check or come up with another solution when all MMX move to intrinsics,
5423 // but don't allow this to be considered legal, we don't want vector_shuffle
5424 // operations to be matched during isel anymore.
5425 if (isMMX && SVOp->isSplat())
5426 return Op;
5427
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005428 // Vector shuffle lowering takes 3 steps:
5429 //
5430 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
5431 // narrowing and commutation of operands should be handled.
5432 // 2) Matching of shuffles with known shuffle masks to x86 target specific
5433 // shuffle nodes.
5434 // 3) Rewriting of unmatched masks into new generic shuffle operations,
5435 // so the shuffle can be broken into other shuffles and the legalizer can
5436 // try the lowering again.
5437 //
5438 // The general ideia is that no vector_shuffle operation should be left to
5439 // be matched during isel, all of them must be converted to a target specific
5440 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00005441
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005442 // Normalize the input vectors. Here splats, zeroed vectors, profitable
5443 // narrowing and commutation of operands should be handled. The actual code
5444 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005445 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005446 if (NewOp.getNode())
5447 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00005448
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005449 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
5450 // unpckh_undef). Only use pshufd if speed is more important than size.
5451 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
5452 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5453 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
5454 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
5455 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5456 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00005457
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005458 if (X86::isMOVDDUPMask(SVOp) && HasSSE3 && V2IsUndef &&
5459 RelaxedMayFoldVectorLoad(V1) && !isMMX)
5460 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
5461
5462 if (!isMMX && X86::isMOVHLPS_v_undef_Mask(SVOp))
5463 return getMOVHighToLow(Op, dl, DAG);
5464
5465 // Use to match splats
5466 if (HasSSE2 && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
5467 (VT == MVT::v2f64 || VT == MVT::v2i64))
5468 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5469
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005470 if (X86::isPSHUFDMask(SVOp)) {
5471 // The actual implementation will match the mask in the if above and then
5472 // during isel it can match several different instructions, not only pshufd
5473 // as its name says, sad but true, emulate the behavior for now...
5474 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
5475 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
5476
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005477 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5478
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005479 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005480 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
5481
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005482 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005483 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V1,
5484 TargetMask, DAG);
5485
5486 if (VT == MVT::v4f32)
5487 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V1,
5488 TargetMask, DAG);
5489 }
Eric Christopherfd179292009-08-27 18:07:15 +00005490
Evan Chengf26ffe92008-05-29 08:22:04 +00005491 // Check if this can be converted into a logical shift.
5492 bool isLeft = false;
5493 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00005494 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00005495 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00005496 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00005497 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005498 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00005499 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005500 EVT EltVT = VT.getVectorElementType();
5501 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005502 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005503 }
Eric Christopherfd179292009-08-27 18:07:15 +00005504
Nate Begeman9008ca62009-04-27 18:41:29 +00005505 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005506 if (V1IsUndef)
5507 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00005508 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00005509 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005510 if (!isMMX && !X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005511 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005512 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5513
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005514 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005515 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5516 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00005517 }
Eric Christopherfd179292009-08-27 18:07:15 +00005518
Nate Begeman9008ca62009-04-27 18:41:29 +00005519 // FIXME: fold these into legal mask.
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005520 if (!isMMX) {
Daniel Dunbar31394222010-09-03 19:38:11 +00005521 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005522 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
5523
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005524 if (X86::isMOVHLPSMask(SVOp))
5525 return getMOVHighToLow(Op, dl, DAG);
5526
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005527 if (X86::isMOVSHDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5528 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
5529
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00005530 if (X86::isMOVSLDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5531 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
5532
5533 if (X86::isMOVLPMask(SVOp))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005534 return getMOVLP(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005535 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005536
Nate Begeman9008ca62009-04-27 18:41:29 +00005537 if (ShouldXformToMOVHLPS(SVOp) ||
5538 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
5539 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005540
Evan Chengf26ffe92008-05-29 08:22:04 +00005541 if (isShift) {
5542 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005543 EVT EltVT = VT.getVectorElementType();
5544 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005545 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005546 }
Eric Christopherfd179292009-08-27 18:07:15 +00005547
Evan Cheng9eca5e82006-10-25 21:49:50 +00005548 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00005549 // FIXME: This should also accept a bitcast of a splat? Be careful, not
5550 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00005551 V1IsSplat = isSplatVector(V1.getNode());
5552 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005553
Chris Lattner8a594482007-11-25 00:24:49 +00005554 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00005555 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005556 Op = CommuteVectorShuffle(SVOp, DAG);
5557 SVOp = cast<ShuffleVectorSDNode>(Op);
5558 V1 = SVOp->getOperand(0);
5559 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00005560 std::swap(V1IsSplat, V2IsSplat);
5561 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005562 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00005563 }
5564
Nate Begeman9008ca62009-04-27 18:41:29 +00005565 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
5566 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00005567 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00005568 return V1;
5569 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
5570 // the instruction selector will not match, so get a canonical MOVL with
5571 // swapped operands to undo the commute.
5572 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00005573 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005574
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005575 if (X86::isUNPCKLMask(SVOp))
5576 return (isMMX) ?
5577 Op : getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V2, DAG);
5578
5579 if (X86::isUNPCKHMask(SVOp))
5580 return (isMMX) ?
5581 Op : getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00005582
Evan Cheng9bbbb982006-10-25 20:48:19 +00005583 if (V2IsSplat) {
5584 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005585 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00005586 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00005587 SDValue NewMask = NormalizeMask(SVOp, DAG);
5588 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
5589 if (NSVOp != SVOp) {
5590 if (X86::isUNPCKLMask(NSVOp, true)) {
5591 return NewMask;
5592 } else if (X86::isUNPCKHMask(NSVOp, true)) {
5593 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005594 }
5595 }
5596 }
5597
Evan Cheng9eca5e82006-10-25 21:49:50 +00005598 if (Commuted) {
5599 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00005600 // FIXME: this seems wrong.
5601 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
5602 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005603
5604 if (X86::isUNPCKLMask(NewSVOp))
5605 return (isMMX) ?
5606 NewOp : getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V2, V1, DAG);
5607
5608 if (X86::isUNPCKHMask(NewSVOp))
5609 return (isMMX) ?
5610 NewOp : getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005611 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005612
Nate Begemanb9a47b82009-02-23 08:49:38 +00005613 // FIXME: for mmx, bitcast v2i32 to v4i16 for shuffle.
Nate Begeman9008ca62009-04-27 18:41:29 +00005614
5615 // Normalize the node to match x86 shuffle ops if needed
5616 if (!isMMX && V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
5617 return CommuteVectorShuffle(SVOp, DAG);
5618
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00005619 // The checks below are all present in isShuffleMaskLegal, but they are
5620 // inlined here right now to enable us to directly emit target specific
5621 // nodes, and remove one by one until they don't return Op anymore.
5622 SmallVector<int, 16> M;
5623 SVOp->getMask(M);
5624
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005625 if (isPALIGNRMask(M, VT, HasSSSE3))
5626 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
5627 X86::getShufflePALIGNRImmediate(SVOp),
5628 DAG);
5629
Bruno Cardoso Lopes2eb63df2010-09-04 02:58:56 +00005630 // Only a few shuffle masks are handled for 64-bit vectors (MMX), and
5631 // 64-bit vectors which made to this point can't be handled, they are
5632 // expanded.
Bruno Cardoso Lopes67fc1e72010-09-07 18:24:00 +00005633 if (isMMX)
Bruno Cardoso Lopes828f6ae2010-09-04 02:50:13 +00005634 return SDValue();
5635
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00005636 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
5637 SVOp->getSplatIndex() == 0 && V2IsUndef) {
5638 if (VT == MVT::v2f64)
5639 return getTargetShuffleNode(X86ISD::UNPCKLPD, dl, VT, V1, V1, DAG);
5640 if (VT == MVT::v2i64)
5641 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
5642 }
5643
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00005644 if (isPSHUFHWMask(M, VT))
5645 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
5646 X86::getShufflePSHUFHWImmediate(SVOp),
5647 DAG);
5648
5649 if (isPSHUFLWMask(M, VT))
5650 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
5651 X86::getShufflePSHUFLWImmediate(SVOp),
5652 DAG);
5653
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00005654 if (isSHUFPMask(M, VT)) {
5655 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5656 if (VT == MVT::v4f32 || VT == MVT::v4i32)
5657 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V2,
5658 TargetMask, DAG);
5659 if (VT == MVT::v2f64 || VT == MVT::v2i64)
5660 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V2,
5661 TargetMask, DAG);
5662 }
5663
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005664 if (X86::isUNPCKL_v_undef_Mask(SVOp))
5665 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5666 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
5667 if (X86::isUNPCKH_v_undef_Mask(SVOp))
5668 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5669 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5670
Evan Cheng14b32e12007-12-11 01:46:18 +00005671 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00005672 if (VT == MVT::v8i16) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005673 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005674 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00005675 return NewOp;
5676 }
5677
Owen Anderson825b72b2009-08-11 20:47:22 +00005678 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005679 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005680 if (NewOp.getNode())
5681 return NewOp;
5682 }
Eric Christopherfd179292009-08-27 18:07:15 +00005683
Evan Chengace3c172008-07-22 21:13:36 +00005684 // Handle all 4 wide cases with a number of shuffles except for MMX.
5685 if (NumElems == 4 && !isMMX)
Nate Begeman9008ca62009-04-27 18:41:29 +00005686 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005687
Dan Gohman475871a2008-07-27 21:46:04 +00005688 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005689}
5690
Dan Gohman475871a2008-07-27 21:46:04 +00005691SDValue
5692X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00005693 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005694 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005695 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005696 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005697 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005698 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005699 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005700 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005701 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005702 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00005703 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
5704 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
5705 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005706 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
5707 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Dale Johannesenace16102009-02-03 19:33:06 +00005708 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005709 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00005710 Op.getOperand(0)),
5711 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005712 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005713 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005714 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005715 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005716 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00005717 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00005718 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
5719 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005720 // result has a single use which is a store or a bitcast to i32. And in
5721 // the case of a store, it's not worth it if the index is a constant 0,
5722 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00005723 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00005724 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00005725 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005726 if ((User->getOpcode() != ISD::STORE ||
5727 (isa<ConstantSDNode>(Op.getOperand(1)) &&
5728 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman171c11e2008-04-16 02:32:24 +00005729 (User->getOpcode() != ISD::BIT_CONVERT ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005730 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00005731 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00005732 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
5733 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00005734 Op.getOperand(0)),
5735 Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005736 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Extract);
5737 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00005738 // ExtractPS works with constant index.
5739 if (isa<ConstantSDNode>(Op.getOperand(1)))
5740 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00005741 }
Dan Gohman475871a2008-07-27 21:46:04 +00005742 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005743}
5744
5745
Dan Gohman475871a2008-07-27 21:46:04 +00005746SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005747X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
5748 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005749 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00005750 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005751
Evan Cheng62a3f152008-03-24 21:52:23 +00005752 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005753 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005754 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00005755 return Res;
5756 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00005757
Owen Andersone50ed302009-08-10 22:56:29 +00005758 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005759 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005760 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005761 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005762 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005763 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005764 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005765 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
5766 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Scott Michelfdc40a02009-02-17 22:15:04 +00005767 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005768 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00005769 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005770 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00005771 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00005772 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00005773 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00005774 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00005775 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005776 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005777 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005778 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005779 if (Idx == 0)
5780 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00005781
Evan Cheng0db9fe62006-04-25 20:13:52 +00005782 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00005783 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00005784 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00005785 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00005786 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00005787 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00005788 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00005789 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00005790 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
5791 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
5792 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005793 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005794 if (Idx == 0)
5795 return Op;
5796
5797 // UNPCKHPD the element to the lowest double word, then movsd.
5798 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
5799 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00005800 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00005801 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00005802 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00005803 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00005804 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00005805 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005806 }
5807
Dan Gohman475871a2008-07-27 21:46:04 +00005808 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005809}
5810
Dan Gohman475871a2008-07-27 21:46:04 +00005811SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005812X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
5813 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005814 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00005815 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005816 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005817
Dan Gohman475871a2008-07-27 21:46:04 +00005818 SDValue N0 = Op.getOperand(0);
5819 SDValue N1 = Op.getOperand(1);
5820 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00005821
Dan Gohman8a55ce42009-09-23 21:02:20 +00005822 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00005823 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00005824 unsigned Opc;
5825 if (VT == MVT::v8i16)
5826 Opc = X86ISD::PINSRW;
5827 else if (VT == MVT::v4i16)
5828 Opc = X86ISD::MMX_PINSRW;
5829 else if (VT == MVT::v16i8)
5830 Opc = X86ISD::PINSRB;
5831 else
5832 Opc = X86ISD::PINSRB;
5833
Nate Begeman14d12ca2008-02-11 04:19:36 +00005834 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
5835 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00005836 if (N1.getValueType() != MVT::i32)
5837 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
5838 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005839 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00005840 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00005841 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00005842 // Bits [7:6] of the constant are the source select. This will always be
5843 // zero here. The DAG Combiner may combine an extract_elt index into these
5844 // bits. For example (insert (extract, 3), 2) could be matched by putting
5845 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00005846 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00005847 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00005848 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00005849 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005850 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00005851 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00005852 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00005853 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00005854 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00005855 // PINSR* works with constant index.
5856 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00005857 }
Dan Gohman475871a2008-07-27 21:46:04 +00005858 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005859}
5860
Dan Gohman475871a2008-07-27 21:46:04 +00005861SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005862X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005863 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00005864 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005865
5866 if (Subtarget->hasSSE41())
5867 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
5868
Dan Gohman8a55ce42009-09-23 21:02:20 +00005869 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00005870 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00005871
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005872 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005873 SDValue N0 = Op.getOperand(0);
5874 SDValue N1 = Op.getOperand(1);
5875 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00005876
Dan Gohman8a55ce42009-09-23 21:02:20 +00005877 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00005878 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
5879 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00005880 if (N1.getValueType() != MVT::i32)
5881 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
5882 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005883 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00005884 return DAG.getNode(VT == MVT::v8i16 ? X86ISD::PINSRW : X86ISD::MMX_PINSRW,
5885 dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005886 }
Dan Gohman475871a2008-07-27 21:46:04 +00005887 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005888}
5889
Dan Gohman475871a2008-07-27 21:46:04 +00005890SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005891X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005892 DebugLoc dl = Op.getDebugLoc();
Chris Lattnerf172ecd2010-07-04 23:07:25 +00005893
5894 if (Op.getValueType() == MVT::v1i64 &&
5895 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00005896 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00005897
Owen Anderson825b72b2009-08-11 20:47:22 +00005898 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
5899 EVT VT = MVT::v2i32;
5900 switch (Op.getValueType().getSimpleVT().SimpleTy) {
Evan Chengefec7512008-02-18 23:04:32 +00005901 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00005902 case MVT::v16i8:
5903 case MVT::v8i16:
5904 VT = MVT::v4i32;
Evan Chengefec7512008-02-18 23:04:32 +00005905 break;
5906 }
Dale Johannesenace16102009-02-03 19:33:06 +00005907 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(),
5908 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005909}
5910
Bill Wendling056292f2008-09-16 21:48:12 +00005911// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
5912// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
5913// one of the above mentioned nodes. It has to be wrapped because otherwise
5914// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
5915// be used to form addressing mode. These wrapped nodes will be selected
5916// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00005917SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005918X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005919 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00005920
Chris Lattner41621a22009-06-26 19:22:52 +00005921 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5922 // global base reg.
5923 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005924 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005925 CodeModel::Model M = getTargetMachine().getCodeModel();
5926
Chris Lattner4f066492009-07-11 20:29:19 +00005927 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005928 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005929 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005930 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005931 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005932 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005933 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005934
Evan Cheng1606e8e2009-03-13 07:51:59 +00005935 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00005936 CP->getAlignment(),
5937 CP->getOffset(), OpFlag);
5938 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00005939 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005940 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00005941 if (OpFlag) {
5942 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005943 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005944 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005945 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005946 }
5947
5948 return Result;
5949}
5950
Dan Gohmand858e902010-04-17 15:26:15 +00005951SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00005952 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00005953
Chris Lattner18c59872009-06-27 04:16:01 +00005954 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5955 // global base reg.
5956 unsigned char OpFlag = 0;
5957 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005958 CodeModel::Model M = getTargetMachine().getCodeModel();
5959
Chris Lattner4f066492009-07-11 20:29:19 +00005960 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005961 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005962 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005963 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005964 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005965 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005966 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005967
Chris Lattner18c59872009-06-27 04:16:01 +00005968 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
5969 OpFlag);
5970 DebugLoc DL = JT->getDebugLoc();
5971 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005972
Chris Lattner18c59872009-06-27 04:16:01 +00005973 // With PIC, the address is actually $g + Offset.
5974 if (OpFlag) {
5975 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5976 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005977 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00005978 Result);
5979 }
Eric Christopherfd179292009-08-27 18:07:15 +00005980
Chris Lattner18c59872009-06-27 04:16:01 +00005981 return Result;
5982}
5983
5984SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005985X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00005986 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00005987
Chris Lattner18c59872009-06-27 04:16:01 +00005988 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5989 // global base reg.
5990 unsigned char OpFlag = 0;
5991 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005992 CodeModel::Model M = getTargetMachine().getCodeModel();
5993
Chris Lattner4f066492009-07-11 20:29:19 +00005994 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005995 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005996 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005997 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005998 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005999 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006000 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006001
Chris Lattner18c59872009-06-27 04:16:01 +00006002 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00006003
Chris Lattner18c59872009-06-27 04:16:01 +00006004 DebugLoc DL = Op.getDebugLoc();
6005 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006006
6007
Chris Lattner18c59872009-06-27 04:16:01 +00006008 // With PIC, the address is actually $g + Offset.
6009 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00006010 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00006011 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6012 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006013 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006014 Result);
6015 }
Eric Christopherfd179292009-08-27 18:07:15 +00006016
Chris Lattner18c59872009-06-27 04:16:01 +00006017 return Result;
6018}
6019
Dan Gohman475871a2008-07-27 21:46:04 +00006020SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006021X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00006022 // Create the TargetBlockAddressAddress node.
6023 unsigned char OpFlags =
6024 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00006025 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00006026 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00006027 DebugLoc dl = Op.getDebugLoc();
6028 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
6029 /*isTarget=*/true, OpFlags);
6030
Dan Gohmanf705adb2009-10-30 01:28:02 +00006031 if (Subtarget->isPICStyleRIPRel() &&
6032 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00006033 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6034 else
6035 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00006036
Dan Gohman29cbade2009-11-20 23:18:13 +00006037 // With PIC, the address is actually $g + Offset.
6038 if (isGlobalRelativeToPICBase(OpFlags)) {
6039 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6040 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
6041 Result);
6042 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00006043
6044 return Result;
6045}
6046
6047SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00006048X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00006049 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00006050 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00006051 // Create the TargetGlobalAddress node, folding in the constant
6052 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00006053 unsigned char OpFlags =
6054 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006055 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00006056 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006057 if (OpFlags == X86II::MO_NO_FLAG &&
6058 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00006059 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00006060 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00006061 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006062 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00006063 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006064 }
Eric Christopherfd179292009-08-27 18:07:15 +00006065
Chris Lattner4f066492009-07-11 20:29:19 +00006066 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006067 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00006068 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6069 else
6070 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00006071
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006072 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00006073 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006074 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6075 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006076 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006077 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006078
Chris Lattner36c25012009-07-10 07:34:39 +00006079 // For globals that require a load from a stub to get the address, emit the
6080 // load.
6081 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00006082 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006083 MachinePointerInfo::getGOT(), false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006084
Dan Gohman6520e202008-10-18 02:06:02 +00006085 // If there was a non-zero offset that we didn't fold, create an explicit
6086 // addition for it.
6087 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006088 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00006089 DAG.getConstant(Offset, getPointerTy()));
6090
Evan Cheng0db9fe62006-04-25 20:13:52 +00006091 return Result;
6092}
6093
Evan Chengda43bcf2008-09-24 00:05:32 +00006094SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006095X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00006096 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00006097 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006098 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00006099}
6100
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006101static SDValue
6102GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00006103 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00006104 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006105 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Owen Anderson825b72b2009-08-11 20:47:22 +00006106 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006107 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006108 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006109 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006110 GA->getOffset(),
6111 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006112 if (InFlag) {
6113 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006114 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006115 } else {
6116 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006117 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006118 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006119
6120 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00006121 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006122
Rafael Espindola15f1b662009-04-24 12:59:40 +00006123 SDValue Flag = Chain.getValue(1);
6124 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006125}
6126
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006127// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006128static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006129LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006130 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00006131 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00006132 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
6133 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006134 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006135 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006136 InFlag = Chain.getValue(1);
6137
Chris Lattnerb903bed2009-06-26 21:20:29 +00006138 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006139}
6140
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006141// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006142static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006143LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006144 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006145 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
6146 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006147}
6148
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006149// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
6150// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00006151static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006152 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00006153 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006154 DebugLoc dl = GA->getDebugLoc();
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006155
6156 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
6157 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
6158 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00006159
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006160 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
6161 DAG.getIntPtrConstant(0),
6162 MachinePointerInfo(Ptr), false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00006163
Chris Lattnerb903bed2009-06-26 21:20:29 +00006164 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006165 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
6166 // initialexec.
6167 unsigned WrapperKind = X86ISD::Wrapper;
6168 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006169 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00006170 } else if (is64Bit) {
6171 assert(model == TLSModel::InitialExec);
6172 OperandFlags = X86II::MO_GOTTPOFF;
6173 WrapperKind = X86ISD::WrapperRIP;
6174 } else {
6175 assert(model == TLSModel::InitialExec);
6176 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00006177 }
Eric Christopherfd179292009-08-27 18:07:15 +00006178
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006179 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
6180 // exec)
Devang Patel0d881da2010-07-06 22:08:15 +00006181 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
6182 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006183 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006184 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006185
Rafael Espindola9a580232009-02-27 13:37:18 +00006186 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006187 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006188 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006189
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006190 // The address of the thread local variable is the add of the thread
6191 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00006192 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006193}
6194
Dan Gohman475871a2008-07-27 21:46:04 +00006195SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006196X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Eric Christopher30ef0e52010-06-03 04:07:48 +00006197
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006198 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00006199 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00006200
Eric Christopher30ef0e52010-06-03 04:07:48 +00006201 if (Subtarget->isTargetELF()) {
6202 // TODO: implement the "local dynamic" model
6203 // TODO: implement the "initial exec"model for pic executables
6204
6205 // If GV is an alias then use the aliasee for determining
6206 // thread-localness.
6207 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
6208 GV = GA->resolveAliasedGlobal(false);
6209
6210 TLSModel::Model model
6211 = getTLSModel(GV, getTargetMachine().getRelocationModel());
6212
6213 switch (model) {
6214 case TLSModel::GeneralDynamic:
6215 case TLSModel::LocalDynamic: // not implemented
6216 if (Subtarget->is64Bit())
6217 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
6218 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
6219
6220 case TLSModel::InitialExec:
6221 case TLSModel::LocalExec:
6222 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
6223 Subtarget->is64Bit());
6224 }
6225 } else if (Subtarget->isTargetDarwin()) {
6226 // Darwin only has one model of TLS. Lower to that.
6227 unsigned char OpFlag = 0;
6228 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
6229 X86ISD::WrapperRIP : X86ISD::Wrapper;
6230
6231 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6232 // global base reg.
6233 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
6234 !Subtarget->is64Bit();
6235 if (PIC32)
6236 OpFlag = X86II::MO_TLVP_PIC_BASE;
6237 else
6238 OpFlag = X86II::MO_TLVP;
Devang Patel0d881da2010-07-06 22:08:15 +00006239 DebugLoc DL = Op.getDebugLoc();
6240 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopher30ef0e52010-06-03 04:07:48 +00006241 getPointerTy(),
6242 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00006243 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
6244
6245 // With PIC32, the address is actually $g + Offset.
6246 if (PIC32)
6247 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6248 DAG.getNode(X86ISD::GlobalBaseReg,
6249 DebugLoc(), getPointerTy()),
6250 Offset);
6251
6252 // Lowering the machine isd will make sure everything is in the right
6253 // location.
6254 SDValue Args[] = { Offset };
6255 SDValue Chain = DAG.getNode(X86ISD::TLSCALL, DL, MVT::Other, Args, 1);
6256
6257 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
6258 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6259 MFI->setAdjustsStack(true);
Eric Christopherfd179292009-08-27 18:07:15 +00006260
Eric Christopher30ef0e52010-06-03 04:07:48 +00006261 // And our return value (tls address) is in the standard call return value
6262 // location.
6263 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
6264 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006265 }
Eric Christopher30ef0e52010-06-03 04:07:48 +00006266
6267 assert(false &&
6268 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00006269
Torok Edwinc23197a2009-07-14 16:55:14 +00006270 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00006271 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006272}
6273
Evan Cheng0db9fe62006-04-25 20:13:52 +00006274
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006275/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00006276/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00006277SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00006278 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00006279 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006280 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006281 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006282 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00006283 SDValue ShOpLo = Op.getOperand(0);
6284 SDValue ShOpHi = Op.getOperand(1);
6285 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00006286 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00006287 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00006288 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00006289
Dan Gohman475871a2008-07-27 21:46:04 +00006290 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006291 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006292 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
6293 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006294 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006295 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
6296 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006297 }
Evan Chenge3413162006-01-09 18:33:28 +00006298
Owen Anderson825b72b2009-08-11 20:47:22 +00006299 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
6300 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00006301 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00006302 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00006303
Dan Gohman475871a2008-07-27 21:46:04 +00006304 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00006305 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00006306 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
6307 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00006308
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006309 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006310 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6311 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006312 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006313 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6314 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006315 }
6316
Dan Gohman475871a2008-07-27 21:46:04 +00006317 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00006318 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006319}
Evan Chenga3195e82006-01-12 22:54:21 +00006320
Dan Gohmand858e902010-04-17 15:26:15 +00006321SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
6322 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006323 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00006324
6325 if (SrcVT.isVector()) {
Chris Lattner492a43e2010-09-22 01:28:21 +00006326 if (SrcVT == MVT::v2i32 && Op.getValueType() == MVT::v2f64)
Eli Friedman23ef1052009-06-06 03:57:58 +00006327 return Op;
Eli Friedman23ef1052009-06-06 03:57:58 +00006328 return SDValue();
6329 }
6330
Owen Anderson825b72b2009-08-11 20:47:22 +00006331 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00006332 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00006333
Eli Friedman36df4992009-05-27 00:47:34 +00006334 // These are really Legal; return the operand so the caller accepts it as
6335 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006336 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00006337 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00006338 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00006339 Subtarget->is64Bit()) {
6340 return Op;
6341 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006342
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006343 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006344 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006345 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00006346 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006347 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00006348 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00006349 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006350 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006351 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006352 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
6353}
Evan Cheng0db9fe62006-04-25 20:13:52 +00006354
Owen Andersone50ed302009-08-10 22:56:29 +00006355SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006356 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00006357 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006358 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00006359 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00006360 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00006361 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006362 if (useSSE)
Owen Anderson825b72b2009-08-11 20:47:22 +00006363 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00006364 else
Owen Anderson825b72b2009-08-11 20:47:22 +00006365 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Chris Lattner492a43e2010-09-22 01:28:21 +00006366
6367 unsigned ByteSize = SrcVT.getSizeInBits()/8;
6368
6369 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6370 MachineMemOperand *MMO =
6371 DAG.getMachineFunction()
6372 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6373 MachineMemOperand::MOLoad, ByteSize, ByteSize);
6374
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006375 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006376 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
6377 X86ISD::FILD, DL,
6378 Tys, Ops, array_lengthof(Ops),
6379 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006380
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006381 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006382 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00006383 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006384
6385 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
6386 // shouldn't be necessary except that RFP cannot be live across
6387 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006388 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006389 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
6390 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006391 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00006392 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006393 SDValue Ops[] = {
6394 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
6395 };
Chris Lattner492a43e2010-09-22 01:28:21 +00006396 MachineMemOperand *MMO =
6397 DAG.getMachineFunction()
6398 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006399 MachineMemOperand::MOStore, SSFISize, SSFISize);
Chris Lattner492a43e2010-09-22 01:28:21 +00006400
6401 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
6402 Ops, array_lengthof(Ops),
6403 Op.getValueType(), MMO);
6404 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006405 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006406 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006407 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006408
Evan Cheng0db9fe62006-04-25 20:13:52 +00006409 return Result;
6410}
6411
Bill Wendling8b8a6362009-01-17 03:56:04 +00006412// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006413SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
6414 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00006415 // This algorithm is not obvious. Here it is in C code, more or less:
6416 /*
6417 double uint64_to_double( uint32_t hi, uint32_t lo ) {
6418 static const __m128i exp = { 0x4330000045300000ULL, 0 };
6419 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00006420
Bill Wendling8b8a6362009-01-17 03:56:04 +00006421 // Copy ints to xmm registers.
6422 __m128i xh = _mm_cvtsi32_si128( hi );
6423 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00006424
Bill Wendling8b8a6362009-01-17 03:56:04 +00006425 // Combine into low half of a single xmm register.
6426 __m128i x = _mm_unpacklo_epi32( xh, xl );
6427 __m128d d;
6428 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00006429
Bill Wendling8b8a6362009-01-17 03:56:04 +00006430 // Merge in appropriate exponents to give the integer bits the right
6431 // magnitude.
6432 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00006433
Bill Wendling8b8a6362009-01-17 03:56:04 +00006434 // Subtract away the biases to deal with the IEEE-754 double precision
6435 // implicit 1.
6436 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00006437
Bill Wendling8b8a6362009-01-17 03:56:04 +00006438 // All conversions up to here are exact. The correctly rounded result is
6439 // calculated using the current rounding mode using the following
6440 // horizontal add.
6441 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
6442 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
6443 // store doesn't really need to be here (except
6444 // maybe to zero the other double)
6445 return sd;
6446 }
6447 */
Dale Johannesen040225f2008-10-21 23:07:49 +00006448
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006449 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00006450 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00006451
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006452 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00006453 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00006454 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
6455 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
6456 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
6457 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006458 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006459 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006460
Bill Wendling8b8a6362009-01-17 03:56:04 +00006461 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00006462 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006463 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00006464 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006465 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006466 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006467 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006468
Owen Anderson825b72b2009-08-11 20:47:22 +00006469 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6470 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006471 Op.getOperand(0),
6472 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006473 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6474 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006475 Op.getOperand(0),
6476 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006477 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
6478 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00006479 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006480 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006481 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
6482 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Unpck2);
6483 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00006484 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006485 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006486 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006487
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006488 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00006489 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00006490 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
6491 DAG.getUNDEF(MVT::v2f64), ShufMask);
6492 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
6493 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006494 DAG.getIntPtrConstant(0));
6495}
6496
Bill Wendling8b8a6362009-01-17 03:56:04 +00006497// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006498SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
6499 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006500 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006501 // FP constant to bias correct the final result.
6502 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00006503 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006504
6505 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00006506 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6507 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00006508 Op.getOperand(0),
6509 DAG.getIntPtrConstant(0)));
6510
Owen Anderson825b72b2009-08-11 20:47:22 +00006511 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
6512 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006513 DAG.getIntPtrConstant(0));
6514
6515 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006516 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
6517 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006518 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006519 MVT::v2f64, Load)),
6520 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006521 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006522 MVT::v2f64, Bias)));
6523 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
6524 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006525 DAG.getIntPtrConstant(0));
6526
6527 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006528 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006529
6530 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00006531 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00006532
Owen Anderson825b72b2009-08-11 20:47:22 +00006533 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006534 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00006535 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00006536 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006537 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00006538 }
6539
6540 // Handle final rounding.
6541 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00006542}
6543
Dan Gohmand858e902010-04-17 15:26:15 +00006544SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
6545 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00006546 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006547 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006548
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006549 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00006550 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
6551 // the optimization here.
6552 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00006553 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00006554
Owen Andersone50ed302009-08-10 22:56:29 +00006555 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006556 EVT DstVT = Op.getValueType();
6557 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006558 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006559 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006560 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00006561
6562 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00006563 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006564 if (SrcVT == MVT::i32) {
6565 SDValue WordOff = DAG.getConstant(4, getPointerTy());
6566 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
6567 getPointerTy(), StackSlot, WordOff);
6568 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006569 StackSlot, MachinePointerInfo(),
6570 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006571 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006572 OffsetSlot, MachinePointerInfo(),
6573 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006574 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
6575 return Fild;
6576 }
6577
6578 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
6579 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006580 StackSlot, MachinePointerInfo(),
6581 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006582 // For i64 source, we need to add the appropriate power of 2 if the input
6583 // was negative. This is the same as the optimization in
6584 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
6585 // we must be careful to do the computation in x87 extended precision, not
6586 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00006587 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6588 MachineMemOperand *MMO =
6589 DAG.getMachineFunction()
6590 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6591 MachineMemOperand::MOLoad, 8, 8);
6592
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006593 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
6594 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006595 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
6596 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006597
6598 APInt FF(32, 0x5F800000ULL);
6599
6600 // Check whether the sign bit is set.
6601 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
6602 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
6603 ISD::SETLT);
6604
6605 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
6606 SDValue FudgePtr = DAG.getConstantPool(
6607 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
6608 getPointerTy());
6609
6610 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
6611 SDValue Zero = DAG.getIntPtrConstant(0);
6612 SDValue Four = DAG.getIntPtrConstant(4);
6613 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
6614 Zero, Four);
6615 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
6616
6617 // Load the value out, extending it from f32 to f80.
6618 // FIXME: Avoid the extend by constructing the right constant pool?
Evan Chengbcc80172010-07-07 22:15:37 +00006619 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, MVT::f80, dl, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00006620 FudgePtr, MachinePointerInfo::getConstantPool(),
6621 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006622 // Extend everything to 80 bits to force it to be done on x87.
6623 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
6624 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00006625}
6626
Dan Gohman475871a2008-07-27 21:46:04 +00006627std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00006628FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00006629 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00006630
Owen Andersone50ed302009-08-10 22:56:29 +00006631 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00006632
6633 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006634 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
6635 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00006636 }
6637
Owen Anderson825b72b2009-08-11 20:47:22 +00006638 assert(DstTy.getSimpleVT() <= MVT::i64 &&
6639 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00006640 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00006641
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006642 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006643 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00006644 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00006645 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00006646 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00006647 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00006648 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00006649 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006650
Evan Cheng87c89352007-10-15 20:11:21 +00006651 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
6652 // stack slot.
6653 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00006654 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00006655 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006656 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00006657
Chris Lattner07290932010-09-22 01:05:16 +00006658
6659
Evan Cheng0db9fe62006-04-25 20:13:52 +00006660 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00006661 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006662 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00006663 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
6664 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
6665 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006666 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006667
Dan Gohman475871a2008-07-27 21:46:04 +00006668 SDValue Chain = DAG.getEntryNode();
6669 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00006670 EVT TheVT = Op.getOperand(0).getValueType();
6671 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006672 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00006673 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006674 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006675 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00006676 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00006677 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00006678 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00006679 };
Chris Lattner492a43e2010-09-22 01:28:21 +00006680
6681 MachineMemOperand *MMO =
6682 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6683 MachineMemOperand::MOLoad, MemSize, MemSize);
6684 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
6685 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006686 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00006687 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006688 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
6689 }
Chris Lattner07290932010-09-22 01:05:16 +00006690
6691 MachineMemOperand *MMO =
6692 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6693 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006694
Evan Cheng0db9fe62006-04-25 20:13:52 +00006695 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00006696 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00006697 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
6698 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00006699
Chris Lattner27a6c732007-11-24 07:07:01 +00006700 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006701}
6702
Dan Gohmand858e902010-04-17 15:26:15 +00006703SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
6704 SelectionDAG &DAG) const {
Eli Friedman23ef1052009-06-06 03:57:58 +00006705 if (Op.getValueType().isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006706 if (Op.getValueType() == MVT::v2i32 &&
6707 Op.getOperand(0).getValueType() == MVT::v2f64) {
Eli Friedman23ef1052009-06-06 03:57:58 +00006708 return Op;
6709 }
6710 return SDValue();
6711 }
6712
Eli Friedman948e95a2009-05-23 09:59:16 +00006713 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00006714 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00006715 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
6716 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00006717
Chris Lattner27a6c732007-11-24 07:07:01 +00006718 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006719 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00006720 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00006721}
6722
Dan Gohmand858e902010-04-17 15:26:15 +00006723SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
6724 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00006725 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
6726 SDValue FIST = Vals.first, StackSlot = Vals.second;
6727 assert(FIST.getNode() && "Unexpected failure");
6728
6729 // Load the result.
6730 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00006731 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006732}
6733
Dan Gohmand858e902010-04-17 15:26:15 +00006734SDValue X86TargetLowering::LowerFABS(SDValue Op,
6735 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00006736 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006737 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006738 EVT VT = Op.getValueType();
6739 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006740 if (VT.isVector())
6741 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006742 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00006743 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006744 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00006745 CV.push_back(C);
6746 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006747 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006748 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00006749 CV.push_back(C);
6750 CV.push_back(C);
6751 CV.push_back(C);
6752 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006753 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006754 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006755 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006756 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006757 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006758 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006759 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006760}
6761
Dan Gohmand858e902010-04-17 15:26:15 +00006762SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00006763 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006764 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006765 EVT VT = Op.getValueType();
6766 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00006767 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00006768 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006769 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00006770 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006771 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00006772 CV.push_back(C);
6773 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006774 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006775 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00006776 CV.push_back(C);
6777 CV.push_back(C);
6778 CV.push_back(C);
6779 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006780 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006781 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006782 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006783 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006784 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006785 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006786 if (VT.isVector()) {
Dale Johannesenace16102009-02-03 19:33:06 +00006787 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00006788 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
6789 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006790 Op.getOperand(0)),
Owen Anderson825b72b2009-08-11 20:47:22 +00006791 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00006792 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006793 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00006794 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006795}
6796
Dan Gohmand858e902010-04-17 15:26:15 +00006797SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00006798 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00006799 SDValue Op0 = Op.getOperand(0);
6800 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006801 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006802 EVT VT = Op.getValueType();
6803 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00006804
6805 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00006806 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006807 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00006808 SrcVT = VT;
6809 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00006810 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00006811 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006812 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00006813 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00006814 }
6815
6816 // At this point the operands and the result should have the same
6817 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00006818
Evan Cheng68c47cb2007-01-05 07:55:56 +00006819 // First get the sign bit of second operand.
6820 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00006821 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006822 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
6823 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00006824 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006825 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
6826 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6827 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6828 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00006829 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006830 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006831 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006832 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006833 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006834 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006835 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00006836
6837 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00006838 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006839 // Op0 is MVT::f32, Op1 is MVT::f64.
6840 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
6841 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
6842 DAG.getConstant(32, MVT::i32));
6843 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, SignBit);
6844 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00006845 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00006846 }
6847
Evan Cheng73d6cf12007-01-05 21:37:56 +00006848 // Clear first operand sign bit.
6849 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00006850 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006851 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
6852 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00006853 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006854 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
6855 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6856 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6857 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00006858 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006859 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006860 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006861 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006862 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006863 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006864 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00006865
6866 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00006867 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00006868}
6869
Dan Gohman076aee32009-03-04 19:44:21 +00006870/// Emit nodes that will be selected as "test Op0,Op0", or something
6871/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00006872SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00006873 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00006874 DebugLoc dl = Op.getDebugLoc();
6875
Dan Gohman31125812009-03-07 01:58:32 +00006876 // CF and OF aren't always set the way we want. Determine which
6877 // of these we need.
6878 bool NeedCF = false;
6879 bool NeedOF = false;
6880 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006881 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00006882 case X86::COND_A: case X86::COND_AE:
6883 case X86::COND_B: case X86::COND_BE:
6884 NeedCF = true;
6885 break;
6886 case X86::COND_G: case X86::COND_GE:
6887 case X86::COND_L: case X86::COND_LE:
6888 case X86::COND_O: case X86::COND_NO:
6889 NeedOF = true;
6890 break;
Dan Gohman31125812009-03-07 01:58:32 +00006891 }
6892
Dan Gohman076aee32009-03-04 19:44:21 +00006893 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00006894 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
6895 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006896 if (Op.getResNo() != 0 || NeedOF || NeedCF)
6897 // Emit a CMP with 0, which is the TEST pattern.
6898 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
6899 DAG.getConstant(0, Op.getValueType()));
6900
6901 unsigned Opcode = 0;
6902 unsigned NumOperands = 0;
6903 switch (Op.getNode()->getOpcode()) {
6904 case ISD::ADD:
6905 // Due to an isel shortcoming, be conservative if this add is likely to be
6906 // selected as part of a load-modify-store instruction. When the root node
6907 // in a match is a store, isel doesn't know how to remap non-chain non-flag
6908 // uses of other nodes in the match, such as the ADD in this case. This
6909 // leads to the ADD being left around and reselected, with the result being
6910 // two adds in the output. Alas, even if none our users are stores, that
6911 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
6912 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
6913 // climbing the DAG back to the root, and it doesn't seem to be worth the
6914 // effort.
6915 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00006916 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006917 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
6918 goto default_case;
6919
6920 if (ConstantSDNode *C =
6921 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
6922 // An add of one will be selected as an INC.
6923 if (C->getAPIntValue() == 1) {
6924 Opcode = X86ISD::INC;
6925 NumOperands = 1;
6926 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00006927 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006928
6929 // An add of negative one (subtract of one) will be selected as a DEC.
6930 if (C->getAPIntValue().isAllOnesValue()) {
6931 Opcode = X86ISD::DEC;
6932 NumOperands = 1;
6933 break;
6934 }
Dan Gohman076aee32009-03-04 19:44:21 +00006935 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006936
6937 // Otherwise use a regular EFLAGS-setting add.
6938 Opcode = X86ISD::ADD;
6939 NumOperands = 2;
6940 break;
6941 case ISD::AND: {
6942 // If the primary and result isn't used, don't bother using X86ISD::AND,
6943 // because a TEST instruction will be better.
6944 bool NonFlagUse = false;
6945 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
6946 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
6947 SDNode *User = *UI;
6948 unsigned UOpNo = UI.getOperandNo();
6949 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
6950 // Look pass truncate.
6951 UOpNo = User->use_begin().getOperandNo();
6952 User = *User->use_begin();
6953 }
6954
6955 if (User->getOpcode() != ISD::BRCOND &&
6956 User->getOpcode() != ISD::SETCC &&
6957 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
6958 NonFlagUse = true;
6959 break;
6960 }
Dan Gohman076aee32009-03-04 19:44:21 +00006961 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006962
6963 if (!NonFlagUse)
6964 break;
6965 }
6966 // FALL THROUGH
6967 case ISD::SUB:
6968 case ISD::OR:
6969 case ISD::XOR:
6970 // Due to the ISEL shortcoming noted above, be conservative if this op is
6971 // likely to be selected as part of a load-modify-store instruction.
6972 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
6973 UE = Op.getNode()->use_end(); UI != UE; ++UI)
6974 if (UI->getOpcode() == ISD::STORE)
6975 goto default_case;
6976
6977 // Otherwise use a regular EFLAGS-setting instruction.
6978 switch (Op.getNode()->getOpcode()) {
6979 default: llvm_unreachable("unexpected operator!");
6980 case ISD::SUB: Opcode = X86ISD::SUB; break;
6981 case ISD::OR: Opcode = X86ISD::OR; break;
6982 case ISD::XOR: Opcode = X86ISD::XOR; break;
6983 case ISD::AND: Opcode = X86ISD::AND; break;
6984 }
6985
6986 NumOperands = 2;
6987 break;
6988 case X86ISD::ADD:
6989 case X86ISD::SUB:
6990 case X86ISD::INC:
6991 case X86ISD::DEC:
6992 case X86ISD::OR:
6993 case X86ISD::XOR:
6994 case X86ISD::AND:
6995 return SDValue(Op.getNode(), 1);
6996 default:
6997 default_case:
6998 break;
Dan Gohman076aee32009-03-04 19:44:21 +00006999 }
7000
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007001 if (Opcode == 0)
7002 // Emit a CMP with 0, which is the TEST pattern.
7003 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7004 DAG.getConstant(0, Op.getValueType()));
7005
7006 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
7007 SmallVector<SDValue, 4> Ops;
7008 for (unsigned i = 0; i != NumOperands; ++i)
7009 Ops.push_back(Op.getOperand(i));
7010
7011 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
7012 DAG.ReplaceAllUsesWith(Op, New);
7013 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00007014}
7015
7016/// Emit nodes that will be selected as "cmp Op0,Op1", or something
7017/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007018SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007019 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007020 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
7021 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00007022 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00007023
7024 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00007025 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00007026}
7027
Evan Chengd40d03e2010-01-06 19:38:29 +00007028/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
7029/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00007030SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
7031 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007032 SDValue Op0 = And.getOperand(0);
7033 SDValue Op1 = And.getOperand(1);
7034 if (Op0.getOpcode() == ISD::TRUNCATE)
7035 Op0 = Op0.getOperand(0);
7036 if (Op1.getOpcode() == ISD::TRUNCATE)
7037 Op1 = Op1.getOperand(0);
7038
Evan Chengd40d03e2010-01-06 19:38:29 +00007039 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007040 if (Op1.getOpcode() == ISD::SHL)
7041 std::swap(Op0, Op1);
7042 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007043 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
7044 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007045 // If we looked past a truncate, check that it's only truncating away
7046 // known zeros.
7047 unsigned BitWidth = Op0.getValueSizeInBits();
7048 unsigned AndBitWidth = And.getValueSizeInBits();
7049 if (BitWidth > AndBitWidth) {
7050 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
7051 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
7052 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
7053 return SDValue();
7054 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007055 LHS = Op1;
7056 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00007057 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007058 } else if (Op1.getOpcode() == ISD::Constant) {
7059 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
7060 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00007061 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
7062 LHS = AndLHS.getOperand(0);
7063 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007064 }
Evan Chengd40d03e2010-01-06 19:38:29 +00007065 }
Evan Cheng0488db92007-09-25 01:57:46 +00007066
Evan Chengd40d03e2010-01-06 19:38:29 +00007067 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00007068 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00007069 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00007070 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00007071 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00007072 // Also promote i16 to i32 for performance / code size reason.
7073 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00007074 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00007075 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00007076
Evan Chengd40d03e2010-01-06 19:38:29 +00007077 // If the operand types disagree, extend the shift amount to match. Since
7078 // BT ignores high bits (like shifts) we can use anyextend.
7079 if (LHS.getValueType() != RHS.getValueType())
7080 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007081
Evan Chengd40d03e2010-01-06 19:38:29 +00007082 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
7083 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
7084 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7085 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00007086 }
7087
Evan Cheng54de3ea2010-01-05 06:52:31 +00007088 return SDValue();
7089}
7090
Dan Gohmand858e902010-04-17 15:26:15 +00007091SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng54de3ea2010-01-05 06:52:31 +00007092 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
7093 SDValue Op0 = Op.getOperand(0);
7094 SDValue Op1 = Op.getOperand(1);
7095 DebugLoc dl = Op.getDebugLoc();
7096 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
7097
7098 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00007099 // Lower (X & (1 << N)) == 0 to BT(X, N).
7100 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
7101 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
7102 if (Op0.getOpcode() == ISD::AND &&
7103 Op0.hasOneUse() &&
7104 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00007105 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007106 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
7107 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
7108 if (NewSetCC.getNode())
7109 return NewSetCC;
7110 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00007111
Evan Cheng2c755ba2010-02-27 07:36:59 +00007112 // Look for "(setcc) == / != 1" to avoid unncessary setcc.
7113 if (Op0.getOpcode() == X86ISD::SETCC &&
7114 Op1.getOpcode() == ISD::Constant &&
7115 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
7116 cast<ConstantSDNode>(Op1)->isNullValue()) &&
7117 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
7118 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
7119 bool Invert = (CC == ISD::SETNE) ^
7120 cast<ConstantSDNode>(Op1)->isNullValue();
7121 if (Invert)
7122 CCode = X86::GetOppositeBranchCondition(CCode);
7123 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7124 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
7125 }
7126
Evan Chenge5b51ac2010-04-17 06:13:15 +00007127 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00007128 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007129 if (X86CC == X86::COND_INVALID)
7130 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007131
Evan Cheng552f09a2010-04-26 19:06:11 +00007132 SDValue Cond = EmitCmp(Op0, Op1, X86CC, DAG);
Evan Chengad9c0a32009-12-15 00:53:42 +00007133
7134 // Use sbb x, x to materialize carry bit into a GPR.
Evan Cheng2e489c42009-12-16 00:53:11 +00007135 if (X86CC == X86::COND_B)
Evan Chengad9c0a32009-12-15 00:53:42 +00007136 return DAG.getNode(ISD::AND, dl, MVT::i8,
7137 DAG.getNode(X86ISD::SETCC_CARRY, dl, MVT::i8,
7138 DAG.getConstant(X86CC, MVT::i8), Cond),
7139 DAG.getConstant(1, MVT::i8));
Evan Chengad9c0a32009-12-15 00:53:42 +00007140
Owen Anderson825b72b2009-08-11 20:47:22 +00007141 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7142 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00007143}
7144
Dan Gohmand858e902010-04-17 15:26:15 +00007145SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007146 SDValue Cond;
7147 SDValue Op0 = Op.getOperand(0);
7148 SDValue Op1 = Op.getOperand(1);
7149 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00007150 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00007151 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
7152 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007153 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00007154
7155 if (isFP) {
7156 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00007157 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007158 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
7159 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00007160 bool Swap = false;
7161
7162 switch (SetCCOpcode) {
7163 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007164 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00007165 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00007166 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00007167 case ISD::SETGT: Swap = true; // Fallthrough
7168 case ISD::SETLT:
7169 case ISD::SETOLT: SSECC = 1; break;
7170 case ISD::SETOGE:
7171 case ISD::SETGE: Swap = true; // Fallthrough
7172 case ISD::SETLE:
7173 case ISD::SETOLE: SSECC = 2; break;
7174 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007175 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00007176 case ISD::SETNE: SSECC = 4; break;
7177 case ISD::SETULE: Swap = true;
7178 case ISD::SETUGE: SSECC = 5; break;
7179 case ISD::SETULT: Swap = true;
7180 case ISD::SETUGT: SSECC = 6; break;
7181 case ISD::SETO: SSECC = 7; break;
7182 }
7183 if (Swap)
7184 std::swap(Op0, Op1);
7185
Nate Begemanfb8ead02008-07-25 19:05:58 +00007186 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00007187 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00007188 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00007189 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007190 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
7191 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007192 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007193 }
7194 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00007195 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007196 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
7197 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007198 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007199 }
Torok Edwinc23197a2009-07-14 16:55:14 +00007200 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00007201 }
7202 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00007203 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00007204 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007205
Nate Begeman30a0de92008-07-17 16:51:19 +00007206 // We are handling one of the integer comparisons here. Since SSE only has
7207 // GT and EQ comparisons for integer, swapping operands and multiple
7208 // operations may be required for some comparisons.
7209 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
7210 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007211
Owen Anderson825b72b2009-08-11 20:47:22 +00007212 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00007213 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007214 case MVT::v8i8:
7215 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
7216 case MVT::v4i16:
7217 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
7218 case MVT::v2i32:
7219 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
7220 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00007221 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007222
Nate Begeman30a0de92008-07-17 16:51:19 +00007223 switch (SetCCOpcode) {
7224 default: break;
7225 case ISD::SETNE: Invert = true;
7226 case ISD::SETEQ: Opc = EQOpc; break;
7227 case ISD::SETLT: Swap = true;
7228 case ISD::SETGT: Opc = GTOpc; break;
7229 case ISD::SETGE: Swap = true;
7230 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
7231 case ISD::SETULT: Swap = true;
7232 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
7233 case ISD::SETUGE: Swap = true;
7234 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
7235 }
7236 if (Swap)
7237 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007238
Nate Begeman30a0de92008-07-17 16:51:19 +00007239 // Since SSE has no unsigned integer comparisons, we need to flip the sign
7240 // bits of the inputs before performing those operations.
7241 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00007242 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00007243 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
7244 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00007245 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00007246 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
7247 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00007248 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
7249 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00007250 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007251
Dale Johannesenace16102009-02-03 19:33:06 +00007252 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00007253
7254 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00007255 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00007256 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00007257
Nate Begeman30a0de92008-07-17 16:51:19 +00007258 return Result;
7259}
Evan Cheng0488db92007-09-25 01:57:46 +00007260
Evan Cheng370e5342008-12-03 08:38:43 +00007261// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00007262static bool isX86LogicalCmp(SDValue Op) {
7263 unsigned Opc = Op.getNode()->getOpcode();
7264 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
7265 return true;
7266 if (Op.getResNo() == 1 &&
7267 (Opc == X86ISD::ADD ||
7268 Opc == X86ISD::SUB ||
7269 Opc == X86ISD::SMUL ||
7270 Opc == X86ISD::UMUL ||
7271 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00007272 Opc == X86ISD::DEC ||
7273 Opc == X86ISD::OR ||
7274 Opc == X86ISD::XOR ||
7275 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00007276 return true;
7277
7278 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00007279}
7280
Dan Gohmand858e902010-04-17 15:26:15 +00007281SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007282 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007283 SDValue Cond = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007284 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007285 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00007286
Dan Gohman1a492952009-10-20 16:22:37 +00007287 if (Cond.getOpcode() == ISD::SETCC) {
7288 SDValue NewCond = LowerSETCC(Cond, DAG);
7289 if (NewCond.getNode())
7290 Cond = NewCond;
7291 }
Evan Cheng734503b2006-09-11 02:19:56 +00007292
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007293 // (select (x == 0), -1, 0) -> (sign_bit (x - 1))
7294 SDValue Op1 = Op.getOperand(1);
7295 SDValue Op2 = Op.getOperand(2);
7296 if (Cond.getOpcode() == X86ISD::SETCC &&
7297 cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue() == X86::COND_E) {
7298 SDValue Cmp = Cond.getOperand(1);
7299 if (Cmp.getOpcode() == X86ISD::CMP) {
7300 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op1);
7301 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
7302 ConstantSDNode *RHSC =
7303 dyn_cast<ConstantSDNode>(Cmp.getOperand(1).getNode());
7304 if (N1C && N1C->isAllOnesValue() &&
7305 N2C && N2C->isNullValue() &&
7306 RHSC && RHSC->isNullValue()) {
7307 SDValue CmpOp0 = Cmp.getOperand(0);
Chris Lattnerda0688e2010-03-14 18:44:35 +00007308 Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007309 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
7310 return DAG.getNode(X86ISD::SETCC_CARRY, dl, Op.getValueType(),
7311 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
7312 }
7313 }
7314 }
7315
Evan Chengad9c0a32009-12-15 00:53:42 +00007316 // Look pass (and (setcc_carry (cmp ...)), 1).
7317 if (Cond.getOpcode() == ISD::AND &&
7318 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7319 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
7320 if (C && C->getAPIntValue() == 1)
7321 Cond = Cond.getOperand(0);
7322 }
7323
Evan Cheng3f41d662007-10-08 22:16:29 +00007324 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7325 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007326 if (Cond.getOpcode() == X86ISD::SETCC ||
7327 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007328 CC = Cond.getOperand(0);
7329
Dan Gohman475871a2008-07-27 21:46:04 +00007330 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007331 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00007332 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00007333
Evan Cheng3f41d662007-10-08 22:16:29 +00007334 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007335 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00007336 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00007337 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00007338
Chris Lattnerd1980a52009-03-12 06:52:53 +00007339 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
7340 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00007341 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007342 addTest = false;
7343 }
7344 }
7345
7346 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007347 // Look pass the truncate.
7348 if (Cond.getOpcode() == ISD::TRUNCATE)
7349 Cond = Cond.getOperand(0);
7350
7351 // We know the result of AND is compared against zero. Try to match
7352 // it to BT.
7353 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
7354 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
7355 if (NewSetCC.getNode()) {
7356 CC = NewSetCC.getOperand(0);
7357 Cond = NewSetCC.getOperand(1);
7358 addTest = false;
7359 }
7360 }
7361 }
7362
7363 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007364 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007365 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007366 }
7367
Evan Cheng0488db92007-09-25 01:57:46 +00007368 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
7369 // condition is true.
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007370 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
7371 SDValue Ops[] = { Op2, Op1, CC, Cond };
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007372 return DAG.getNode(X86ISD::CMOV, dl, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00007373}
7374
Evan Cheng370e5342008-12-03 08:38:43 +00007375// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
7376// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
7377// from the AND / OR.
7378static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
7379 Opc = Op.getOpcode();
7380 if (Opc != ISD::OR && Opc != ISD::AND)
7381 return false;
7382 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7383 Op.getOperand(0).hasOneUse() &&
7384 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
7385 Op.getOperand(1).hasOneUse());
7386}
7387
Evan Cheng961d6d42009-02-02 08:19:07 +00007388// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
7389// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00007390static bool isXor1OfSetCC(SDValue Op) {
7391 if (Op.getOpcode() != ISD::XOR)
7392 return false;
7393 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7394 if (N1C && N1C->getAPIntValue() == 1) {
7395 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7396 Op.getOperand(0).hasOneUse();
7397 }
7398 return false;
7399}
7400
Dan Gohmand858e902010-04-17 15:26:15 +00007401SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007402 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007403 SDValue Chain = Op.getOperand(0);
7404 SDValue Cond = Op.getOperand(1);
7405 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007406 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007407 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00007408
Dan Gohman1a492952009-10-20 16:22:37 +00007409 if (Cond.getOpcode() == ISD::SETCC) {
7410 SDValue NewCond = LowerSETCC(Cond, DAG);
7411 if (NewCond.getNode())
7412 Cond = NewCond;
7413 }
Chris Lattnere55484e2008-12-25 05:34:37 +00007414#if 0
7415 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00007416 else if (Cond.getOpcode() == X86ISD::ADD ||
7417 Cond.getOpcode() == X86ISD::SUB ||
7418 Cond.getOpcode() == X86ISD::SMUL ||
7419 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00007420 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00007421#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00007422
Evan Chengad9c0a32009-12-15 00:53:42 +00007423 // Look pass (and (setcc_carry (cmp ...)), 1).
7424 if (Cond.getOpcode() == ISD::AND &&
7425 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7426 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
7427 if (C && C->getAPIntValue() == 1)
7428 Cond = Cond.getOperand(0);
7429 }
7430
Evan Cheng3f41d662007-10-08 22:16:29 +00007431 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7432 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007433 if (Cond.getOpcode() == X86ISD::SETCC ||
7434 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007435 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007436
Dan Gohman475871a2008-07-27 21:46:04 +00007437 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007438 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00007439 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00007440 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00007441 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007442 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00007443 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00007444 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007445 default: break;
7446 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00007447 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00007448 // These can only come from an arithmetic instruction with overflow,
7449 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007450 Cond = Cond.getNode()->getOperand(1);
7451 addTest = false;
7452 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007453 }
Evan Cheng0488db92007-09-25 01:57:46 +00007454 }
Evan Cheng370e5342008-12-03 08:38:43 +00007455 } else {
7456 unsigned CondOpc;
7457 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
7458 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00007459 if (CondOpc == ISD::OR) {
7460 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
7461 // two branches instead of an explicit OR instruction with a
7462 // separate test.
7463 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007464 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00007465 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007466 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007467 Chain, Dest, CC, Cmp);
7468 CC = Cond.getOperand(1).getOperand(0);
7469 Cond = Cmp;
7470 addTest = false;
7471 }
7472 } else { // ISD::AND
7473 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
7474 // two branches instead of an explicit AND instruction with a
7475 // separate test. However, we only do this if this block doesn't
7476 // have a fall-through edge, because this requires an explicit
7477 // jmp when the condition is false.
7478 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007479 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00007480 Op.getNode()->hasOneUse()) {
7481 X86::CondCode CCode =
7482 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7483 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007484 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00007485 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00007486 // Look for an unconditional branch following this conditional branch.
7487 // We need this because we need to reverse the successors in order
7488 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00007489 if (User->getOpcode() == ISD::BR) {
7490 SDValue FalseBB = User->getOperand(1);
7491 SDNode *NewBR =
7492 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00007493 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00007494 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00007495 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00007496
Dale Johannesene4d209d2009-02-03 20:21:25 +00007497 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007498 Chain, Dest, CC, Cmp);
7499 X86::CondCode CCode =
7500 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
7501 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007502 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00007503 Cond = Cmp;
7504 addTest = false;
7505 }
7506 }
Dan Gohman279c22e2008-10-21 03:29:32 +00007507 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00007508 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
7509 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
7510 // It should be transformed during dag combiner except when the condition
7511 // is set by a arithmetics with overflow node.
7512 X86::CondCode CCode =
7513 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7514 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007515 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00007516 Cond = Cond.getOperand(0).getOperand(1);
7517 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00007518 }
Evan Cheng0488db92007-09-25 01:57:46 +00007519 }
7520
7521 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007522 // Look pass the truncate.
7523 if (Cond.getOpcode() == ISD::TRUNCATE)
7524 Cond = Cond.getOperand(0);
7525
7526 // We know the result of AND is compared against zero. Try to match
7527 // it to BT.
7528 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
7529 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
7530 if (NewSetCC.getNode()) {
7531 CC = NewSetCC.getOperand(0);
7532 Cond = NewSetCC.getOperand(1);
7533 addTest = false;
7534 }
7535 }
7536 }
7537
7538 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007539 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007540 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007541 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00007542 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00007543 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00007544}
7545
Anton Korobeynikove060b532007-04-17 19:34:00 +00007546
7547// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
7548// Calls to _alloca is needed to probe the stack when allocating more than 4k
7549// bytes in one go. Touching the stack at 4K increments is necessary to ensure
7550// that the guard pages used by the OS virtual memory manager are allocated in
7551// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00007552SDValue
7553X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007554 SelectionDAG &DAG) const {
Anton Korobeynikove060b532007-04-17 19:34:00 +00007555 assert(Subtarget->isTargetCygMing() &&
7556 "This should be used only on Cygwin/Mingw targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007557 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007558
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007559 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00007560 SDValue Chain = Op.getOperand(0);
7561 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007562 // FIXME: Ensure alignment here
7563
Dan Gohman475871a2008-07-27 21:46:04 +00007564 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007565
Owen Anderson825b72b2009-08-11 20:47:22 +00007566 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007567
Dale Johannesendd64c412009-02-04 00:33:20 +00007568 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007569 Flag = Chain.getValue(1);
7570
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00007571 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007572
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00007573 Chain = DAG.getNode(X86ISD::MINGW_ALLOCA, dl, NodeTys, Chain, Flag);
7574 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007575
Dale Johannesendd64c412009-02-04 00:33:20 +00007576 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007577
Dan Gohman475871a2008-07-27 21:46:04 +00007578 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007579 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007580}
7581
Dan Gohmand858e902010-04-17 15:26:15 +00007582SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00007583 MachineFunction &MF = DAG.getMachineFunction();
7584 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
7585
Dan Gohman69de1932008-02-06 22:27:42 +00007586 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00007587 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00007588
Evan Cheng25ab6902006-09-08 06:48:29 +00007589 if (!Subtarget->is64Bit()) {
7590 // vastart just stores the address of the VarArgsFrameIndex slot into the
7591 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00007592 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
7593 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007594 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
7595 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007596 }
7597
7598 // __va_list_tag:
7599 // gp_offset (0 - 6 * 8)
7600 // fp_offset (48 - 48 + 8 * 16)
7601 // overflow_arg_area (point to parameters coming in memory).
7602 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00007603 SmallVector<SDValue, 8> MemOps;
7604 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00007605 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00007606 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00007607 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
7608 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007609 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007610 MemOps.push_back(Store);
7611
7612 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00007613 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007614 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00007615 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00007616 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
7617 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007618 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007619 MemOps.push_back(Store);
7620
7621 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00007622 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007623 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00007624 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
7625 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007626 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
7627 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00007628 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007629 MemOps.push_back(Store);
7630
7631 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00007632 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007633 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00007634 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
7635 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007636 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
7637 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007638 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00007639 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00007640 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00007641}
7642
Dan Gohmand858e902010-04-17 15:26:15 +00007643SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman9018e832008-05-10 01:26:14 +00007644 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
7645 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman9018e832008-05-10 01:26:14 +00007646
Chris Lattner75361b62010-04-07 22:58:41 +00007647 report_fatal_error("VAArgInst is not yet implemented for x86-64!");
Dan Gohman475871a2008-07-27 21:46:04 +00007648 return SDValue();
Dan Gohman9018e832008-05-10 01:26:14 +00007649}
7650
Dan Gohmand858e902010-04-17 15:26:15 +00007651SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00007652 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00007653 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00007654 SDValue Chain = Op.getOperand(0);
7655 SDValue DstPtr = Op.getOperand(1);
7656 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00007657 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
7658 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00007659 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00007660
Chris Lattnere72f2022010-09-21 05:40:29 +00007661 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00007662 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Chris Lattnere72f2022010-09-21 05:40:29 +00007663 false,
7664 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00007665}
7666
Dan Gohman475871a2008-07-27 21:46:04 +00007667SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007668X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007669 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007670 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007671 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00007672 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00007673 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00007674 case Intrinsic::x86_sse_comieq_ss:
7675 case Intrinsic::x86_sse_comilt_ss:
7676 case Intrinsic::x86_sse_comile_ss:
7677 case Intrinsic::x86_sse_comigt_ss:
7678 case Intrinsic::x86_sse_comige_ss:
7679 case Intrinsic::x86_sse_comineq_ss:
7680 case Intrinsic::x86_sse_ucomieq_ss:
7681 case Intrinsic::x86_sse_ucomilt_ss:
7682 case Intrinsic::x86_sse_ucomile_ss:
7683 case Intrinsic::x86_sse_ucomigt_ss:
7684 case Intrinsic::x86_sse_ucomige_ss:
7685 case Intrinsic::x86_sse_ucomineq_ss:
7686 case Intrinsic::x86_sse2_comieq_sd:
7687 case Intrinsic::x86_sse2_comilt_sd:
7688 case Intrinsic::x86_sse2_comile_sd:
7689 case Intrinsic::x86_sse2_comigt_sd:
7690 case Intrinsic::x86_sse2_comige_sd:
7691 case Intrinsic::x86_sse2_comineq_sd:
7692 case Intrinsic::x86_sse2_ucomieq_sd:
7693 case Intrinsic::x86_sse2_ucomilt_sd:
7694 case Intrinsic::x86_sse2_ucomile_sd:
7695 case Intrinsic::x86_sse2_ucomigt_sd:
7696 case Intrinsic::x86_sse2_ucomige_sd:
7697 case Intrinsic::x86_sse2_ucomineq_sd: {
7698 unsigned Opc = 0;
7699 ISD::CondCode CC = ISD::SETCC_INVALID;
7700 switch (IntNo) {
7701 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007702 case Intrinsic::x86_sse_comieq_ss:
7703 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007704 Opc = X86ISD::COMI;
7705 CC = ISD::SETEQ;
7706 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00007707 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007708 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007709 Opc = X86ISD::COMI;
7710 CC = ISD::SETLT;
7711 break;
7712 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007713 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007714 Opc = X86ISD::COMI;
7715 CC = ISD::SETLE;
7716 break;
7717 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007718 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007719 Opc = X86ISD::COMI;
7720 CC = ISD::SETGT;
7721 break;
7722 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007723 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007724 Opc = X86ISD::COMI;
7725 CC = ISD::SETGE;
7726 break;
7727 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007728 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007729 Opc = X86ISD::COMI;
7730 CC = ISD::SETNE;
7731 break;
7732 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007733 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007734 Opc = X86ISD::UCOMI;
7735 CC = ISD::SETEQ;
7736 break;
7737 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007738 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007739 Opc = X86ISD::UCOMI;
7740 CC = ISD::SETLT;
7741 break;
7742 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007743 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007744 Opc = X86ISD::UCOMI;
7745 CC = ISD::SETLE;
7746 break;
7747 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007748 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007749 Opc = X86ISD::UCOMI;
7750 CC = ISD::SETGT;
7751 break;
7752 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007753 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007754 Opc = X86ISD::UCOMI;
7755 CC = ISD::SETGE;
7756 break;
7757 case Intrinsic::x86_sse_ucomineq_ss:
7758 case Intrinsic::x86_sse2_ucomineq_sd:
7759 Opc = X86ISD::UCOMI;
7760 CC = ISD::SETNE;
7761 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00007762 }
Evan Cheng734503b2006-09-11 02:19:56 +00007763
Dan Gohman475871a2008-07-27 21:46:04 +00007764 SDValue LHS = Op.getOperand(1);
7765 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00007766 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007767 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007768 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
7769 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7770 DAG.getConstant(X86CC, MVT::i8), Cond);
7771 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00007772 }
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007773 // ptest and testp intrinsics. The intrinsic these come from are designed to
7774 // return an integer value, not just an instruction so lower it to the ptest
7775 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00007776 case Intrinsic::x86_sse41_ptestz:
7777 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007778 case Intrinsic::x86_sse41_ptestnzc:
7779 case Intrinsic::x86_avx_ptestz_256:
7780 case Intrinsic::x86_avx_ptestc_256:
7781 case Intrinsic::x86_avx_ptestnzc_256:
7782 case Intrinsic::x86_avx_vtestz_ps:
7783 case Intrinsic::x86_avx_vtestc_ps:
7784 case Intrinsic::x86_avx_vtestnzc_ps:
7785 case Intrinsic::x86_avx_vtestz_pd:
7786 case Intrinsic::x86_avx_vtestc_pd:
7787 case Intrinsic::x86_avx_vtestnzc_pd:
7788 case Intrinsic::x86_avx_vtestz_ps_256:
7789 case Intrinsic::x86_avx_vtestc_ps_256:
7790 case Intrinsic::x86_avx_vtestnzc_ps_256:
7791 case Intrinsic::x86_avx_vtestz_pd_256:
7792 case Intrinsic::x86_avx_vtestc_pd_256:
7793 case Intrinsic::x86_avx_vtestnzc_pd_256: {
7794 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00007795 unsigned X86CC = 0;
7796 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00007797 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007798 case Intrinsic::x86_avx_vtestz_ps:
7799 case Intrinsic::x86_avx_vtestz_pd:
7800 case Intrinsic::x86_avx_vtestz_ps_256:
7801 case Intrinsic::x86_avx_vtestz_pd_256:
7802 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00007803 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007804 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00007805 // ZF = 1
7806 X86CC = X86::COND_E;
7807 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007808 case Intrinsic::x86_avx_vtestc_ps:
7809 case Intrinsic::x86_avx_vtestc_pd:
7810 case Intrinsic::x86_avx_vtestc_ps_256:
7811 case Intrinsic::x86_avx_vtestc_pd_256:
7812 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00007813 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007814 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00007815 // CF = 1
7816 X86CC = X86::COND_B;
7817 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007818 case Intrinsic::x86_avx_vtestnzc_ps:
7819 case Intrinsic::x86_avx_vtestnzc_pd:
7820 case Intrinsic::x86_avx_vtestnzc_ps_256:
7821 case Intrinsic::x86_avx_vtestnzc_pd_256:
7822 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00007823 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007824 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00007825 // ZF and CF = 0
7826 X86CC = X86::COND_A;
7827 break;
7828 }
Eric Christopherfd179292009-08-27 18:07:15 +00007829
Eric Christopher71c67532009-07-29 00:28:05 +00007830 SDValue LHS = Op.getOperand(1);
7831 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007832 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
7833 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00007834 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
7835 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
7836 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00007837 }
Evan Cheng5759f972008-05-04 09:15:50 +00007838
7839 // Fix vector shift instructions where the last operand is a non-immediate
7840 // i32 value.
7841 case Intrinsic::x86_sse2_pslli_w:
7842 case Intrinsic::x86_sse2_pslli_d:
7843 case Intrinsic::x86_sse2_pslli_q:
7844 case Intrinsic::x86_sse2_psrli_w:
7845 case Intrinsic::x86_sse2_psrli_d:
7846 case Intrinsic::x86_sse2_psrli_q:
7847 case Intrinsic::x86_sse2_psrai_w:
7848 case Intrinsic::x86_sse2_psrai_d:
7849 case Intrinsic::x86_mmx_pslli_w:
7850 case Intrinsic::x86_mmx_pslli_d:
7851 case Intrinsic::x86_mmx_pslli_q:
7852 case Intrinsic::x86_mmx_psrli_w:
7853 case Intrinsic::x86_mmx_psrli_d:
7854 case Intrinsic::x86_mmx_psrli_q:
7855 case Intrinsic::x86_mmx_psrai_w:
7856 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00007857 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00007858 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00007859 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00007860
7861 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00007862 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00007863 switch (IntNo) {
7864 case Intrinsic::x86_sse2_pslli_w:
7865 NewIntNo = Intrinsic::x86_sse2_psll_w;
7866 break;
7867 case Intrinsic::x86_sse2_pslli_d:
7868 NewIntNo = Intrinsic::x86_sse2_psll_d;
7869 break;
7870 case Intrinsic::x86_sse2_pslli_q:
7871 NewIntNo = Intrinsic::x86_sse2_psll_q;
7872 break;
7873 case Intrinsic::x86_sse2_psrli_w:
7874 NewIntNo = Intrinsic::x86_sse2_psrl_w;
7875 break;
7876 case Intrinsic::x86_sse2_psrli_d:
7877 NewIntNo = Intrinsic::x86_sse2_psrl_d;
7878 break;
7879 case Intrinsic::x86_sse2_psrli_q:
7880 NewIntNo = Intrinsic::x86_sse2_psrl_q;
7881 break;
7882 case Intrinsic::x86_sse2_psrai_w:
7883 NewIntNo = Intrinsic::x86_sse2_psra_w;
7884 break;
7885 case Intrinsic::x86_sse2_psrai_d:
7886 NewIntNo = Intrinsic::x86_sse2_psra_d;
7887 break;
7888 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00007889 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00007890 switch (IntNo) {
7891 case Intrinsic::x86_mmx_pslli_w:
7892 NewIntNo = Intrinsic::x86_mmx_psll_w;
7893 break;
7894 case Intrinsic::x86_mmx_pslli_d:
7895 NewIntNo = Intrinsic::x86_mmx_psll_d;
7896 break;
7897 case Intrinsic::x86_mmx_pslli_q:
7898 NewIntNo = Intrinsic::x86_mmx_psll_q;
7899 break;
7900 case Intrinsic::x86_mmx_psrli_w:
7901 NewIntNo = Intrinsic::x86_mmx_psrl_w;
7902 break;
7903 case Intrinsic::x86_mmx_psrli_d:
7904 NewIntNo = Intrinsic::x86_mmx_psrl_d;
7905 break;
7906 case Intrinsic::x86_mmx_psrli_q:
7907 NewIntNo = Intrinsic::x86_mmx_psrl_q;
7908 break;
7909 case Intrinsic::x86_mmx_psrai_w:
7910 NewIntNo = Intrinsic::x86_mmx_psra_w;
7911 break;
7912 case Intrinsic::x86_mmx_psrai_d:
7913 NewIntNo = Intrinsic::x86_mmx_psra_d;
7914 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00007915 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00007916 }
7917 break;
7918 }
7919 }
Mon P Wangefa42202009-09-03 19:56:25 +00007920
7921 // The vector shift intrinsics with scalars uses 32b shift amounts but
7922 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
7923 // to be zero.
7924 SDValue ShOps[4];
7925 ShOps[0] = ShAmt;
7926 ShOps[1] = DAG.getConstant(0, MVT::i32);
7927 if (ShAmtVT == MVT::v4i32) {
7928 ShOps[2] = DAG.getUNDEF(MVT::i32);
7929 ShOps[3] = DAG.getUNDEF(MVT::i32);
7930 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
7931 } else {
7932 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
7933 }
7934
Owen Andersone50ed302009-08-10 22:56:29 +00007935 EVT VT = Op.getValueType();
Mon P Wangefa42202009-09-03 19:56:25 +00007936 ShAmt = DAG.getNode(ISD::BIT_CONVERT, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007937 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007938 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00007939 Op.getOperand(1), ShAmt);
7940 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00007941 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007942}
Evan Cheng72261582005-12-20 06:22:03 +00007943
Dan Gohmand858e902010-04-17 15:26:15 +00007944SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
7945 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00007946 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7947 MFI->setReturnAddressIsTaken(true);
7948
Bill Wendling64e87322009-01-16 19:25:27 +00007949 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007950 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00007951
7952 if (Depth > 0) {
7953 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
7954 SDValue Offset =
7955 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00007956 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007957 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00007958 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007959 FrameAddr, Offset),
Chris Lattner51abfe42010-09-21 06:02:19 +00007960 MachinePointerInfo(), false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00007961 }
7962
7963 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00007964 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00007965 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007966 RetAddrFI, MachinePointerInfo(), false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00007967}
7968
Dan Gohmand858e902010-04-17 15:26:15 +00007969SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00007970 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7971 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00007972
Owen Andersone50ed302009-08-10 22:56:29 +00007973 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007974 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00007975 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
7976 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00007977 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00007978 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00007979 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
7980 MachinePointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +00007981 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00007982 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00007983}
7984
Dan Gohman475871a2008-07-27 21:46:04 +00007985SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007986 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00007987 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007988}
7989
Dan Gohmand858e902010-04-17 15:26:15 +00007990SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007991 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00007992 SDValue Chain = Op.getOperand(0);
7993 SDValue Offset = Op.getOperand(1);
7994 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007995 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007996
Dan Gohmand8816272010-08-11 18:14:00 +00007997 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
7998 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
7999 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008000 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008001
Dan Gohmand8816272010-08-11 18:14:00 +00008002 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
8003 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008004 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00008005 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
8006 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00008007 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008008 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008009
Dale Johannesene4d209d2009-02-03 20:21:25 +00008010 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008011 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008012 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008013}
8014
Dan Gohman475871a2008-07-27 21:46:04 +00008015SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008016 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008017 SDValue Root = Op.getOperand(0);
8018 SDValue Trmp = Op.getOperand(1); // trampoline
8019 SDValue FPtr = Op.getOperand(2); // nested function
8020 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008021 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008022
Dan Gohman69de1932008-02-06 22:27:42 +00008023 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008024
8025 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00008026 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00008027
8028 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00008029 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
8030 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00008031
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008032 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
8033 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00008034
8035 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
8036
8037 // Load the pointer to the nested function into R11.
8038 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00008039 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00008040 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008041 Addr, MachinePointerInfo(TrmpAddr),
8042 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008043
Owen Anderson825b72b2009-08-11 20:47:22 +00008044 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8045 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008046 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
8047 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00008048 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008049
8050 // Load the 'nest' parameter value into R10.
8051 // R10 is specified in X86CallingConv.td
8052 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00008053 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8054 DAG.getConstant(10, MVT::i64));
8055 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008056 Addr, MachinePointerInfo(TrmpAddr, 10),
8057 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008058
Owen Anderson825b72b2009-08-11 20:47:22 +00008059 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8060 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008061 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
8062 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00008063 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008064
8065 // Jump to the nested function.
8066 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00008067 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8068 DAG.getConstant(20, MVT::i64));
8069 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008070 Addr, MachinePointerInfo(TrmpAddr, 20),
8071 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008072
8073 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00008074 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8075 DAG.getConstant(22, MVT::i64));
8076 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008077 MachinePointerInfo(TrmpAddr, 22),
8078 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008079
Dan Gohman475871a2008-07-27 21:46:04 +00008080 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008081 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008082 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008083 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00008084 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00008085 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00008086 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00008087 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008088
8089 switch (CC) {
8090 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008091 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008092 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008093 case CallingConv::X86_StdCall: {
8094 // Pass 'nest' parameter in ECX.
8095 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008096 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008097
8098 // Check that ECX wasn't needed by an 'inreg' parameter.
8099 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00008100 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008101
Chris Lattner58d74912008-03-12 17:45:29 +00008102 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00008103 unsigned InRegCount = 0;
8104 unsigned Idx = 1;
8105
8106 for (FunctionType::param_iterator I = FTy->param_begin(),
8107 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00008108 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00008109 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008110 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008111
8112 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00008113 report_fatal_error("Nest register in use - reduce number of inreg"
8114 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008115 }
8116 }
8117 break;
8118 }
8119 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00008120 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00008121 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008122 // Pass 'nest' parameter in EAX.
8123 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008124 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008125 break;
8126 }
8127
Dan Gohman475871a2008-07-27 21:46:04 +00008128 SDValue OutChains[4];
8129 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008130
Owen Anderson825b72b2009-08-11 20:47:22 +00008131 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8132 DAG.getConstant(10, MVT::i32));
8133 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008134
Chris Lattnera62fe662010-02-05 19:20:30 +00008135 // This is storing the opcode for MOV32ri.
8136 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008137 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00008138 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008139 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008140 Trmp, MachinePointerInfo(TrmpAddr),
8141 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008142
Owen Anderson825b72b2009-08-11 20:47:22 +00008143 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8144 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008145 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
8146 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00008147 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008148
Chris Lattnera62fe662010-02-05 19:20:30 +00008149 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00008150 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8151 DAG.getConstant(5, MVT::i32));
8152 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008153 MachinePointerInfo(TrmpAddr, 5),
8154 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008155
Owen Anderson825b72b2009-08-11 20:47:22 +00008156 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8157 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008158 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
8159 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00008160 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008161
Dan Gohman475871a2008-07-27 21:46:04 +00008162 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008163 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008164 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008165 }
8166}
8167
Dan Gohmand858e902010-04-17 15:26:15 +00008168SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
8169 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008170 /*
8171 The rounding mode is in bits 11:10 of FPSR, and has the following
8172 settings:
8173 00 Round to nearest
8174 01 Round to -inf
8175 10 Round to +inf
8176 11 Round to 0
8177
8178 FLT_ROUNDS, on the other hand, expects the following:
8179 -1 Undefined
8180 0 Round to 0
8181 1 Round to nearest
8182 2 Round to +inf
8183 3 Round to -inf
8184
8185 To perform the conversion, we do:
8186 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
8187 */
8188
8189 MachineFunction &MF = DAG.getMachineFunction();
8190 const TargetMachine &TM = MF.getTarget();
8191 const TargetFrameInfo &TFI = *TM.getFrameInfo();
8192 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00008193 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00008194 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008195
8196 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00008197 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008198 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008199
Chris Lattner2156b792010-09-22 01:11:26 +00008200
8201 MachineMemOperand *MMO =
8202 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8203 MachineMemOperand::MOStore, 2, 2);
8204
8205 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
8206 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
8207 DAG.getVTList(MVT::Other),
8208 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008209
8210 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00008211 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Chris Lattner51abfe42010-09-21 06:02:19 +00008212 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008213
8214 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00008215 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00008216 DAG.getNode(ISD::SRL, DL, MVT::i16,
8217 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008218 CWD, DAG.getConstant(0x800, MVT::i16)),
8219 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00008220 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00008221 DAG.getNode(ISD::SRL, DL, MVT::i16,
8222 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008223 CWD, DAG.getConstant(0x400, MVT::i16)),
8224 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008225
Dan Gohman475871a2008-07-27 21:46:04 +00008226 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00008227 DAG.getNode(ISD::AND, DL, MVT::i16,
8228 DAG.getNode(ISD::ADD, DL, MVT::i16,
8229 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00008230 DAG.getConstant(1, MVT::i16)),
8231 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008232
8233
Duncan Sands83ec4b62008-06-06 12:08:01 +00008234 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00008235 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008236}
8237
Dan Gohmand858e902010-04-17 15:26:15 +00008238SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008239 EVT VT = Op.getValueType();
8240 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008241 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008242 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008243
8244 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008245 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00008246 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00008247 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008248 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008249 }
Evan Cheng18efe262007-12-14 02:13:44 +00008250
Evan Cheng152804e2007-12-14 08:30:15 +00008251 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008252 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008253 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008254
8255 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008256 SDValue Ops[] = {
8257 Op,
8258 DAG.getConstant(NumBits+NumBits-1, OpVT),
8259 DAG.getConstant(X86::COND_E, MVT::i8),
8260 Op.getValue(1)
8261 };
8262 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008263
8264 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00008265 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00008266
Owen Anderson825b72b2009-08-11 20:47:22 +00008267 if (VT == MVT::i8)
8268 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008269 return Op;
8270}
8271
Dan Gohmand858e902010-04-17 15:26:15 +00008272SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008273 EVT VT = Op.getValueType();
8274 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008275 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008276 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008277
8278 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008279 if (VT == MVT::i8) {
8280 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008281 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008282 }
Evan Cheng152804e2007-12-14 08:30:15 +00008283
8284 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008285 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008286 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008287
8288 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008289 SDValue Ops[] = {
8290 Op,
8291 DAG.getConstant(NumBits, OpVT),
8292 DAG.getConstant(X86::COND_E, MVT::i8),
8293 Op.getValue(1)
8294 };
8295 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008296
Owen Anderson825b72b2009-08-11 20:47:22 +00008297 if (VT == MVT::i8)
8298 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008299 return Op;
8300}
8301
Dan Gohmand858e902010-04-17 15:26:15 +00008302SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008303 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00008304 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008305 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00008306
Mon P Wangaf9b9522008-12-18 21:42:19 +00008307 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
8308 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
8309 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
8310 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
8311 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
8312 //
8313 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
8314 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
8315 // return AloBlo + AloBhi + AhiBlo;
8316
8317 SDValue A = Op.getOperand(0);
8318 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008319
Dale Johannesene4d209d2009-02-03 20:21:25 +00008320 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008321 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8322 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008323 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008324 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8325 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008326 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008327 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008328 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008329 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008330 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008331 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008332 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008333 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008334 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008335 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008336 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8337 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008338 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008339 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8340 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008341 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
8342 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008343 return Res;
8344}
8345
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008346SDValue X86TargetLowering::LowerSHL(SDValue Op, SelectionDAG &DAG) const {
8347 EVT VT = Op.getValueType();
8348 DebugLoc dl = Op.getDebugLoc();
8349 SDValue R = Op.getOperand(0);
8350
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008351 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008352
Nate Begeman51409212010-07-28 00:21:48 +00008353 assert(Subtarget->hasSSE41() && "Cannot lower SHL without SSE4.1 or later");
8354
8355 if (VT == MVT::v4i32) {
8356 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8357 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8358 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
8359
8360 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
8361
8362 std::vector<Constant*> CV(4, CI);
8363 Constant *C = ConstantVector::get(CV);
8364 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8365 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008366 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008367 false, false, 16);
8368
8369 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
8370 Op = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, Op);
8371 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
8372 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
8373 }
8374 if (VT == MVT::v16i8) {
8375 // a = a << 5;
8376 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8377 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8378 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
8379
8380 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
8381 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
8382
8383 std::vector<Constant*> CVM1(16, CM1);
8384 std::vector<Constant*> CVM2(16, CM2);
8385 Constant *C = ConstantVector::get(CVM1);
8386 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8387 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008388 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008389 false, false, 16);
8390
8391 // r = pblendv(r, psllw(r & (char16)15, 4), a);
8392 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8393 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8394 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8395 DAG.getConstant(4, MVT::i32));
8396 R = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8397 DAG.getConstant(Intrinsic::x86_sse41_pblendvb, MVT::i32),
8398 R, M, Op);
8399 // a += a
8400 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
8401
8402 C = ConstantVector::get(CVM2);
8403 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8404 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008405 MachinePointerInfo::getConstantPool(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008406 false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +00008407
8408 // r = pblendv(r, psllw(r & (char16)63, 2), a);
8409 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8410 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8411 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8412 DAG.getConstant(2, MVT::i32));
8413 R = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8414 DAG.getConstant(Intrinsic::x86_sse41_pblendvb, MVT::i32),
8415 R, M, Op);
8416 // a += a
8417 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
8418
8419 // return pblendv(r, r+r, a);
8420 R = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8421 DAG.getConstant(Intrinsic::x86_sse41_pblendvb, MVT::i32),
8422 R, DAG.getNode(ISD::ADD, dl, VT, R, R), Op);
8423 return R;
8424 }
8425 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008426}
Mon P Wangaf9b9522008-12-18 21:42:19 +00008427
Dan Gohmand858e902010-04-17 15:26:15 +00008428SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +00008429 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
8430 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00008431 // looks for this combo and may remove the "setcc" instruction if the "setcc"
8432 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00008433 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00008434 SDValue LHS = N->getOperand(0);
8435 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00008436 unsigned BaseOp = 0;
8437 unsigned Cond = 0;
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008438 DebugLoc dl = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00008439
8440 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008441 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00008442 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00008443 // A subtract of one will be selected as a INC. Note that INC doesn't
8444 // set CF, so we can't do this for UADDO.
8445 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
8446 if (C->getAPIntValue() == 1) {
8447 BaseOp = X86ISD::INC;
8448 Cond = X86::COND_O;
8449 break;
8450 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008451 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00008452 Cond = X86::COND_O;
8453 break;
8454 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008455 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00008456 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008457 break;
8458 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00008459 // A subtract of one will be selected as a DEC. Note that DEC doesn't
8460 // set CF, so we can't do this for USUBO.
8461 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
8462 if (C->getAPIntValue() == 1) {
8463 BaseOp = X86ISD::DEC;
8464 Cond = X86::COND_O;
8465 break;
8466 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008467 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00008468 Cond = X86::COND_O;
8469 break;
8470 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008471 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00008472 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008473 break;
8474 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00008475 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00008476 Cond = X86::COND_O;
8477 break;
8478 case ISD::UMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00008479 BaseOp = X86ISD::UMUL;
Dan Gohman653456c2009-01-07 00:15:08 +00008480 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008481 break;
8482 }
Bill Wendling3fafd932008-11-26 22:37:40 +00008483
Bill Wendling61edeb52008-12-02 01:06:39 +00008484 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008485 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008486 SDValue Sum = DAG.getNode(BaseOp, dl, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00008487
Bill Wendling61edeb52008-12-02 01:06:39 +00008488 SDValue SetCC =
Dale Johannesene4d209d2009-02-03 20:21:25 +00008489 DAG.getNode(X86ISD::SETCC, dl, N->getValueType(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00008490 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00008491
Bill Wendling61edeb52008-12-02 01:06:39 +00008492 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
8493 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00008494}
8495
Eric Christopher9a9d2752010-07-22 02:48:34 +00008496SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
8497 DebugLoc dl = Op.getDebugLoc();
8498
Eric Christopherb6729dc2010-08-04 23:03:04 +00008499 if (!Subtarget->hasSSE2()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +00008500 SDValue Chain = Op.getOperand(0);
8501 SDValue Zero = DAG.getConstant(0,
Eric Christopherb6729dc2010-08-04 23:03:04 +00008502 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +00008503 SDValue Ops[] = {
8504 DAG.getRegister(X86::ESP, MVT::i32), // Base
8505 DAG.getTargetConstant(1, MVT::i8), // Scale
8506 DAG.getRegister(0, MVT::i32), // Index
8507 DAG.getTargetConstant(0, MVT::i32), // Disp
8508 DAG.getRegister(0, MVT::i32), // Segment.
8509 Zero,
8510 Chain
8511 };
8512 SDNode *Res =
8513 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
8514 array_lengthof(Ops));
8515 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +00008516 }
Eric Christopher9a9d2752010-07-22 02:48:34 +00008517
8518 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +00008519 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +00008520 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Chris Lattner132929a2010-08-14 17:26:09 +00008521
8522 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
8523 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
8524 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
8525 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
8526
8527 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
8528 if (!Op1 && !Op2 && !Op3 && Op4)
8529 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
8530
8531 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
8532 if (Op1 && !Op2 && !Op3 && !Op4)
8533 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
8534
8535 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
8536 // (MFENCE)>;
8537 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +00008538}
8539
Dan Gohmand858e902010-04-17 15:26:15 +00008540SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008541 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008542 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00008543 unsigned Reg = 0;
8544 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00008545 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008546 default:
8547 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008548 case MVT::i8: Reg = X86::AL; size = 1; break;
8549 case MVT::i16: Reg = X86::AX; size = 2; break;
8550 case MVT::i32: Reg = X86::EAX; size = 4; break;
8551 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00008552 assert(Subtarget->is64Bit() && "Node not type legal!");
8553 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00008554 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008555 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008556 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00008557 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00008558 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00008559 Op.getOperand(1),
8560 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00008561 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00008562 cpIn.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00008563 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008564 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
8565 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
8566 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +00008567 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008568 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00008569 return cpOut;
8570}
8571
Duncan Sands1607f052008-12-01 11:39:25 +00008572SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008573 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +00008574 assert(Subtarget->is64Bit() && "Result not type legalized?");
Owen Anderson825b72b2009-08-11 20:47:22 +00008575 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00008576 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008577 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00008578 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008579 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
8580 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00008581 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00008582 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
8583 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00008584 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00008585 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00008586 rdx.getValue(1)
8587 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008588 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008589}
8590
Dale Johannesen7d07b482010-05-21 00:52:33 +00008591SDValue X86TargetLowering::LowerBIT_CONVERT(SDValue Op,
8592 SelectionDAG &DAG) const {
8593 EVT SrcVT = Op.getOperand(0).getValueType();
8594 EVT DstVT = Op.getValueType();
8595 assert((Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
8596 Subtarget->hasMMX() && !DisableMMX) &&
8597 "Unexpected custom BIT_CONVERT");
8598 assert((DstVT == MVT::i64 ||
8599 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
8600 "Unexpected custom BIT_CONVERT");
8601 // i64 <=> MMX conversions are Legal.
8602 if (SrcVT==MVT::i64 && DstVT.isVector())
8603 return Op;
8604 if (DstVT==MVT::i64 && SrcVT.isVector())
8605 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +00008606 // MMX <=> MMX conversions are Legal.
8607 if (SrcVT.isVector() && DstVT.isVector())
8608 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +00008609 // All other conversions need to be expanded.
8610 return SDValue();
8611}
Dan Gohmand858e902010-04-17 15:26:15 +00008612SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +00008613 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00008614 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008615 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008616 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00008617 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008618 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008619 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00008620 Node->getOperand(0),
8621 Node->getOperand(1), negOp,
8622 cast<AtomicSDNode>(Node)->getSrcValue(),
8623 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00008624}
8625
Evan Cheng0db9fe62006-04-25 20:13:52 +00008626/// LowerOperation - Provide custom lowering hooks for some operations.
8627///
Dan Gohmand858e902010-04-17 15:26:15 +00008628SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00008629 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008630 default: llvm_unreachable("Should not custom lower this!");
Eric Christopher9a9d2752010-07-22 02:48:34 +00008631 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008632 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
8633 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008634 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00008635 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008636 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
8637 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
8638 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
8639 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
8640 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
8641 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00008642 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00008643 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00008644 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008645 case ISD::SHL_PARTS:
8646 case ISD::SRA_PARTS:
8647 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
8648 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008649 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008650 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00008651 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008652 case ISD::FABS: return LowerFABS(Op, DAG);
8653 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008654 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00008655 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00008656 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00008657 case ISD::SELECT: return LowerSELECT(Op, DAG);
8658 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008659 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008660 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00008661 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00008662 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008663 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00008664 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
8665 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008666 case ISD::FRAME_TO_ARGS_OFFSET:
8667 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008668 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008669 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008670 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00008671 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00008672 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
8673 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008674 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008675 case ISD::SHL: return LowerSHL(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00008676 case ISD::SADDO:
8677 case ISD::UADDO:
8678 case ISD::SSUBO:
8679 case ISD::USUBO:
8680 case ISD::SMULO:
8681 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00008682 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Dale Johannesen7d07b482010-05-21 00:52:33 +00008683 case ISD::BIT_CONVERT: return LowerBIT_CONVERT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008684 }
Chris Lattner27a6c732007-11-24 07:07:01 +00008685}
8686
Duncan Sands1607f052008-12-01 11:39:25 +00008687void X86TargetLowering::
8688ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00008689 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008690 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008691 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00008692 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00008693
8694 SDValue Chain = Node->getOperand(0);
8695 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008696 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00008697 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00008698 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00008699 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00008700 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00008701 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00008702 SDValue Result =
8703 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
8704 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00008705 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00008706 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00008707 Results.push_back(Result.getValue(2));
8708}
8709
Duncan Sands126d9072008-07-04 11:47:58 +00008710/// ReplaceNodeResults - Replace a node with an illegal result type
8711/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00008712void X86TargetLowering::ReplaceNodeResults(SDNode *N,
8713 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00008714 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00008715 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00008716 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00008717 default:
Duncan Sands1607f052008-12-01 11:39:25 +00008718 assert(false && "Do not know how to custom type legalize this operation!");
8719 return;
8720 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00008721 std::pair<SDValue,SDValue> Vals =
8722 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00008723 SDValue FIST = Vals.first, StackSlot = Vals.second;
8724 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00008725 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00008726 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +00008727 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
8728 MachinePointerInfo(), false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00008729 }
8730 return;
8731 }
8732 case ISD::READCYCLECOUNTER: {
Owen Anderson825b72b2009-08-11 20:47:22 +00008733 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00008734 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008735 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008736 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00008737 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008738 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00008739 eax.getValue(2));
8740 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
8741 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00008742 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00008743 Results.push_back(edx.getValue(1));
8744 return;
8745 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008746 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00008747 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008748 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00008749 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00008750 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
8751 DAG.getConstant(0, MVT::i32));
8752 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
8753 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00008754 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
8755 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00008756 cpInL.getValue(1));
8757 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00008758 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
8759 DAG.getConstant(0, MVT::i32));
8760 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
8761 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00008762 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00008763 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00008764 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00008765 swapInL.getValue(1));
8766 SDValue Ops[] = { swapInH.getValue(0),
8767 N->getOperand(1),
8768 swapInH.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00008769 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008770 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, dl, Tys, Ops, 3);
Dale Johannesendd64c412009-02-04 00:33:20 +00008771 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00008772 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00008773 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00008774 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00008775 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00008776 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00008777 Results.push_back(cpOutH.getValue(1));
8778 return;
8779 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008780 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00008781 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
8782 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008783 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00008784 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
8785 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008786 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00008787 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
8788 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008789 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00008790 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
8791 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008792 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00008793 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
8794 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008795 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00008796 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
8797 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008798 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00008799 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
8800 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00008801 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008802}
8803
Evan Cheng72261582005-12-20 06:22:03 +00008804const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
8805 switch (Opcode) {
8806 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00008807 case X86ISD::BSF: return "X86ISD::BSF";
8808 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00008809 case X86ISD::SHLD: return "X86ISD::SHLD";
8810 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00008811 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00008812 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00008813 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00008814 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00008815 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00008816 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00008817 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
8818 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
8819 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00008820 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00008821 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00008822 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00008823 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00008824 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00008825 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00008826 case X86ISD::COMI: return "X86ISD::COMI";
8827 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00008828 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00008829 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Evan Cheng72261582005-12-20 06:22:03 +00008830 case X86ISD::CMOV: return "X86ISD::CMOV";
8831 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00008832 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00008833 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
8834 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00008835 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00008836 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00008837 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00008838 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00008839 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00008840 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
8841 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00008842 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00008843 case X86ISD::MMX_PINSRW: return "X86ISD::MMX_PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00008844 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Evan Cheng8ca29322006-11-10 21:43:37 +00008845 case X86ISD::FMAX: return "X86ISD::FMAX";
8846 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00008847 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
8848 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00008849 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +00008850 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008851 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00008852 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008853 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00008854 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
8855 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008856 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
8857 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
8858 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
8859 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
8860 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
8861 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00008862 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
8863 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00008864 case X86ISD::VSHL: return "X86ISD::VSHL";
8865 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00008866 case X86ISD::CMPPD: return "X86ISD::CMPPD";
8867 case X86ISD::CMPPS: return "X86ISD::CMPPS";
8868 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
8869 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
8870 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
8871 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
8872 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
8873 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
8874 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
8875 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008876 case X86ISD::ADD: return "X86ISD::ADD";
8877 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingd350e022008-12-12 21:15:41 +00008878 case X86ISD::SMUL: return "X86ISD::SMUL";
8879 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00008880 case X86ISD::INC: return "X86ISD::INC";
8881 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00008882 case X86ISD::OR: return "X86ISD::OR";
8883 case X86ISD::XOR: return "X86ISD::XOR";
8884 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00008885 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00008886 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008887 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00008888 case X86ISD::PALIGN: return "X86ISD::PALIGN";
8889 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
8890 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
8891 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
8892 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
8893 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
8894 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
8895 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
8896 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00008897 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00008898 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00008899 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00008900 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
8901 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00008902 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
8903 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
8904 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
8905 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
8906 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
8907 case X86ISD::MOVSD: return "X86ISD::MOVSD";
8908 case X86ISD::MOVSS: return "X86ISD::MOVSS";
8909 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
8910 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
8911 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
8912 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
8913 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
8914 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
8915 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
8916 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
8917 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
8918 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
8919 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
8920 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +00008921 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008922 case X86ISD::MINGW_ALLOCA: return "X86ISD::MINGW_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00008923 }
8924}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008925
Chris Lattnerc9addb72007-03-30 23:15:24 +00008926// isLegalAddressingMode - Return true if the addressing mode represented
8927// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00008928bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00008929 const Type *Ty) const {
8930 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008931 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +00008932 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00008933
Chris Lattnerc9addb72007-03-30 23:15:24 +00008934 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008935 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00008936 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00008937
Chris Lattnerc9addb72007-03-30 23:15:24 +00008938 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00008939 unsigned GVFlags =
8940 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008941
Chris Lattnerdfed4132009-07-10 07:38:24 +00008942 // If a reference to this global requires an extra load, we can't fold it.
8943 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00008944 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008945
Chris Lattnerdfed4132009-07-10 07:38:24 +00008946 // If BaseGV requires a register for the PIC base, we cannot also have a
8947 // BaseReg specified.
8948 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00008949 return false;
Evan Cheng52787842007-08-01 23:46:47 +00008950
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008951 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +00008952 if ((M != CodeModel::Small || R != Reloc::Static) &&
8953 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008954 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00008955 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008956
Chris Lattnerc9addb72007-03-30 23:15:24 +00008957 switch (AM.Scale) {
8958 case 0:
8959 case 1:
8960 case 2:
8961 case 4:
8962 case 8:
8963 // These scales always work.
8964 break;
8965 case 3:
8966 case 5:
8967 case 9:
8968 // These scales are formed with basereg+scalereg. Only accept if there is
8969 // no basereg yet.
8970 if (AM.HasBaseReg)
8971 return false;
8972 break;
8973 default: // Other stuff never works.
8974 return false;
8975 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008976
Chris Lattnerc9addb72007-03-30 23:15:24 +00008977 return true;
8978}
8979
8980
Evan Cheng2bd122c2007-10-26 01:56:11 +00008981bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00008982 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00008983 return false;
Evan Chenge127a732007-10-29 07:57:50 +00008984 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
8985 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00008986 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00008987 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00008988 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00008989}
8990
Owen Andersone50ed302009-08-10 22:56:29 +00008991bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008992 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00008993 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008994 unsigned NumBits1 = VT1.getSizeInBits();
8995 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00008996 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00008997 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00008998 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00008999}
Evan Cheng2bd122c2007-10-26 01:56:11 +00009000
Dan Gohman97121ba2009-04-08 00:15:30 +00009001bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009002 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009003 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009004}
9005
Owen Andersone50ed302009-08-10 22:56:29 +00009006bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009007 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00009008 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009009}
9010
Owen Andersone50ed302009-08-10 22:56:29 +00009011bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00009012 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00009013 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00009014}
9015
Evan Cheng60c07e12006-07-05 22:17:51 +00009016/// isShuffleMaskLegal - Targets can use this to indicate that they only
9017/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
9018/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
9019/// are assumed to be legal.
9020bool
Eric Christopherfd179292009-08-27 18:07:15 +00009021X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00009022 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +00009023 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +00009024 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +00009025 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +00009026
Nate Begemana09008b2009-10-19 02:17:23 +00009027 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00009028 return (VT.getVectorNumElements() == 2 ||
9029 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
9030 isMOVLMask(M, VT) ||
9031 isSHUFPMask(M, VT) ||
9032 isPSHUFDMask(M, VT) ||
9033 isPSHUFHWMask(M, VT) ||
9034 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00009035 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00009036 isUNPCKLMask(M, VT) ||
9037 isUNPCKHMask(M, VT) ||
9038 isUNPCKL_v_undef_Mask(M, VT) ||
9039 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009040}
9041
Dan Gohman7d8143f2008-04-09 20:09:42 +00009042bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00009043X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00009044 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00009045 unsigned NumElts = VT.getVectorNumElements();
9046 // FIXME: This collection of masks seems suspect.
9047 if (NumElts == 2)
9048 return true;
9049 if (NumElts == 4 && VT.getSizeInBits() == 128) {
9050 return (isMOVLMask(Mask, VT) ||
9051 isCommutedMOVLMask(Mask, VT, true) ||
9052 isSHUFPMask(Mask, VT) ||
9053 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009054 }
9055 return false;
9056}
9057
9058//===----------------------------------------------------------------------===//
9059// X86 Scheduler Hooks
9060//===----------------------------------------------------------------------===//
9061
Mon P Wang63307c32008-05-05 19:05:59 +00009062// private utility function
9063MachineBasicBlock *
9064X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
9065 MachineBasicBlock *MBB,
9066 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009067 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009068 unsigned LoadOpc,
9069 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009070 unsigned notOpc,
9071 unsigned EAXreg,
9072 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009073 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009074 // For the atomic bitwise operator, we generate
9075 // thisMBB:
9076 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009077 // ld t1 = [bitinstr.addr]
9078 // op t2 = t1, [bitinstr.val]
9079 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009080 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9081 // bz newMBB
9082 // fallthrough -->nextMBB
9083 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9084 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009085 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009086 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009087
Mon P Wang63307c32008-05-05 19:05:59 +00009088 /// First build the CFG
9089 MachineFunction *F = MBB->getParent();
9090 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009091 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9092 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9093 F->insert(MBBIter, newMBB);
9094 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009095
Dan Gohman14152b42010-07-06 20:24:04 +00009096 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9097 nextMBB->splice(nextMBB->begin(), thisMBB,
9098 llvm::next(MachineBasicBlock::iterator(bInstr)),
9099 thisMBB->end());
9100 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009101
Mon P Wang63307c32008-05-05 19:05:59 +00009102 // Update thisMBB to fall through to newMBB
9103 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009104
Mon P Wang63307c32008-05-05 19:05:59 +00009105 // newMBB jumps to itself and fall through to nextMBB
9106 newMBB->addSuccessor(nextMBB);
9107 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009108
Mon P Wang63307c32008-05-05 19:05:59 +00009109 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009110 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009111 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00009112 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009113 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009114 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009115 int numArgs = bInstr->getNumOperands() - 1;
9116 for (int i=0; i < numArgs; ++i)
9117 argOpers[i] = &bInstr->getOperand(i+1);
9118
9119 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009120 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009121 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009122
Dale Johannesen140be2d2008-08-19 18:47:28 +00009123 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009124 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009125 for (int i=0; i <= lastAddrIndx; ++i)
9126 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009127
Dale Johannesen140be2d2008-08-19 18:47:28 +00009128 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009129 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009130 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009131 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009132 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009133 tt = t1;
9134
Dale Johannesen140be2d2008-08-19 18:47:28 +00009135 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00009136 assert((argOpers[valArgIndx]->isReg() ||
9137 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009138 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00009139 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009140 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009141 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009142 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009143 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00009144 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009145
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009146 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00009147 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009148
Dale Johannesene4d209d2009-02-03 20:21:25 +00009149 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00009150 for (int i=0; i <= lastAddrIndx; ++i)
9151 (*MIB).addOperand(*argOpers[i]);
9152 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00009153 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009154 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9155 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00009156
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009157 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00009158 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009159
Mon P Wang63307c32008-05-05 19:05:59 +00009160 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009161 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009162
Dan Gohman14152b42010-07-06 20:24:04 +00009163 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009164 return nextMBB;
9165}
9166
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00009167// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00009168MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009169X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
9170 MachineBasicBlock *MBB,
9171 unsigned regOpcL,
9172 unsigned regOpcH,
9173 unsigned immOpcL,
9174 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009175 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009176 // For the atomic bitwise operator, we generate
9177 // thisMBB (instructions are in pairs, except cmpxchg8b)
9178 // ld t1,t2 = [bitinstr.addr]
9179 // newMBB:
9180 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
9181 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00009182 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009183 // mov ECX, EBX <- t5, t6
9184 // mov EAX, EDX <- t1, t2
9185 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
9186 // mov t3, t4 <- EAX, EDX
9187 // bz newMBB
9188 // result in out1, out2
9189 // fallthrough -->nextMBB
9190
9191 const TargetRegisterClass *RC = X86::GR32RegisterClass;
9192 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009193 const unsigned NotOpc = X86::NOT32r;
9194 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9195 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9196 MachineFunction::iterator MBBIter = MBB;
9197 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009198
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009199 /// First build the CFG
9200 MachineFunction *F = MBB->getParent();
9201 MachineBasicBlock *thisMBB = MBB;
9202 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9203 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9204 F->insert(MBBIter, newMBB);
9205 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009206
Dan Gohman14152b42010-07-06 20:24:04 +00009207 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9208 nextMBB->splice(nextMBB->begin(), thisMBB,
9209 llvm::next(MachineBasicBlock::iterator(bInstr)),
9210 thisMBB->end());
9211 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009212
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009213 // Update thisMBB to fall through to newMBB
9214 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009215
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009216 // newMBB jumps to itself and fall through to nextMBB
9217 newMBB->addSuccessor(nextMBB);
9218 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009219
Dale Johannesene4d209d2009-02-03 20:21:25 +00009220 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009221 // Insert instructions into newMBB based on incoming instruction
9222 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009223 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009224 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009225 MachineOperand& dest1Oper = bInstr->getOperand(0);
9226 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009227 MachineOperand* argOpers[2 + X86::AddrNumOperands];
9228 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009229 argOpers[i] = &bInstr->getOperand(i+2);
9230
Dan Gohman71ea4e52010-05-14 21:01:44 +00009231 // We use some of the operands multiple times, so conservatively just
9232 // clear any kill flags that might be present.
9233 if (argOpers[i]->isReg() && argOpers[i]->isUse())
9234 argOpers[i]->setIsKill(false);
9235 }
9236
Evan Chengad5b52f2010-01-08 19:14:57 +00009237 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009238 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00009239
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009240 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009241 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009242 for (int i=0; i <= lastAddrIndx; ++i)
9243 (*MIB).addOperand(*argOpers[i]);
9244 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009245 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00009246 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00009247 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009248 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00009249 MachineOperand newOp3 = *(argOpers[3]);
9250 if (newOp3.isImm())
9251 newOp3.setImm(newOp3.getImm()+4);
9252 else
9253 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009254 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00009255 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009256
9257 // t3/4 are defined later, at the bottom of the loop
9258 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
9259 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009260 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009261 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009262 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009263 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
9264
Evan Cheng306b4ca2010-01-08 23:41:50 +00009265 // The subsequent operations should be using the destination registers of
9266 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00009267 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009268 t1 = F->getRegInfo().createVirtualRegister(RC);
9269 t2 = F->getRegInfo().createVirtualRegister(RC);
9270 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
9271 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009272 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009273 t1 = dest1Oper.getReg();
9274 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009275 }
9276
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009277 int valArgIndx = lastAddrIndx + 1;
9278 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00009279 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009280 "invalid operand");
9281 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
9282 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009283 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009284 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009285 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009286 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00009287 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009288 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009289 (*MIB).addOperand(*argOpers[valArgIndx]);
9290 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009291 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009292 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009293 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009294 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009295 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009296 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009297 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00009298 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009299 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009300 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009301
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009302 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009303 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009304 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009305 MIB.addReg(t2);
9306
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009307 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009308 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009309 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009310 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00009311
Dale Johannesene4d209d2009-02-03 20:21:25 +00009312 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009313 for (int i=0; i <= lastAddrIndx; ++i)
9314 (*MIB).addOperand(*argOpers[i]);
9315
9316 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009317 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9318 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009319
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009320 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009321 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009322 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009323 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009324
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009325 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009326 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009327
Dan Gohman14152b42010-07-06 20:24:04 +00009328 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009329 return nextMBB;
9330}
9331
9332// private utility function
9333MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00009334X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
9335 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009336 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009337 // For the atomic min/max operator, we generate
9338 // thisMBB:
9339 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009340 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00009341 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00009342 // cmp t1, t2
9343 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00009344 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009345 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9346 // bz newMBB
9347 // fallthrough -->nextMBB
9348 //
9349 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9350 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009351 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009352 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009353
Mon P Wang63307c32008-05-05 19:05:59 +00009354 /// First build the CFG
9355 MachineFunction *F = MBB->getParent();
9356 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009357 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9358 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9359 F->insert(MBBIter, newMBB);
9360 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009361
Dan Gohman14152b42010-07-06 20:24:04 +00009362 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9363 nextMBB->splice(nextMBB->begin(), thisMBB,
9364 llvm::next(MachineBasicBlock::iterator(mInstr)),
9365 thisMBB->end());
9366 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009367
Mon P Wang63307c32008-05-05 19:05:59 +00009368 // Update thisMBB to fall through to newMBB
9369 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009370
Mon P Wang63307c32008-05-05 19:05:59 +00009371 // newMBB jumps to newMBB and fall through to nextMBB
9372 newMBB->addSuccessor(nextMBB);
9373 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009374
Dale Johannesene4d209d2009-02-03 20:21:25 +00009375 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009376 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009377 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009378 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00009379 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009380 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009381 int numArgs = mInstr->getNumOperands() - 1;
9382 for (int i=0; i < numArgs; ++i)
9383 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009384
Mon P Wang63307c32008-05-05 19:05:59 +00009385 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009386 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009387 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009388
Mon P Wangab3e7472008-05-05 22:56:23 +00009389 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009390 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009391 for (int i=0; i <= lastAddrIndx; ++i)
9392 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00009393
Mon P Wang63307c32008-05-05 19:05:59 +00009394 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00009395 assert((argOpers[valArgIndx]->isReg() ||
9396 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009397 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00009398
9399 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00009400 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009401 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00009402 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009403 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009404 (*MIB).addOperand(*argOpers[valArgIndx]);
9405
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009406 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00009407 MIB.addReg(t1);
9408
Dale Johannesene4d209d2009-02-03 20:21:25 +00009409 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00009410 MIB.addReg(t1);
9411 MIB.addReg(t2);
9412
9413 // Generate movc
9414 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009415 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00009416 MIB.addReg(t2);
9417 MIB.addReg(t1);
9418
9419 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00009420 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00009421 for (int i=0; i <= lastAddrIndx; ++i)
9422 (*MIB).addOperand(*argOpers[i]);
9423 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00009424 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009425 (*MIB).setMemRefs(mInstr->memoperands_begin(),
9426 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +00009427
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009428 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00009429 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009430
Mon P Wang63307c32008-05-05 19:05:59 +00009431 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009432 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009433
Dan Gohman14152b42010-07-06 20:24:04 +00009434 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009435 return nextMBB;
9436}
9437
Eric Christopherf83a5de2009-08-27 18:08:16 +00009438// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009439// or XMM0_V32I8 in AVX all of this code can be replaced with that
9440// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +00009441MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +00009442X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +00009443 unsigned numArgs, bool memArg) const {
Eric Christopherb120ab42009-08-18 22:50:32 +00009444
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009445 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
9446 "Target must have SSE4.2 or AVX features enabled");
9447
Eric Christopherb120ab42009-08-18 22:50:32 +00009448 DebugLoc dl = MI->getDebugLoc();
9449 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9450
9451 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009452
9453 if (!Subtarget->hasAVX()) {
9454 if (memArg)
9455 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
9456 else
9457 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
9458 } else {
9459 if (memArg)
9460 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
9461 else
9462 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
9463 }
Eric Christopherb120ab42009-08-18 22:50:32 +00009464
9465 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(Opc));
9466
9467 for (unsigned i = 0; i < numArgs; ++i) {
9468 MachineOperand &Op = MI->getOperand(i+1);
9469
9470 if (!(Op.isReg() && Op.isImplicit()))
9471 MIB.addOperand(Op);
9472 }
9473
9474 BuildMI(BB, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
9475 .addReg(X86::XMM0);
9476
Dan Gohman14152b42010-07-06 20:24:04 +00009477 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +00009478
9479 return BB;
9480}
9481
9482MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +00009483X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
9484 MachineInstr *MI,
9485 MachineBasicBlock *MBB) const {
9486 // Emit code to save XMM registers to the stack. The ABI says that the
9487 // number of registers to save is given in %al, so it's theoretically
9488 // possible to do an indirect jump trick to avoid saving all of them,
9489 // however this code takes a simpler approach and just executes all
9490 // of the stores if %al is non-zero. It's less code, and it's probably
9491 // easier on the hardware branch predictor, and stores aren't all that
9492 // expensive anyway.
9493
9494 // Create the new basic blocks. One block contains all the XMM stores,
9495 // and one block is the final destination regardless of whether any
9496 // stores were performed.
9497 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9498 MachineFunction *F = MBB->getParent();
9499 MachineFunction::iterator MBBIter = MBB;
9500 ++MBBIter;
9501 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
9502 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
9503 F->insert(MBBIter, XMMSaveMBB);
9504 F->insert(MBBIter, EndMBB);
9505
Dan Gohman14152b42010-07-06 20:24:04 +00009506 // Transfer the remainder of MBB and its successor edges to EndMBB.
9507 EndMBB->splice(EndMBB->begin(), MBB,
9508 llvm::next(MachineBasicBlock::iterator(MI)),
9509 MBB->end());
9510 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
9511
Dan Gohmand6708ea2009-08-15 01:38:56 +00009512 // The original block will now fall through to the XMM save block.
9513 MBB->addSuccessor(XMMSaveMBB);
9514 // The XMMSaveMBB will fall through to the end block.
9515 XMMSaveMBB->addSuccessor(EndMBB);
9516
9517 // Now add the instructions.
9518 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9519 DebugLoc DL = MI->getDebugLoc();
9520
9521 unsigned CountReg = MI->getOperand(0).getReg();
9522 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
9523 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
9524
9525 if (!Subtarget->isTargetWin64()) {
9526 // If %al is 0, branch around the XMM save block.
9527 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009528 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +00009529 MBB->addSuccessor(EndMBB);
9530 }
9531
9532 // In the XMM save block, save all the XMM argument registers.
9533 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
9534 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +00009535 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +00009536 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +00009537 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +00009538 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +00009539 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +00009540 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
9541 .addFrameIndex(RegSaveFrameIndex)
9542 .addImm(/*Scale=*/1)
9543 .addReg(/*IndexReg=*/0)
9544 .addImm(/*Disp=*/Offset)
9545 .addReg(/*Segment=*/0)
9546 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +00009547 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +00009548 }
9549
Dan Gohman14152b42010-07-06 20:24:04 +00009550 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +00009551
9552 return EndMBB;
9553}
Mon P Wang63307c32008-05-05 19:05:59 +00009554
Evan Cheng60c07e12006-07-05 22:17:51 +00009555MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +00009556X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00009557 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +00009558 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9559 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +00009560
Chris Lattner52600972009-09-02 05:57:00 +00009561 // To "insert" a SELECT_CC instruction, we actually have to insert the
9562 // diamond control-flow pattern. The incoming instruction knows the
9563 // destination vreg to set, the condition code register to branch on, the
9564 // true/false values to select between, and a branch opcode to use.
9565 const BasicBlock *LLVM_BB = BB->getBasicBlock();
9566 MachineFunction::iterator It = BB;
9567 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +00009568
Chris Lattner52600972009-09-02 05:57:00 +00009569 // thisMBB:
9570 // ...
9571 // TrueVal = ...
9572 // cmpTY ccX, r1, r2
9573 // bCC copy1MBB
9574 // fallthrough --> copy0MBB
9575 MachineBasicBlock *thisMBB = BB;
9576 MachineFunction *F = BB->getParent();
9577 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
9578 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +00009579 F->insert(It, copy0MBB);
9580 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +00009581
Bill Wendling730c07e2010-06-25 20:48:10 +00009582 // If the EFLAGS register isn't dead in the terminator, then claim that it's
9583 // live into the sink and copy blocks.
9584 const MachineFunction *MF = BB->getParent();
9585 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
9586 BitVector ReservedRegs = TRI->getReservedRegs(*MF);
Bill Wendling730c07e2010-06-25 20:48:10 +00009587
Dan Gohman14152b42010-07-06 20:24:04 +00009588 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
9589 const MachineOperand &MO = MI->getOperand(I);
9590 if (!MO.isReg() || !MO.isUse() || MO.isKill()) continue;
Bill Wendling730c07e2010-06-25 20:48:10 +00009591 unsigned Reg = MO.getReg();
9592 if (Reg != X86::EFLAGS) continue;
9593 copy0MBB->addLiveIn(Reg);
9594 sinkMBB->addLiveIn(Reg);
9595 }
9596
Dan Gohman14152b42010-07-06 20:24:04 +00009597 // Transfer the remainder of BB and its successor edges to sinkMBB.
9598 sinkMBB->splice(sinkMBB->begin(), BB,
9599 llvm::next(MachineBasicBlock::iterator(MI)),
9600 BB->end());
9601 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
9602
9603 // Add the true and fallthrough blocks as its successors.
9604 BB->addSuccessor(copy0MBB);
9605 BB->addSuccessor(sinkMBB);
9606
9607 // Create the conditional branch instruction.
9608 unsigned Opc =
9609 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
9610 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
9611
Chris Lattner52600972009-09-02 05:57:00 +00009612 // copy0MBB:
9613 // %FalseValue = ...
9614 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +00009615 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00009616
Chris Lattner52600972009-09-02 05:57:00 +00009617 // sinkMBB:
9618 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
9619 // ...
Dan Gohman14152b42010-07-06 20:24:04 +00009620 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
9621 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +00009622 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
9623 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
9624
Dan Gohman14152b42010-07-06 20:24:04 +00009625 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +00009626 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +00009627}
9628
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00009629MachineBasicBlock *
9630X86TargetLowering::EmitLoweredMingwAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00009631 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00009632 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9633 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00009634
9635 // The lowering is pretty easy: we're just emitting the call to _alloca. The
9636 // non-trivial part is impdef of ESP.
9637 // FIXME: The code should be tweaked as soon as we'll try to do codegen for
9638 // mingw-w64.
9639
Dan Gohman14152b42010-07-06 20:24:04 +00009640 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00009641 .addExternalSymbol("_alloca")
9642 .addReg(X86::EAX, RegState::Implicit)
9643 .addReg(X86::ESP, RegState::Implicit)
9644 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
Anton Korobeynikov9f7f83b2010-08-25 07:50:11 +00009645 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
9646 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00009647
Dan Gohman14152b42010-07-06 20:24:04 +00009648 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00009649 return BB;
9650}
Chris Lattner52600972009-09-02 05:57:00 +00009651
9652MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +00009653X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
9654 MachineBasicBlock *BB) const {
9655 // This is pretty easy. We're taking the value that we received from
9656 // our load from the relocation, sticking it in either RDI (x86-64)
9657 // or EAX and doing an indirect call. The return value will then
9658 // be in the normal return register.
Eric Christopher54415362010-06-08 22:04:25 +00009659 const X86InstrInfo *TII
9660 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +00009661 DebugLoc DL = MI->getDebugLoc();
9662 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +00009663
9664 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +00009665 assert(MI->getOperand(3).isGlobal() && "This should be a global");
9666
Eric Christopher30ef0e52010-06-03 04:07:48 +00009667 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +00009668 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
9669 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +00009670 .addReg(X86::RIP)
9671 .addImm(0).addReg(0)
9672 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
9673 MI->getOperand(3).getTargetFlags())
9674 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +00009675 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +00009676 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +00009677 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +00009678 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
9679 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +00009680 .addReg(0)
9681 .addImm(0).addReg(0)
9682 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
9683 MI->getOperand(3).getTargetFlags())
9684 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +00009685 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +00009686 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +00009687 } else {
Dan Gohman14152b42010-07-06 20:24:04 +00009688 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
9689 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +00009690 .addReg(TII->getGlobalBaseReg(F))
9691 .addImm(0).addReg(0)
9692 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
9693 MI->getOperand(3).getTargetFlags())
9694 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +00009695 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +00009696 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +00009697 }
9698
Dan Gohman14152b42010-07-06 20:24:04 +00009699 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +00009700 return BB;
9701}
9702
9703MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00009704X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00009705 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00009706 switch (MI->getOpcode()) {
9707 default: assert(false && "Unexpected instr type to insert");
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00009708 case X86::MINGW_ALLOCA:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00009709 return EmitLoweredMingwAlloca(MI, BB);
Eric Christopher30ef0e52010-06-03 04:07:48 +00009710 case X86::TLSCall_32:
9711 case X86::TLSCall_64:
9712 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +00009713 case X86::CMOV_GR8:
Mon P Wang9e5ecb82008-12-12 01:25:51 +00009714 case X86::CMOV_V1I64:
Evan Cheng60c07e12006-07-05 22:17:51 +00009715 case X86::CMOV_FR32:
9716 case X86::CMOV_FR64:
9717 case X86::CMOV_V4F32:
9718 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +00009719 case X86::CMOV_V2I64:
Chris Lattner314a1132010-03-14 18:31:44 +00009720 case X86::CMOV_GR16:
9721 case X86::CMOV_GR32:
9722 case X86::CMOV_RFP32:
9723 case X86::CMOV_RFP64:
9724 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00009725 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +00009726
Dale Johannesen849f2142007-07-03 00:53:03 +00009727 case X86::FP32_TO_INT16_IN_MEM:
9728 case X86::FP32_TO_INT32_IN_MEM:
9729 case X86::FP32_TO_INT64_IN_MEM:
9730 case X86::FP64_TO_INT16_IN_MEM:
9731 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00009732 case X86::FP64_TO_INT64_IN_MEM:
9733 case X86::FP80_TO_INT16_IN_MEM:
9734 case X86::FP80_TO_INT32_IN_MEM:
9735 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +00009736 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9737 DebugLoc DL = MI->getDebugLoc();
9738
Evan Cheng60c07e12006-07-05 22:17:51 +00009739 // Change the floating point control register to use "round towards zero"
9740 // mode when truncating to an integer value.
9741 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +00009742 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +00009743 addFrameReference(BuildMI(*BB, MI, DL,
9744 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00009745
9746 // Load the old value of the high byte of the control word...
9747 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +00009748 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +00009749 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009750 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00009751
9752 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +00009753 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00009754 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00009755
9756 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +00009757 addFrameReference(BuildMI(*BB, MI, DL,
9758 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00009759
9760 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +00009761 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00009762 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00009763
9764 // Get the X86 opcode to use.
9765 unsigned Opc;
9766 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009767 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00009768 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
9769 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
9770 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
9771 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
9772 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
9773 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00009774 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
9775 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
9776 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00009777 }
9778
9779 X86AddressMode AM;
9780 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +00009781 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00009782 AM.BaseType = X86AddressMode::RegBase;
9783 AM.Base.Reg = Op.getReg();
9784 } else {
9785 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +00009786 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +00009787 }
9788 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +00009789 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00009790 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00009791 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +00009792 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00009793 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00009794 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +00009795 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00009796 AM.GV = Op.getGlobal();
9797 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00009798 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00009799 }
Dan Gohman14152b42010-07-06 20:24:04 +00009800 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009801 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00009802
9803 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +00009804 addFrameReference(BuildMI(*BB, MI, DL,
9805 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00009806
Dan Gohman14152b42010-07-06 20:24:04 +00009807 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00009808 return BB;
9809 }
Eric Christopherb120ab42009-08-18 22:50:32 +00009810 // String/text processing lowering.
9811 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009812 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +00009813 return EmitPCMP(MI, BB, 3, false /* in-mem */);
9814 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009815 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +00009816 return EmitPCMP(MI, BB, 3, true /* in-mem */);
9817 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009818 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +00009819 return EmitPCMP(MI, BB, 5, false /* in mem */);
9820 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009821 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +00009822 return EmitPCMP(MI, BB, 5, true /* in mem */);
9823
9824 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +00009825 case X86::ATOMAND32:
9826 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00009827 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009828 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009829 X86::NOT32r, X86::EAX,
9830 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00009831 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +00009832 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
9833 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009834 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009835 X86::NOT32r, X86::EAX,
9836 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00009837 case X86::ATOMXOR32:
9838 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00009839 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009840 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009841 X86::NOT32r, X86::EAX,
9842 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009843 case X86::ATOMNAND32:
9844 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009845 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009846 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009847 X86::NOT32r, X86::EAX,
9848 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +00009849 case X86::ATOMMIN32:
9850 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
9851 case X86::ATOMMAX32:
9852 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
9853 case X86::ATOMUMIN32:
9854 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
9855 case X86::ATOMUMAX32:
9856 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +00009857
9858 case X86::ATOMAND16:
9859 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
9860 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009861 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009862 X86::NOT16r, X86::AX,
9863 X86::GR16RegisterClass);
9864 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +00009865 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009866 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009867 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009868 X86::NOT16r, X86::AX,
9869 X86::GR16RegisterClass);
9870 case X86::ATOMXOR16:
9871 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
9872 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009873 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009874 X86::NOT16r, X86::AX,
9875 X86::GR16RegisterClass);
9876 case X86::ATOMNAND16:
9877 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
9878 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009879 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009880 X86::NOT16r, X86::AX,
9881 X86::GR16RegisterClass, true);
9882 case X86::ATOMMIN16:
9883 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
9884 case X86::ATOMMAX16:
9885 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
9886 case X86::ATOMUMIN16:
9887 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
9888 case X86::ATOMUMAX16:
9889 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
9890
9891 case X86::ATOMAND8:
9892 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
9893 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009894 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009895 X86::NOT8r, X86::AL,
9896 X86::GR8RegisterClass);
9897 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +00009898 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009899 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009900 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009901 X86::NOT8r, X86::AL,
9902 X86::GR8RegisterClass);
9903 case X86::ATOMXOR8:
9904 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
9905 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009906 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009907 X86::NOT8r, X86::AL,
9908 X86::GR8RegisterClass);
9909 case X86::ATOMNAND8:
9910 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
9911 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009912 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009913 X86::NOT8r, X86::AL,
9914 X86::GR8RegisterClass, true);
9915 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009916 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +00009917 case X86::ATOMAND64:
9918 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00009919 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009920 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +00009921 X86::NOT64r, X86::RAX,
9922 X86::GR64RegisterClass);
9923 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +00009924 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
9925 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009926 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +00009927 X86::NOT64r, X86::RAX,
9928 X86::GR64RegisterClass);
9929 case X86::ATOMXOR64:
9930 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00009931 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009932 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +00009933 X86::NOT64r, X86::RAX,
9934 X86::GR64RegisterClass);
9935 case X86::ATOMNAND64:
9936 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
9937 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009938 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +00009939 X86::NOT64r, X86::RAX,
9940 X86::GR64RegisterClass, true);
9941 case X86::ATOMMIN64:
9942 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
9943 case X86::ATOMMAX64:
9944 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
9945 case X86::ATOMUMIN64:
9946 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
9947 case X86::ATOMUMAX64:
9948 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009949
9950 // This group does 64-bit operations on a 32-bit host.
9951 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00009952 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009953 X86::AND32rr, X86::AND32rr,
9954 X86::AND32ri, X86::AND32ri,
9955 false);
9956 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00009957 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009958 X86::OR32rr, X86::OR32rr,
9959 X86::OR32ri, X86::OR32ri,
9960 false);
9961 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00009962 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009963 X86::XOR32rr, X86::XOR32rr,
9964 X86::XOR32ri, X86::XOR32ri,
9965 false);
9966 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00009967 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009968 X86::AND32rr, X86::AND32rr,
9969 X86::AND32ri, X86::AND32ri,
9970 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009971 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00009972 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009973 X86::ADD32rr, X86::ADC32rr,
9974 X86::ADD32ri, X86::ADC32ri,
9975 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009976 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00009977 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009978 X86::SUB32rr, X86::SBB32rr,
9979 X86::SUB32ri, X86::SBB32ri,
9980 false);
Dale Johannesen880ae362008-10-03 22:25:52 +00009981 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00009982 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +00009983 X86::MOV32rr, X86::MOV32rr,
9984 X86::MOV32ri, X86::MOV32ri,
9985 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +00009986 case X86::VASTART_SAVE_XMM_REGS:
9987 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +00009988 }
9989}
9990
9991//===----------------------------------------------------------------------===//
9992// X86 Optimization Hooks
9993//===----------------------------------------------------------------------===//
9994
Dan Gohman475871a2008-07-27 21:46:04 +00009995void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00009996 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00009997 APInt &KnownZero,
9998 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00009999 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000010000 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010001 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000010002 assert((Opc >= ISD::BUILTIN_OP_END ||
10003 Opc == ISD::INTRINSIC_WO_CHAIN ||
10004 Opc == ISD::INTRINSIC_W_CHAIN ||
10005 Opc == ISD::INTRINSIC_VOID) &&
10006 "Should use MaskedValueIsZero if you don't know whether Op"
10007 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010008
Dan Gohmanf4f92f52008-02-13 23:07:24 +000010009 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010010 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000010011 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010012 case X86ISD::ADD:
10013 case X86ISD::SUB:
10014 case X86ISD::SMUL:
10015 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000010016 case X86ISD::INC:
10017 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000010018 case X86ISD::OR:
10019 case X86ISD::XOR:
10020 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010021 // These nodes' second result is a boolean.
10022 if (Op.getResNo() == 0)
10023 break;
10024 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010025 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010026 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
10027 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000010028 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010029 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010030}
Chris Lattner259e97c2006-01-31 19:43:35 +000010031
Owen Andersonbc146b02010-09-21 20:42:50 +000010032unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
10033 unsigned Depth) const {
10034 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
10035 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
10036 return Op.getValueType().getScalarType().getSizeInBits();
10037
10038 // Fallback case.
10039 return 1;
10040}
10041
Evan Cheng206ee9d2006-07-07 08:33:52 +000010042/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000010043/// node is a GlobalAddress + offset.
10044bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000010045 const GlobalValue* &GA,
10046 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000010047 if (N->getOpcode() == X86ISD::Wrapper) {
10048 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000010049 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000010050 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000010051 return true;
10052 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000010053 }
Evan Chengad4196b2008-05-12 19:56:52 +000010054 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000010055}
10056
Evan Cheng206ee9d2006-07-07 08:33:52 +000010057/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
10058/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
10059/// if the load addresses are consecutive, non-overlapping, and in the right
Nate Begemanfdea31a2010-03-24 20:49:50 +000010060/// order.
Dan Gohman475871a2008-07-27 21:46:04 +000010061static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +000010062 const TargetLowering &TLI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010063 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000010064 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000010065
Eli Friedman7a5e5552009-06-07 06:52:44 +000010066 if (VT.getSizeInBits() != 128)
10067 return SDValue();
10068
Nate Begemanfdea31a2010-03-24 20:49:50 +000010069 SmallVector<SDValue, 16> Elts;
10070 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000010071 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000010072
Nate Begemanfdea31a2010-03-24 20:49:50 +000010073 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000010074}
Evan Chengd880b972008-05-09 21:53:03 +000010075
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000010076/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
10077/// generation and convert it from being a bunch of shuffles and extracts
10078/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010079static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
10080 const TargetLowering &TLI) {
10081 SDValue InputVector = N->getOperand(0);
10082
10083 // Only operate on vectors of 4 elements, where the alternative shuffling
10084 // gets to be more expensive.
10085 if (InputVector.getValueType() != MVT::v4i32)
10086 return SDValue();
10087
10088 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
10089 // single use which is a sign-extend or zero-extend, and all elements are
10090 // used.
10091 SmallVector<SDNode *, 4> Uses;
10092 unsigned ExtractedElements = 0;
10093 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
10094 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
10095 if (UI.getUse().getResNo() != InputVector.getResNo())
10096 return SDValue();
10097
10098 SDNode *Extract = *UI;
10099 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
10100 return SDValue();
10101
10102 if (Extract->getValueType(0) != MVT::i32)
10103 return SDValue();
10104 if (!Extract->hasOneUse())
10105 return SDValue();
10106 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
10107 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
10108 return SDValue();
10109 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
10110 return SDValue();
10111
10112 // Record which element was extracted.
10113 ExtractedElements |=
10114 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
10115
10116 Uses.push_back(Extract);
10117 }
10118
10119 // If not all the elements were used, this may not be worthwhile.
10120 if (ExtractedElements != 15)
10121 return SDValue();
10122
10123 // Ok, we've now decided to do the transformation.
10124 DebugLoc dl = InputVector.getDebugLoc();
10125
10126 // Store the value to a temporary stack slot.
10127 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010128 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
10129 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010130
10131 // Replace each use (extract) with a load of the appropriate element.
10132 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
10133 UE = Uses.end(); UI != UE; ++UI) {
10134 SDNode *Extract = *UI;
10135
10136 // Compute the element's address.
10137 SDValue Idx = Extract->getOperand(1);
10138 unsigned EltSize =
10139 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
10140 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
10141 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
10142
Eric Christopher90eb4022010-07-22 00:26:08 +000010143 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(),
Chris Lattner51abfe42010-09-21 06:02:19 +000010144 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010145
10146 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000010147 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000010148 ScalarAddr, MachinePointerInfo(),
10149 false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010150
10151 // Replace the exact with the load.
10152 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
10153 }
10154
10155 // The replacement was made in place; don't return anything.
10156 return SDValue();
10157}
10158
Chris Lattner83e6c992006-10-04 06:57:07 +000010159/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000010160static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000010161 const X86Subtarget *Subtarget) {
10162 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000010163 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000010164 // Get the LHS/RHS of the select.
10165 SDValue LHS = N->getOperand(1);
10166 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +000010167
Dan Gohman670e5392009-09-21 18:03:22 +000010168 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000010169 // instructions match the semantics of the common C idiom x<y?x:y but not
10170 // x<=y?x:y, because of how they handle negative zero (which can be
10171 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +000010172 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +000010173 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +000010174 Cond.getOpcode() == ISD::SETCC) {
10175 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010176
Chris Lattner47b4ce82009-03-11 05:48:52 +000010177 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000010178 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000010179 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
10180 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000010181 switch (CC) {
10182 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000010183 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000010184 // Converting this to a min would handle NaNs incorrectly, and swapping
10185 // the operands would cause it to handle comparisons between positive
10186 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010187 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000010188 if (!UnsafeFPMath &&
10189 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
10190 break;
10191 std::swap(LHS, RHS);
10192 }
Dan Gohman670e5392009-09-21 18:03:22 +000010193 Opcode = X86ISD::FMIN;
10194 break;
10195 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000010196 // Converting this to a min would handle comparisons between positive
10197 // and negative zero incorrectly.
10198 if (!UnsafeFPMath &&
10199 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
10200 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010201 Opcode = X86ISD::FMIN;
10202 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000010203 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000010204 // Converting this to a min would handle both negative zeros and NaNs
10205 // incorrectly, but we can swap the operands to fix both.
10206 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010207 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010208 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000010209 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010210 Opcode = X86ISD::FMIN;
10211 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010212
Dan Gohman670e5392009-09-21 18:03:22 +000010213 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010214 // Converting this to a max would handle comparisons between positive
10215 // and negative zero incorrectly.
10216 if (!UnsafeFPMath &&
10217 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
10218 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010219 Opcode = X86ISD::FMAX;
10220 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000010221 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000010222 // Converting this to a max would handle NaNs incorrectly, and swapping
10223 // the operands would cause it to handle comparisons between positive
10224 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010225 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000010226 if (!UnsafeFPMath &&
10227 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
10228 break;
10229 std::swap(LHS, RHS);
10230 }
Dan Gohman670e5392009-09-21 18:03:22 +000010231 Opcode = X86ISD::FMAX;
10232 break;
10233 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010234 // Converting this to a max would handle both negative zeros and NaNs
10235 // incorrectly, but we can swap the operands to fix both.
10236 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010237 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010238 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010239 case ISD::SETGE:
10240 Opcode = X86ISD::FMAX;
10241 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000010242 }
Dan Gohman670e5392009-09-21 18:03:22 +000010243 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000010244 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
10245 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000010246 switch (CC) {
10247 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000010248 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010249 // Converting this to a min would handle comparisons between positive
10250 // and negative zero incorrectly, and swapping the operands would
10251 // cause it to handle NaNs incorrectly.
10252 if (!UnsafeFPMath &&
10253 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000010254 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000010255 break;
10256 std::swap(LHS, RHS);
10257 }
Dan Gohman670e5392009-09-21 18:03:22 +000010258 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000010259 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010260 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000010261 // Converting this to a min would handle NaNs incorrectly.
10262 if (!UnsafeFPMath &&
10263 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
10264 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010265 Opcode = X86ISD::FMIN;
10266 break;
10267 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010268 // Converting this to a min would handle both negative zeros and NaNs
10269 // incorrectly, but we can swap the operands to fix both.
10270 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010271 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010272 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010273 case ISD::SETGE:
10274 Opcode = X86ISD::FMIN;
10275 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010276
Dan Gohman670e5392009-09-21 18:03:22 +000010277 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000010278 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010279 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000010280 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010281 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000010282 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010283 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000010284 // Converting this to a max would handle comparisons between positive
10285 // and negative zero incorrectly, and swapping the operands would
10286 // cause it to handle NaNs incorrectly.
10287 if (!UnsafeFPMath &&
10288 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000010289 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000010290 break;
10291 std::swap(LHS, RHS);
10292 }
Dan Gohman670e5392009-09-21 18:03:22 +000010293 Opcode = X86ISD::FMAX;
10294 break;
10295 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000010296 // Converting this to a max would handle both negative zeros and NaNs
10297 // incorrectly, but we can swap the operands to fix both.
10298 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010299 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010300 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000010301 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010302 Opcode = X86ISD::FMAX;
10303 break;
10304 }
Chris Lattner83e6c992006-10-04 06:57:07 +000010305 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010306
Chris Lattner47b4ce82009-03-11 05:48:52 +000010307 if (Opcode)
10308 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000010309 }
Eric Christopherfd179292009-08-27 18:07:15 +000010310
Chris Lattnerd1980a52009-03-12 06:52:53 +000010311 // If this is a select between two integer constants, try to do some
10312 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000010313 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
10314 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000010315 // Don't do this for crazy integer types.
10316 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
10317 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000010318 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000010319 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000010320
Chris Lattnercee56e72009-03-13 05:53:31 +000010321 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000010322 // Efficiently invertible.
10323 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
10324 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
10325 isa<ConstantSDNode>(Cond.getOperand(1))))) {
10326 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000010327 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000010328 }
Eric Christopherfd179292009-08-27 18:07:15 +000010329
Chris Lattnerd1980a52009-03-12 06:52:53 +000010330 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000010331 if (FalseC->getAPIntValue() == 0 &&
10332 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000010333 if (NeedsCondInvert) // Invert the condition if needed.
10334 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
10335 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010336
Chris Lattnerd1980a52009-03-12 06:52:53 +000010337 // Zero extend the condition if needed.
10338 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010339
Chris Lattnercee56e72009-03-13 05:53:31 +000010340 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000010341 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000010342 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000010343 }
Eric Christopherfd179292009-08-27 18:07:15 +000010344
Chris Lattner97a29a52009-03-13 05:22:11 +000010345 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000010346 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000010347 if (NeedsCondInvert) // Invert the condition if needed.
10348 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
10349 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010350
Chris Lattner97a29a52009-03-13 05:22:11 +000010351 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000010352 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
10353 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000010354 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000010355 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000010356 }
Eric Christopherfd179292009-08-27 18:07:15 +000010357
Chris Lattnercee56e72009-03-13 05:53:31 +000010358 // Optimize cases that will turn into an LEA instruction. This requires
10359 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000010360 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000010361 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000010362 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000010363
Chris Lattnercee56e72009-03-13 05:53:31 +000010364 bool isFastMultiplier = false;
10365 if (Diff < 10) {
10366 switch ((unsigned char)Diff) {
10367 default: break;
10368 case 1: // result = add base, cond
10369 case 2: // result = lea base( , cond*2)
10370 case 3: // result = lea base(cond, cond*2)
10371 case 4: // result = lea base( , cond*4)
10372 case 5: // result = lea base(cond, cond*4)
10373 case 8: // result = lea base( , cond*8)
10374 case 9: // result = lea base(cond, cond*8)
10375 isFastMultiplier = true;
10376 break;
10377 }
10378 }
Eric Christopherfd179292009-08-27 18:07:15 +000010379
Chris Lattnercee56e72009-03-13 05:53:31 +000010380 if (isFastMultiplier) {
10381 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
10382 if (NeedsCondInvert) // Invert the condition if needed.
10383 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
10384 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010385
Chris Lattnercee56e72009-03-13 05:53:31 +000010386 // Zero extend the condition if needed.
10387 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
10388 Cond);
10389 // Scale the condition by the difference.
10390 if (Diff != 1)
10391 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
10392 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010393
Chris Lattnercee56e72009-03-13 05:53:31 +000010394 // Add the base if non-zero.
10395 if (FalseC->getAPIntValue() != 0)
10396 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
10397 SDValue(FalseC, 0));
10398 return Cond;
10399 }
Eric Christopherfd179292009-08-27 18:07:15 +000010400 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000010401 }
10402 }
Eric Christopherfd179292009-08-27 18:07:15 +000010403
Dan Gohman475871a2008-07-27 21:46:04 +000010404 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000010405}
10406
Chris Lattnerd1980a52009-03-12 06:52:53 +000010407/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
10408static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
10409 TargetLowering::DAGCombinerInfo &DCI) {
10410 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000010411
Chris Lattnerd1980a52009-03-12 06:52:53 +000010412 // If the flag operand isn't dead, don't touch this CMOV.
10413 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
10414 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000010415
Chris Lattnerd1980a52009-03-12 06:52:53 +000010416 // If this is a select between two integer constants, try to do some
10417 // optimizations. Note that the operands are ordered the opposite of SELECT
10418 // operands.
10419 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
10420 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
10421 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
10422 // larger than FalseC (the false value).
10423 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
Eric Christopherfd179292009-08-27 18:07:15 +000010424
Chris Lattnerd1980a52009-03-12 06:52:53 +000010425 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
10426 CC = X86::GetOppositeBranchCondition(CC);
10427 std::swap(TrueC, FalseC);
10428 }
Eric Christopherfd179292009-08-27 18:07:15 +000010429
Chris Lattnerd1980a52009-03-12 06:52:53 +000010430 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000010431 // This is efficient for any integer data type (including i8/i16) and
10432 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000010433 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
10434 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000010435 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10436 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010437
Chris Lattnerd1980a52009-03-12 06:52:53 +000010438 // Zero extend the condition if needed.
10439 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010440
Chris Lattnerd1980a52009-03-12 06:52:53 +000010441 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
10442 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000010443 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000010444 if (N->getNumValues() == 2) // Dead flag value?
10445 return DCI.CombineTo(N, Cond, SDValue());
10446 return Cond;
10447 }
Eric Christopherfd179292009-08-27 18:07:15 +000010448
Chris Lattnercee56e72009-03-13 05:53:31 +000010449 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
10450 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000010451 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
10452 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000010453 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10454 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010455
Chris Lattner97a29a52009-03-13 05:22:11 +000010456 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000010457 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
10458 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000010459 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
10460 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000010461
Chris Lattner97a29a52009-03-13 05:22:11 +000010462 if (N->getNumValues() == 2) // Dead flag value?
10463 return DCI.CombineTo(N, Cond, SDValue());
10464 return Cond;
10465 }
Eric Christopherfd179292009-08-27 18:07:15 +000010466
Chris Lattnercee56e72009-03-13 05:53:31 +000010467 // Optimize cases that will turn into an LEA instruction. This requires
10468 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000010469 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000010470 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000010471 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000010472
Chris Lattnercee56e72009-03-13 05:53:31 +000010473 bool isFastMultiplier = false;
10474 if (Diff < 10) {
10475 switch ((unsigned char)Diff) {
10476 default: break;
10477 case 1: // result = add base, cond
10478 case 2: // result = lea base( , cond*2)
10479 case 3: // result = lea base(cond, cond*2)
10480 case 4: // result = lea base( , cond*4)
10481 case 5: // result = lea base(cond, cond*4)
10482 case 8: // result = lea base( , cond*8)
10483 case 9: // result = lea base(cond, cond*8)
10484 isFastMultiplier = true;
10485 break;
10486 }
10487 }
Eric Christopherfd179292009-08-27 18:07:15 +000010488
Chris Lattnercee56e72009-03-13 05:53:31 +000010489 if (isFastMultiplier) {
10490 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
10491 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000010492 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10493 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000010494 // Zero extend the condition if needed.
10495 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
10496 Cond);
10497 // Scale the condition by the difference.
10498 if (Diff != 1)
10499 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
10500 DAG.getConstant(Diff, Cond.getValueType()));
10501
10502 // Add the base if non-zero.
10503 if (FalseC->getAPIntValue() != 0)
10504 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
10505 SDValue(FalseC, 0));
10506 if (N->getNumValues() == 2) // Dead flag value?
10507 return DCI.CombineTo(N, Cond, SDValue());
10508 return Cond;
10509 }
Eric Christopherfd179292009-08-27 18:07:15 +000010510 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000010511 }
10512 }
10513 return SDValue();
10514}
10515
10516
Evan Cheng0b0cd912009-03-28 05:57:29 +000010517/// PerformMulCombine - Optimize a single multiply with constant into two
10518/// in order to implement it with two cheaper instructions, e.g.
10519/// LEA + SHL, LEA + LEA.
10520static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
10521 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000010522 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
10523 return SDValue();
10524
Owen Andersone50ed302009-08-10 22:56:29 +000010525 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010526 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000010527 return SDValue();
10528
10529 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
10530 if (!C)
10531 return SDValue();
10532 uint64_t MulAmt = C->getZExtValue();
10533 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
10534 return SDValue();
10535
10536 uint64_t MulAmt1 = 0;
10537 uint64_t MulAmt2 = 0;
10538 if ((MulAmt % 9) == 0) {
10539 MulAmt1 = 9;
10540 MulAmt2 = MulAmt / 9;
10541 } else if ((MulAmt % 5) == 0) {
10542 MulAmt1 = 5;
10543 MulAmt2 = MulAmt / 5;
10544 } else if ((MulAmt % 3) == 0) {
10545 MulAmt1 = 3;
10546 MulAmt2 = MulAmt / 3;
10547 }
10548 if (MulAmt2 &&
10549 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
10550 DebugLoc DL = N->getDebugLoc();
10551
10552 if (isPowerOf2_64(MulAmt2) &&
10553 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
10554 // If second multiplifer is pow2, issue it first. We want the multiply by
10555 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
10556 // is an add.
10557 std::swap(MulAmt1, MulAmt2);
10558
10559 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000010560 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000010561 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000010562 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000010563 else
Evan Cheng73f24c92009-03-30 21:36:47 +000010564 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000010565 DAG.getConstant(MulAmt1, VT));
10566
Eric Christopherfd179292009-08-27 18:07:15 +000010567 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000010568 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000010569 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000010570 else
Evan Cheng73f24c92009-03-30 21:36:47 +000010571 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000010572 DAG.getConstant(MulAmt2, VT));
10573
10574 // Do not add new nodes to DAG combiner worklist.
10575 DCI.CombineTo(N, NewMul, false);
10576 }
10577 return SDValue();
10578}
10579
Evan Chengad9c0a32009-12-15 00:53:42 +000010580static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
10581 SDValue N0 = N->getOperand(0);
10582 SDValue N1 = N->getOperand(1);
10583 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
10584 EVT VT = N0.getValueType();
10585
10586 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
10587 // since the result of setcc_c is all zero's or all ones.
10588 if (N1C && N0.getOpcode() == ISD::AND &&
10589 N0.getOperand(1).getOpcode() == ISD::Constant) {
10590 SDValue N00 = N0.getOperand(0);
10591 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
10592 ((N00.getOpcode() == ISD::ANY_EXTEND ||
10593 N00.getOpcode() == ISD::ZERO_EXTEND) &&
10594 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
10595 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
10596 APInt ShAmt = N1C->getAPIntValue();
10597 Mask = Mask.shl(ShAmt);
10598 if (Mask != 0)
10599 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
10600 N00, DAG.getConstant(Mask, VT));
10601 }
10602 }
10603
10604 return SDValue();
10605}
Evan Cheng0b0cd912009-03-28 05:57:29 +000010606
Nate Begeman740ab032009-01-26 00:52:55 +000010607/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
10608/// when possible.
10609static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
10610 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000010611 EVT VT = N->getValueType(0);
10612 if (!VT.isVector() && VT.isInteger() &&
10613 N->getOpcode() == ISD::SHL)
10614 return PerformSHLCombine(N, DAG);
10615
Nate Begeman740ab032009-01-26 00:52:55 +000010616 // On X86 with SSE2 support, we can transform this to a vector shift if
10617 // all elements are shifted by the same amount. We can't do this in legalize
10618 // because the a constant vector is typically transformed to a constant pool
10619 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +000010620 if (!Subtarget->hasSSE2())
10621 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000010622
Owen Anderson825b72b2009-08-11 20:47:22 +000010623 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +000010624 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000010625
Mon P Wang3becd092009-01-28 08:12:05 +000010626 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000010627 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000010628 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000010629 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000010630 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
10631 unsigned NumElts = VT.getVectorNumElements();
10632 unsigned i = 0;
10633 for (; i != NumElts; ++i) {
10634 SDValue Arg = ShAmtOp.getOperand(i);
10635 if (Arg.getOpcode() == ISD::UNDEF) continue;
10636 BaseShAmt = Arg;
10637 break;
10638 }
10639 for (; i != NumElts; ++i) {
10640 SDValue Arg = ShAmtOp.getOperand(i);
10641 if (Arg.getOpcode() == ISD::UNDEF) continue;
10642 if (Arg != BaseShAmt) {
10643 return SDValue();
10644 }
10645 }
10646 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000010647 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000010648 SDValue InVec = ShAmtOp.getOperand(0);
10649 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
10650 unsigned NumElts = InVec.getValueType().getVectorNumElements();
10651 unsigned i = 0;
10652 for (; i != NumElts; ++i) {
10653 SDValue Arg = InVec.getOperand(i);
10654 if (Arg.getOpcode() == ISD::UNDEF) continue;
10655 BaseShAmt = Arg;
10656 break;
10657 }
10658 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
10659 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000010660 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000010661 if (C->getZExtValue() == SplatIdx)
10662 BaseShAmt = InVec.getOperand(1);
10663 }
10664 }
10665 if (BaseShAmt.getNode() == 0)
10666 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
10667 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000010668 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000010669 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000010670
Mon P Wangefa42202009-09-03 19:56:25 +000010671 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000010672 if (EltVT.bitsGT(MVT::i32))
10673 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
10674 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000010675 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000010676
Nate Begemanc2fd67f2009-01-26 03:15:31 +000010677 // The shift amount is identical so we can do a vector shift.
10678 SDValue ValOp = N->getOperand(0);
10679 switch (N->getOpcode()) {
10680 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000010681 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000010682 break;
10683 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000010684 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000010685 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010686 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000010687 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000010688 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000010689 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010690 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000010691 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000010692 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000010693 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010694 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000010695 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000010696 break;
10697 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000010698 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000010699 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010700 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000010701 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000010702 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000010703 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010704 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000010705 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000010706 break;
10707 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000010708 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000010709 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010710 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000010711 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000010712 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000010713 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010714 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000010715 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000010716 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000010717 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010718 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000010719 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000010720 break;
Nate Begeman740ab032009-01-26 00:52:55 +000010721 }
10722 return SDValue();
10723}
10724
Evan Cheng760d1942010-01-04 21:22:48 +000010725static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000010726 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000010727 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000010728 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000010729 return SDValue();
10730
Evan Cheng760d1942010-01-04 21:22:48 +000010731 EVT VT = N->getValueType(0);
Evan Cheng8b1190a2010-04-28 01:18:01 +000010732 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
Evan Cheng760d1942010-01-04 21:22:48 +000010733 return SDValue();
10734
10735 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
10736 SDValue N0 = N->getOperand(0);
10737 SDValue N1 = N->getOperand(1);
10738 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
10739 std::swap(N0, N1);
10740 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
10741 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000010742 if (!N0.hasOneUse() || !N1.hasOneUse())
10743 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000010744
10745 SDValue ShAmt0 = N0.getOperand(1);
10746 if (ShAmt0.getValueType() != MVT::i8)
10747 return SDValue();
10748 SDValue ShAmt1 = N1.getOperand(1);
10749 if (ShAmt1.getValueType() != MVT::i8)
10750 return SDValue();
10751 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
10752 ShAmt0 = ShAmt0.getOperand(0);
10753 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
10754 ShAmt1 = ShAmt1.getOperand(0);
10755
10756 DebugLoc DL = N->getDebugLoc();
10757 unsigned Opc = X86ISD::SHLD;
10758 SDValue Op0 = N0.getOperand(0);
10759 SDValue Op1 = N1.getOperand(0);
10760 if (ShAmt0.getOpcode() == ISD::SUB) {
10761 Opc = X86ISD::SHRD;
10762 std::swap(Op0, Op1);
10763 std::swap(ShAmt0, ShAmt1);
10764 }
10765
Evan Cheng8b1190a2010-04-28 01:18:01 +000010766 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000010767 if (ShAmt1.getOpcode() == ISD::SUB) {
10768 SDValue Sum = ShAmt1.getOperand(0);
10769 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000010770 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
10771 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
10772 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
10773 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000010774 return DAG.getNode(Opc, DL, VT,
10775 Op0, Op1,
10776 DAG.getNode(ISD::TRUNCATE, DL,
10777 MVT::i8, ShAmt0));
10778 }
10779 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
10780 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
10781 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000010782 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000010783 return DAG.getNode(Opc, DL, VT,
10784 N0.getOperand(0), N1.getOperand(0),
10785 DAG.getNode(ISD::TRUNCATE, DL,
10786 MVT::i8, ShAmt0));
10787 }
10788
10789 return SDValue();
10790}
10791
Chris Lattner149a4e52008-02-22 02:09:43 +000010792/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000010793static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000010794 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +000010795 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
10796 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000010797 // A preferable solution to the general problem is to figure out the right
10798 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000010799
10800 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +000010801 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +000010802 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +000010803 if (VT.getSizeInBits() != 64)
10804 return SDValue();
10805
Devang Patel578efa92009-06-05 21:57:13 +000010806 const Function *F = DAG.getMachineFunction().getFunction();
10807 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000010808 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +000010809 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000010810 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000010811 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000010812 isa<LoadSDNode>(St->getValue()) &&
10813 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
10814 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000010815 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000010816 LoadSDNode *Ld = 0;
10817 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000010818 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000010819 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000010820 // Must be a store of a load. We currently handle two cases: the load
10821 // is a direct child, and it's under an intervening TokenFactor. It is
10822 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000010823 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000010824 Ld = cast<LoadSDNode>(St->getChain());
10825 else if (St->getValue().hasOneUse() &&
10826 ChainVal->getOpcode() == ISD::TokenFactor) {
10827 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000010828 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000010829 TokenFactorIndex = i;
10830 Ld = cast<LoadSDNode>(St->getValue());
10831 } else
10832 Ops.push_back(ChainVal->getOperand(i));
10833 }
10834 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000010835
Evan Cheng536e6672009-03-12 05:59:15 +000010836 if (!Ld || !ISD::isNormalLoad(Ld))
10837 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000010838
Evan Cheng536e6672009-03-12 05:59:15 +000010839 // If this is not the MMX case, i.e. we are just turning i64 load/store
10840 // into f64 load/store, avoid the transformation if there are multiple
10841 // uses of the loaded value.
10842 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
10843 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000010844
Evan Cheng536e6672009-03-12 05:59:15 +000010845 DebugLoc LdDL = Ld->getDebugLoc();
10846 DebugLoc StDL = N->getDebugLoc();
10847 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
10848 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
10849 // pair instead.
10850 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000010851 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000010852 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
10853 Ld->getPointerInfo(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000010854 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000010855 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000010856 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000010857 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000010858 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000010859 Ops.size());
10860 }
Evan Cheng536e6672009-03-12 05:59:15 +000010861 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000010862 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000010863 St->isVolatile(), St->isNonTemporal(),
10864 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000010865 }
Evan Cheng536e6672009-03-12 05:59:15 +000010866
10867 // Otherwise, lower to two pairs of 32-bit loads / stores.
10868 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000010869 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
10870 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000010871
Owen Anderson825b72b2009-08-11 20:47:22 +000010872 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000010873 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000010874 Ld->isVolatile(), Ld->isNonTemporal(),
10875 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000010876 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000010877 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000010878 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000010879 MinAlign(Ld->getAlignment(), 4));
10880
10881 SDValue NewChain = LoLd.getValue(1);
10882 if (TokenFactorIndex != -1) {
10883 Ops.push_back(LoLd);
10884 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000010885 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000010886 Ops.size());
10887 }
10888
10889 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000010890 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
10891 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000010892
10893 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000010894 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000010895 St->isVolatile(), St->isNonTemporal(),
10896 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000010897 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000010898 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000010899 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000010900 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000010901 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000010902 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000010903 }
Dan Gohman475871a2008-07-27 21:46:04 +000010904 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000010905}
10906
Chris Lattner6cf73262008-01-25 06:14:17 +000010907/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
10908/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000010909static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000010910 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
10911 // F[X]OR(0.0, x) -> x
10912 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000010913 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
10914 if (C->getValueAPF().isPosZero())
10915 return N->getOperand(1);
10916 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
10917 if (C->getValueAPF().isPosZero())
10918 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000010919 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000010920}
10921
10922/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000010923static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000010924 // FAND(0.0, x) -> 0.0
10925 // FAND(x, 0.0) -> 0.0
10926 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
10927 if (C->getValueAPF().isPosZero())
10928 return N->getOperand(0);
10929 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
10930 if (C->getValueAPF().isPosZero())
10931 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000010932 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000010933}
10934
Dan Gohmane5af2d32009-01-29 01:59:02 +000010935static SDValue PerformBTCombine(SDNode *N,
10936 SelectionDAG &DAG,
10937 TargetLowering::DAGCombinerInfo &DCI) {
10938 // BT ignores high bits in the bit index operand.
10939 SDValue Op1 = N->getOperand(1);
10940 if (Op1.hasOneUse()) {
10941 unsigned BitWidth = Op1.getValueSizeInBits();
10942 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
10943 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000010944 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
10945 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000010946 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000010947 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
10948 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
10949 DCI.CommitTargetLoweringOpt(TLO);
10950 }
10951 return SDValue();
10952}
Chris Lattner83e6c992006-10-04 06:57:07 +000010953
Eli Friedman7a5e5552009-06-07 06:52:44 +000010954static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
10955 SDValue Op = N->getOperand(0);
10956 if (Op.getOpcode() == ISD::BIT_CONVERT)
10957 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000010958 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000010959 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000010960 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000010961 OpVT.getVectorElementType().getSizeInBits()) {
10962 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT, Op);
10963 }
10964 return SDValue();
10965}
10966
Evan Cheng2e489c42009-12-16 00:53:11 +000010967static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
10968 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
10969 // (and (i32 x86isd::setcc_carry), 1)
10970 // This eliminates the zext. This transformation is necessary because
10971 // ISD::SETCC is always legalized to i8.
10972 DebugLoc dl = N->getDebugLoc();
10973 SDValue N0 = N->getOperand(0);
10974 EVT VT = N->getValueType(0);
10975 if (N0.getOpcode() == ISD::AND &&
10976 N0.hasOneUse() &&
10977 N0.getOperand(0).hasOneUse()) {
10978 SDValue N00 = N0.getOperand(0);
10979 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
10980 return SDValue();
10981 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
10982 if (!C || C->getZExtValue() != 1)
10983 return SDValue();
10984 return DAG.getNode(ISD::AND, dl, VT,
10985 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
10986 N00.getOperand(0), N00.getOperand(1)),
10987 DAG.getConstant(1, VT));
10988 }
10989
10990 return SDValue();
10991}
10992
Dan Gohman475871a2008-07-27 21:46:04 +000010993SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000010994 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000010995 SelectionDAG &DAG = DCI.DAG;
10996 switch (N->getOpcode()) {
10997 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010998 case ISD::EXTRACT_VECTOR_ELT:
10999 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +000011000 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000011001 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000011002 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000011003 case ISD::SHL:
11004 case ISD::SRA:
11005 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000011006 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000011007 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000011008 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000011009 case X86ISD::FOR: return PerformFORCombine(N, DAG);
11010 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000011011 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000011012 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000011013 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000011014 case X86ISD::SHUFPS: // Handle all target specific shuffles
11015 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000011016 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000011017 case X86ISD::PUNPCKHBW:
11018 case X86ISD::PUNPCKHWD:
11019 case X86ISD::PUNPCKHDQ:
11020 case X86ISD::PUNPCKHQDQ:
11021 case X86ISD::UNPCKHPS:
11022 case X86ISD::UNPCKHPD:
11023 case X86ISD::PUNPCKLBW:
11024 case X86ISD::PUNPCKLWD:
11025 case X86ISD::PUNPCKLDQ:
11026 case X86ISD::PUNPCKLQDQ:
11027 case X86ISD::UNPCKLPS:
11028 case X86ISD::UNPCKLPD:
11029 case X86ISD::MOVHLPS:
11030 case X86ISD::MOVLHPS:
11031 case X86ISD::PSHUFD:
11032 case X86ISD::PSHUFHW:
11033 case X86ISD::PSHUFLW:
11034 case X86ISD::MOVSS:
11035 case X86ISD::MOVSD:
11036 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
Evan Cheng206ee9d2006-07-07 08:33:52 +000011037 }
11038
Dan Gohman475871a2008-07-27 21:46:04 +000011039 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000011040}
11041
Evan Chenge5b51ac2010-04-17 06:13:15 +000011042/// isTypeDesirableForOp - Return true if the target has native support for
11043/// the specified value type and it is 'desirable' to use the type for the
11044/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
11045/// instruction encodings are longer and some i16 instructions are slow.
11046bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
11047 if (!isTypeLegal(VT))
11048 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000011049 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000011050 return true;
11051
11052 switch (Opc) {
11053 default:
11054 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000011055 case ISD::LOAD:
11056 case ISD::SIGN_EXTEND:
11057 case ISD::ZERO_EXTEND:
11058 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000011059 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000011060 case ISD::SRL:
11061 case ISD::SUB:
11062 case ISD::ADD:
11063 case ISD::MUL:
11064 case ISD::AND:
11065 case ISD::OR:
11066 case ISD::XOR:
11067 return false;
11068 }
11069}
11070
11071/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000011072/// beneficial for dag combiner to promote the specified node. If true, it
11073/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000011074bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000011075 EVT VT = Op.getValueType();
11076 if (VT != MVT::i16)
11077 return false;
11078
Evan Cheng4c26e932010-04-19 19:29:22 +000011079 bool Promote = false;
11080 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000011081 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000011082 default: break;
11083 case ISD::LOAD: {
11084 LoadSDNode *LD = cast<LoadSDNode>(Op);
11085 // If the non-extending load has a single use and it's not live out, then it
11086 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000011087 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
11088 Op.hasOneUse()*/) {
11089 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
11090 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
11091 // The only case where we'd want to promote LOAD (rather then it being
11092 // promoted as an operand is when it's only use is liveout.
11093 if (UI->getOpcode() != ISD::CopyToReg)
11094 return false;
11095 }
11096 }
Evan Cheng4c26e932010-04-19 19:29:22 +000011097 Promote = true;
11098 break;
11099 }
11100 case ISD::SIGN_EXTEND:
11101 case ISD::ZERO_EXTEND:
11102 case ISD::ANY_EXTEND:
11103 Promote = true;
11104 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000011105 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000011106 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000011107 SDValue N0 = Op.getOperand(0);
11108 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000011109 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000011110 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000011111 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000011112 break;
11113 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000011114 case ISD::ADD:
11115 case ISD::MUL:
11116 case ISD::AND:
11117 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000011118 case ISD::XOR:
11119 Commute = true;
11120 // fallthrough
11121 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000011122 SDValue N0 = Op.getOperand(0);
11123 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000011124 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000011125 return false;
11126 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000011127 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000011128 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000011129 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000011130 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000011131 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000011132 }
11133 }
11134
11135 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000011136 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000011137}
11138
Evan Cheng60c07e12006-07-05 22:17:51 +000011139//===----------------------------------------------------------------------===//
11140// X86 Inline Assembly Support
11141//===----------------------------------------------------------------------===//
11142
Chris Lattnerb8105652009-07-20 17:51:36 +000011143static bool LowerToBSwap(CallInst *CI) {
11144 // FIXME: this should verify that we are targetting a 486 or better. If not,
11145 // we will turn this bswap into something that will be lowered to logical ops
11146 // instead of emitting the bswap asm. For now, we don't support 486 or lower
11147 // so don't worry about this.
Eric Christopherfd179292009-08-27 18:07:15 +000011148
Chris Lattnerb8105652009-07-20 17:51:36 +000011149 // Verify this is a simple bswap.
Gabor Greife1c2b9c2010-06-30 13:03:37 +000011150 if (CI->getNumArgOperands() != 1 ||
Gabor Greif1cfe44a2010-06-26 11:51:52 +000011151 CI->getType() != CI->getArgOperand(0)->getType() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011152 !CI->getType()->isIntegerTy())
Chris Lattnerb8105652009-07-20 17:51:36 +000011153 return false;
Eric Christopherfd179292009-08-27 18:07:15 +000011154
Chris Lattnerb8105652009-07-20 17:51:36 +000011155 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
11156 if (!Ty || Ty->getBitWidth() % 16 != 0)
11157 return false;
Eric Christopherfd179292009-08-27 18:07:15 +000011158
Chris Lattnerb8105652009-07-20 17:51:36 +000011159 // Okay, we can do this xform, do so now.
11160 const Type *Tys[] = { Ty };
11161 Module *M = CI->getParent()->getParent()->getParent();
11162 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
Eric Christopherfd179292009-08-27 18:07:15 +000011163
Gabor Greif1cfe44a2010-06-26 11:51:52 +000011164 Value *Op = CI->getArgOperand(0);
Chris Lattnerb8105652009-07-20 17:51:36 +000011165 Op = CallInst::Create(Int, Op, CI->getName(), CI);
Eric Christopherfd179292009-08-27 18:07:15 +000011166
Chris Lattnerb8105652009-07-20 17:51:36 +000011167 CI->replaceAllUsesWith(Op);
11168 CI->eraseFromParent();
11169 return true;
11170}
11171
11172bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
11173 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
11174 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
11175
11176 std::string AsmStr = IA->getAsmString();
11177
11178 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000011179 SmallVector<StringRef, 4> AsmPieces;
Chris Lattnerb8105652009-07-20 17:51:36 +000011180 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
11181
11182 switch (AsmPieces.size()) {
11183 default: return false;
11184 case 1:
11185 AsmStr = AsmPieces[0];
11186 AsmPieces.clear();
11187 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
11188
11189 // bswap $0
11190 if (AsmPieces.size() == 2 &&
11191 (AsmPieces[0] == "bswap" ||
11192 AsmPieces[0] == "bswapq" ||
11193 AsmPieces[0] == "bswapl") &&
11194 (AsmPieces[1] == "$0" ||
11195 AsmPieces[1] == "${0:q}")) {
11196 // No need to check constraints, nothing other than the equivalent of
11197 // "=r,0" would be valid here.
11198 return LowerToBSwap(CI);
11199 }
11200 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011201 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000011202 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000011203 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000011204 AsmPieces[1] == "$$8," &&
11205 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000011206 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
11207 AsmPieces.clear();
Benjamin Kramer018cbd52010-03-12 13:54:59 +000011208 const std::string &Constraints = IA->getConstraintString();
11209 SplitString(StringRef(Constraints).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000011210 std::sort(AsmPieces.begin(), AsmPieces.end());
11211 if (AsmPieces.size() == 4 &&
11212 AsmPieces[0] == "~{cc}" &&
11213 AsmPieces[1] == "~{dirflag}" &&
11214 AsmPieces[2] == "~{flags}" &&
11215 AsmPieces[3] == "~{fpsr}") {
11216 return LowerToBSwap(CI);
11217 }
Chris Lattnerb8105652009-07-20 17:51:36 +000011218 }
11219 break;
11220 case 3:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011221 if (CI->getType()->isIntegerTy(64) &&
Owen Anderson1d0be152009-08-13 21:58:54 +000011222 Constraints.size() >= 2 &&
Chris Lattnerb8105652009-07-20 17:51:36 +000011223 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
11224 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
11225 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramerd4f19592010-01-11 18:03:24 +000011226 SmallVector<StringRef, 4> Words;
Chris Lattnerb8105652009-07-20 17:51:36 +000011227 SplitString(AsmPieces[0], Words, " \t");
11228 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
11229 Words.clear();
11230 SplitString(AsmPieces[1], Words, " \t");
11231 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
11232 Words.clear();
11233 SplitString(AsmPieces[2], Words, " \t,");
11234 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
11235 Words[2] == "%edx") {
11236 return LowerToBSwap(CI);
11237 }
11238 }
11239 }
11240 }
11241 break;
11242 }
11243 return false;
11244}
11245
11246
11247
Chris Lattnerf4dff842006-07-11 02:54:03 +000011248/// getConstraintType - Given a constraint letter, return the type of
11249/// constraint it is for this target.
11250X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000011251X86TargetLowering::getConstraintType(const std::string &Constraint) const {
11252 if (Constraint.size() == 1) {
11253 switch (Constraint[0]) {
11254 case 'A':
Dale Johannesen330169f2008-11-13 21:52:36 +000011255 return C_Register;
Chris Lattnerfce84ac2008-03-11 19:06:29 +000011256 case 'f':
Chris Lattner4234f572007-03-25 02:14:49 +000011257 case 'r':
11258 case 'R':
11259 case 'l':
11260 case 'q':
11261 case 'Q':
11262 case 'x':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000011263 case 'y':
Chris Lattner4234f572007-03-25 02:14:49 +000011264 case 'Y':
11265 return C_RegisterClass;
Dale Johannesen78e3e522009-02-12 20:58:09 +000011266 case 'e':
11267 case 'Z':
11268 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000011269 default:
11270 break;
11271 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000011272 }
Chris Lattner4234f572007-03-25 02:14:49 +000011273 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000011274}
11275
John Thompsoneac6e1d2010-09-13 18:15:37 +000011276/// Examine constraint type and operand type and determine a weight value,
11277/// where: -1 = invalid match, and 0 = so-so match to 3 = good match.
11278/// This object must already have been set up with the operand type
11279/// and the current alternative constraint selected.
11280int X86TargetLowering::getSingleConstraintMatchWeight(
11281 AsmOperandInfo &info, const char *constraint) const {
11282 int weight = -1;
11283 Value *CallOperandVal = info.CallOperandVal;
11284 // If we don't have a value, we can't do a match,
11285 // but allow it at the lowest weight.
11286 if (CallOperandVal == NULL)
11287 return 0;
11288 // Look at the constraint type.
11289 switch (*constraint) {
11290 default:
11291 return TargetLowering::getSingleConstraintMatchWeight(info, constraint);
11292 break;
11293 case 'I':
11294 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
11295 if (C->getZExtValue() <= 31)
11296 weight = 3;
11297 }
11298 break;
11299 // etc.
11300 }
11301 return weight;
11302}
11303
Dale Johannesenba2a0b92008-01-29 02:21:21 +000011304/// LowerXConstraint - try to replace an X constraint, which matches anything,
11305/// with another that has more specific requirements based on the type of the
11306/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000011307const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000011308LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000011309 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
11310 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000011311 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesenba2a0b92008-01-29 02:21:21 +000011312 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +000011313 return "Y";
11314 if (Subtarget->hasSSE1())
11315 return "x";
11316 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011317
Chris Lattner5e764232008-04-26 23:02:14 +000011318 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000011319}
11320
Chris Lattner48884cd2007-08-25 00:47:38 +000011321/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
11322/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000011323void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +000011324 char Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000011325 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000011326 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000011327 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000011328
Chris Lattner22aaf1d2006-10-31 20:13:11 +000011329 switch (Constraint) {
11330 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000011331 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000011332 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000011333 if (C->getZExtValue() <= 31) {
11334 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000011335 break;
11336 }
Devang Patel84f7fd22007-03-17 00:13:28 +000011337 }
Chris Lattner48884cd2007-08-25 00:47:38 +000011338 return;
Evan Cheng364091e2008-09-22 23:57:37 +000011339 case 'J':
11340 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000011341 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000011342 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
11343 break;
11344 }
11345 }
11346 return;
11347 case 'K':
11348 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000011349 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000011350 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
11351 break;
11352 }
11353 }
11354 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000011355 case 'N':
11356 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000011357 if (C->getZExtValue() <= 255) {
11358 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000011359 break;
11360 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000011361 }
Chris Lattner48884cd2007-08-25 00:47:38 +000011362 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000011363 case 'e': {
11364 // 32-bit signed value
11365 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000011366 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
11367 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000011368 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000011369 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000011370 break;
11371 }
11372 // FIXME gcc accepts some relocatable values here too, but only in certain
11373 // memory models; it's complicated.
11374 }
11375 return;
11376 }
11377 case 'Z': {
11378 // 32-bit unsigned value
11379 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000011380 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
11381 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000011382 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
11383 break;
11384 }
11385 }
11386 // FIXME gcc accepts some relocatable values here too, but only in certain
11387 // memory models; it's complicated.
11388 return;
11389 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000011390 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000011391 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000011392 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000011393 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000011394 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000011395 break;
11396 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011397
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000011398 // In any sort of PIC mode addresses need to be computed at runtime by
11399 // adding in a register or some sort of table lookup. These can't
11400 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000011401 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000011402 return;
11403
Chris Lattnerdc43a882007-05-03 16:52:29 +000011404 // If we are in non-pic codegen mode, we allow the address of a global (with
11405 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000011406 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000011407 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000011408
Chris Lattner49921962009-05-08 18:23:14 +000011409 // Match either (GA), (GA+C), (GA+C1+C2), etc.
11410 while (1) {
11411 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
11412 Offset += GA->getOffset();
11413 break;
11414 } else if (Op.getOpcode() == ISD::ADD) {
11415 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
11416 Offset += C->getZExtValue();
11417 Op = Op.getOperand(0);
11418 continue;
11419 }
11420 } else if (Op.getOpcode() == ISD::SUB) {
11421 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
11422 Offset += -C->getZExtValue();
11423 Op = Op.getOperand(0);
11424 continue;
11425 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000011426 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000011427
Chris Lattner49921962009-05-08 18:23:14 +000011428 // Otherwise, this isn't something we can handle, reject it.
11429 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000011430 }
Eric Christopherfd179292009-08-27 18:07:15 +000011431
Dan Gohman46510a72010-04-15 01:51:59 +000011432 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000011433 // If we require an extra load to get this address, as in PIC mode, we
11434 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000011435 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
11436 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000011437 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000011438
Devang Patel0d881da2010-07-06 22:08:15 +000011439 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
11440 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000011441 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000011442 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000011443 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011444
Gabor Greifba36cb52008-08-28 21:40:38 +000011445 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000011446 Ops.push_back(Result);
11447 return;
11448 }
Dale Johannesen1784d162010-06-25 21:55:36 +000011449 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000011450}
11451
Chris Lattner259e97c2006-01-31 19:43:35 +000011452std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +000011453getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000011454 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +000011455 if (Constraint.size() == 1) {
11456 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +000011457 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +000011458 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +000011459 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
11460 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011461 if (VT == MVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +000011462 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
11463 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
11464 X86::R10D,X86::R11D,X86::R12D,
11465 X86::R13D,X86::R14D,X86::R15D,
11466 X86::EBP, X86::ESP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011467 else if (VT == MVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +000011468 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
11469 X86::SI, X86::DI, X86::R8W,X86::R9W,
11470 X86::R10W,X86::R11W,X86::R12W,
11471 X86::R13W,X86::R14W,X86::R15W,
11472 X86::BP, X86::SP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011473 else if (VT == MVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +000011474 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
11475 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
11476 X86::R10B,X86::R11B,X86::R12B,
11477 X86::R13B,X86::R14B,X86::R15B,
11478 X86::BPL, X86::SPL, 0);
11479
Owen Anderson825b72b2009-08-11 20:47:22 +000011480 else if (VT == MVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +000011481 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
11482 X86::RSI, X86::RDI, X86::R8, X86::R9,
11483 X86::R10, X86::R11, X86::R12,
11484 X86::R13, X86::R14, X86::R15,
11485 X86::RBP, X86::RSP, 0);
11486
11487 break;
11488 }
Eric Christopherfd179292009-08-27 18:07:15 +000011489 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +000011490 case 'Q': // Q_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000011491 if (VT == MVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000011492 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011493 else if (VT == MVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000011494 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011495 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +000011496 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011497 else if (VT == MVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +000011498 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
11499 break;
Chris Lattner259e97c2006-01-31 19:43:35 +000011500 }
11501 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011502
Chris Lattner1efa40f2006-02-22 00:56:39 +000011503 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +000011504}
Chris Lattnerf76d1802006-07-31 23:26:50 +000011505
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011506std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000011507X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000011508 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000011509 // First, see if this is a constraint that directly corresponds to an LLVM
11510 // register class.
11511 if (Constraint.size() == 1) {
11512 // GCC Constraint Letters
11513 switch (Constraint[0]) {
11514 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000011515 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000011516 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000011517 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +000011518 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000011519 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000011520 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000011521 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000011522 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000011523 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000011524 case 'R': // LEGACY_REGS
11525 if (VT == MVT::i8)
11526 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
11527 if (VT == MVT::i16)
11528 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
11529 if (VT == MVT::i32 || !Subtarget->is64Bit())
11530 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
11531 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000011532 case 'f': // FP Stack registers.
11533 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
11534 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000011535 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000011536 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000011537 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000011538 return std::make_pair(0U, X86::RFP64RegisterClass);
11539 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000011540 case 'y': // MMX_REGS if MMX allowed.
11541 if (!Subtarget->hasMMX()) break;
11542 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000011543 case 'Y': // SSE_REGS if SSE2 allowed
11544 if (!Subtarget->hasSSE2()) break;
11545 // FALL THROUGH.
11546 case 'x': // SSE_REGS if SSE1 allowed
11547 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000011548
Owen Anderson825b72b2009-08-11 20:47:22 +000011549 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000011550 default: break;
11551 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000011552 case MVT::f32:
11553 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000011554 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000011555 case MVT::f64:
11556 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000011557 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000011558 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000011559 case MVT::v16i8:
11560 case MVT::v8i16:
11561 case MVT::v4i32:
11562 case MVT::v2i64:
11563 case MVT::v4f32:
11564 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000011565 return std::make_pair(0U, X86::VR128RegisterClass);
11566 }
Chris Lattnerad043e82007-04-09 05:11:28 +000011567 break;
11568 }
11569 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011570
Chris Lattnerf76d1802006-07-31 23:26:50 +000011571 // Use the default implementation in TargetLowering to convert the register
11572 // constraint into a member of a register class.
11573 std::pair<unsigned, const TargetRegisterClass*> Res;
11574 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000011575
11576 // Not found as a standard register?
11577 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000011578 // Map st(0) -> st(7) -> ST0
11579 if (Constraint.size() == 7 && Constraint[0] == '{' &&
11580 tolower(Constraint[1]) == 's' &&
11581 tolower(Constraint[2]) == 't' &&
11582 Constraint[3] == '(' &&
11583 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
11584 Constraint[5] == ')' &&
11585 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000011586
Chris Lattner56d77c72009-09-13 22:41:48 +000011587 Res.first = X86::ST0+Constraint[4]-'0';
11588 Res.second = X86::RFP80RegisterClass;
11589 return Res;
11590 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000011591
Chris Lattner56d77c72009-09-13 22:41:48 +000011592 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000011593 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000011594 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000011595 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000011596 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000011597 }
Chris Lattner56d77c72009-09-13 22:41:48 +000011598
11599 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000011600 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000011601 Res.first = X86::EFLAGS;
11602 Res.second = X86::CCRRegisterClass;
11603 return Res;
11604 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000011605
Dale Johannesen330169f2008-11-13 21:52:36 +000011606 // 'A' means EAX + EDX.
11607 if (Constraint == "A") {
11608 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000011609 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000011610 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000011611 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000011612 return Res;
11613 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011614
Chris Lattnerf76d1802006-07-31 23:26:50 +000011615 // Otherwise, check to see if this is a register class of the wrong value
11616 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
11617 // turn into {ax},{dx}.
11618 if (Res.second->hasType(VT))
11619 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011620
Chris Lattnerf76d1802006-07-31 23:26:50 +000011621 // All of the single-register GCC register classes map their values onto
11622 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
11623 // really want an 8-bit or 32-bit register, map to the appropriate register
11624 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000011625 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011626 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000011627 unsigned DestReg = 0;
11628 switch (Res.first) {
11629 default: break;
11630 case X86::AX: DestReg = X86::AL; break;
11631 case X86::DX: DestReg = X86::DL; break;
11632 case X86::CX: DestReg = X86::CL; break;
11633 case X86::BX: DestReg = X86::BL; break;
11634 }
11635 if (DestReg) {
11636 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000011637 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000011638 }
Owen Anderson825b72b2009-08-11 20:47:22 +000011639 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000011640 unsigned DestReg = 0;
11641 switch (Res.first) {
11642 default: break;
11643 case X86::AX: DestReg = X86::EAX; break;
11644 case X86::DX: DestReg = X86::EDX; break;
11645 case X86::CX: DestReg = X86::ECX; break;
11646 case X86::BX: DestReg = X86::EBX; break;
11647 case X86::SI: DestReg = X86::ESI; break;
11648 case X86::DI: DestReg = X86::EDI; break;
11649 case X86::BP: DestReg = X86::EBP; break;
11650 case X86::SP: DestReg = X86::ESP; break;
11651 }
11652 if (DestReg) {
11653 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000011654 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000011655 }
Owen Anderson825b72b2009-08-11 20:47:22 +000011656 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000011657 unsigned DestReg = 0;
11658 switch (Res.first) {
11659 default: break;
11660 case X86::AX: DestReg = X86::RAX; break;
11661 case X86::DX: DestReg = X86::RDX; break;
11662 case X86::CX: DestReg = X86::RCX; break;
11663 case X86::BX: DestReg = X86::RBX; break;
11664 case X86::SI: DestReg = X86::RSI; break;
11665 case X86::DI: DestReg = X86::RDI; break;
11666 case X86::BP: DestReg = X86::RBP; break;
11667 case X86::SP: DestReg = X86::RSP; break;
11668 }
11669 if (DestReg) {
11670 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000011671 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000011672 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000011673 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000011674 } else if (Res.second == X86::FR32RegisterClass ||
11675 Res.second == X86::FR64RegisterClass ||
11676 Res.second == X86::VR128RegisterClass) {
11677 // Handle references to XMM physical registers that got mapped into the
11678 // wrong class. This can happen with constraints like {xmm0} where the
11679 // target independent register mapper will just pick the first match it can
11680 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000011681 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000011682 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000011683 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000011684 Res.second = X86::FR64RegisterClass;
11685 else if (X86::VR128RegisterClass->hasType(VT))
11686 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000011687 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011688
Chris Lattnerf76d1802006-07-31 23:26:50 +000011689 return Res;
11690}