blob: 7da9ad6da2116e662db5a4ddd7dc87ab90b3e448 [file] [log] [blame]
Arnold Schwaighofer92226dd2007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Evan Chengb1712452010-01-27 06:25:16 +000015#define DEBUG_TYPE "x86-isel"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000016#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000017#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000018#include "X86ISelLowering.h"
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000019#include "X86ShuffleDecode.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000020#include "X86TargetMachine.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000021#include "X86TargetObjectFile.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000022#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000023#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000024#include "llvm/DerivedTypes.h"
Chris Lattnerb903bed2009-06-26 21:20:29 +000025#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000026#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000027#include "llvm/Function.h"
Chris Lattnerb8105652009-07-20 17:51:36 +000028#include "llvm/Instructions.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000029#include "llvm/Intrinsics.h"
Owen Andersona90b3dc2009-07-15 21:51:10 +000030#include "llvm/LLVMContext.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000031#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000032#include "llvm/CodeGen/MachineFunction.h"
33#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner5e1df8d2010-01-25 23:38:14 +000034#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000035#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000036#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000037#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner589c6f62010-01-26 06:28:43 +000038#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000039#include "llvm/MC/MCContext.h"
Daniel Dunbar4e815f82010-03-15 23:51:06 +000040#include "llvm/MC/MCExpr.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000041#include "llvm/MC/MCSymbol.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000042#include "llvm/ADT/BitVector.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000043#include "llvm/ADT/SmallSet.h"
Evan Chengb1712452010-01-27 06:25:16 +000044#include "llvm/ADT/Statistic.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000045#include "llvm/ADT/StringExtras.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000046#include "llvm/ADT/VectorExtras.h"
Mon P Wang3c81d352008-11-23 04:37:22 +000047#include "llvm/Support/CommandLine.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000048#include "llvm/Support/Debug.h"
Bill Wendlingec041eb2010-03-12 19:20:40 +000049#include "llvm/Support/Dwarf.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000050#include "llvm/Support/ErrorHandling.h"
51#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000052#include "llvm/Support/raw_ostream.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000053using namespace llvm;
Bill Wendlingec041eb2010-03-12 19:20:40 +000054using namespace dwarf;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000055
Evan Chengb1712452010-01-27 06:25:16 +000056STATISTIC(NumTailCalls, "Number of tail calls");
57
Mon P Wang3c81d352008-11-23 04:37:22 +000058static cl::opt<bool>
Mon P Wang9f22a4a2008-11-24 02:10:43 +000059DisableMMX("disable-mmx", cl::Hidden, cl::desc("Disable use of MMX"));
Mon P Wang3c81d352008-11-23 04:37:22 +000060
Evan Cheng10e86422008-04-25 19:11:04 +000061// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000062static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000063 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000064
Chris Lattnerf0144122009-07-28 03:13:23 +000065static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
Eric Christopher62f35a22010-07-05 19:26:33 +000066
67 bool is64Bit = TM.getSubtarget<X86Subtarget>().is64Bit();
68
69 if (TM.getSubtarget<X86Subtarget>().isTargetDarwin()) {
70 if (is64Bit) return new X8664_MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +000071 return new TargetLoweringObjectFileMachO();
Eric Christopher62f35a22010-07-05 19:26:33 +000072 } else if (TM.getSubtarget<X86Subtarget>().isTargetELF() ){
73 if (is64Bit) return new X8664_ELFTargetObjectFile(TM);
Anton Korobeynikov9184b252010-02-15 22:35:59 +000074 return new X8632_ELFTargetObjectFile(TM);
Eric Christopher62f35a22010-07-05 19:26:33 +000075 } else if (TM.getSubtarget<X86Subtarget>().isTargetCOFF()) {
Chris Lattnerf0144122009-07-28 03:13:23 +000076 return new TargetLoweringObjectFileCOFF();
Eric Christopher62f35a22010-07-05 19:26:33 +000077 }
78 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +000079}
80
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +000081X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +000082 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +000083 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesenf1fc3a82007-09-23 14:52:20 +000084 X86ScalarSSEf64 = Subtarget->hasSSE2();
85 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng25ab6902006-09-08 06:48:29 +000086 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000087
Anton Korobeynikov2365f512007-07-14 14:06:15 +000088 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000089 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000090
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000091 // Set up the TargetLowering object.
92
93 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Owen Anderson825b72b2009-08-11 20:47:22 +000094 setShiftAmountType(MVT::i8);
Duncan Sands03228082008-11-23 15:47:28 +000095 setBooleanContents(ZeroOrOneBooleanContent);
Evan Cheng211ffa12010-05-19 20:19:50 +000096 setSchedulingPreference(Sched::RegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +000097 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +000098
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000099 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000100 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000101 setUseUnderscoreSetJmp(false);
102 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000103 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000104 // MS runtime is weird: it exports _setjmp, but longjmp!
105 setUseUnderscoreSetJmp(true);
106 setUseUnderscoreLongJmp(false);
107 } else {
108 setUseUnderscoreSetJmp(true);
109 setUseUnderscoreLongJmp(true);
110 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000111
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000112 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000113 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman71edb242010-04-30 18:30:26 +0000114 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000115 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000116 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000117 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000118
Owen Anderson825b72b2009-08-11 20:47:22 +0000119 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000120
Scott Michelfdc40a02009-02-17 22:15:04 +0000121 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000122 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000123 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000124 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000125 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000126 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
127 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000128
129 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000130 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
131 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
132 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
133 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
134 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
135 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000136
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000137 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
138 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000139 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
140 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
141 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000142
Evan Cheng25ab6902006-09-08 06:48:29 +0000143 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000144 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
145 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000146 } else if (!UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000147 // We have an algorithm for SSE2->double, and we turn this into a
148 // 64-bit FILD followed by conditional FADD for other targets.
149 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000150 // We have an algorithm for SSE2, and we turn this into a 64-bit
151 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000152 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000153 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000154
155 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
156 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000157 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
158 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000159
Devang Patel6a784892009-06-05 18:48:29 +0000160 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000161 // SSE has no i16 to fp conversion, only i32
162 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000163 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000164 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000165 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000166 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000167 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
168 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000169 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000170 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000171 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
172 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000173 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000174
Dale Johannesen73328d12007-09-19 23:55:34 +0000175 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
176 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000177 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
178 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000179
Evan Cheng02568ff2006-01-30 22:13:22 +0000180 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
181 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000182 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
183 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000184
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000185 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000186 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000187 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000188 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000189 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000190 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
191 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000192 }
193
194 // Handle FP_TO_UINT by promoting the destination to a larger signed
195 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000196 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
197 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
198 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000199
Evan Cheng25ab6902006-09-08 06:48:29 +0000200 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000201 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
202 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000203 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000204 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000205 // Expand FP_TO_UINT into a select.
206 // FIXME: We would like to use a Custom expander here eventually to do
207 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000208 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000209 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000210 // With SSE3 we can use fisttpll to convert to a signed i64; without
211 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000212 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000213 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000214
Chris Lattner399610a2006-12-05 18:22:22 +0000215 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesenacbf6342010-05-21 18:44:47 +0000216 if (!X86ScalarSSEf64) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000217 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
218 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000219 if (Subtarget->is64Bit()) {
Dale Johannesen7d07b482010-05-21 00:52:33 +0000220 setOperationAction(ISD::BIT_CONVERT , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000221 // Without SSE, i64->f64 goes through memory.
222 setOperationAction(ISD::BIT_CONVERT , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000223 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000224 }
Chris Lattner21f66852005-12-23 05:15:23 +0000225
Dan Gohmanb00ee212008-02-18 19:34:53 +0000226 // Scalar integer divide and remainder are lowered to use operations that
227 // produce two results, to match the available instructions. This exposes
228 // the two-result form to trivial CSE, which is able to combine x/y and x%y
229 // into a single instruction.
230 //
231 // Scalar integer multiply-high is also lowered to use two-result
232 // operations, to match the available instructions. However, plain multiply
233 // (low) operations are left as Legal, as there are single-result
234 // instructions for this in x86. Using the two-result multiply instructions
235 // when both high and low results are needed must be arranged by dagcombine.
Owen Anderson825b72b2009-08-11 20:47:22 +0000236 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
237 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
238 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
239 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
240 setOperationAction(ISD::SREM , MVT::i8 , Expand);
241 setOperationAction(ISD::UREM , MVT::i8 , Expand);
242 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
243 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
244 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
245 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
246 setOperationAction(ISD::SREM , MVT::i16 , Expand);
247 setOperationAction(ISD::UREM , MVT::i16 , Expand);
248 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
249 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
250 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
251 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
252 setOperationAction(ISD::SREM , MVT::i32 , Expand);
253 setOperationAction(ISD::UREM , MVT::i32 , Expand);
254 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
255 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
256 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
257 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
258 setOperationAction(ISD::SREM , MVT::i64 , Expand);
259 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohmana37c9f72007-09-25 18:23:27 +0000260
Owen Anderson825b72b2009-08-11 20:47:22 +0000261 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
262 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
263 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
264 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000265 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000266 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
267 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
268 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
269 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
270 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
271 setOperationAction(ISD::FREM , MVT::f32 , Expand);
272 setOperationAction(ISD::FREM , MVT::f64 , Expand);
273 setOperationAction(ISD::FREM , MVT::f80 , Expand);
274 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000275
Owen Anderson825b72b2009-08-11 20:47:22 +0000276 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
277 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
278 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
279 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000280 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
281 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000282 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
283 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
284 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000285 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000286 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
287 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
288 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000289 }
290
Owen Anderson825b72b2009-08-11 20:47:22 +0000291 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
292 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000293
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000294 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000295 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000296 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000297 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000298 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000299 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
300 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
301 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
302 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
303 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000304 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000305 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
306 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
307 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
308 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000309 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000310 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
311 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000312 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000313 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000314
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000315 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000316 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
317 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
318 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
319 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000320 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000321 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
322 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000323 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000324 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000325 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
326 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
327 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
328 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000329 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000330 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000331 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000332 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
333 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
334 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000335 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000336 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
337 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
338 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000339 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000340
Evan Chengd2cde682008-03-10 19:38:10 +0000341 if (Subtarget->hasSSE1())
Owen Anderson825b72b2009-08-11 20:47:22 +0000342 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000343
Eric Christopher9a9d2752010-07-22 02:48:34 +0000344 // We may not have a libcall for MEMBARRIER so we should lower this.
345 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
346
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000347 // On X86 and X86-64, atomic operations are lowered to locked instructions.
348 // Locked instructions, in turn, have implicit fence semantics (all memory
349 // operations are flushed before issuing the locked instruction, and they
350 // are not buffered), so we can fold away the common pattern of
351 // fence-atomic-fence.
352 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000353
Mon P Wang63307c32008-05-05 19:05:59 +0000354 // Expand certain atomics
Owen Anderson825b72b2009-08-11 20:47:22 +0000355 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Custom);
356 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Custom);
357 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
358 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Bill Wendling5bf1b4e2008-08-20 00:28:16 +0000359
Owen Anderson825b72b2009-08-11 20:47:22 +0000360 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Custom);
361 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Custom);
362 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
363 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000364
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000365 if (!Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000366 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
367 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
368 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
369 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
370 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
371 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
372 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000373 }
374
Evan Cheng3c992d22006-03-07 02:02:57 +0000375 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000376 if (!Subtarget->isTargetDarwin() &&
377 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000378 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000379 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000380 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000381
Owen Anderson825b72b2009-08-11 20:47:22 +0000382 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
383 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
384 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
385 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000386 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000387 setExceptionPointerRegister(X86::RAX);
388 setExceptionSelectorRegister(X86::RDX);
389 } else {
390 setExceptionPointerRegister(X86::EAX);
391 setExceptionSelectorRegister(X86::EDX);
392 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000393 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
394 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000395
Owen Anderson825b72b2009-08-11 20:47:22 +0000396 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000397
Owen Anderson825b72b2009-08-11 20:47:22 +0000398 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000399
Nate Begemanacc398c2006-01-25 18:21:52 +0000400 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000401 setOperationAction(ISD::VASTART , MVT::Other, Custom);
402 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000403 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000404 setOperationAction(ISD::VAARG , MVT::Other, Custom);
405 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000406 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000407 setOperationAction(ISD::VAARG , MVT::Other, Expand);
408 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000409 }
Evan Chengae642192007-03-02 23:16:35 +0000410
Owen Anderson825b72b2009-08-11 20:47:22 +0000411 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
412 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000413 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000414 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000415 if (Subtarget->isTargetCygMing())
Owen Anderson825b72b2009-08-11 20:47:22 +0000416 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000417 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000418 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000419
Evan Chengc7ce29b2009-02-13 22:36:38 +0000420 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000421 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000422 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000423 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
424 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000425
Evan Cheng223547a2006-01-31 22:28:30 +0000426 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000427 setOperationAction(ISD::FABS , MVT::f64, Custom);
428 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000429
430 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000431 setOperationAction(ISD::FNEG , MVT::f64, Custom);
432 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000433
Evan Cheng68c47cb2007-01-05 07:55:56 +0000434 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000435 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
436 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000437
Evan Chengd25e9e82006-02-02 00:28:23 +0000438 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000439 setOperationAction(ISD::FSIN , MVT::f64, Expand);
440 setOperationAction(ISD::FCOS , MVT::f64, Expand);
441 setOperationAction(ISD::FSIN , MVT::f32, Expand);
442 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000443
Chris Lattnera54aa942006-01-29 06:26:08 +0000444 // Expand FP immediates into loads from the stack, except for the special
445 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000446 addLegalFPImmediate(APFloat(+0.0)); // xorpd
447 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000448 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000449 // Use SSE for f32, x87 for f64.
450 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000451 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
452 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000453
454 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000455 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000456
457 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000458 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000459
Owen Anderson825b72b2009-08-11 20:47:22 +0000460 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000461
462 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000463 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
464 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000465
466 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000467 setOperationAction(ISD::FSIN , MVT::f32, Expand);
468 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000469
Nate Begemane1795842008-02-14 08:57:00 +0000470 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000471 addLegalFPImmediate(APFloat(+0.0f)); // xorps
472 addLegalFPImmediate(APFloat(+0.0)); // FLD0
473 addLegalFPImmediate(APFloat(+1.0)); // FLD1
474 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
475 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
476
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000477 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000478 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
479 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000480 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000481 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000482 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000483 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000484 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
485 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000486
Owen Anderson825b72b2009-08-11 20:47:22 +0000487 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
488 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
489 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
490 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000491
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000492 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000493 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
494 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000495 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000496 addLegalFPImmediate(APFloat(+0.0)); // FLD0
497 addLegalFPImmediate(APFloat(+1.0)); // FLD1
498 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
499 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000500 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
501 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
502 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
503 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000504 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000505
Dale Johannesen59a58732007-08-05 18:49:15 +0000506 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000507 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000508 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
509 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
510 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000511 {
512 bool ignored;
513 APFloat TmpFlt(+0.0);
514 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
515 &ignored);
516 addLegalFPImmediate(TmpFlt); // FLD0
517 TmpFlt.changeSign();
518 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
519 APFloat TmpFlt2(+1.0);
520 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
521 &ignored);
522 addLegalFPImmediate(TmpFlt2); // FLD1
523 TmpFlt2.changeSign();
524 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
525 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000526
Evan Chengc7ce29b2009-02-13 22:36:38 +0000527 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000528 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
529 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000530 }
Dale Johannesen2f429012007-09-26 21:10:55 +0000531 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000532
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000533 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000534 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
535 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
536 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000537
Owen Anderson825b72b2009-08-11 20:47:22 +0000538 setOperationAction(ISD::FLOG, MVT::f80, Expand);
539 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
540 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
541 setOperationAction(ISD::FEXP, MVT::f80, Expand);
542 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000543
Mon P Wangf007a8b2008-11-06 05:31:54 +0000544 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000545 // (for widening) or expand (for scalarization). Then we will selectively
546 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000547 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
548 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
549 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
550 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
551 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
552 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
553 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
554 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
555 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
556 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
557 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
558 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
559 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
560 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
561 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
562 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
563 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
564 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
565 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
566 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
567 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
568 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
569 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
570 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
571 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
572 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
573 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
574 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
575 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
576 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
577 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
578 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
579 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
580 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
581 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
582 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
583 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
584 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
585 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
586 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
587 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
588 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
589 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
590 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
591 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
592 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
593 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
594 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
595 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
596 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000597 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000598 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
599 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
600 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
601 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
602 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
603 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
604 setTruncStoreAction((MVT::SimpleValueType)VT,
605 (MVT::SimpleValueType)InnerVT, Expand);
606 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
607 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
608 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000609 }
610
Evan Chengc7ce29b2009-02-13 22:36:38 +0000611 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
612 // with -msoft-float, disable use of MMX as well.
Evan Cheng92722532009-03-26 23:06:32 +0000613 if (!UseSoftFloat && !DisableMMX && Subtarget->hasMMX()) {
Bill Wendlingd8dd5752010-09-07 20:03:56 +0000614 addRegisterClass(MVT::x86mmx, X86::VR64RegisterClass, false);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000615 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000616 }
617
Dale Johannesen0488fb62010-09-30 23:57:10 +0000618 // MMX-sized vectors (other than x86mmx) are expected to be expanded
619 // into smaller operations.
620 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
621 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
622 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
623 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
624 setOperationAction(ISD::AND, MVT::v8i8, Expand);
625 setOperationAction(ISD::AND, MVT::v4i16, Expand);
626 setOperationAction(ISD::AND, MVT::v2i32, Expand);
627 setOperationAction(ISD::AND, MVT::v1i64, Expand);
628 setOperationAction(ISD::OR, MVT::v8i8, Expand);
629 setOperationAction(ISD::OR, MVT::v4i16, Expand);
630 setOperationAction(ISD::OR, MVT::v2i32, Expand);
631 setOperationAction(ISD::OR, MVT::v1i64, Expand);
632 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
633 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
634 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
635 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
636 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
637 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
638 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
639 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
640 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
641 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
642 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
643 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
644 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
645 setOperationAction(ISD::BIT_CONVERT, MVT::v8i8, Expand);
646 setOperationAction(ISD::BIT_CONVERT, MVT::v4i16, Expand);
647 setOperationAction(ISD::BIT_CONVERT, MVT::v2i32, Expand);
648 setOperationAction(ISD::BIT_CONVERT, MVT::v1i64, Expand);
649
Evan Cheng92722532009-03-26 23:06:32 +0000650 if (!UseSoftFloat && Subtarget->hasSSE1()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000651 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000652
Owen Anderson825b72b2009-08-11 20:47:22 +0000653 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
654 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
655 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
656 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
657 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
658 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
659 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
660 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
661 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
662 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
663 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
664 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000665 }
666
Evan Cheng92722532009-03-26 23:06:32 +0000667 if (!UseSoftFloat && Subtarget->hasSSE2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000668 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000669
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000670 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
671 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000672 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
673 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
674 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
675 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000676
Owen Anderson825b72b2009-08-11 20:47:22 +0000677 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
678 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
679 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
680 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
681 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
682 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
683 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
684 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
685 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
686 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
687 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
688 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
689 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
690 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
691 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
692 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000693
Owen Anderson825b72b2009-08-11 20:47:22 +0000694 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
695 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
696 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
697 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000698
Owen Anderson825b72b2009-08-11 20:47:22 +0000699 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
700 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
701 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
702 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
703 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000704
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000705 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
706 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
707 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
708 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
709 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
710
Evan Cheng2c3ae372006-04-12 21:21:57 +0000711 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000712 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
713 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000714 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000715 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000716 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000717 // Do not attempt to custom lower non-128-bit vectors
718 if (!VT.is128BitVector())
719 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000720 setOperationAction(ISD::BUILD_VECTOR,
721 VT.getSimpleVT().SimpleTy, Custom);
722 setOperationAction(ISD::VECTOR_SHUFFLE,
723 VT.getSimpleVT().SimpleTy, Custom);
724 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
725 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000726 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000727
Owen Anderson825b72b2009-08-11 20:47:22 +0000728 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
729 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
730 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
731 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
732 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
733 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000734
Nate Begemancdd1eec2008-02-12 22:51:28 +0000735 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000736 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
737 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000738 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000739
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000740 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000741 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
742 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000743 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000744
745 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000746 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000747 continue;
Eric Christopher4bd24c22010-03-30 01:04:59 +0000748
Owen Andersond6662ad2009-08-10 20:46:15 +0000749 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000750 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000751 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000752 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000753 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000754 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000755 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000756 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000757 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000758 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000759 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000760
Owen Anderson825b72b2009-08-11 20:47:22 +0000761 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000762
Evan Cheng2c3ae372006-04-12 21:21:57 +0000763 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000764 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
765 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
766 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
767 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000768
Owen Anderson825b72b2009-08-11 20:47:22 +0000769 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
770 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000771 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000772
Nate Begeman14d12ca2008-02-11 04:19:36 +0000773 if (Subtarget->hasSSE41()) {
Dale Johannesen54feef22010-05-27 20:12:41 +0000774 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
775 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
776 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
777 setOperationAction(ISD::FRINT, MVT::f32, Legal);
778 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
779 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
780 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
781 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
782 setOperationAction(ISD::FRINT, MVT::f64, Legal);
783 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
784
Nate Begeman14d12ca2008-02-11 04:19:36 +0000785 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000786 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000787
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000788 // Can turn SHL into an integer multiply.
789 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
Nate Begeman51409212010-07-28 00:21:48 +0000790 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000791
Nate Begeman14d12ca2008-02-11 04:19:36 +0000792 // i8 and i16 vectors are custom , because the source register and source
793 // source memory operand types are not the same width. f32 vectors are
794 // custom since the immediate controlling the insert encodes additional
795 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000796 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
797 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
798 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
799 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000800
Owen Anderson825b72b2009-08-11 20:47:22 +0000801 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
802 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
803 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
804 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000805
806 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000807 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
808 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000809 }
810 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000811
Nate Begeman30a0de92008-07-17 16:51:19 +0000812 if (Subtarget->hasSSE42()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000813 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
Nate Begeman30a0de92008-07-17 16:51:19 +0000814 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000815
David Greene9b9838d2009-06-29 16:47:10 +0000816 if (!UseSoftFloat && Subtarget->hasAVX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000817 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
818 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
819 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
820 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
Bruno Cardoso Lopes405f11b2010-08-10 01:43:16 +0000821 addRegisterClass(MVT::v32i8, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000822
Owen Anderson825b72b2009-08-11 20:47:22 +0000823 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
824 setOperationAction(ISD::LOAD, MVT::v8i32, Legal);
825 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
826 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
827 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
828 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
829 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
830 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
831 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
832 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +0000833 setOperationAction(ISD::BUILD_VECTOR, MVT::v8f32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000834 //setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Custom);
835 //setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8f32, Custom);
836 //setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
837 //setOperationAction(ISD::VSETCC, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000838
839 // Operations to consider commented out -v16i16 v32i8
Owen Anderson825b72b2009-08-11 20:47:22 +0000840 //setOperationAction(ISD::ADD, MVT::v16i16, Legal);
841 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
842 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
843 //setOperationAction(ISD::SUB, MVT::v32i8, Legal);
844 //setOperationAction(ISD::SUB, MVT::v16i16, Legal);
845 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
846 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
847 //setOperationAction(ISD::MUL, MVT::v16i16, Legal);
848 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
849 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
850 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
851 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
852 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
853 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000854
Owen Anderson825b72b2009-08-11 20:47:22 +0000855 setOperationAction(ISD::VSETCC, MVT::v4f64, Custom);
856 // setOperationAction(ISD::VSETCC, MVT::v32i8, Custom);
857 // setOperationAction(ISD::VSETCC, MVT::v16i16, Custom);
858 setOperationAction(ISD::VSETCC, MVT::v8i32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000859
Owen Anderson825b72b2009-08-11 20:47:22 +0000860 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v32i8, Custom);
861 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i16, Custom);
862 // setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i16, Custom);
863 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i32, Custom);
864 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000865
Owen Anderson825b72b2009-08-11 20:47:22 +0000866 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f64, Custom);
867 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i64, Custom);
868 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f64, Custom);
869 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i64, Custom);
870 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f64, Custom);
871 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000872
873#if 0
874 // Not sure we want to do this since there are no 256-bit integer
875 // operations in AVX
876
877 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
878 // This includes 256-bit vectors
Owen Anderson825b72b2009-08-11 20:47:22 +0000879 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; ++i) {
880 EVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000881
882 // Do not attempt to custom lower non-power-of-2 vectors
883 if (!isPowerOf2_32(VT.getVectorNumElements()))
884 continue;
885
886 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
887 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
888 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
889 }
890
891 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000892 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i64, Custom);
893 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i64, Custom);
Eric Christopherfd179292009-08-27 18:07:15 +0000894 }
David Greene9b9838d2009-06-29 16:47:10 +0000895#endif
896
897#if 0
898 // Not sure we want to do this since there are no 256-bit integer
899 // operations in AVX
900
901 // Promote v32i8, v16i16, v8i32 load, select, and, or, xor to v4i64.
902 // Including 256-bit vectors
Owen Anderson825b72b2009-08-11 20:47:22 +0000903 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; i++) {
904 EVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000905
906 if (!VT.is256BitVector()) {
907 continue;
908 }
909 setOperationAction(ISD::AND, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000910 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000911 setOperationAction(ISD::OR, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000912 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000913 setOperationAction(ISD::XOR, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000914 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000915 setOperationAction(ISD::LOAD, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000916 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000917 setOperationAction(ISD::SELECT, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000918 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000919 }
920
Owen Anderson825b72b2009-08-11 20:47:22 +0000921 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
David Greene9b9838d2009-06-29 16:47:10 +0000922#endif
923 }
924
Evan Cheng6be2c582006-04-05 23:38:46 +0000925 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000926 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +0000927
Bill Wendling74c37652008-12-09 22:08:41 +0000928 // Add/Sub/Mul with overflow operations are custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000929 setOperationAction(ISD::SADDO, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000930 setOperationAction(ISD::UADDO, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000931 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000932 setOperationAction(ISD::USUBO, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000933 setOperationAction(ISD::SMULO, MVT::i32, Custom);
Dan Gohman71c62a22010-06-02 19:13:40 +0000934
Eli Friedman962f5492010-06-02 19:35:46 +0000935 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
936 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +0000937 //
Eli Friedman962f5492010-06-02 19:35:46 +0000938 // FIXME: We really should do custom legalization for addition and
939 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
940 // than generic legalization for 64-bit multiplication-with-overflow, though.
Eli Friedmana993f0a2010-06-02 00:27:18 +0000941 if (Subtarget->is64Bit()) {
942 setOperationAction(ISD::SADDO, MVT::i64, Custom);
943 setOperationAction(ISD::UADDO, MVT::i64, Custom);
944 setOperationAction(ISD::SSUBO, MVT::i64, Custom);
945 setOperationAction(ISD::USUBO, MVT::i64, Custom);
946 setOperationAction(ISD::SMULO, MVT::i64, Custom);
947 }
Bill Wendling41ea7e72008-11-24 19:21:46 +0000948
Evan Chengd54f2d52009-03-31 19:38:51 +0000949 if (!Subtarget->is64Bit()) {
950 // These libcalls are not available in 32-bit.
951 setLibcallName(RTLIB::SHL_I128, 0);
952 setLibcallName(RTLIB::SRL_I128, 0);
953 setLibcallName(RTLIB::SRA_I128, 0);
954 }
955
Evan Cheng206ee9d2006-07-07 08:33:52 +0000956 // We have target-specific dag combine patterns for the following nodes:
957 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +0000958 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +0000959 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +0000960 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +0000961 setTargetDAGCombine(ISD::SHL);
962 setTargetDAGCombine(ISD::SRA);
963 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +0000964 setTargetDAGCombine(ISD::OR);
Chris Lattner149a4e52008-02-22 02:09:43 +0000965 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +0000966 setTargetDAGCombine(ISD::ZERO_EXTEND);
Evan Cheng0b0cd912009-03-28 05:57:29 +0000967 if (Subtarget->is64Bit())
968 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +0000969
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000970 computeRegisterProperties();
971
Evan Cheng87ed7162006-02-14 08:25:08 +0000972 // FIXME: These should be based on subtarget info. Plus, the values should
973 // be smaller when we are in optimizing for size mode.
Dan Gohman87060f52008-06-30 21:00:56 +0000974 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng255f20f2010-04-01 06:04:33 +0000975 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Dan Gohman87060f52008-06-30 21:00:56 +0000976 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Evan Chengfb8075d2008-02-28 00:43:03 +0000977 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +0000978 benefitFromCodePlacementOpt = true;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000979}
980
Scott Michel5b8f82e2008-03-10 15:42:14 +0000981
Owen Anderson825b72b2009-08-11 20:47:22 +0000982MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
983 return MVT::i8;
Scott Michel5b8f82e2008-03-10 15:42:14 +0000984}
985
986
Evan Cheng29286502008-01-23 23:17:41 +0000987/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
988/// the desired ByVal argument alignment.
989static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
990 if (MaxAlign == 16)
991 return;
992 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
993 if (VTy->getBitWidth() == 128)
994 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +0000995 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
996 unsigned EltAlign = 0;
997 getMaxByValAlign(ATy->getElementType(), EltAlign);
998 if (EltAlign > MaxAlign)
999 MaxAlign = EltAlign;
1000 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1001 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1002 unsigned EltAlign = 0;
1003 getMaxByValAlign(STy->getElementType(i), EltAlign);
1004 if (EltAlign > MaxAlign)
1005 MaxAlign = EltAlign;
1006 if (MaxAlign == 16)
1007 break;
1008 }
1009 }
1010 return;
1011}
1012
1013/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1014/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001015/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1016/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +00001017unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001018 if (Subtarget->is64Bit()) {
1019 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001020 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001021 if (TyAlign > 8)
1022 return TyAlign;
1023 return 8;
1024 }
1025
Evan Cheng29286502008-01-23 23:17:41 +00001026 unsigned Align = 4;
Dale Johannesen0c191872008-02-08 19:48:20 +00001027 if (Subtarget->hasSSE1())
1028 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001029 return Align;
1030}
Chris Lattner2b02a442007-02-25 08:29:00 +00001031
Evan Chengf0df0312008-05-15 08:39:06 +00001032/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001033/// and store operations as a result of memset, memcpy, and memmove
1034/// lowering. If DstAlign is zero that means it's safe to destination
1035/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1036/// means there isn't a need to check it against alignment requirement,
1037/// probably because the source does not need to be loaded. If
1038/// 'NonScalarIntSafe' is true, that means it's safe to return a
1039/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1040/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1041/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001042/// It returns EVT::Other if the type should be determined using generic
1043/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001044EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001045X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1046 unsigned DstAlign, unsigned SrcAlign,
Evan Chengf28f8bc2010-04-02 19:36:14 +00001047 bool NonScalarIntSafe,
Evan Chengc3b0c342010-04-08 07:37:57 +00001048 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001049 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001050 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1051 // linux. This is because the stack realignment code can't handle certain
1052 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001053 const Function *F = MF.getFunction();
Evan Chengf28f8bc2010-04-02 19:36:14 +00001054 if (NonScalarIntSafe &&
1055 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001056 if (Size >= 16 &&
1057 (Subtarget->isUnalignedMemAccessFast() ||
Chandler Carruthae1d41c2010-04-02 01:31:24 +00001058 ((DstAlign == 0 || DstAlign >= 16) &&
1059 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001060 Subtarget->getStackAlignment() >= 16) {
1061 if (Subtarget->hasSSE2())
1062 return MVT::v4i32;
Evan Chengf28f8bc2010-04-02 19:36:14 +00001063 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001064 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001065 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001066 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001067 Subtarget->getStackAlignment() >= 8 &&
Evan Chengc3b0c342010-04-08 07:37:57 +00001068 Subtarget->hasSSE2()) {
1069 // Do not use f64 to lower memcpy if source is string constant. It's
1070 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001071 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001072 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001073 }
Evan Chengf0df0312008-05-15 08:39:06 +00001074 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001075 return MVT::i64;
1076 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001077}
1078
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001079/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1080/// current function. The returned value is a member of the
1081/// MachineJumpTableInfo::JTEntryKind enum.
1082unsigned X86TargetLowering::getJumpTableEncoding() const {
1083 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1084 // symbol.
1085 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1086 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001087 return MachineJumpTableInfo::EK_Custom32;
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001088
1089 // Otherwise, use the normal jump table encoding heuristics.
1090 return TargetLowering::getJumpTableEncoding();
1091}
1092
Chris Lattner589c6f62010-01-26 06:28:43 +00001093/// getPICBaseSymbol - Return the X86-32 PIC base.
1094MCSymbol *
1095X86TargetLowering::getPICBaseSymbol(const MachineFunction *MF,
1096 MCContext &Ctx) const {
1097 const MCAsmInfo &MAI = *getTargetMachine().getMCAsmInfo();
Chris Lattner9b97a732010-03-30 18:10:53 +00001098 return Ctx.GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix())+
1099 Twine(MF->getFunctionNumber())+"$pb");
Chris Lattner589c6f62010-01-26 06:28:43 +00001100}
1101
1102
Chris Lattnerc64daab2010-01-26 05:02:42 +00001103const MCExpr *
1104X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1105 const MachineBasicBlock *MBB,
1106 unsigned uid,MCContext &Ctx) const{
1107 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1108 Subtarget->isPICStyleGOT());
1109 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1110 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001111 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1112 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001113}
1114
Evan Chengcc415862007-11-09 01:32:10 +00001115/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1116/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001117SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001118 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001119 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001120 // This doesn't have DebugLoc associated with it, but is not really the
1121 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001122 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001123 return Table;
1124}
1125
Chris Lattner589c6f62010-01-26 06:28:43 +00001126/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1127/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1128/// MCExpr.
1129const MCExpr *X86TargetLowering::
1130getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1131 MCContext &Ctx) const {
1132 // X86-64 uses RIP relative addressing based on the jump table label.
1133 if (Subtarget->isPICStyleRIPRel())
1134 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1135
1136 // Otherwise, the reference is relative to the PIC base.
1137 return MCSymbolRefExpr::Create(getPICBaseSymbol(MF, Ctx), Ctx);
1138}
1139
Bill Wendlingb4202b82009-07-01 18:50:55 +00001140/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +00001141unsigned X86TargetLowering::getFunctionAlignment(const Function *F) const {
Dan Gohman25103a22009-08-18 00:20:06 +00001142 return F->hasFnAttr(Attribute::OptimizeForSize) ? 0 : 4;
Bill Wendling20c568f2009-06-30 22:38:32 +00001143}
1144
Evan Chengdee81012010-07-26 21:50:05 +00001145std::pair<const TargetRegisterClass*, uint8_t>
1146X86TargetLowering::findRepresentativeClass(EVT VT) const{
1147 const TargetRegisterClass *RRC = 0;
1148 uint8_t Cost = 1;
1149 switch (VT.getSimpleVT().SimpleTy) {
1150 default:
1151 return TargetLowering::findRepresentativeClass(VT);
1152 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1153 RRC = (Subtarget->is64Bit()
1154 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1155 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001156 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001157 RRC = X86::VR64RegisterClass;
1158 break;
1159 case MVT::f32: case MVT::f64:
1160 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1161 case MVT::v4f32: case MVT::v2f64:
1162 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1163 case MVT::v4f64:
1164 RRC = X86::VR128RegisterClass;
1165 break;
1166 }
1167 return std::make_pair(RRC, Cost);
1168}
1169
Evan Cheng70017e42010-07-24 00:39:05 +00001170unsigned
1171X86TargetLowering::getRegPressureLimit(const TargetRegisterClass *RC,
1172 MachineFunction &MF) const {
1173 unsigned FPDiff = RegInfo->hasFP(MF) ? 1 : 0;
1174 switch (RC->getID()) {
1175 default:
1176 return 0;
1177 case X86::GR32RegClassID:
1178 return 4 - FPDiff;
1179 case X86::GR64RegClassID:
1180 return 8 - FPDiff;
1181 case X86::VR128RegClassID:
1182 return Subtarget->is64Bit() ? 10 : 4;
1183 case X86::VR64RegClassID:
1184 return 4;
1185 }
1186}
1187
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001188bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1189 unsigned &Offset) const {
1190 if (!Subtarget->isTargetLinux())
1191 return false;
1192
1193 if (Subtarget->is64Bit()) {
1194 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1195 Offset = 0x28;
1196 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1197 AddressSpace = 256;
1198 else
1199 AddressSpace = 257;
1200 } else {
1201 // %gs:0x14 on i386
1202 Offset = 0x14;
1203 AddressSpace = 256;
1204 }
1205 return true;
1206}
1207
1208
Chris Lattner2b02a442007-02-25 08:29:00 +00001209//===----------------------------------------------------------------------===//
1210// Return Value Calling Convention Implementation
1211//===----------------------------------------------------------------------===//
1212
Chris Lattner59ed56b2007-02-28 04:55:35 +00001213#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001214
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001215bool
1216X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001217 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001218 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001219 SmallVector<CCValAssign, 16> RVLocs;
1220 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001221 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001222 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001223}
1224
Dan Gohman98ca4f22009-08-05 01:29:28 +00001225SDValue
1226X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001227 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001228 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001229 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001230 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001231 MachineFunction &MF = DAG.getMachineFunction();
1232 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001233
Chris Lattner9774c912007-02-27 05:28:59 +00001234 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001235 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1236 RVLocs, *DAG.getContext());
1237 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001238
Evan Chengdcea1632010-02-04 02:40:39 +00001239 // Add the regs to the liveout set for the function.
1240 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1241 for (unsigned i = 0; i != RVLocs.size(); ++i)
1242 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1243 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001244
Dan Gohman475871a2008-07-27 21:46:04 +00001245 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001246
Dan Gohman475871a2008-07-27 21:46:04 +00001247 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001248 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1249 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001250 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1251 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001252
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001253 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001254 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1255 CCValAssign &VA = RVLocs[i];
1256 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001257 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001258 EVT ValVT = ValToCopy.getValueType();
1259
Dale Johannesenc4510512010-09-24 19:05:48 +00001260 // If this is x86-64, and we disabled SSE, we can't return FP values,
1261 // or SSE or MMX vectors.
1262 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1263 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
1264 (Subtarget->is64Bit() && !Subtarget->hasSSE1())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001265 report_fatal_error("SSE register return with SSE disabled");
1266 }
1267 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1268 // llvm-gcc has never done it right and no one has noticed, so this
1269 // should be OK for now.
1270 if (ValVT == MVT::f64 &&
Chris Lattner83069682010-08-26 05:51:22 +00001271 (Subtarget->is64Bit() && !Subtarget->hasSSE2()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001272 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001273
Chris Lattner447ff682008-03-11 03:23:40 +00001274 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1275 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001276 if (VA.getLocReg() == X86::ST0 ||
1277 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001278 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1279 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001280 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001281 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001282 RetOps.push_back(ValToCopy);
1283 // Don't emit a copytoreg.
1284 continue;
1285 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001286
Evan Cheng242b38b2009-02-23 09:03:22 +00001287 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1288 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001289 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001290 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001291 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001292 ValToCopy = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001293 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1294 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001295 // If we don't have SSE2 available, convert to v4f32 so the generated
1296 // register is legal.
1297 if (!Subtarget->hasSSE2())
1298 ValToCopy = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32,ValToCopy);
1299 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001300 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001301 }
Chris Lattner97a2a562010-08-26 05:24:29 +00001302
Dale Johannesendd64c412009-02-04 00:33:20 +00001303 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001304 Flag = Chain.getValue(1);
1305 }
Dan Gohman61a92132008-04-21 23:59:07 +00001306
1307 // The x86-64 ABI for returning structs by value requires that we copy
1308 // the sret argument into %rax for the return. We saved the argument into
1309 // a virtual register in the entry block, so now we copy the value out
1310 // and into %rax.
1311 if (Subtarget->is64Bit() &&
1312 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1313 MachineFunction &MF = DAG.getMachineFunction();
1314 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1315 unsigned Reg = FuncInfo->getSRetReturnReg();
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001316 assert(Reg &&
1317 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001318 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001319
Dale Johannesendd64c412009-02-04 00:33:20 +00001320 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001321 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001322
1323 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001324 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001325 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001326
Chris Lattner447ff682008-03-11 03:23:40 +00001327 RetOps[0] = Chain; // Update chain.
1328
1329 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001330 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001331 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001332
1333 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001334 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001335}
1336
Dan Gohman98ca4f22009-08-05 01:29:28 +00001337/// LowerCallResult - Lower the result values of a call into the
1338/// appropriate copies out of appropriate physical registers.
1339///
1340SDValue
1341X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001342 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001343 const SmallVectorImpl<ISD::InputArg> &Ins,
1344 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001345 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001346
Chris Lattnere32bbf62007-02-28 07:09:55 +00001347 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001348 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001349 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001350 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001351 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001352 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001353
Chris Lattner3085e152007-02-25 08:59:22 +00001354 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001355 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001356 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001357 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001358
Torok Edwin3f142c32009-02-01 18:15:56 +00001359 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001360 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Dan Gohman98ca4f22009-08-05 01:29:28 +00001361 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001362 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001363 }
1364
Evan Cheng79fb3b42009-02-20 20:43:02 +00001365 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001366
1367 // If this is a call to a function that returns an fp value on the floating
1368 // point stack, we must guarantee the the value is popped from the stack, so
1369 // a CopyFromReg is not good enough - the copy instruction may be eliminated
1370 // if the return value is not used. We use the FpGET_ST0 instructions
1371 // instead.
1372 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1373 // If we prefer to use the value in xmm registers, copy it out as f80 and
1374 // use a truncate to move it from fp stack reg to xmm reg.
1375 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
1376 bool isST0 = VA.getLocReg() == X86::ST0;
1377 unsigned Opc = 0;
1378 if (CopyVT == MVT::f32) Opc = isST0 ? X86::FpGET_ST0_32:X86::FpGET_ST1_32;
1379 if (CopyVT == MVT::f64) Opc = isST0 ? X86::FpGET_ST0_64:X86::FpGET_ST1_64;
1380 if (CopyVT == MVT::f80) Opc = isST0 ? X86::FpGET_ST0_80:X86::FpGET_ST1_80;
1381 SDValue Ops[] = { Chain, InFlag };
1382 Chain = SDValue(DAG.getMachineNode(Opc, dl, CopyVT, MVT::Other, MVT::Flag,
1383 Ops, 2), 1);
1384 Val = Chain.getValue(0);
1385
1386 // Round the f80 to the right size, which also moves it to the appropriate
1387 // xmm register.
1388 if (CopyVT != VA.getValVT())
1389 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1390 // This truncation won't change the value.
1391 DAG.getIntPtrConstant(1));
1392 } else if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001393 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1394 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1395 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001396 MVT::v2i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001397 Val = Chain.getValue(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001398 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1399 Val, DAG.getConstant(0, MVT::i64));
Evan Cheng242b38b2009-02-23 09:03:22 +00001400 } else {
1401 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001402 MVT::i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001403 Val = Chain.getValue(0);
1404 }
Evan Cheng79fb3b42009-02-20 20:43:02 +00001405 Val = DAG.getNode(ISD::BIT_CONVERT, dl, CopyVT, Val);
1406 } else {
1407 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1408 CopyVT, InFlag).getValue(1);
1409 Val = Chain.getValue(0);
1410 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001411 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001412 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001413 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001414
Dan Gohman98ca4f22009-08-05 01:29:28 +00001415 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001416}
1417
1418
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001419//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001420// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001421//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001422// StdCall calling convention seems to be standard for many Windows' API
1423// routines and around. It differs from C calling convention just a little:
1424// callee should clean up the stack, not caller. Symbols should be also
1425// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001426// For info on fast calling convention see Fast Calling Convention (tail call)
1427// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001428
Dan Gohman98ca4f22009-08-05 01:29:28 +00001429/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001430/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001431static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1432 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001433 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001434
Dan Gohman98ca4f22009-08-05 01:29:28 +00001435 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001436}
1437
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001438/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001439/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001440static bool
1441ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1442 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001443 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001444
Dan Gohman98ca4f22009-08-05 01:29:28 +00001445 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001446}
1447
Dan Gohman095cc292008-09-13 01:54:27 +00001448/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1449/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001450CCAssignFn *X86TargetLowering::CCAssignFnForNode(CallingConv::ID CC) const {
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001451 if (Subtarget->is64Bit()) {
Chris Lattner29689432010-03-11 00:22:57 +00001452 if (CC == CallingConv::GHC)
1453 return CC_X86_64_GHC;
1454 else if (Subtarget->isTargetWin64())
Anton Korobeynikov8f88cb02008-03-22 20:37:30 +00001455 return CC_X86_Win64_C;
Evan Chenge9ac9e62008-09-07 09:07:23 +00001456 else
1457 return CC_X86_64_C;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001458 }
1459
Gordon Henriksen86737662008-01-05 16:56:59 +00001460 if (CC == CallingConv::X86_FastCall)
1461 return CC_X86_32_FastCall;
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001462 else if (CC == CallingConv::X86_ThisCall)
1463 return CC_X86_32_ThisCall;
Evan Chengb188dd92008-09-10 18:25:29 +00001464 else if (CC == CallingConv::Fast)
1465 return CC_X86_32_FastCC;
Chris Lattner29689432010-03-11 00:22:57 +00001466 else if (CC == CallingConv::GHC)
1467 return CC_X86_32_GHC;
Gordon Henriksen86737662008-01-05 16:56:59 +00001468 else
1469 return CC_X86_32_C;
1470}
1471
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001472/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1473/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001474/// the specific parameter attribute. The copy will be passed as a byval
1475/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001476static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001477CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001478 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1479 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001480 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
1481
Dale Johannesendd64c412009-02-04 00:33:20 +00001482 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001483 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001484 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001485}
1486
Chris Lattner29689432010-03-11 00:22:57 +00001487/// IsTailCallConvention - Return true if the calling convention is one that
1488/// supports tail call optimization.
1489static bool IsTailCallConvention(CallingConv::ID CC) {
1490 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1491}
1492
Evan Cheng0c439eb2010-01-27 00:07:07 +00001493/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1494/// a tailcall target by changing its ABI.
1495static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001496 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001497}
1498
Dan Gohman98ca4f22009-08-05 01:29:28 +00001499SDValue
1500X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001501 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001502 const SmallVectorImpl<ISD::InputArg> &Ins,
1503 DebugLoc dl, SelectionDAG &DAG,
1504 const CCValAssign &VA,
1505 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001506 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001507 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001508 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001509 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001510 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001511 EVT ValVT;
1512
1513 // If value is passed by pointer we have address passed instead of the value
1514 // itself.
1515 if (VA.getLocInfo() == CCValAssign::Indirect)
1516 ValVT = VA.getLocVT();
1517 else
1518 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001519
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001520 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001521 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001522 // In case of tail call optimization mark all arguments mutable. Since they
1523 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001524 if (Flags.isByVal()) {
1525 int FI = MFI->CreateFixedObject(Flags.getByValSize(),
Evan Chenged2ae132010-07-03 00:40:23 +00001526 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001527 return DAG.getFrameIndex(FI, getPointerTy());
1528 } else {
1529 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001530 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001531 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1532 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001533 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001534 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001535 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001536}
1537
Dan Gohman475871a2008-07-27 21:46:04 +00001538SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001539X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001540 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001541 bool isVarArg,
1542 const SmallVectorImpl<ISD::InputArg> &Ins,
1543 DebugLoc dl,
1544 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001545 SmallVectorImpl<SDValue> &InVals)
1546 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001547 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001548 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001549
Gordon Henriksen86737662008-01-05 16:56:59 +00001550 const Function* Fn = MF.getFunction();
1551 if (Fn->hasExternalLinkage() &&
1552 Subtarget->isTargetCygMing() &&
1553 Fn->getName() == "main")
1554 FuncInfo->setForceFramePointer(true);
1555
Evan Cheng1bc78042006-04-26 01:20:17 +00001556 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001557 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001558 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001559
Chris Lattner29689432010-03-11 00:22:57 +00001560 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1561 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001562
Chris Lattner638402b2007-02-28 07:00:42 +00001563 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001564 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001565 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1566 ArgLocs, *DAG.getContext());
1567 CCInfo.AnalyzeFormalArguments(Ins, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001568
Chris Lattnerf39f7712007-02-28 05:46:49 +00001569 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001570 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001571 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1572 CCValAssign &VA = ArgLocs[i];
1573 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1574 // places.
1575 assert(VA.getValNo() != LastVal &&
1576 "Don't support value assigned to multiple locs yet");
1577 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001578
Chris Lattnerf39f7712007-02-28 05:46:49 +00001579 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001580 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001581 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001582 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001583 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001584 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001585 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001586 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001587 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001588 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001589 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001590 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1591 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001592 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001593 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001594 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001595 RC = X86::VR64RegisterClass;
1596 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001597 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001598
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001599 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001600 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001601
Chris Lattnerf39f7712007-02-28 05:46:49 +00001602 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1603 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1604 // right size.
1605 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001606 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001607 DAG.getValueType(VA.getValVT()));
1608 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001609 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001610 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001611 else if (VA.getLocInfo() == CCValAssign::BCvt)
Anton Korobeynikov6dde14b2009-08-03 08:14:14 +00001612 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001613
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001614 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001615 // Handle MMX values passed in XMM regs.
1616 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001617 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1618 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001619 } else
1620 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001621 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001622 } else {
1623 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001624 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001625 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001626
1627 // If value is passed via pointer - do a load.
1628 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001629 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
1630 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001631
Dan Gohman98ca4f22009-08-05 01:29:28 +00001632 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001633 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001634
Dan Gohman61a92132008-04-21 23:59:07 +00001635 // The x86-64 ABI for returning structs by value requires that we copy
1636 // the sret argument into %rax for the return. Save the argument into
1637 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001638 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001639 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1640 unsigned Reg = FuncInfo->getSRetReturnReg();
1641 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001642 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001643 FuncInfo->setSRetReturnReg(Reg);
1644 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001645 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001646 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001647 }
1648
Chris Lattnerf39f7712007-02-28 05:46:49 +00001649 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001650 // Align stack specially for tail calls.
1651 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001652 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001653
Evan Cheng1bc78042006-04-26 01:20:17 +00001654 // If the function takes variable number of arguments, make a frame index for
1655 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001656 if (isVarArg) {
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001657 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1658 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001659 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001660 }
1661 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001662 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1663
1664 // FIXME: We should really autogenerate these arrays
1665 static const unsigned GPR64ArgRegsWin64[] = {
1666 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001667 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001668 static const unsigned XMMArgRegsWin64[] = {
1669 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1670 };
1671 static const unsigned GPR64ArgRegs64Bit[] = {
1672 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1673 };
1674 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001675 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1676 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1677 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001678 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1679
1680 if (IsWin64) {
1681 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1682 GPR64ArgRegs = GPR64ArgRegsWin64;
1683 XMMArgRegs = XMMArgRegsWin64;
1684 } else {
1685 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1686 GPR64ArgRegs = GPR64ArgRegs64Bit;
1687 XMMArgRegs = XMMArgRegs64Bit;
1688 }
1689 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1690 TotalNumIntRegs);
1691 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1692 TotalNumXMMRegs);
1693
Devang Patel578efa92009-06-05 21:57:13 +00001694 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Evan Chengc7ce29b2009-02-13 22:36:38 +00001695 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001696 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001697 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001698 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001699 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00001700 // Kernel mode asks for SSE to be disabled, so don't push them
1701 // on the stack.
1702 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001703
Gordon Henriksen86737662008-01-05 16:56:59 +00001704 // For X86-64, if there are vararg parameters that are passed via
1705 // registers, then we must store them to their spots on the stack so they
1706 // may be loaded by deferencing the result of va_next.
Dan Gohman1e93df62010-04-17 14:41:14 +00001707 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1708 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1709 FuncInfo->setRegSaveFrameIndex(
1710 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
1711 false));
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001712
Gordon Henriksen86737662008-01-05 16:56:59 +00001713 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001714 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001715 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1716 getPointerTy());
1717 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001718 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001719 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1720 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001721 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
1722 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001723 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001724 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001725 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001726 MachinePointerInfo::getFixedStack(
1727 FuncInfo->getRegSaveFrameIndex(), Offset),
1728 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001729 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001730 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001731 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001732
Dan Gohmanface41a2009-08-16 21:24:25 +00001733 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1734 // Now store the XMM (fp + vector) parameter registers.
1735 SmallVector<SDValue, 11> SaveXMMOps;
1736 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001737
Dan Gohmanface41a2009-08-16 21:24:25 +00001738 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
1739 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1740 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001741
Dan Gohman1e93df62010-04-17 14:41:14 +00001742 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1743 FuncInfo->getRegSaveFrameIndex()));
1744 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1745 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001746
Dan Gohmanface41a2009-08-16 21:24:25 +00001747 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
1748 unsigned VReg = MF.addLiveIn(XMMArgRegs[NumXMMRegs],
1749 X86::VR128RegisterClass);
1750 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1751 SaveXMMOps.push_back(Val);
1752 }
1753 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1754 MVT::Other,
1755 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001756 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001757
1758 if (!MemOps.empty())
1759 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1760 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001761 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001762 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001763
Gordon Henriksen86737662008-01-05 16:56:59 +00001764 // Some CCs need callee pop.
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001765 if (Subtarget->IsCalleePop(isVarArg, CallConv)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001766 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001767 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001768 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001769 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001770 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001771 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001772 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001773
Gordon Henriksen86737662008-01-05 16:56:59 +00001774 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001775 // RegSaveFrameIndex is X86-64 only.
1776 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001777 if (CallConv == CallingConv::X86_FastCall ||
1778 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001779 // fastcc functions can't have varargs.
1780 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001781 }
Evan Cheng25caf632006-05-23 21:06:34 +00001782
Dan Gohman98ca4f22009-08-05 01:29:28 +00001783 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001784}
1785
Dan Gohman475871a2008-07-27 21:46:04 +00001786SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001787X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1788 SDValue StackPtr, SDValue Arg,
1789 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001790 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001791 ISD::ArgFlagsTy Flags) const {
Anton Korobeynikovc7c62bb2010-09-02 22:31:32 +00001792 const unsigned FirstStackArgOffset = (Subtarget->isTargetWin64() ? 32 : 0);
1793 unsigned LocMemOffset = FirstStackArgOffset + VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001794 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001795 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001796 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00001797 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001798
1799 return DAG.getStore(Chain, dl, Arg, PtrOff,
1800 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00001801 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001802}
1803
Bill Wendling64e87322009-01-16 19:25:27 +00001804/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001805/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001806SDValue
1807X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001808 SDValue &OutRetAddr, SDValue Chain,
1809 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001810 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001811 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001812 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001813 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001814
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001815 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00001816 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
1817 false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001818 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001819}
1820
1821/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1822/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001823static SDValue
1824EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001825 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001826 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001827 // Store the return address to the appropriate stack slot.
1828 if (!FPDiff) return Chain;
1829 // Calculate the new stack slot for the return address.
1830 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001831 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00001832 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001833 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001834 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001835 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00001836 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00001837 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001838 return Chain;
1839}
1840
Dan Gohman98ca4f22009-08-05 01:29:28 +00001841SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001842X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001843 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001844 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001845 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001846 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001847 const SmallVectorImpl<ISD::InputArg> &Ins,
1848 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001849 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001850 MachineFunction &MF = DAG.getMachineFunction();
1851 bool Is64Bit = Subtarget->is64Bit();
1852 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00001853 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001854
Evan Cheng5f941932010-02-05 02:21:12 +00001855 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001856 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00001857 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1858 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001859 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00001860
1861 // Sibcalls are automatically detected tailcalls which do not require
1862 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00001863 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00001864 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00001865
1866 if (isTailCall)
1867 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00001868 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00001869
Chris Lattner29689432010-03-11 00:22:57 +00001870 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1871 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001872
Chris Lattner638402b2007-02-28 07:00:42 +00001873 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001874 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001875 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1876 ArgLocs, *DAG.getContext());
1877 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001878
Chris Lattner423c5f42007-02-28 05:31:48 +00001879 // Get a count of how many bytes are to be pushed on the stack.
1880 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00001881 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00001882 // This is a sibcall. The memory operands are available in caller's
1883 // own caller's stack.
1884 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00001885 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00001886 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001887
Gordon Henriksen86737662008-01-05 16:56:59 +00001888 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00001889 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001890 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00001891 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00001892 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1893 FPDiff = NumBytesCallerPushed - NumBytes;
1894
1895 // Set the delta of movement of the returnaddr stackslot.
1896 // But only set if delta is greater than previous delta.
1897 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1898 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1899 }
1900
Evan Chengf22f9b32010-02-06 03:28:46 +00001901 if (!IsSibcall)
1902 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001903
Dan Gohman475871a2008-07-27 21:46:04 +00001904 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001905 // Load return adress for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00001906 if (isTailCall && FPDiff)
1907 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
1908 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001909
Dan Gohman475871a2008-07-27 21:46:04 +00001910 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1911 SmallVector<SDValue, 8> MemOpChains;
1912 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001913
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001914 // Walk the register/memloc assignments, inserting copies/loads. In the case
1915 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00001916 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1917 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001918 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001919 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001920 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00001921 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00001922
Chris Lattner423c5f42007-02-28 05:31:48 +00001923 // Promote the value if needed.
1924 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001925 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00001926 case CCValAssign::Full: break;
1927 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001928 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001929 break;
1930 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001931 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001932 break;
1933 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001934 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
1935 // Special case: passing MMX values in XMM registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001936 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
1937 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
1938 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001939 } else
1940 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
1941 break;
1942 case CCValAssign::BCvt:
1943 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001944 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001945 case CCValAssign::Indirect: {
1946 // Store the argument.
1947 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00001948 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001949 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00001950 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001951 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001952 Arg = SpillSlot;
1953 break;
1954 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00001955 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001956
Chris Lattner423c5f42007-02-28 05:31:48 +00001957 if (VA.isRegLoc()) {
1958 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00001959 if (isVarArg && Subtarget->isTargetWin64()) {
1960 // Win64 ABI requires argument XMM reg to be copied to the corresponding
1961 // shadow reg if callee is a varargs function.
1962 unsigned ShadowReg = 0;
1963 switch (VA.getLocReg()) {
1964 case X86::XMM0: ShadowReg = X86::RCX; break;
1965 case X86::XMM1: ShadowReg = X86::RDX; break;
1966 case X86::XMM2: ShadowReg = X86::R8; break;
1967 case X86::XMM3: ShadowReg = X86::R9; break;
1968 }
1969 if (ShadowReg)
1970 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
1971 }
Evan Chengf22f9b32010-02-06 03:28:46 +00001972 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00001973 assert(VA.isMemLoc());
1974 if (StackPtr.getNode() == 0)
1975 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
1976 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1977 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001978 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001979 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001980
Evan Cheng32fe1032006-05-25 00:59:30 +00001981 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001982 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001983 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001984
Evan Cheng347d5f72006-04-28 21:29:37 +00001985 // Build a sequence of copy-to-reg nodes chained together with token chain
1986 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001987 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001988 // Tail call byval lowering might overwrite argument registers so in case of
1989 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001990 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001991 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001992 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001993 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001994 InFlag = Chain.getValue(1);
1995 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001996
Chris Lattner88e1fd52009-07-09 04:24:46 +00001997 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001998 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1999 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002000 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002001 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2002 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002003 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002004 InFlag);
2005 InFlag = Chain.getValue(1);
2006 } else {
2007 // If we are tail calling and generating PIC/GOT style code load the
2008 // address of the callee into ECX. The value in ecx is used as target of
2009 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2010 // for tail calls on PIC/GOT architectures. Normally we would just put the
2011 // address of GOT into ebx and then call target@PLT. But for tail calls
2012 // ebx would be restored (since ebx is callee saved) before jumping to the
2013 // target@PLT.
2014
2015 // Note: The actual moving to ECX is done further down.
2016 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2017 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2018 !G->getGlobal()->hasProtectedVisibility())
2019 Callee = LowerGlobalAddress(Callee, DAG);
2020 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002021 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002022 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002023 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002024
Nate Begemanc8ea6732010-07-21 20:49:52 +00002025 if (Is64Bit && isVarArg && !Subtarget->isTargetWin64()) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002026 // From AMD64 ABI document:
2027 // For calls that may call functions that use varargs or stdargs
2028 // (prototype-less calls or calls to functions containing ellipsis (...) in
2029 // the declaration) %al is used as hidden argument to specify the number
2030 // of SSE registers used. The contents of %al do not need to match exactly
2031 // the number of registers, but must be an ubound on the number of SSE
2032 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002033
Gordon Henriksen86737662008-01-05 16:56:59 +00002034 // Count the number of XMM registers allocated.
2035 static const unsigned XMMArgRegs[] = {
2036 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2037 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2038 };
2039 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Scott Michelfdc40a02009-02-17 22:15:04 +00002040 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002041 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002042
Dale Johannesendd64c412009-02-04 00:33:20 +00002043 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002044 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002045 InFlag = Chain.getValue(1);
2046 }
2047
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002048
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002049 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002050 if (isTailCall) {
2051 // Force all the incoming stack arguments to be loaded from the stack
2052 // before any new outgoing arguments are stored to the stack, because the
2053 // outgoing stack slots may alias the incoming argument stack slots, and
2054 // the alias isn't otherwise explicit. This is slightly more conservative
2055 // than necessary, because it means that each store effectively depends
2056 // on every argument instead of just those arguments it would clobber.
2057 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2058
Dan Gohman475871a2008-07-27 21:46:04 +00002059 SmallVector<SDValue, 8> MemOpChains2;
2060 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002061 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002062 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002063 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002064 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002065 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2066 CCValAssign &VA = ArgLocs[i];
2067 if (VA.isRegLoc())
2068 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002069 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002070 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002071 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002072 // Create frame index.
2073 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002074 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002075 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002076 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002077
Duncan Sands276dcbd2008-03-21 09:14:45 +00002078 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002079 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002080 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002081 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002082 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002083 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002084 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002085
Dan Gohman98ca4f22009-08-05 01:29:28 +00002086 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2087 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002088 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002089 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002090 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002091 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002092 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002093 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002094 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002095 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002096 }
2097 }
2098
2099 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002100 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002101 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002102
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002103 // Copy arguments to their registers.
2104 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002105 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002106 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002107 InFlag = Chain.getValue(1);
2108 }
Dan Gohman475871a2008-07-27 21:46:04 +00002109 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002110
Gordon Henriksen86737662008-01-05 16:56:59 +00002111 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002112 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002113 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002114 }
2115
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002116 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2117 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2118 // In the 64-bit large code model, we have to make all calls
2119 // through a register, since the call instruction's 32-bit
2120 // pc-relative offset may not be large enough to hold the whole
2121 // address.
2122 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002123 // If the callee is a GlobalAddress node (quite common, every direct call
2124 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2125 // it.
2126
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002127 // We should use extra load for direct calls to dllimported functions in
2128 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002129 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002130 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002131 unsigned char OpFlags = 0;
Eric Christopherfd179292009-08-27 18:07:15 +00002132
Chris Lattner48a7d022009-07-09 05:02:21 +00002133 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2134 // external symbols most go through the PLT in PIC mode. If the symbol
2135 // has hidden or protected visibility, or if it is static or local, then
2136 // we don't need to use the PLT - we can directly call it.
2137 if (Subtarget->isTargetELF() &&
2138 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002139 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002140 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002141 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002142 (GV->isDeclaration() || GV->isWeakForLinker()) &&
2143 Subtarget->getDarwinVers() < 9) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002144 // PC-relative references to external symbols should go through $stub,
2145 // unless we're building with the leopard linker or later, which
2146 // automatically synthesizes these stubs.
2147 OpFlags = X86II::MO_DARWIN_STUB;
2148 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002149
Devang Patel0d881da2010-07-06 22:08:15 +00002150 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002151 G->getOffset(), OpFlags);
2152 }
Bill Wendling056292f2008-09-16 21:48:12 +00002153 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002154 unsigned char OpFlags = 0;
2155
2156 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to external
2157 // symbols should go through the PLT.
2158 if (Subtarget->isTargetELF() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002159 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002160 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002161 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002162 Subtarget->getDarwinVers() < 9) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002163 // PC-relative references to external symbols should go through $stub,
2164 // unless we're building with the leopard linker or later, which
2165 // automatically synthesizes these stubs.
2166 OpFlags = X86II::MO_DARWIN_STUB;
2167 }
Eric Christopherfd179292009-08-27 18:07:15 +00002168
Chris Lattner48a7d022009-07-09 05:02:21 +00002169 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2170 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002171 }
2172
Chris Lattnerd96d0722007-02-25 06:40:16 +00002173 // Returns a chain & a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +00002174 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00002175 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002176
Evan Chengf22f9b32010-02-06 03:28:46 +00002177 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002178 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2179 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002180 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002181 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002182
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002183 Ops.push_back(Chain);
2184 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002185
Dan Gohman98ca4f22009-08-05 01:29:28 +00002186 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002187 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002188
Gordon Henriksen86737662008-01-05 16:56:59 +00002189 // Add argument registers to the end of the list so that they are known live
2190 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002191 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2192 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2193 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002194
Evan Cheng586ccac2008-03-18 23:36:35 +00002195 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002196 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002197 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2198
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002199 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
2200 if (Is64Bit && isVarArg && !Subtarget->isTargetWin64())
Owen Anderson825b72b2009-08-11 20:47:22 +00002201 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002202
Gabor Greifba36cb52008-08-28 21:40:38 +00002203 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002204 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002205
Dan Gohman98ca4f22009-08-05 01:29:28 +00002206 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002207 // We used to do:
2208 //// If this is the first return lowered for this function, add the regs
2209 //// to the liveout set for the function.
2210 // This isn't right, although it's probably harmless on x86; liveouts
2211 // should be computed from returns not tail calls. Consider a void
2212 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002213 return DAG.getNode(X86ISD::TC_RETURN, dl,
2214 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002215 }
2216
Dale Johannesenace16102009-02-03 19:33:06 +00002217 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002218 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002219
Chris Lattner2d297092006-05-23 18:50:38 +00002220 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002221 unsigned NumBytesForCalleeToPush;
Dan Gohman4d3d6e12010-05-27 18:43:40 +00002222 if (Subtarget->IsCalleePop(isVarArg, CallConv))
Gordon Henriksen86737662008-01-05 16:56:59 +00002223 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002224 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002225 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002226 // pops the hidden struct pointer, so we have to push it back.
2227 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002228 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002229 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002230 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002231
Gordon Henriksenae636f82008-01-03 16:47:34 +00002232 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002233 if (!IsSibcall) {
2234 Chain = DAG.getCALLSEQ_END(Chain,
2235 DAG.getIntPtrConstant(NumBytes, true),
2236 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2237 true),
2238 InFlag);
2239 InFlag = Chain.getValue(1);
2240 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002241
Chris Lattner3085e152007-02-25 08:59:22 +00002242 // Handle result values, copying them out of physregs into vregs that we
2243 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002244 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2245 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002246}
2247
Evan Cheng25ab6902006-09-08 06:48:29 +00002248
2249//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002250// Fast Calling Convention (tail call) implementation
2251//===----------------------------------------------------------------------===//
2252
2253// Like std call, callee cleans arguments, convention except that ECX is
2254// reserved for storing the tail called function address. Only 2 registers are
2255// free for argument passing (inreg). Tail call optimization is performed
2256// provided:
2257// * tailcallopt is enabled
2258// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002259// On X86_64 architecture with GOT-style position independent code only local
2260// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002261// To keep the stack aligned according to platform abi the function
2262// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2263// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002264// If a tail called function callee has more arguments than the caller the
2265// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002266// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002267// original REtADDR, but before the saved framepointer or the spilled registers
2268// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2269// stack layout:
2270// arg1
2271// arg2
2272// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002273// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002274// move area ]
2275// (possible EBP)
2276// ESI
2277// EDI
2278// local1 ..
2279
2280/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2281/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002282unsigned
2283X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2284 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002285 MachineFunction &MF = DAG.getMachineFunction();
2286 const TargetMachine &TM = MF.getTarget();
2287 const TargetFrameInfo &TFI = *TM.getFrameInfo();
2288 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002289 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002290 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002291 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002292 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2293 // Number smaller than 12 so just add the difference.
2294 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2295 } else {
2296 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002297 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002298 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002299 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002300 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002301}
2302
Evan Cheng5f941932010-02-05 02:21:12 +00002303/// MatchingStackOffset - Return true if the given stack call argument is
2304/// already available in the same position (relatively) of the caller's
2305/// incoming argument stack.
2306static
2307bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2308 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2309 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002310 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2311 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002312 if (Arg.getOpcode() == ISD::CopyFromReg) {
2313 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
2314 if (!VR || TargetRegisterInfo::isPhysicalRegister(VR))
2315 return false;
2316 MachineInstr *Def = MRI->getVRegDef(VR);
2317 if (!Def)
2318 return false;
2319 if (!Flags.isByVal()) {
2320 if (!TII->isLoadFromStackSlot(Def, FI))
2321 return false;
2322 } else {
2323 unsigned Opcode = Def->getOpcode();
2324 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2325 Def->getOperand(1).isFI()) {
2326 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002327 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002328 } else
2329 return false;
2330 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002331 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2332 if (Flags.isByVal())
2333 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002334 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002335 // define @foo(%struct.X* %A) {
2336 // tail call @bar(%struct.X* byval %A)
2337 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002338 return false;
2339 SDValue Ptr = Ld->getBasePtr();
2340 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2341 if (!FINode)
2342 return false;
2343 FI = FINode->getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002344 } else
2345 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002346
Evan Cheng4cae1332010-03-05 08:38:04 +00002347 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002348 if (!MFI->isFixedObjectIndex(FI))
2349 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002350 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002351}
2352
Dan Gohman98ca4f22009-08-05 01:29:28 +00002353/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2354/// for tail call optimization. Targets which want to do tail call
2355/// optimization should implement this function.
2356bool
2357X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002358 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002359 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002360 bool isCalleeStructRet,
2361 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002362 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002363 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002364 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002365 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002366 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002367 CalleeCC != CallingConv::C)
2368 return false;
2369
Evan Cheng7096ae42010-01-29 06:45:59 +00002370 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002371 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002372 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002373 CallingConv::ID CallerCC = CallerF->getCallingConv();
2374 bool CCMatch = CallerCC == CalleeCC;
2375
Dan Gohman1797ed52010-02-08 20:27:50 +00002376 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002377 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002378 return true;
2379 return false;
2380 }
2381
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002382 // Look for obvious safe cases to perform tail call optimization that do not
2383 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002384
Evan Cheng2c12cb42010-03-26 16:26:03 +00002385 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2386 // emit a special epilogue.
2387 if (RegInfo->needsStackRealignment(MF))
2388 return false;
2389
Eric Christopher90eb4022010-07-22 00:26:08 +00002390 // Do not sibcall optimize vararg calls unless the call site is not passing
2391 // any arguments.
Evan Cheng3c262ee2010-03-26 02:13:13 +00002392 if (isVarArg && !Outs.empty())
Evan Cheng843bd692010-01-31 06:44:49 +00002393 return false;
2394
Evan Chenga375d472010-03-15 18:54:48 +00002395 // Also avoid sibcall optimization if either caller or callee uses struct
2396 // return semantics.
2397 if (isCalleeStructRet || isCallerStructRet)
2398 return false;
2399
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002400 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2401 // Therefore if it's not used by the call it is not safe to optimize this into
2402 // a sibcall.
2403 bool Unused = false;
2404 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2405 if (!Ins[i].Used) {
2406 Unused = true;
2407 break;
2408 }
2409 }
2410 if (Unused) {
2411 SmallVector<CCValAssign, 16> RVLocs;
2412 CCState CCInfo(CalleeCC, false, getTargetMachine(),
2413 RVLocs, *DAG.getContext());
2414 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002415 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002416 CCValAssign &VA = RVLocs[i];
2417 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2418 return false;
2419 }
2420 }
2421
Evan Cheng13617962010-04-30 01:12:32 +00002422 // If the calling conventions do not match, then we'd better make sure the
2423 // results are returned in the same way as what the caller expects.
2424 if (!CCMatch) {
2425 SmallVector<CCValAssign, 16> RVLocs1;
2426 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
2427 RVLocs1, *DAG.getContext());
2428 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2429
2430 SmallVector<CCValAssign, 16> RVLocs2;
2431 CCState CCInfo2(CallerCC, false, getTargetMachine(),
2432 RVLocs2, *DAG.getContext());
2433 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2434
2435 if (RVLocs1.size() != RVLocs2.size())
2436 return false;
2437 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2438 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2439 return false;
2440 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2441 return false;
2442 if (RVLocs1[i].isRegLoc()) {
2443 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2444 return false;
2445 } else {
2446 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2447 return false;
2448 }
2449 }
2450 }
2451
Evan Chenga6bff982010-01-30 01:22:00 +00002452 // If the callee takes no arguments then go on to check the results of the
2453 // call.
2454 if (!Outs.empty()) {
2455 // Check if stack adjustment is needed. For now, do not do this if any
2456 // argument is passed on the stack.
2457 SmallVector<CCValAssign, 16> ArgLocs;
2458 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
2459 ArgLocs, *DAG.getContext());
2460 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CalleeCC));
Evan Chengb2c92902010-02-02 02:22:50 +00002461 if (CCInfo.getNextStackOffset()) {
2462 MachineFunction &MF = DAG.getMachineFunction();
2463 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2464 return false;
2465 if (Subtarget->isTargetWin64())
2466 // Win64 ABI has additional complications.
2467 return false;
2468
2469 // Check if the arguments are already laid out in the right way as
2470 // the caller's fixed stack objects.
2471 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002472 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2473 const X86InstrInfo *TII =
2474 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002475 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2476 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002477 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002478 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002479 if (VA.getLocInfo() == CCValAssign::Indirect)
2480 return false;
2481 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002482 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2483 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002484 return false;
2485 }
2486 }
2487 }
Evan Cheng9c044672010-05-29 01:35:22 +00002488
2489 // If the tailcall address may be in a register, then make sure it's
2490 // possible to register allocate for it. In 32-bit, the call address can
2491 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002492 // callee-saved registers are restored. These happen to be the same
2493 // registers used to pass 'inreg' arguments so watch out for those.
2494 if (!Subtarget->is64Bit() &&
2495 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002496 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002497 unsigned NumInRegs = 0;
2498 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2499 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002500 if (!VA.isRegLoc())
2501 continue;
2502 unsigned Reg = VA.getLocReg();
2503 switch (Reg) {
2504 default: break;
2505 case X86::EAX: case X86::EDX: case X86::ECX:
2506 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002507 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002508 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002509 }
2510 }
2511 }
Evan Chenga6bff982010-01-30 01:22:00 +00002512 }
Evan Chengb1712452010-01-27 06:25:16 +00002513
Evan Cheng86809cc2010-02-03 03:28:02 +00002514 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002515}
2516
Dan Gohman3df24e62008-09-03 23:12:08 +00002517FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002518X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2519 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002520}
2521
2522
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002523//===----------------------------------------------------------------------===//
2524// Other Lowering Hooks
2525//===----------------------------------------------------------------------===//
2526
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002527static bool MayFoldLoad(SDValue Op) {
2528 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2529}
2530
2531static bool MayFoldIntoStore(SDValue Op) {
2532 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2533}
2534
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002535static bool isTargetShuffle(unsigned Opcode) {
2536 switch(Opcode) {
2537 default: return false;
2538 case X86ISD::PSHUFD:
2539 case X86ISD::PSHUFHW:
2540 case X86ISD::PSHUFLW:
2541 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002542 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002543 case X86ISD::SHUFPS:
2544 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002545 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002546 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002547 case X86ISD::MOVLPS:
2548 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002549 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002550 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002551 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002552 case X86ISD::MOVSS:
2553 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002554 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002555 case X86ISD::UNPCKLPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002556 case X86ISD::PUNPCKLWD:
2557 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002558 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002559 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002560 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002561 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002562 case X86ISD::PUNPCKHWD:
2563 case X86ISD::PUNPCKHBW:
2564 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002565 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002566 return true;
2567 }
2568 return false;
2569}
2570
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002571static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002572 SDValue V1, SelectionDAG &DAG) {
2573 switch(Opc) {
2574 default: llvm_unreachable("Unknown x86 shuffle node");
2575 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002576 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002577 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002578 return DAG.getNode(Opc, dl, VT, V1);
2579 }
2580
2581 return SDValue();
2582}
2583
2584static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002585 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002586 switch(Opc) {
2587 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002588 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002589 case X86ISD::PSHUFHW:
2590 case X86ISD::PSHUFLW:
2591 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2592 }
2593
2594 return SDValue();
2595}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002596
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002597static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2598 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2599 switch(Opc) {
2600 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002601 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002602 case X86ISD::SHUFPD:
2603 case X86ISD::SHUFPS:
2604 return DAG.getNode(Opc, dl, VT, V1, V2,
2605 DAG.getConstant(TargetMask, MVT::i8));
2606 }
2607 return SDValue();
2608}
2609
2610static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2611 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2612 switch(Opc) {
2613 default: llvm_unreachable("Unknown x86 shuffle node");
2614 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002615 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002616 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002617 case X86ISD::MOVLPS:
2618 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002619 case X86ISD::MOVSS:
2620 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002621 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002622 case X86ISD::UNPCKLPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002623 case X86ISD::PUNPCKLWD:
2624 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002625 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002626 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002627 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002628 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002629 case X86ISD::PUNPCKHWD:
2630 case X86ISD::PUNPCKHBW:
2631 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002632 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002633 return DAG.getNode(Opc, dl, VT, V1, V2);
2634 }
2635 return SDValue();
2636}
2637
Dan Gohmand858e902010-04-17 15:26:15 +00002638SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002639 MachineFunction &MF = DAG.getMachineFunction();
2640 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2641 int ReturnAddrIndex = FuncInfo->getRAIndex();
2642
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002643 if (ReturnAddrIndex == 0) {
2644 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002645 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002646 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002647 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002648 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002649 }
2650
Evan Cheng25ab6902006-09-08 06:48:29 +00002651 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002652}
2653
2654
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002655bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2656 bool hasSymbolicDisplacement) {
2657 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002658 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002659 return false;
2660
2661 // If we don't have a symbolic displacement - we don't have any extra
2662 // restrictions.
2663 if (!hasSymbolicDisplacement)
2664 return true;
2665
2666 // FIXME: Some tweaks might be needed for medium code model.
2667 if (M != CodeModel::Small && M != CodeModel::Kernel)
2668 return false;
2669
2670 // For small code model we assume that latest object is 16MB before end of 31
2671 // bits boundary. We may also accept pretty large negative constants knowing
2672 // that all objects are in the positive half of address space.
2673 if (M == CodeModel::Small && Offset < 16*1024*1024)
2674 return true;
2675
2676 // For kernel code model we know that all object resist in the negative half
2677 // of 32bits address space. We may not accept negative offsets, since they may
2678 // be just off and we may accept pretty large positive ones.
2679 if (M == CodeModel::Kernel && Offset > 0)
2680 return true;
2681
2682 return false;
2683}
2684
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002685/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2686/// specific condition code, returning the condition code and the LHS/RHS of the
2687/// comparison to make.
2688static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2689 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002690 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002691 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2692 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2693 // X > -1 -> X == 0, jump !sign.
2694 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002695 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002696 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2697 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002698 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002699 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002700 // X < 1 -> X <= 0
2701 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002702 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002703 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002704 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002705
Evan Chengd9558e02006-01-06 00:43:03 +00002706 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002707 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002708 case ISD::SETEQ: return X86::COND_E;
2709 case ISD::SETGT: return X86::COND_G;
2710 case ISD::SETGE: return X86::COND_GE;
2711 case ISD::SETLT: return X86::COND_L;
2712 case ISD::SETLE: return X86::COND_LE;
2713 case ISD::SETNE: return X86::COND_NE;
2714 case ISD::SETULT: return X86::COND_B;
2715 case ISD::SETUGT: return X86::COND_A;
2716 case ISD::SETULE: return X86::COND_BE;
2717 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002718 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002719 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002720
Chris Lattner4c78e022008-12-23 23:42:27 +00002721 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002722
Chris Lattner4c78e022008-12-23 23:42:27 +00002723 // If LHS is a foldable load, but RHS is not, flip the condition.
2724 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2725 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2726 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2727 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002728 }
2729
Chris Lattner4c78e022008-12-23 23:42:27 +00002730 switch (SetCCOpcode) {
2731 default: break;
2732 case ISD::SETOLT:
2733 case ISD::SETOLE:
2734 case ISD::SETUGT:
2735 case ISD::SETUGE:
2736 std::swap(LHS, RHS);
2737 break;
2738 }
2739
2740 // On a floating point condition, the flags are set as follows:
2741 // ZF PF CF op
2742 // 0 | 0 | 0 | X > Y
2743 // 0 | 0 | 1 | X < Y
2744 // 1 | 0 | 0 | X == Y
2745 // 1 | 1 | 1 | unordered
2746 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002747 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002748 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002749 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002750 case ISD::SETOLT: // flipped
2751 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002752 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002753 case ISD::SETOLE: // flipped
2754 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002755 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002756 case ISD::SETUGT: // flipped
2757 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002758 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002759 case ISD::SETUGE: // flipped
2760 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002761 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002762 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002763 case ISD::SETNE: return X86::COND_NE;
2764 case ISD::SETUO: return X86::COND_P;
2765 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002766 case ISD::SETOEQ:
2767 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002768 }
Evan Chengd9558e02006-01-06 00:43:03 +00002769}
2770
Evan Cheng4a460802006-01-11 00:33:36 +00002771/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2772/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002773/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002774static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002775 switch (X86CC) {
2776 default:
2777 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002778 case X86::COND_B:
2779 case X86::COND_BE:
2780 case X86::COND_E:
2781 case X86::COND_P:
2782 case X86::COND_A:
2783 case X86::COND_AE:
2784 case X86::COND_NE:
2785 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002786 return true;
2787 }
2788}
2789
Evan Chengeb2f9692009-10-27 19:56:55 +00002790/// isFPImmLegal - Returns true if the target can instruction select the
2791/// specified FP immediate natively. If false, the legalizer will
2792/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002793bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00002794 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
2795 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
2796 return true;
2797 }
2798 return false;
2799}
2800
Nate Begeman9008ca62009-04-27 18:41:29 +00002801/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2802/// the specified range (L, H].
2803static bool isUndefOrInRange(int Val, int Low, int Hi) {
2804 return (Val < 0) || (Val >= Low && Val < Hi);
2805}
2806
2807/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2808/// specified value.
2809static bool isUndefOrEqual(int Val, int CmpVal) {
2810 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002811 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002812 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002813}
2814
Nate Begeman9008ca62009-04-27 18:41:29 +00002815/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2816/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2817/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00002818static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00002819 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00002820 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002821 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00002822 return (Mask[0] < 2 && Mask[1] < 2);
2823 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002824}
2825
Nate Begeman9008ca62009-04-27 18:41:29 +00002826bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002827 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002828 N->getMask(M);
2829 return ::isPSHUFDMask(M, N->getValueType(0));
2830}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002831
Nate Begeman9008ca62009-04-27 18:41:29 +00002832/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2833/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00002834static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002835 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002836 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002837
Nate Begeman9008ca62009-04-27 18:41:29 +00002838 // Lower quadword copied in order or undef.
2839 for (int i = 0; i != 4; ++i)
2840 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002841 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002842
Evan Cheng506d3df2006-03-29 23:07:14 +00002843 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002844 for (int i = 4; i != 8; ++i)
2845 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002846 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002847
Evan Cheng506d3df2006-03-29 23:07:14 +00002848 return true;
2849}
2850
Nate Begeman9008ca62009-04-27 18:41:29 +00002851bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002852 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002853 N->getMask(M);
2854 return ::isPSHUFHWMask(M, N->getValueType(0));
2855}
Evan Cheng506d3df2006-03-29 23:07:14 +00002856
Nate Begeman9008ca62009-04-27 18:41:29 +00002857/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2858/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00002859static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002860 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002861 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002862
Rafael Espindola15684b22009-04-24 12:40:33 +00002863 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00002864 for (int i = 4; i != 8; ++i)
2865 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00002866 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002867
Rafael Espindola15684b22009-04-24 12:40:33 +00002868 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002869 for (int i = 0; i != 4; ++i)
2870 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00002871 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002872
Rafael Espindola15684b22009-04-24 12:40:33 +00002873 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002874}
2875
Nate Begeman9008ca62009-04-27 18:41:29 +00002876bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002877 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002878 N->getMask(M);
2879 return ::isPSHUFLWMask(M, N->getValueType(0));
2880}
2881
Nate Begemana09008b2009-10-19 02:17:23 +00002882/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
2883/// is suitable for input to PALIGNR.
2884static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
2885 bool hasSSSE3) {
2886 int i, e = VT.getVectorNumElements();
2887
2888 // Do not handle v2i64 / v2f64 shuffles with palignr.
2889 if (e < 4 || !hasSSSE3)
2890 return false;
2891
2892 for (i = 0; i != e; ++i)
2893 if (Mask[i] >= 0)
2894 break;
2895
2896 // All undef, not a palignr.
2897 if (i == e)
2898 return false;
2899
2900 // Determine if it's ok to perform a palignr with only the LHS, since we
2901 // don't have access to the actual shuffle elements to see if RHS is undef.
2902 bool Unary = Mask[i] < (int)e;
2903 bool NeedsUnary = false;
2904
2905 int s = Mask[i] - i;
2906
2907 // Check the rest of the elements to see if they are consecutive.
2908 for (++i; i != e; ++i) {
2909 int m = Mask[i];
2910 if (m < 0)
2911 continue;
2912
2913 Unary = Unary && (m < (int)e);
2914 NeedsUnary = NeedsUnary || (m < s);
2915
2916 if (NeedsUnary && !Unary)
2917 return false;
2918 if (Unary && m != ((s+i) & (e-1)))
2919 return false;
2920 if (!Unary && m != (s+i))
2921 return false;
2922 }
2923 return true;
2924}
2925
2926bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
2927 SmallVector<int, 8> M;
2928 N->getMask(M);
2929 return ::isPALIGNRMask(M, N->getValueType(0), true);
2930}
2931
Evan Cheng14aed5e2006-03-24 01:18:28 +00002932/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2933/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00002934static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002935 int NumElems = VT.getVectorNumElements();
2936 if (NumElems != 2 && NumElems != 4)
2937 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002938
Nate Begeman9008ca62009-04-27 18:41:29 +00002939 int Half = NumElems / 2;
2940 for (int i = 0; i < Half; ++i)
2941 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002942 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002943 for (int i = Half; i < NumElems; ++i)
2944 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002945 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002946
Evan Cheng14aed5e2006-03-24 01:18:28 +00002947 return true;
2948}
2949
Nate Begeman9008ca62009-04-27 18:41:29 +00002950bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
2951 SmallVector<int, 8> M;
2952 N->getMask(M);
2953 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002954}
2955
Evan Cheng213d2cf2007-05-17 18:45:50 +00002956/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002957/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2958/// half elements to come from vector 1 (which would equal the dest.) and
2959/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00002960static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002961 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002962
2963 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00002964 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002965
Nate Begeman9008ca62009-04-27 18:41:29 +00002966 int Half = NumElems / 2;
2967 for (int i = 0; i < Half; ++i)
2968 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002969 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002970 for (int i = Half; i < NumElems; ++i)
2971 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002972 return false;
2973 return true;
2974}
2975
Nate Begeman9008ca62009-04-27 18:41:29 +00002976static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
2977 SmallVector<int, 8> M;
2978 N->getMask(M);
2979 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002980}
2981
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002982/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2983/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002984bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
2985 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002986 return false;
2987
Evan Cheng2064a2b2006-03-28 06:50:32 +00002988 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00002989 return isUndefOrEqual(N->getMaskElt(0), 6) &&
2990 isUndefOrEqual(N->getMaskElt(1), 7) &&
2991 isUndefOrEqual(N->getMaskElt(2), 2) &&
2992 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002993}
2994
Nate Begeman0b10b912009-11-07 23:17:15 +00002995/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2996/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2997/// <2, 3, 2, 3>
2998bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
2999 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3000
3001 if (NumElems != 4)
3002 return false;
3003
3004 return isUndefOrEqual(N->getMaskElt(0), 2) &&
3005 isUndefOrEqual(N->getMaskElt(1), 3) &&
3006 isUndefOrEqual(N->getMaskElt(2), 2) &&
3007 isUndefOrEqual(N->getMaskElt(3), 3);
3008}
3009
Evan Cheng5ced1d82006-04-06 23:23:56 +00003010/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3011/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003012bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3013 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003014
Evan Cheng5ced1d82006-04-06 23:23:56 +00003015 if (NumElems != 2 && NumElems != 4)
3016 return false;
3017
Evan Chengc5cdff22006-04-07 21:53:05 +00003018 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003019 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003020 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003021
Evan Chengc5cdff22006-04-07 21:53:05 +00003022 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003023 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003024 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003025
3026 return true;
3027}
3028
Nate Begeman0b10b912009-11-07 23:17:15 +00003029/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3030/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3031bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003032 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003033
Evan Cheng5ced1d82006-04-06 23:23:56 +00003034 if (NumElems != 2 && NumElems != 4)
3035 return false;
3036
Evan Chengc5cdff22006-04-07 21:53:05 +00003037 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003038 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003039 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003040
Nate Begeman9008ca62009-04-27 18:41:29 +00003041 for (unsigned i = 0; i < NumElems/2; ++i)
3042 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003043 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003044
3045 return true;
3046}
3047
Evan Cheng0038e592006-03-28 00:39:58 +00003048/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3049/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003050static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003051 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003052 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003053 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00003054 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003055
Nate Begeman9008ca62009-04-27 18:41:29 +00003056 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3057 int BitI = Mask[i];
3058 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00003059 if (!isUndefOrEqual(BitI, j))
3060 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003061 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003062 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003063 return false;
3064 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003065 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003066 return false;
3067 }
Evan Cheng0038e592006-03-28 00:39:58 +00003068 }
Evan Cheng0038e592006-03-28 00:39:58 +00003069 return true;
3070}
3071
Nate Begeman9008ca62009-04-27 18:41:29 +00003072bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3073 SmallVector<int, 8> M;
3074 N->getMask(M);
3075 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003076}
3077
Evan Cheng4fcb9222006-03-28 02:43:26 +00003078/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3079/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003080static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003081 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003082 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003083 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003084 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003085
Nate Begeman9008ca62009-04-27 18:41:29 +00003086 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3087 int BitI = Mask[i];
3088 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00003089 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00003090 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003091 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00003092 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003093 return false;
3094 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003095 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003096 return false;
3097 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003098 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003099 return true;
3100}
3101
Nate Begeman9008ca62009-04-27 18:41:29 +00003102bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3103 SmallVector<int, 8> M;
3104 N->getMask(M);
3105 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003106}
3107
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003108/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3109/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3110/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003111static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003112 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003113 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +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 != NumElems; 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;
3121 if (!isUndefOrEqual(BitI1, j))
3122 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003123 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003124 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003125}
3126
Nate Begeman9008ca62009-04-27 18:41:29 +00003127bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3128 SmallVector<int, 8> M;
3129 N->getMask(M);
3130 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3131}
3132
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003133/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3134/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3135/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003136static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003137 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003138 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3139 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003140
Nate Begeman9008ca62009-04-27 18:41:29 +00003141 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3142 int BitI = Mask[i];
3143 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003144 if (!isUndefOrEqual(BitI, j))
3145 return false;
3146 if (!isUndefOrEqual(BitI1, j))
3147 return false;
3148 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003149 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003150}
3151
Nate Begeman9008ca62009-04-27 18:41:29 +00003152bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3153 SmallVector<int, 8> M;
3154 N->getMask(M);
3155 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3156}
3157
Evan Cheng017dcc62006-04-21 01:05:10 +00003158/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3159/// specifies a shuffle of elements that is suitable for input to MOVSS,
3160/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003161static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003162 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003163 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003164
3165 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003166
Nate Begeman9008ca62009-04-27 18:41:29 +00003167 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003168 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003169
Nate Begeman9008ca62009-04-27 18:41:29 +00003170 for (int i = 1; i < NumElts; ++i)
3171 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003172 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003173
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003174 return true;
3175}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003176
Nate Begeman9008ca62009-04-27 18:41:29 +00003177bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3178 SmallVector<int, 8> M;
3179 N->getMask(M);
3180 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003181}
3182
Evan Cheng017dcc62006-04-21 01:05:10 +00003183/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3184/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003185/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003186static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003187 bool V2IsSplat = false, bool V2IsUndef = false) {
3188 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003189 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003190 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003191
Nate Begeman9008ca62009-04-27 18:41:29 +00003192 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003193 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003194
Nate Begeman9008ca62009-04-27 18:41:29 +00003195 for (int i = 1; i < NumOps; ++i)
3196 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3197 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3198 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003199 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003200
Evan Cheng39623da2006-04-20 08:58:49 +00003201 return true;
3202}
3203
Nate Begeman9008ca62009-04-27 18:41:29 +00003204static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003205 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003206 SmallVector<int, 8> M;
3207 N->getMask(M);
3208 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003209}
3210
Evan Chengd9539472006-04-14 21:59:03 +00003211/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3212/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003213bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
3214 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003215 return false;
3216
3217 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00003218 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003219 int Elt = N->getMaskElt(i);
3220 if (Elt >= 0 && Elt != 1)
3221 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00003222 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003223
3224 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003225 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003226 int Elt = N->getMaskElt(i);
3227 if (Elt >= 0 && Elt != 3)
3228 return false;
3229 if (Elt == 3)
3230 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003231 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003232 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00003233 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003234 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003235}
3236
3237/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3238/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003239bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
3240 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003241 return false;
3242
3243 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00003244 for (unsigned i = 0; i < 2; ++i)
3245 if (N->getMaskElt(i) > 0)
3246 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003247
3248 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003249 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003250 int Elt = N->getMaskElt(i);
3251 if (Elt >= 0 && Elt != 2)
3252 return false;
3253 if (Elt == 2)
3254 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003255 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003256 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003257 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003258}
3259
Evan Cheng0b457f02008-09-25 20:50:48 +00003260/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3261/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003262bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3263 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003264
Nate Begeman9008ca62009-04-27 18:41:29 +00003265 for (int i = 0; i < e; ++i)
3266 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003267 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003268 for (int i = 0; i < e; ++i)
3269 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003270 return false;
3271 return true;
3272}
3273
Evan Cheng63d33002006-03-22 08:01:21 +00003274/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003275/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003276unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003277 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3278 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3279
Evan Chengb9df0ca2006-03-22 02:53:00 +00003280 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3281 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003282 for (int i = 0; i < NumOperands; ++i) {
3283 int Val = SVOp->getMaskElt(NumOperands-i-1);
3284 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003285 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003286 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003287 if (i != NumOperands - 1)
3288 Mask <<= Shift;
3289 }
Evan Cheng63d33002006-03-22 08:01:21 +00003290 return Mask;
3291}
3292
Evan Cheng506d3df2006-03-29 23:07:14 +00003293/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003294/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003295unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003296 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003297 unsigned Mask = 0;
3298 // 8 nodes, but we only care about the last 4.
3299 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003300 int Val = SVOp->getMaskElt(i);
3301 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003302 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003303 if (i != 4)
3304 Mask <<= 2;
3305 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003306 return Mask;
3307}
3308
3309/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003310/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003311unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003312 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003313 unsigned Mask = 0;
3314 // 8 nodes, but we only care about the first 4.
3315 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003316 int Val = SVOp->getMaskElt(i);
3317 if (Val >= 0)
3318 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003319 if (i != 0)
3320 Mask <<= 2;
3321 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003322 return Mask;
3323}
3324
Nate Begemana09008b2009-10-19 02:17:23 +00003325/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3326/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3327unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3328 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3329 EVT VVT = N->getValueType(0);
3330 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3331 int Val = 0;
3332
3333 unsigned i, e;
3334 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3335 Val = SVOp->getMaskElt(i);
3336 if (Val >= 0)
3337 break;
3338 }
3339 return (Val - i) * EltSize;
3340}
3341
Evan Cheng37b73872009-07-30 08:33:02 +00003342/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3343/// constant +0.0.
3344bool X86::isZeroNode(SDValue Elt) {
3345 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00003346 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00003347 (isa<ConstantFPSDNode>(Elt) &&
3348 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3349}
3350
Nate Begeman9008ca62009-04-27 18:41:29 +00003351/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3352/// their permute mask.
3353static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3354 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003355 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003356 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003357 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003358
Nate Begeman5a5ca152009-04-29 05:20:52 +00003359 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003360 int idx = SVOp->getMaskElt(i);
3361 if (idx < 0)
3362 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003363 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003364 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003365 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003366 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003367 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003368 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3369 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003370}
3371
Evan Cheng779ccea2007-12-07 21:30:01 +00003372/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3373/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003374static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003375 unsigned NumElems = VT.getVectorNumElements();
3376 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003377 int idx = Mask[i];
3378 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003379 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003380 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003381 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003382 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003383 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003384 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003385}
3386
Evan Cheng533a0aa2006-04-19 20:35:22 +00003387/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3388/// match movhlps. The lower half elements should come from upper half of
3389/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003390/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003391static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3392 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003393 return false;
3394 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003395 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003396 return false;
3397 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003398 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003399 return false;
3400 return true;
3401}
3402
Evan Cheng5ced1d82006-04-06 23:23:56 +00003403/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003404/// is promoted to a vector. It also returns the LoadSDNode by reference if
3405/// required.
3406static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003407 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3408 return false;
3409 N = N->getOperand(0).getNode();
3410 if (!ISD::isNON_EXTLoad(N))
3411 return false;
3412 if (LD)
3413 *LD = cast<LoadSDNode>(N);
3414 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003415}
3416
Evan Cheng533a0aa2006-04-19 20:35:22 +00003417/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3418/// match movlp{s|d}. The lower half elements should come from lower half of
3419/// V1 (and in order), and the upper half elements should come from the upper
3420/// half of V2 (and in order). And since V1 will become the source of the
3421/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003422static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3423 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003424 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003425 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003426 // Is V2 is a vector load, don't do this transformation. We will try to use
3427 // load folding shufps op.
3428 if (ISD::isNON_EXTLoad(V2))
3429 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003430
Nate Begeman5a5ca152009-04-29 05:20:52 +00003431 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003432
Evan Cheng533a0aa2006-04-19 20:35:22 +00003433 if (NumElems != 2 && NumElems != 4)
3434 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003435 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003436 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003437 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003438 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003439 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003440 return false;
3441 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003442}
3443
Evan Cheng39623da2006-04-20 08:58:49 +00003444/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3445/// all the same.
3446static bool isSplatVector(SDNode *N) {
3447 if (N->getOpcode() != ISD::BUILD_VECTOR)
3448 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003449
Dan Gohman475871a2008-07-27 21:46:04 +00003450 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003451 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3452 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003453 return false;
3454 return true;
3455}
3456
Evan Cheng213d2cf2007-05-17 18:45:50 +00003457/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003458/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003459/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003460static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003461 SDValue V1 = N->getOperand(0);
3462 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003463 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3464 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003465 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003466 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003467 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003468 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3469 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003470 if (Opc != ISD::BUILD_VECTOR ||
3471 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003472 return false;
3473 } else if (Idx >= 0) {
3474 unsigned Opc = V1.getOpcode();
3475 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3476 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003477 if (Opc != ISD::BUILD_VECTOR ||
3478 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003479 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003480 }
3481 }
3482 return true;
3483}
3484
3485/// getZeroVector - Returns a vector of specified type with all zero elements.
3486///
Owen Andersone50ed302009-08-10 22:56:29 +00003487static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003488 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003489 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003490
Dale Johannesen0488fb62010-09-30 23:57:10 +00003491 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003492 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003493 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003494 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003495 if (HasSSE2) { // SSE2
3496 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3497 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
3498 } else { // SSE1
3499 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3500 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
3501 }
3502 } else if (VT.getSizeInBits() == 256) { // AVX
3503 // 256-bit logic and arithmetic instructions in AVX are
3504 // all floating-point, no support for integer ops. Default
3505 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00003506 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003507 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
3508 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00003509 }
Dale Johannesenace16102009-02-03 19:33:06 +00003510 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003511}
3512
Chris Lattner8a594482007-11-25 00:24:49 +00003513/// getOnesVector - Returns a vector of specified type with all bits set.
3514///
Owen Andersone50ed302009-08-10 22:56:29 +00003515static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003516 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003517
Chris Lattner8a594482007-11-25 00:24:49 +00003518 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3519 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003520 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003521 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003522 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Dale Johannesenace16102009-02-03 19:33:06 +00003523 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003524}
3525
3526
Evan Cheng39623da2006-04-20 08:58:49 +00003527/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3528/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003529static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003530 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003531 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003532
Evan Cheng39623da2006-04-20 08:58:49 +00003533 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003534 SmallVector<int, 8> MaskVec;
3535 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003536
Nate Begeman5a5ca152009-04-29 05:20:52 +00003537 for (unsigned i = 0; i != NumElems; ++i) {
3538 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003539 MaskVec[i] = NumElems;
3540 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003541 }
Evan Cheng39623da2006-04-20 08:58:49 +00003542 }
Evan Cheng39623da2006-04-20 08:58:49 +00003543 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003544 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3545 SVOp->getOperand(1), &MaskVec[0]);
3546 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003547}
3548
Evan Cheng017dcc62006-04-21 01:05:10 +00003549/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3550/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003551static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003552 SDValue V2) {
3553 unsigned NumElems = VT.getVectorNumElements();
3554 SmallVector<int, 8> Mask;
3555 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003556 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003557 Mask.push_back(i);
3558 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003559}
3560
Nate Begeman9008ca62009-04-27 18:41:29 +00003561/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003562static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003563 SDValue V2) {
3564 unsigned NumElems = VT.getVectorNumElements();
3565 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003566 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003567 Mask.push_back(i);
3568 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003569 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003570 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003571}
3572
Nate Begeman9008ca62009-04-27 18:41:29 +00003573/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003574static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003575 SDValue V2) {
3576 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003577 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003578 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003579 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003580 Mask.push_back(i + Half);
3581 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003582 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003583 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003584}
3585
Bruno Cardoso Lopesbb0a9482010-08-13 17:50:47 +00003586/// PromoteSplat - Promote a splat of v4i32, v8i16 or v16i8 to v4f32.
3587static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003588 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003589 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003590 DebugLoc dl = SV->getDebugLoc();
3591 SDValue V1 = SV->getOperand(0);
3592 int NumElems = VT.getVectorNumElements();
3593 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003594
Nate Begeman9008ca62009-04-27 18:41:29 +00003595 // unpack elements to the correct location
3596 while (NumElems > 4) {
3597 if (EltNo < NumElems/2) {
3598 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3599 } else {
3600 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3601 EltNo -= NumElems/2;
3602 }
3603 NumElems >>= 1;
3604 }
Eric Christopherfd179292009-08-27 18:07:15 +00003605
Nate Begeman9008ca62009-04-27 18:41:29 +00003606 // Perform the splat.
3607 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Dale Johannesenace16102009-02-03 19:33:06 +00003608 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003609 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
3610 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003611}
3612
Evan Chengba05f722006-04-21 23:03:30 +00003613/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003614/// vector of zero or undef vector. This produces a shuffle where the low
3615/// element of V2 is swizzled into the zero/undef vector, landing at element
3616/// 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 +00003617static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003618 bool isZero, bool HasSSE2,
3619 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003620 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003621 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003622 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3623 unsigned NumElems = VT.getVectorNumElements();
3624 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003625 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003626 // If this is the insertion idx, put the low elt of V2 here.
3627 MaskVec.push_back(i == Idx ? NumElems : i);
3628 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003629}
3630
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003631/// getShuffleScalarElt - Returns the scalar element that will make up the ith
3632/// element of the result of the vector shuffle.
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003633SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
3634 unsigned Depth) {
3635 if (Depth == 6)
3636 return SDValue(); // Limit search depth.
3637
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003638 SDValue V = SDValue(N, 0);
3639 EVT VT = V.getValueType();
3640 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003641
3642 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
3643 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
3644 Index = SV->getMaskElt(Index);
3645
3646 if (Index < 0)
3647 return DAG.getUNDEF(VT.getVectorElementType());
3648
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003649 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003650 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003651 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003652 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003653
3654 // Recurse into target specific vector shuffles to find scalars.
3655 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003656 int NumElems = VT.getVectorNumElements();
3657 SmallVector<unsigned, 16> ShuffleMask;
3658 SDValue ImmN;
3659
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003660 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003661 case X86ISD::SHUFPS:
3662 case X86ISD::SHUFPD:
3663 ImmN = N->getOperand(N->getNumOperands()-1);
3664 DecodeSHUFPSMask(NumElems,
3665 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3666 ShuffleMask);
3667 break;
3668 case X86ISD::PUNPCKHBW:
3669 case X86ISD::PUNPCKHWD:
3670 case X86ISD::PUNPCKHDQ:
3671 case X86ISD::PUNPCKHQDQ:
3672 DecodePUNPCKHMask(NumElems, ShuffleMask);
3673 break;
3674 case X86ISD::UNPCKHPS:
3675 case X86ISD::UNPCKHPD:
3676 DecodeUNPCKHPMask(NumElems, ShuffleMask);
3677 break;
3678 case X86ISD::PUNPCKLBW:
3679 case X86ISD::PUNPCKLWD:
3680 case X86ISD::PUNPCKLDQ:
3681 case X86ISD::PUNPCKLQDQ:
3682 DecodePUNPCKLMask(NumElems, ShuffleMask);
3683 break;
3684 case X86ISD::UNPCKLPS:
3685 case X86ISD::UNPCKLPD:
3686 DecodeUNPCKLPMask(NumElems, ShuffleMask);
3687 break;
3688 case X86ISD::MOVHLPS:
3689 DecodeMOVHLPSMask(NumElems, ShuffleMask);
3690 break;
3691 case X86ISD::MOVLHPS:
3692 DecodeMOVLHPSMask(NumElems, ShuffleMask);
3693 break;
3694 case X86ISD::PSHUFD:
3695 ImmN = N->getOperand(N->getNumOperands()-1);
3696 DecodePSHUFMask(NumElems,
3697 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3698 ShuffleMask);
3699 break;
3700 case X86ISD::PSHUFHW:
3701 ImmN = N->getOperand(N->getNumOperands()-1);
3702 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3703 ShuffleMask);
3704 break;
3705 case X86ISD::PSHUFLW:
3706 ImmN = N->getOperand(N->getNumOperands()-1);
3707 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3708 ShuffleMask);
3709 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003710 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00003711 case X86ISD::MOVSD: {
3712 // The index 0 always comes from the first element of the second source,
3713 // this is why MOVSS and MOVSD are used in the first place. The other
3714 // elements come from the other positions of the first source vector.
3715 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003716 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
3717 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00003718 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003719 default:
3720 assert("not implemented for target shuffle node");
3721 return SDValue();
3722 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003723
3724 Index = ShuffleMask[Index];
3725 if (Index < 0)
3726 return DAG.getUNDEF(VT.getVectorElementType());
3727
3728 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
3729 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
3730 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003731 }
3732
3733 // Actual nodes that may contain scalar elements
3734 if (Opcode == ISD::BIT_CONVERT) {
3735 V = V.getOperand(0);
3736 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003737 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003738
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003739 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003740 return SDValue();
3741 }
3742
3743 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
3744 return (Index == 0) ? V.getOperand(0)
3745 : DAG.getUNDEF(VT.getVectorElementType());
3746
3747 if (V.getOpcode() == ISD::BUILD_VECTOR)
3748 return V.getOperand(Index);
3749
3750 return SDValue();
3751}
3752
3753/// getNumOfConsecutiveZeros - Return the number of elements of a vector
3754/// shuffle operation which come from a consecutively from a zero. The
3755/// search can start in two diferent directions, from left or right.
3756static
3757unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
3758 bool ZerosFromLeft, SelectionDAG &DAG) {
3759 int i = 0;
3760
3761 while (i < NumElems) {
3762 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003763 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003764 if (!(Elt.getNode() &&
3765 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
3766 break;
3767 ++i;
3768 }
3769
3770 return i;
3771}
3772
3773/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
3774/// MaskE correspond consecutively to elements from one of the vector operands,
3775/// starting from its index OpIdx. Also tell OpNum which source vector operand.
3776static
3777bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
3778 int OpIdx, int NumElems, unsigned &OpNum) {
3779 bool SeenV1 = false;
3780 bool SeenV2 = false;
3781
3782 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
3783 int Idx = SVOp->getMaskElt(i);
3784 // Ignore undef indicies
3785 if (Idx < 0)
3786 continue;
3787
3788 if (Idx < NumElems)
3789 SeenV1 = true;
3790 else
3791 SeenV2 = true;
3792
3793 // Only accept consecutive elements from the same vector
3794 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
3795 return false;
3796 }
3797
3798 OpNum = SeenV1 ? 0 : 1;
3799 return true;
3800}
3801
3802/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
3803/// logical left shift of a vector.
3804static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
3805 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
3806 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
3807 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
3808 false /* check zeros from right */, DAG);
3809 unsigned OpSrc;
3810
3811 if (!NumZeros)
3812 return false;
3813
3814 // Considering the elements in the mask that are not consecutive zeros,
3815 // check if they consecutively come from only one of the source vectors.
3816 //
3817 // V1 = {X, A, B, C} 0
3818 // \ \ \ /
3819 // vector_shuffle V1, V2 <1, 2, 3, X>
3820 //
3821 if (!isShuffleMaskConsecutive(SVOp,
3822 0, // Mask Start Index
3823 NumElems-NumZeros-1, // Mask End Index
3824 NumZeros, // Where to start looking in the src vector
3825 NumElems, // Number of elements in vector
3826 OpSrc)) // Which source operand ?
3827 return false;
3828
3829 isLeft = false;
3830 ShAmt = NumZeros;
3831 ShVal = SVOp->getOperand(OpSrc);
3832 return true;
3833}
3834
3835/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
3836/// logical left shift of a vector.
3837static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
3838 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
3839 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
3840 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
3841 true /* check zeros from left */, DAG);
3842 unsigned OpSrc;
3843
3844 if (!NumZeros)
3845 return false;
3846
3847 // Considering the elements in the mask that are not consecutive zeros,
3848 // check if they consecutively come from only one of the source vectors.
3849 //
3850 // 0 { A, B, X, X } = V2
3851 // / \ / /
3852 // vector_shuffle V1, V2 <X, X, 4, 5>
3853 //
3854 if (!isShuffleMaskConsecutive(SVOp,
3855 NumZeros, // Mask Start Index
3856 NumElems-1, // Mask End Index
3857 0, // Where to start looking in the src vector
3858 NumElems, // Number of elements in vector
3859 OpSrc)) // Which source operand ?
3860 return false;
3861
3862 isLeft = true;
3863 ShAmt = NumZeros;
3864 ShVal = SVOp->getOperand(OpSrc);
3865 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00003866}
3867
3868/// isVectorShift - Returns true if the shuffle can be implemented as a
3869/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003870static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00003871 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003872 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
3873 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
3874 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00003875
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003876 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00003877}
3878
Evan Chengc78d3b42006-04-24 18:01:45 +00003879/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3880///
Dan Gohman475871a2008-07-27 21:46:04 +00003881static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003882 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00003883 SelectionDAG &DAG,
3884 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003885 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00003886 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003887
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003888 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003889 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003890 bool First = true;
3891 for (unsigned i = 0; i < 16; ++i) {
3892 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3893 if (ThisIsNonZero && First) {
3894 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003895 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003896 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003897 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003898 First = false;
3899 }
3900
3901 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00003902 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003903 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3904 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003905 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003906 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00003907 }
3908 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003909 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
3910 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
3911 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00003912 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003913 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00003914 } else
3915 ThisElt = LastElt;
3916
Gabor Greifba36cb52008-08-28 21:40:38 +00003917 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00003918 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00003919 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00003920 }
3921 }
3922
Owen Anderson825b72b2009-08-11 20:47:22 +00003923 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00003924}
3925
Bill Wendlinga348c562007-03-22 18:42:45 +00003926/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003927///
Dan Gohman475871a2008-07-27 21:46:04 +00003928static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00003929 unsigned NumNonZero, unsigned NumZero,
3930 SelectionDAG &DAG,
3931 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003932 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00003933 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003934
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003935 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003936 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003937 bool First = true;
3938 for (unsigned i = 0; i < 8; ++i) {
3939 bool isNonZero = (NonZeros & (1 << i)) != 0;
3940 if (isNonZero) {
3941 if (First) {
3942 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003943 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003944 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003945 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003946 First = false;
3947 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003948 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003949 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00003950 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00003951 }
3952 }
3953
3954 return V;
3955}
3956
Evan Chengf26ffe92008-05-29 08:22:04 +00003957/// getVShift - Return a vector logical shift node.
3958///
Owen Andersone50ed302009-08-10 22:56:29 +00003959static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00003960 unsigned NumBits, SelectionDAG &DAG,
3961 const TargetLowering &TLI, DebugLoc dl) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003962 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00003963 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Dale Johannesenace16102009-02-03 19:33:06 +00003964 SrcOp = DAG.getNode(ISD::BIT_CONVERT, dl, ShVT, SrcOp);
3965 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3966 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00003967 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00003968}
3969
Dan Gohman475871a2008-07-27 21:46:04 +00003970SDValue
Evan Chengc3630942009-12-09 21:00:30 +00003971X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00003972 SelectionDAG &DAG) const {
Evan Chengc3630942009-12-09 21:00:30 +00003973
3974 // Check if the scalar load can be widened into a vector load. And if
3975 // the address is "base + cst" see if the cst can be "absorbed" into
3976 // the shuffle mask.
3977 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
3978 SDValue Ptr = LD->getBasePtr();
3979 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
3980 return SDValue();
3981 EVT PVT = LD->getValueType(0);
3982 if (PVT != MVT::i32 && PVT != MVT::f32)
3983 return SDValue();
3984
3985 int FI = -1;
3986 int64_t Offset = 0;
3987 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
3988 FI = FINode->getIndex();
3989 Offset = 0;
3990 } else if (Ptr.getOpcode() == ISD::ADD &&
3991 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
3992 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
3993 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
3994 Offset = Ptr.getConstantOperandVal(1);
3995 Ptr = Ptr.getOperand(0);
3996 } else {
3997 return SDValue();
3998 }
3999
4000 SDValue Chain = LD->getChain();
4001 // Make sure the stack object alignment is at least 16.
4002 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
4003 if (DAG.InferPtrAlignment(Ptr) < 16) {
4004 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004005 // Can't change the alignment. FIXME: It's possible to compute
4006 // the exact stack offset and reference FI + adjust offset instead.
4007 // If someone *really* cares about this. That's the way to implement it.
4008 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004009 } else {
4010 MFI->setObjectAlignment(FI, 16);
4011 }
4012 }
4013
4014 // (Offset % 16) must be multiple of 4. Then address is then
4015 // Ptr + (Offset & ~15).
4016 if (Offset < 0)
4017 return SDValue();
4018 if ((Offset % 16) & 3)
4019 return SDValue();
4020 int64_t StartOffset = Offset & ~15;
4021 if (StartOffset)
4022 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4023 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4024
4025 int EltNo = (Offset - StartOffset) >> 2;
4026 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
4027 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
Chris Lattner51abfe42010-09-21 06:02:19 +00004028 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,
4029 LD->getPointerInfo().getWithOffset(StartOffset),
David Greene67c9d422010-02-15 16:53:33 +00004030 false, false, 0);
Evan Chengc3630942009-12-09 21:00:30 +00004031 // Canonicalize it to a v4i32 shuffle.
4032 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32, V1);
4033 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4034 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
Chris Lattner51abfe42010-09-21 06:02:19 +00004035 DAG.getUNDEF(MVT::v4i32),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004036 }
4037
4038 return SDValue();
4039}
4040
Nate Begeman1449f292010-03-24 22:19:06 +00004041/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4042/// vector of type 'VT', see if the elements can be replaced by a single large
4043/// load which has the same value as a build_vector whose operands are 'elts'.
4044///
4045/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
4046///
4047/// FIXME: we'd also like to handle the case where the last elements are zero
4048/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4049/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004050static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004051 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004052 EVT EltVT = VT.getVectorElementType();
4053 unsigned NumElems = Elts.size();
4054
Nate Begemanfdea31a2010-03-24 20:49:50 +00004055 LoadSDNode *LDBase = NULL;
4056 unsigned LastLoadedElt = -1U;
Nate Begeman1449f292010-03-24 22:19:06 +00004057
4058 // For each element in the initializer, see if we've found a load or an undef.
4059 // If we don't find an initial load element, or later load elements are
4060 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004061 for (unsigned i = 0; i < NumElems; ++i) {
4062 SDValue Elt = Elts[i];
4063
4064 if (!Elt.getNode() ||
4065 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4066 return SDValue();
4067 if (!LDBase) {
4068 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4069 return SDValue();
4070 LDBase = cast<LoadSDNode>(Elt.getNode());
4071 LastLoadedElt = i;
4072 continue;
4073 }
4074 if (Elt.getOpcode() == ISD::UNDEF)
4075 continue;
4076
4077 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4078 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4079 return SDValue();
4080 LastLoadedElt = i;
4081 }
Nate Begeman1449f292010-03-24 22:19:06 +00004082
4083 // If we have found an entire vector of loads and undefs, then return a large
4084 // load of the entire vector width starting at the base pointer. If we found
4085 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004086 if (LastLoadedElt == NumElems - 1) {
4087 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004088 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004089 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004090 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004091 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004092 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004093 LDBase->isVolatile(), LDBase->isNonTemporal(),
4094 LDBase->getAlignment());
4095 } else if (NumElems == 4 && LastLoadedElt == 1) {
4096 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4097 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Chris Lattner88641552010-09-22 00:34:38 +00004098 SDValue ResNode = DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys,
4099 Ops, 2, MVT::i32,
4100 LDBase->getMemOperand());
4101 return DAG.getNode(ISD::BIT_CONVERT, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00004102 }
4103 return SDValue();
4104}
4105
Evan Chengc3630942009-12-09 21:00:30 +00004106SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004107X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004108 DebugLoc dl = Op.getDebugLoc();
Chris Lattner6e80e442010-08-28 17:15:43 +00004109 // All zero's are handled with pxor in SSE2 and above, xorps in SSE1.
4110 // All one's are handled with pcmpeqd. In AVX, zero's are handled with
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004111 // vpxor in 128-bit and xor{pd,ps} in 256-bit, but no 256 version of pcmpeqd
4112 // is present, so AllOnes is ignored.
4113 if (ISD::isBuildVectorAllZeros(Op.getNode()) ||
4114 (Op.getValueType().getSizeInBits() != 256 &&
4115 ISD::isBuildVectorAllOnes(Op.getNode()))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004116 // Canonicalize this to <4 x i32> (SSE) to
Chris Lattner8a594482007-11-25 00:24:49 +00004117 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
4118 // eliminated on x86-32 hosts.
Dale Johannesen0488fb62010-09-30 23:57:10 +00004119 if (Op.getValueType() == MVT::v4i32)
Chris Lattner8a594482007-11-25 00:24:49 +00004120 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004121
Gabor Greifba36cb52008-08-28 21:40:38 +00004122 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004123 return getOnesVector(Op.getValueType(), DAG, dl);
4124 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00004125 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004126
Owen Andersone50ed302009-08-10 22:56:29 +00004127 EVT VT = Op.getValueType();
4128 EVT ExtVT = VT.getVectorElementType();
4129 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004130
4131 unsigned NumElems = Op.getNumOperands();
4132 unsigned NumZero = 0;
4133 unsigned NumNonZero = 0;
4134 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004135 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004136 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004137 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004138 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00004139 if (Elt.getOpcode() == ISD::UNDEF)
4140 continue;
4141 Values.insert(Elt);
4142 if (Elt.getOpcode() != ISD::Constant &&
4143 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004144 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00004145 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00004146 NumZero++;
4147 else {
4148 NonZeros |= (1 << i);
4149 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004150 }
4151 }
4152
Chris Lattner97a2a562010-08-26 05:24:29 +00004153 // All undef vector. Return an UNDEF. All zero vectors were handled above.
4154 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00004155 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004156
Chris Lattner67f453a2008-03-09 05:42:06 +00004157 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00004158 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004159 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00004160 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00004161
Chris Lattner62098042008-03-09 01:05:04 +00004162 // If this is an insertion of an i64 value on x86-32, and if the top bits of
4163 // the value are obviously zero, truncate the value to i32 and do the
4164 // insertion that way. Only do this if the value is non-constant or if the
4165 // value is a constant being inserted into element 0. It is cheaper to do
4166 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00004167 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00004168 (!IsAllConstants || Idx == 0)) {
4169 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004170 // Handle SSE only.
4171 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
4172 EVT VecVT = MVT::v4i32;
4173 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00004174
Chris Lattner62098042008-03-09 01:05:04 +00004175 // Truncate the value (which may itself be a constant) to i32, and
4176 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00004177 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00004178 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00004179 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4180 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00004181
Chris Lattner62098042008-03-09 01:05:04 +00004182 // Now we have our 32-bit value zero extended in the low element of
4183 // a vector. If Idx != 0, swizzle it into place.
4184 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004185 SmallVector<int, 4> Mask;
4186 Mask.push_back(Idx);
4187 for (unsigned i = 1; i != VecElts; ++i)
4188 Mask.push_back(i);
4189 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00004190 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00004191 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004192 }
Dale Johannesenace16102009-02-03 19:33:06 +00004193 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00004194 }
4195 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004196
Chris Lattner19f79692008-03-08 22:59:52 +00004197 // If we have a constant or non-constant insertion into the low element of
4198 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
4199 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00004200 // depending on what the source datatype is.
4201 if (Idx == 0) {
4202 if (NumZero == 0) {
4203 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00004204 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
4205 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00004206 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
4207 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
4208 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
4209 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00004210 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
4211 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00004212 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
4213 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00004214 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
4215 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4216 Subtarget->hasSSE2(), DAG);
4217 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Item);
4218 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004219 }
Evan Chengf26ffe92008-05-29 08:22:04 +00004220
4221 // Is it a vector logical left shift?
4222 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00004223 X86::isZeroNode(Op.getOperand(0)) &&
4224 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004225 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00004226 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004227 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004228 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00004229 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004230 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004231
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004232 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00004233 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004234
Chris Lattner19f79692008-03-08 22:59:52 +00004235 // Otherwise, if this is a vector with i32 or f32 elements, and the element
4236 // is a non-constant being inserted into an element other than the low one,
4237 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
4238 // movd/movss) to move this into the low element, then shuffle it into
4239 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004240 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00004241 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00004242
Evan Cheng0db9fe62006-04-25 20:13:52 +00004243 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00004244 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
4245 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00004246 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004247 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00004248 MaskVec.push_back(i == Idx ? 0 : 1);
4249 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004250 }
4251 }
4252
Chris Lattner67f453a2008-03-09 05:42:06 +00004253 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00004254 if (Values.size() == 1) {
4255 if (EVTBits == 32) {
4256 // Instead of a shuffle like this:
4257 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
4258 // Check if it's possible to issue this instead.
4259 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
4260 unsigned Idx = CountTrailingZeros_32(NonZeros);
4261 SDValue Item = Op.getOperand(Idx);
4262 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
4263 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
4264 }
Dan Gohman475871a2008-07-27 21:46:04 +00004265 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004266 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004267
Dan Gohmana3941172007-07-24 22:55:08 +00004268 // A vector full of immediates; various special cases are already
4269 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004270 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00004271 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00004272
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00004273 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004274 if (EVTBits == 64) {
4275 if (NumNonZero == 1) {
4276 // One half is zero or undef.
4277 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00004278 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00004279 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00004280 return getShuffleVectorZeroOrUndef(V2, Idx, true,
4281 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00004282 }
Dan Gohman475871a2008-07-27 21:46:04 +00004283 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00004284 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004285
4286 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00004287 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004288 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00004289 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004290 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004291 }
4292
Bill Wendling826f36f2007-03-28 00:57:11 +00004293 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00004294 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00004295 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004296 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004297 }
4298
4299 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004300 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00004301 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004302 if (NumElems == 4 && NumZero > 0) {
4303 for (unsigned i = 0; i < 4; ++i) {
4304 bool isZero = !(NonZeros & (1 << i));
4305 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00004306 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004307 else
Dale Johannesenace16102009-02-03 19:33:06 +00004308 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004309 }
4310
4311 for (unsigned i = 0; i < 2; ++i) {
4312 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
4313 default: break;
4314 case 0:
4315 V[i] = V[i*2]; // Must be a zero vector.
4316 break;
4317 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00004318 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004319 break;
4320 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00004321 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004322 break;
4323 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00004324 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004325 break;
4326 }
4327 }
4328
Nate Begeman9008ca62009-04-27 18:41:29 +00004329 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004330 bool Reverse = (NonZeros & 0x3) == 2;
4331 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004332 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004333 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
4334 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004335 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
4336 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004337 }
4338
Nate Begemanfdea31a2010-03-24 20:49:50 +00004339 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
4340 // Check for a build vector of consecutive loads.
4341 for (unsigned i = 0; i < NumElems; ++i)
4342 V[i] = Op.getOperand(i);
4343
4344 // Check for elements which are consecutive loads.
4345 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
4346 if (LD.getNode())
4347 return LD;
4348
Chris Lattner24faf612010-08-28 17:59:08 +00004349 // For SSE 4.1, use insertps to put the high elements into the low element.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004350 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00004351 SDValue Result;
4352 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
4353 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
4354 else
4355 Result = DAG.getUNDEF(VT);
4356
4357 for (unsigned i = 1; i < NumElems; ++i) {
4358 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
4359 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00004360 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00004361 }
4362 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00004363 }
Nate Begemanfdea31a2010-03-24 20:49:50 +00004364
Chris Lattner6e80e442010-08-28 17:15:43 +00004365 // Otherwise, expand into a number of unpckl*, start by extending each of
4366 // our (non-undef) elements to the full vector width with the element in the
4367 // bottom slot of the vector (which generates no code for SSE).
4368 for (unsigned i = 0; i < NumElems; ++i) {
4369 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
4370 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
4371 else
4372 V[i] = DAG.getUNDEF(VT);
4373 }
4374
4375 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004376 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
4377 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
4378 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00004379 unsigned EltStride = NumElems >> 1;
4380 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00004381 for (unsigned i = 0; i < EltStride; ++i) {
4382 // If V[i+EltStride] is undef and this is the first round of mixing,
4383 // then it is safe to just drop this shuffle: V[i] is already in the
4384 // right place, the one element (since it's the first round) being
4385 // inserted as undef can be dropped. This isn't safe for successive
4386 // rounds because they will permute elements within both vectors.
4387 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
4388 EltStride == NumElems/2)
4389 continue;
4390
Chris Lattner6e80e442010-08-28 17:15:43 +00004391 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00004392 }
Chris Lattner6e80e442010-08-28 17:15:43 +00004393 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004394 }
4395 return V[0];
4396 }
Dan Gohman475871a2008-07-27 21:46:04 +00004397 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004398}
4399
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004400SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004401X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004402 // We support concatenate two MMX registers and place them in a MMX
4403 // register. This is better than doing a stack convert.
4404 DebugLoc dl = Op.getDebugLoc();
4405 EVT ResVT = Op.getValueType();
4406 assert(Op.getNumOperands() == 2);
4407 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
4408 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
4409 int Mask[2];
4410 SDValue InVec = DAG.getNode(ISD::BIT_CONVERT,dl, MVT::v1i64, Op.getOperand(0));
4411 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4412 InVec = Op.getOperand(1);
4413 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4414 unsigned NumElts = ResVT.getVectorNumElements();
4415 VecOp = DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
4416 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
4417 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
4418 } else {
4419 InVec = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v1i64, InVec);
4420 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4421 Mask[0] = 0; Mask[1] = 2;
4422 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
4423 }
4424 return DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
4425}
4426
Nate Begemanb9a47b82009-02-23 08:49:38 +00004427// v8i16 shuffles - Prefer shuffles in the following order:
4428// 1. [all] pshuflw, pshufhw, optional move
4429// 2. [ssse3] 1 x pshufb
4430// 3. [ssse3] 2 x pshufb + 1 x por
4431// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004432SDValue
4433X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
4434 SelectionDAG &DAG) const {
4435 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00004436 SDValue V1 = SVOp->getOperand(0);
4437 SDValue V2 = SVOp->getOperand(1);
4438 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004439 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00004440
Nate Begemanb9a47b82009-02-23 08:49:38 +00004441 // Determine if more than 1 of the words in each of the low and high quadwords
4442 // of the result come from the same quadword of one of the two inputs. Undef
4443 // mask values count as coming from any quadword, for better codegen.
4444 SmallVector<unsigned, 4> LoQuad(4);
4445 SmallVector<unsigned, 4> HiQuad(4);
4446 BitVector InputQuads(4);
4447 for (unsigned i = 0; i < 8; ++i) {
4448 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00004449 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004450 MaskVals.push_back(EltIdx);
4451 if (EltIdx < 0) {
4452 ++Quad[0];
4453 ++Quad[1];
4454 ++Quad[2];
4455 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00004456 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004457 }
4458 ++Quad[EltIdx / 4];
4459 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00004460 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004461
Nate Begemanb9a47b82009-02-23 08:49:38 +00004462 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004463 unsigned MaxQuad = 1;
4464 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004465 if (LoQuad[i] > MaxQuad) {
4466 BestLoQuad = i;
4467 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004468 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004469 }
4470
Nate Begemanb9a47b82009-02-23 08:49:38 +00004471 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004472 MaxQuad = 1;
4473 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004474 if (HiQuad[i] > MaxQuad) {
4475 BestHiQuad = i;
4476 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004477 }
4478 }
4479
Nate Begemanb9a47b82009-02-23 08:49:38 +00004480 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00004481 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00004482 // single pshufb instruction is necessary. If There are more than 2 input
4483 // quads, disable the next transformation since it does not help SSSE3.
4484 bool V1Used = InputQuads[0] || InputQuads[1];
4485 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004486 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004487 if (InputQuads.count() == 2 && V1Used && V2Used) {
4488 BestLoQuad = InputQuads.find_first();
4489 BestHiQuad = InputQuads.find_next(BestLoQuad);
4490 }
4491 if (InputQuads.count() > 2) {
4492 BestLoQuad = -1;
4493 BestHiQuad = -1;
4494 }
4495 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004496
Nate Begemanb9a47b82009-02-23 08:49:38 +00004497 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
4498 // the shuffle mask. If a quad is scored as -1, that means that it contains
4499 // words from all 4 input quadwords.
4500 SDValue NewV;
4501 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004502 SmallVector<int, 8> MaskV;
4503 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
4504 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00004505 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004506 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V1),
4507 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V2), &MaskV[0]);
4508 NewV = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004509
Nate Begemanb9a47b82009-02-23 08:49:38 +00004510 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
4511 // source words for the shuffle, to aid later transformations.
4512 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00004513 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00004514 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004515 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00004516 if (idx != (int)i)
4517 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004518 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00004519 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004520 AllWordsInNewV = false;
4521 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00004522 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004523
Nate Begemanb9a47b82009-02-23 08:49:38 +00004524 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
4525 if (AllWordsInNewV) {
4526 for (int i = 0; i != 8; ++i) {
4527 int idx = MaskVals[i];
4528 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004529 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004530 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004531 if ((idx != i) && idx < 4)
4532 pshufhw = false;
4533 if ((idx != i) && idx > 3)
4534 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00004535 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00004536 V1 = NewV;
4537 V2Used = false;
4538 BestLoQuad = 0;
4539 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004540 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004541
Nate Begemanb9a47b82009-02-23 08:49:38 +00004542 // If we've eliminated the use of V2, and the new mask is a pshuflw or
4543 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00004544 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004545 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
4546 unsigned TargetMask = 0;
4547 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00004548 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004549 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
4550 X86::getShufflePSHUFLWImmediate(NewV.getNode());
4551 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004552 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00004553 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004554 }
Eric Christopherfd179292009-08-27 18:07:15 +00004555
Nate Begemanb9a47b82009-02-23 08:49:38 +00004556 // If we have SSSE3, and all words of the result are from 1 input vector,
4557 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
4558 // is present, fall back to case 4.
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004559 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004560 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004561
Nate Begemanb9a47b82009-02-23 08:49:38 +00004562 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00004563 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00004564 // mask, and elements that come from V1 in the V2 mask, so that the two
4565 // results can be OR'd together.
4566 bool TwoInputs = V1Used && V2Used;
4567 for (unsigned i = 0; i != 8; ++i) {
4568 int EltIdx = MaskVals[i] * 2;
4569 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004570 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4571 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004572 continue;
4573 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004574 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
4575 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004576 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004577 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004578 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004579 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004580 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004581 if (!TwoInputs)
Owen Anderson825b72b2009-08-11 20:47:22 +00004582 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004583
Nate Begemanb9a47b82009-02-23 08:49:38 +00004584 // Calculate the shuffle mask for the second input, shuffle it, and
4585 // OR it with the first shuffled input.
4586 pshufbMask.clear();
4587 for (unsigned i = 0; i != 8; ++i) {
4588 int EltIdx = MaskVals[i] * 2;
4589 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004590 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4591 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004592 continue;
4593 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004594 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
4595 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004596 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004597 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00004598 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004599 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004600 MVT::v16i8, &pshufbMask[0], 16));
4601 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
4602 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004603 }
4604
4605 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
4606 // and update MaskVals with new element order.
4607 BitVector InOrder(8);
4608 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004609 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004610 for (int i = 0; i != 4; ++i) {
4611 int idx = MaskVals[i];
4612 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004613 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004614 InOrder.set(i);
4615 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004616 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004617 InOrder.set(i);
4618 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004619 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004620 }
4621 }
4622 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004623 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00004624 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004625 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004626
4627 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4628 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
4629 NewV.getOperand(0),
4630 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
4631 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004632 }
Eric Christopherfd179292009-08-27 18:07:15 +00004633
Nate Begemanb9a47b82009-02-23 08:49:38 +00004634 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
4635 // and update MaskVals with the new element order.
4636 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004637 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004638 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004639 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004640 for (unsigned i = 4; i != 8; ++i) {
4641 int idx = MaskVals[i];
4642 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004643 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004644 InOrder.set(i);
4645 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004646 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004647 InOrder.set(i);
4648 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004649 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004650 }
4651 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004652 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004653 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004654
4655 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4656 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
4657 NewV.getOperand(0),
4658 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
4659 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004660 }
Eric Christopherfd179292009-08-27 18:07:15 +00004661
Nate Begemanb9a47b82009-02-23 08:49:38 +00004662 // In case BestHi & BestLo were both -1, which means each quadword has a word
4663 // from each of the four input quadwords, calculate the InOrder bitvector now
4664 // before falling through to the insert/extract cleanup.
4665 if (BestLoQuad == -1 && BestHiQuad == -1) {
4666 NewV = V1;
4667 for (int i = 0; i != 8; ++i)
4668 if (MaskVals[i] < 0 || MaskVals[i] == i)
4669 InOrder.set(i);
4670 }
Eric Christopherfd179292009-08-27 18:07:15 +00004671
Nate Begemanb9a47b82009-02-23 08:49:38 +00004672 // The other elements are put in the right place using pextrw and pinsrw.
4673 for (unsigned i = 0; i != 8; ++i) {
4674 if (InOrder[i])
4675 continue;
4676 int EltIdx = MaskVals[i];
4677 if (EltIdx < 0)
4678 continue;
4679 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004680 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004681 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00004682 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004683 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004684 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004685 DAG.getIntPtrConstant(i));
4686 }
4687 return NewV;
4688}
4689
4690// v16i8 shuffles - Prefer shuffles in the following order:
4691// 1. [ssse3] 1 x pshufb
4692// 2. [ssse3] 2 x pshufb + 1 x por
4693// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
4694static
Nate Begeman9008ca62009-04-27 18:41:29 +00004695SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00004696 SelectionDAG &DAG,
4697 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004698 SDValue V1 = SVOp->getOperand(0);
4699 SDValue V2 = SVOp->getOperand(1);
4700 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004701 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00004702 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00004703
Nate Begemanb9a47b82009-02-23 08:49:38 +00004704 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00004705 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00004706 // present, fall back to case 3.
4707 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
4708 bool V1Only = true;
4709 bool V2Only = true;
4710 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004711 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00004712 if (EltIdx < 0)
4713 continue;
4714 if (EltIdx < 16)
4715 V2Only = false;
4716 else
4717 V1Only = false;
4718 }
Eric Christopherfd179292009-08-27 18:07:15 +00004719
Nate Begemanb9a47b82009-02-23 08:49:38 +00004720 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
4721 if (TLI.getSubtarget()->hasSSSE3()) {
4722 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004723
Nate Begemanb9a47b82009-02-23 08:49:38 +00004724 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00004725 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004726 //
4727 // Otherwise, we have elements from both input vectors, and must zero out
4728 // elements that come from V2 in the first mask, and V1 in the second mask
4729 // so that we can OR them together.
4730 bool TwoInputs = !(V1Only || V2Only);
4731 for (unsigned i = 0; i != 16; ++i) {
4732 int EltIdx = MaskVals[i];
4733 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004734 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004735 continue;
4736 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004737 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004738 }
4739 // If all the elements are from V2, assign it to V1 and return after
4740 // building the first pshufb.
4741 if (V2Only)
4742 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00004743 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004744 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004745 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004746 if (!TwoInputs)
4747 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00004748
Nate Begemanb9a47b82009-02-23 08:49:38 +00004749 // Calculate the shuffle mask for the second input, shuffle it, and
4750 // OR it with the first shuffled input.
4751 pshufbMask.clear();
4752 for (unsigned i = 0; i != 16; ++i) {
4753 int EltIdx = MaskVals[i];
4754 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004755 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004756 continue;
4757 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004758 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004759 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004760 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004761 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004762 MVT::v16i8, &pshufbMask[0], 16));
4763 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004764 }
Eric Christopherfd179292009-08-27 18:07:15 +00004765
Nate Begemanb9a47b82009-02-23 08:49:38 +00004766 // No SSSE3 - Calculate in place words and then fix all out of place words
4767 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
4768 // the 16 different words that comprise the two doublequadword input vectors.
Owen Anderson825b72b2009-08-11 20:47:22 +00004769 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
4770 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004771 SDValue NewV = V2Only ? V2 : V1;
4772 for (int i = 0; i != 8; ++i) {
4773 int Elt0 = MaskVals[i*2];
4774 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00004775
Nate Begemanb9a47b82009-02-23 08:49:38 +00004776 // This word of the result is all undef, skip it.
4777 if (Elt0 < 0 && Elt1 < 0)
4778 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004779
Nate Begemanb9a47b82009-02-23 08:49:38 +00004780 // This word of the result is already in the correct place, skip it.
4781 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
4782 continue;
4783 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
4784 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004785
Nate Begemanb9a47b82009-02-23 08:49:38 +00004786 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
4787 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
4788 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00004789
4790 // If Elt0 and Elt1 are defined, are consecutive, and can be load
4791 // using a single extract together, load it and store it.
4792 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004793 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004794 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00004795 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004796 DAG.getIntPtrConstant(i));
4797 continue;
4798 }
4799
Nate Begemanb9a47b82009-02-23 08:49:38 +00004800 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00004801 // source byte is not also odd, shift the extracted word left 8 bits
4802 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004803 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004804 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004805 DAG.getIntPtrConstant(Elt1 / 2));
4806 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004807 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004808 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004809 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004810 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
4811 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004812 }
4813 // If Elt0 is defined, extract it from the appropriate source. If the
4814 // source byte is not also even, shift the extracted word right 8 bits. If
4815 // Elt1 was also defined, OR the extracted values together before
4816 // inserting them in the result.
4817 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004818 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004819 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
4820 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004821 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004822 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004823 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004824 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
4825 DAG.getConstant(0x00FF, MVT::i16));
4826 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00004827 : InsElt0;
4828 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004829 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004830 DAG.getIntPtrConstant(i));
4831 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004832 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004833}
4834
Evan Cheng7a831ce2007-12-15 03:00:47 +00004835/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00004836/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00004837/// done when every pair / quad of shuffle mask elements point to elements in
4838/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00004839/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00004840static
Nate Begeman9008ca62009-04-27 18:41:29 +00004841SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00004842 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00004843 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00004844 SDValue V1 = SVOp->getOperand(0);
4845 SDValue V2 = SVOp->getOperand(1);
4846 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00004847 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00004848 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00004849 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004850 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004851 case MVT::v4f32: NewVT = MVT::v2f64; break;
4852 case MVT::v4i32: NewVT = MVT::v2i64; break;
4853 case MVT::v8i16: NewVT = MVT::v4i32; break;
4854 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004855 }
4856
Nate Begeman9008ca62009-04-27 18:41:29 +00004857 int Scale = NumElems / NewWidth;
4858 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00004859 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004860 int StartIdx = -1;
4861 for (int j = 0; j < Scale; ++j) {
4862 int EltIdx = SVOp->getMaskElt(i+j);
4863 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004864 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00004865 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00004866 StartIdx = EltIdx - (EltIdx % Scale);
4867 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00004868 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004869 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004870 if (StartIdx == -1)
4871 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00004872 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004873 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004874 }
4875
Dale Johannesenace16102009-02-03 19:33:06 +00004876 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V1);
4877 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00004878 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004879}
4880
Evan Chengd880b972008-05-09 21:53:03 +00004881/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004882///
Owen Andersone50ed302009-08-10 22:56:29 +00004883static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00004884 SDValue SrcOp, SelectionDAG &DAG,
4885 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004886 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004887 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00004888 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00004889 LD = dyn_cast<LoadSDNode>(SrcOp);
4890 if (!LD) {
4891 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
4892 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00004893 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
4894 if ((ExtVT.SimpleTy != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00004895 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4896 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00004897 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004898 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00004899 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Dale Johannesenace16102009-02-03 19:33:06 +00004900 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4901 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
4902 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
4903 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00004904 SrcOp.getOperand(0)
4905 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004906 }
4907 }
4908 }
4909
Dale Johannesenace16102009-02-03 19:33:06 +00004910 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4911 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004912 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00004913 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004914}
4915
Evan Chengace3c172008-07-22 21:13:36 +00004916/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
4917/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004918static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00004919LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
4920 SDValue V1 = SVOp->getOperand(0);
4921 SDValue V2 = SVOp->getOperand(1);
4922 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004923 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00004924
Evan Chengace3c172008-07-22 21:13:36 +00004925 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00004926 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00004927 SmallVector<int, 8> Mask1(4U, -1);
4928 SmallVector<int, 8> PermMask;
4929 SVOp->getMask(PermMask);
4930
Evan Chengace3c172008-07-22 21:13:36 +00004931 unsigned NumHi = 0;
4932 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00004933 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004934 int Idx = PermMask[i];
4935 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004936 Locs[i] = std::make_pair(-1, -1);
4937 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004938 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
4939 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004940 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00004941 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004942 NumLo++;
4943 } else {
4944 Locs[i] = std::make_pair(1, NumHi);
4945 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004946 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004947 NumHi++;
4948 }
4949 }
4950 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004951
Evan Chengace3c172008-07-22 21:13:36 +00004952 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004953 // If no more than two elements come from either vector. This can be
4954 // implemented with two shuffles. First shuffle gather the elements.
4955 // The second shuffle, which takes the first shuffle as both of its
4956 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00004957 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004958
Nate Begeman9008ca62009-04-27 18:41:29 +00004959 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00004960
Evan Chengace3c172008-07-22 21:13:36 +00004961 for (unsigned i = 0; i != 4; ++i) {
4962 if (Locs[i].first == -1)
4963 continue;
4964 else {
4965 unsigned Idx = (i < 2) ? 0 : 4;
4966 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004967 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004968 }
4969 }
4970
Nate Begeman9008ca62009-04-27 18:41:29 +00004971 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004972 } else if (NumLo == 3 || NumHi == 3) {
4973 // Otherwise, we must have three elements from one vector, call it X, and
4974 // one element from the other, call it Y. First, use a shufps to build an
4975 // intermediate vector with the one element from Y and the element from X
4976 // that will be in the same half in the final destination (the indexes don't
4977 // matter). Then, use a shufps to build the final vector, taking the half
4978 // containing the element from Y from the intermediate, and the other half
4979 // from X.
4980 if (NumHi == 3) {
4981 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00004982 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004983 std::swap(V1, V2);
4984 }
4985
4986 // Find the element from V2.
4987 unsigned HiIndex;
4988 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004989 int Val = PermMask[HiIndex];
4990 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004991 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004992 if (Val >= 4)
4993 break;
4994 }
4995
Nate Begeman9008ca62009-04-27 18:41:29 +00004996 Mask1[0] = PermMask[HiIndex];
4997 Mask1[1] = -1;
4998 Mask1[2] = PermMask[HiIndex^1];
4999 Mask1[3] = -1;
5000 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005001
5002 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005003 Mask1[0] = PermMask[0];
5004 Mask1[1] = PermMask[1];
5005 Mask1[2] = HiIndex & 1 ? 6 : 4;
5006 Mask1[3] = HiIndex & 1 ? 4 : 6;
5007 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005008 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005009 Mask1[0] = HiIndex & 1 ? 2 : 0;
5010 Mask1[1] = HiIndex & 1 ? 0 : 2;
5011 Mask1[2] = PermMask[2];
5012 Mask1[3] = PermMask[3];
5013 if (Mask1[2] >= 0)
5014 Mask1[2] += 4;
5015 if (Mask1[3] >= 0)
5016 Mask1[3] += 4;
5017 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005018 }
Evan Chengace3c172008-07-22 21:13:36 +00005019 }
5020
5021 // Break it into (shuffle shuffle_hi, shuffle_lo).
5022 Locs.clear();
Nate Begeman9008ca62009-04-27 18:41:29 +00005023 SmallVector<int,8> LoMask(4U, -1);
5024 SmallVector<int,8> HiMask(4U, -1);
5025
5026 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00005027 unsigned MaskIdx = 0;
5028 unsigned LoIdx = 0;
5029 unsigned HiIdx = 2;
5030 for (unsigned i = 0; i != 4; ++i) {
5031 if (i == 2) {
5032 MaskPtr = &HiMask;
5033 MaskIdx = 1;
5034 LoIdx = 0;
5035 HiIdx = 2;
5036 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005037 int Idx = PermMask[i];
5038 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005039 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00005040 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005041 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005042 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005043 LoIdx++;
5044 } else {
5045 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005046 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005047 HiIdx++;
5048 }
5049 }
5050
Nate Begeman9008ca62009-04-27 18:41:29 +00005051 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
5052 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
5053 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00005054 for (unsigned i = 0; i != 4; ++i) {
5055 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005056 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00005057 } else {
5058 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005059 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00005060 }
5061 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005062 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00005063}
5064
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005065static bool MayFoldVectorLoad(SDValue V) {
5066 if (V.hasOneUse() && V.getOpcode() == ISD::BIT_CONVERT)
5067 V = V.getOperand(0);
5068 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5069 V = V.getOperand(0);
5070 if (MayFoldLoad(V))
5071 return true;
5072 return false;
5073}
5074
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005075// FIXME: the version above should always be used. Since there's
5076// a bug where several vector shuffles can't be folded because the
5077// DAG is not updated during lowering and a node claims to have two
5078// uses while it only has one, use this version, and let isel match
5079// another instruction if the load really happens to have more than
5080// one use. Remove this version after this bug get fixed.
5081static bool RelaxedMayFoldVectorLoad(SDValue V) {
5082 if (V.hasOneUse() && V.getOpcode() == ISD::BIT_CONVERT)
5083 V = V.getOperand(0);
5084 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5085 V = V.getOperand(0);
5086 if (ISD::isNormalLoad(V.getNode()))
5087 return true;
5088 return false;
5089}
5090
5091/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
5092/// a vector extract, and if both can be later optimized into a single load.
5093/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
5094/// here because otherwise a target specific shuffle node is going to be
5095/// emitted for this shuffle, and the optimization not done.
5096/// FIXME: This is probably not the best approach, but fix the problem
5097/// until the right path is decided.
5098static
5099bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
5100 const TargetLowering &TLI) {
5101 EVT VT = V.getValueType();
5102 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
5103
5104 // Be sure that the vector shuffle is present in a pattern like this:
5105 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
5106 if (!V.hasOneUse())
5107 return false;
5108
5109 SDNode *N = *V.getNode()->use_begin();
5110 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5111 return false;
5112
5113 SDValue EltNo = N->getOperand(1);
5114 if (!isa<ConstantSDNode>(EltNo))
5115 return false;
5116
5117 // If the bit convert changed the number of elements, it is unsafe
5118 // to examine the mask.
5119 bool HasShuffleIntoBitcast = false;
5120 if (V.getOpcode() == ISD::BIT_CONVERT) {
5121 EVT SrcVT = V.getOperand(0).getValueType();
5122 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
5123 return false;
5124 V = V.getOperand(0);
5125 HasShuffleIntoBitcast = true;
5126 }
5127
5128 // Select the input vector, guarding against out of range extract vector.
5129 unsigned NumElems = VT.getVectorNumElements();
5130 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
5131 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
5132 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
5133
5134 // Skip one more bit_convert if necessary
5135 if (V.getOpcode() == ISD::BIT_CONVERT)
5136 V = V.getOperand(0);
5137
5138 if (ISD::isNormalLoad(V.getNode())) {
5139 // Is the original load suitable?
5140 LoadSDNode *LN0 = cast<LoadSDNode>(V);
5141
5142 // FIXME: avoid the multi-use bug that is preventing lots of
5143 // of foldings to be detected, this is still wrong of course, but
5144 // give the temporary desired behavior, and if it happens that
5145 // the load has real more uses, during isel it will not fold, and
5146 // will generate poor code.
5147 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
5148 return false;
5149
5150 if (!HasShuffleIntoBitcast)
5151 return true;
5152
5153 // If there's a bitcast before the shuffle, check if the load type and
5154 // alignment is valid.
5155 unsigned Align = LN0->getAlignment();
5156 unsigned NewAlign =
5157 TLI.getTargetData()->getABITypeAlignment(
5158 VT.getTypeForEVT(*DAG.getContext()));
5159
5160 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
5161 return false;
5162 }
5163
5164 return true;
5165}
5166
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005167static
5168SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
5169 bool HasSSE2) {
5170 SDValue V1 = Op.getOperand(0);
5171 SDValue V2 = Op.getOperand(1);
5172 EVT VT = Op.getValueType();
5173
5174 assert(VT != MVT::v2i64 && "unsupported shuffle type");
5175
5176 if (HasSSE2 && VT == MVT::v2f64)
5177 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
5178
5179 // v4f32 or v4i32
5180 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V2, DAG);
5181}
5182
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005183static
5184SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
5185 SDValue V1 = Op.getOperand(0);
5186 SDValue V2 = Op.getOperand(1);
5187 EVT VT = Op.getValueType();
5188
5189 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
5190 "unsupported shuffle type");
5191
5192 if (V2.getOpcode() == ISD::UNDEF)
5193 V2 = V1;
5194
5195 // v4i32 or v4f32
5196 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
5197}
5198
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005199static
5200SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
5201 SDValue V1 = Op.getOperand(0);
5202 SDValue V2 = Op.getOperand(1);
5203 EVT VT = Op.getValueType();
5204 unsigned NumElems = VT.getVectorNumElements();
5205
5206 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
5207 // operand of these instructions is only memory, so check if there's a
5208 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
5209 // same masks.
5210 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005211
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00005212 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005213 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005214 CanFoldLoad = true;
5215
5216 // When V1 is a load, it can be folded later into a store in isel, example:
5217 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
5218 // turns into:
5219 // (MOVLPSmr addr:$src1, VR128:$src2)
5220 // So, recognize this potential and also use MOVLPS or MOVLPD
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005221 if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005222 CanFoldLoad = true;
5223
5224 if (CanFoldLoad) {
5225 if (HasSSE2 && NumElems == 2)
5226 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
5227
5228 if (NumElems == 4)
5229 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
5230 }
5231
5232 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5233 // movl and movlp will both match v2i64, but v2i64 is never matched by
5234 // movl earlier because we make it strict to avoid messing with the movlp load
5235 // folding logic (see the code above getMOVLP call). Match it here then,
5236 // this is horrible, but will stay like this until we move all shuffle
5237 // matching to x86 specific nodes. Note that for the 1st condition all
5238 // types are matched with movsd.
5239 if ((HasSSE2 && NumElems == 2) || !X86::isMOVLMask(SVOp))
5240 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5241 else if (HasSSE2)
5242 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5243
5244
5245 assert(VT != MVT::v4i32 && "unsupported shuffle type");
5246
5247 // Invert the operand order and use SHUFPS to match it.
5248 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V2, V1,
5249 X86::getShuffleSHUFImmediate(SVOp), DAG);
5250}
5251
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005252static inline unsigned getUNPCKLOpcode(EVT VT) {
5253 switch(VT.getSimpleVT().SimpleTy) {
5254 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
5255 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
5256 case MVT::v4f32: return X86ISD::UNPCKLPS;
5257 case MVT::v2f64: return X86ISD::UNPCKLPD;
5258 case MVT::v16i8: return X86ISD::PUNPCKLBW;
5259 case MVT::v8i16: return X86ISD::PUNPCKLWD;
5260 default:
5261 llvm_unreachable("Unknow type for unpckl");
5262 }
5263 return 0;
5264}
5265
5266static inline unsigned getUNPCKHOpcode(EVT VT) {
5267 switch(VT.getSimpleVT().SimpleTy) {
5268 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
5269 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
5270 case MVT::v4f32: return X86ISD::UNPCKHPS;
5271 case MVT::v2f64: return X86ISD::UNPCKHPD;
5272 case MVT::v16i8: return X86ISD::PUNPCKHBW;
5273 case MVT::v8i16: return X86ISD::PUNPCKHWD;
5274 default:
5275 llvm_unreachable("Unknow type for unpckh");
5276 }
5277 return 0;
5278}
5279
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005280static
5281SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005282 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005283 const X86Subtarget *Subtarget) {
5284 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5285 EVT VT = Op.getValueType();
5286 DebugLoc dl = Op.getDebugLoc();
5287 SDValue V1 = Op.getOperand(0);
5288 SDValue V2 = Op.getOperand(1);
5289
5290 if (isZeroShuffle(SVOp))
5291 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
5292
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005293 // Handle splat operations
5294 if (SVOp->isSplat()) {
5295 // Special case, this is the only place now where it's
5296 // allowed to return a vector_shuffle operation without
5297 // using a target specific node, because *hopefully* it
5298 // will be optimized away by the dag combiner.
5299 if (VT.getVectorNumElements() <= 4 &&
5300 CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
5301 return Op;
5302
5303 // Handle splats by matching through known masks
5304 if (VT.getVectorNumElements() <= 4)
5305 return SDValue();
5306
5307 // Canonize all of the remaining to v4f32.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005308 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005309 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005310
5311 // If the shuffle can be profitably rewritten as a narrower shuffle, then
5312 // do it!
5313 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
5314 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5315 if (NewOp.getNode())
5316 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, NewOp);
5317 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
5318 // FIXME: Figure out a cleaner way to do this.
5319 // Try to make use of movq to zero out the top part.
5320 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
5321 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5322 if (NewOp.getNode()) {
5323 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
5324 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
5325 DAG, Subtarget, dl);
5326 }
5327 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
5328 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5329 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
5330 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
5331 DAG, Subtarget, dl);
5332 }
5333 }
5334 return SDValue();
5335}
5336
Dan Gohman475871a2008-07-27 21:46:04 +00005337SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005338X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00005339 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00005340 SDValue V1 = Op.getOperand(0);
5341 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005342 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005343 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00005344 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005345 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005346 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
5347 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00005348 bool V1IsSplat = false;
5349 bool V2IsSplat = false;
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005350 bool HasSSE2 = Subtarget->hasSSE2() || Subtarget->hasAVX();
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005351 bool HasSSE3 = Subtarget->hasSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005352 bool HasSSSE3 = Subtarget->hasSSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005353 MachineFunction &MF = DAG.getMachineFunction();
5354 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005355
Dale Johannesen0488fb62010-09-30 23:57:10 +00005356 // Shuffle operations on MMX not supported.
5357 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00005358 return Op;
5359
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005360 // Vector shuffle lowering takes 3 steps:
5361 //
5362 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
5363 // narrowing and commutation of operands should be handled.
5364 // 2) Matching of shuffles with known shuffle masks to x86 target specific
5365 // shuffle nodes.
5366 // 3) Rewriting of unmatched masks into new generic shuffle operations,
5367 // so the shuffle can be broken into other shuffles and the legalizer can
5368 // try the lowering again.
5369 //
5370 // The general ideia is that no vector_shuffle operation should be left to
5371 // be matched during isel, all of them must be converted to a target specific
5372 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00005373
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005374 // Normalize the input vectors. Here splats, zeroed vectors, profitable
5375 // narrowing and commutation of operands should be handled. The actual code
5376 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005377 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005378 if (NewOp.getNode())
5379 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00005380
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005381 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
5382 // unpckh_undef). Only use pshufd if speed is more important than size.
5383 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
5384 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5385 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
5386 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
5387 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5388 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00005389
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005390 if (X86::isMOVDDUPMask(SVOp) && HasSSE3 && V2IsUndef &&
Dale Johannesen0488fb62010-09-30 23:57:10 +00005391 RelaxedMayFoldVectorLoad(V1))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005392 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
5393
Dale Johannesen0488fb62010-09-30 23:57:10 +00005394 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005395 return getMOVHighToLow(Op, dl, DAG);
5396
5397 // Use to match splats
5398 if (HasSSE2 && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
5399 (VT == MVT::v2f64 || VT == MVT::v2i64))
5400 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5401
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005402 if (X86::isPSHUFDMask(SVOp)) {
5403 // The actual implementation will match the mask in the if above and then
5404 // during isel it can match several different instructions, not only pshufd
5405 // as its name says, sad but true, emulate the behavior for now...
5406 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
5407 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
5408
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005409 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5410
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005411 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005412 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
5413
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005414 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005415 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V1,
5416 TargetMask, DAG);
5417
5418 if (VT == MVT::v4f32)
5419 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V1,
5420 TargetMask, DAG);
5421 }
Eric Christopherfd179292009-08-27 18:07:15 +00005422
Evan Chengf26ffe92008-05-29 08:22:04 +00005423 // Check if this can be converted into a logical shift.
5424 bool isLeft = false;
5425 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00005426 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00005427 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00005428 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00005429 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005430 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00005431 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005432 EVT EltVT = VT.getVectorElementType();
5433 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005434 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005435 }
Eric Christopherfd179292009-08-27 18:07:15 +00005436
Nate Begeman9008ca62009-04-27 18:41:29 +00005437 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005438 if (V1IsUndef)
5439 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00005440 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00005441 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005442 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005443 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005444 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5445
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005446 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005447 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5448 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00005449 }
Eric Christopherfd179292009-08-27 18:07:15 +00005450
Nate Begeman9008ca62009-04-27 18:41:29 +00005451 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00005452 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
5453 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005454
Dale Johannesen0488fb62010-09-30 23:57:10 +00005455 if (X86::isMOVHLPSMask(SVOp))
5456 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005457
Dale Johannesen0488fb62010-09-30 23:57:10 +00005458 if (X86::isMOVSHDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5459 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005460
Dale Johannesen0488fb62010-09-30 23:57:10 +00005461 if (X86::isMOVSLDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5462 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00005463
Dale Johannesen0488fb62010-09-30 23:57:10 +00005464 if (X86::isMOVLPMask(SVOp))
5465 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005466
Nate Begeman9008ca62009-04-27 18:41:29 +00005467 if (ShouldXformToMOVHLPS(SVOp) ||
5468 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
5469 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005470
Evan Chengf26ffe92008-05-29 08:22:04 +00005471 if (isShift) {
5472 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005473 EVT EltVT = VT.getVectorElementType();
5474 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005475 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005476 }
Eric Christopherfd179292009-08-27 18:07:15 +00005477
Evan Cheng9eca5e82006-10-25 21:49:50 +00005478 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00005479 // FIXME: This should also accept a bitcast of a splat? Be careful, not
5480 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00005481 V1IsSplat = isSplatVector(V1.getNode());
5482 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005483
Chris Lattner8a594482007-11-25 00:24:49 +00005484 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00005485 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005486 Op = CommuteVectorShuffle(SVOp, DAG);
5487 SVOp = cast<ShuffleVectorSDNode>(Op);
5488 V1 = SVOp->getOperand(0);
5489 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00005490 std::swap(V1IsSplat, V2IsSplat);
5491 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005492 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00005493 }
5494
Nate Begeman9008ca62009-04-27 18:41:29 +00005495 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
5496 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00005497 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00005498 return V1;
5499 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
5500 // the instruction selector will not match, so get a canonical MOVL with
5501 // swapped operands to undo the commute.
5502 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00005503 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005504
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005505 if (X86::isUNPCKLMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005506 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005507
5508 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005509 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00005510
Evan Cheng9bbbb982006-10-25 20:48:19 +00005511 if (V2IsSplat) {
5512 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005513 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00005514 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00005515 SDValue NewMask = NormalizeMask(SVOp, DAG);
5516 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
5517 if (NSVOp != SVOp) {
5518 if (X86::isUNPCKLMask(NSVOp, true)) {
5519 return NewMask;
5520 } else if (X86::isUNPCKHMask(NSVOp, true)) {
5521 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005522 }
5523 }
5524 }
5525
Evan Cheng9eca5e82006-10-25 21:49:50 +00005526 if (Commuted) {
5527 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00005528 // FIXME: this seems wrong.
5529 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
5530 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005531
5532 if (X86::isUNPCKLMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005533 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005534
5535 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005536 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005537 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005538
Nate Begeman9008ca62009-04-27 18:41:29 +00005539 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00005540 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00005541 return CommuteVectorShuffle(SVOp, DAG);
5542
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00005543 // The checks below are all present in isShuffleMaskLegal, but they are
5544 // inlined here right now to enable us to directly emit target specific
5545 // nodes, and remove one by one until they don't return Op anymore.
5546 SmallVector<int, 16> M;
5547 SVOp->getMask(M);
5548
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005549 if (isPALIGNRMask(M, VT, HasSSSE3))
5550 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
5551 X86::getShufflePALIGNRImmediate(SVOp),
5552 DAG);
5553
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00005554 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
5555 SVOp->getSplatIndex() == 0 && V2IsUndef) {
5556 if (VT == MVT::v2f64)
5557 return getTargetShuffleNode(X86ISD::UNPCKLPD, dl, VT, V1, V1, DAG);
5558 if (VT == MVT::v2i64)
5559 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
5560 }
5561
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00005562 if (isPSHUFHWMask(M, VT))
5563 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
5564 X86::getShufflePSHUFHWImmediate(SVOp),
5565 DAG);
5566
5567 if (isPSHUFLWMask(M, VT))
5568 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
5569 X86::getShufflePSHUFLWImmediate(SVOp),
5570 DAG);
5571
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00005572 if (isSHUFPMask(M, VT)) {
5573 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5574 if (VT == MVT::v4f32 || VT == MVT::v4i32)
5575 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V2,
5576 TargetMask, DAG);
5577 if (VT == MVT::v2f64 || VT == MVT::v2i64)
5578 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V2,
5579 TargetMask, DAG);
5580 }
5581
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005582 if (X86::isUNPCKL_v_undef_Mask(SVOp))
5583 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5584 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
5585 if (X86::isUNPCKH_v_undef_Mask(SVOp))
5586 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5587 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5588
Evan Cheng14b32e12007-12-11 01:46:18 +00005589 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00005590 if (VT == MVT::v8i16) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005591 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005592 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00005593 return NewOp;
5594 }
5595
Owen Anderson825b72b2009-08-11 20:47:22 +00005596 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005597 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005598 if (NewOp.getNode())
5599 return NewOp;
5600 }
Eric Christopherfd179292009-08-27 18:07:15 +00005601
Dale Johannesen0488fb62010-09-30 23:57:10 +00005602 // Handle all 4 wide cases with a number of shuffles.
5603 if (NumElems == 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005604 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005605
Dan Gohman475871a2008-07-27 21:46:04 +00005606 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005607}
5608
Dan Gohman475871a2008-07-27 21:46:04 +00005609SDValue
5610X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00005611 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005612 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005613 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005614 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005615 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005616 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005617 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005618 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005619 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005620 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00005621 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
5622 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
5623 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005624 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
5625 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Dale Johannesenace16102009-02-03 19:33:06 +00005626 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005627 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00005628 Op.getOperand(0)),
5629 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005630 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005631 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005632 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005633 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005634 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00005635 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00005636 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
5637 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005638 // result has a single use which is a store or a bitcast to i32. And in
5639 // the case of a store, it's not worth it if the index is a constant 0,
5640 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00005641 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00005642 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00005643 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005644 if ((User->getOpcode() != ISD::STORE ||
5645 (isa<ConstantSDNode>(Op.getOperand(1)) &&
5646 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman171c11e2008-04-16 02:32:24 +00005647 (User->getOpcode() != ISD::BIT_CONVERT ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005648 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00005649 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00005650 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
5651 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00005652 Op.getOperand(0)),
5653 Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005654 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Extract);
5655 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00005656 // ExtractPS works with constant index.
5657 if (isa<ConstantSDNode>(Op.getOperand(1)))
5658 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00005659 }
Dan Gohman475871a2008-07-27 21:46:04 +00005660 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005661}
5662
5663
Dan Gohman475871a2008-07-27 21:46:04 +00005664SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005665X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
5666 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005667 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00005668 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005669
Evan Cheng62a3f152008-03-24 21:52:23 +00005670 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005671 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005672 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00005673 return Res;
5674 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00005675
Owen Andersone50ed302009-08-10 22:56:29 +00005676 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005677 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005678 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005679 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005680 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005681 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005682 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005683 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
5684 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Scott Michelfdc40a02009-02-17 22:15:04 +00005685 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005686 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00005687 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005688 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00005689 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00005690 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00005691 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00005692 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00005693 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005694 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005695 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005696 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005697 if (Idx == 0)
5698 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00005699
Evan Cheng0db9fe62006-04-25 20:13:52 +00005700 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00005701 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00005702 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00005703 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00005704 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00005705 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00005706 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00005707 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00005708 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
5709 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
5710 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005711 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005712 if (Idx == 0)
5713 return Op;
5714
5715 // UNPCKHPD the element to the lowest double word, then movsd.
5716 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
5717 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00005718 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00005719 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00005720 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00005721 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00005722 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00005723 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005724 }
5725
Dan Gohman475871a2008-07-27 21:46:04 +00005726 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005727}
5728
Dan Gohman475871a2008-07-27 21:46:04 +00005729SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005730X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
5731 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005732 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00005733 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005734 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005735
Dan Gohman475871a2008-07-27 21:46:04 +00005736 SDValue N0 = Op.getOperand(0);
5737 SDValue N1 = Op.getOperand(1);
5738 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00005739
Dan Gohman8a55ce42009-09-23 21:02:20 +00005740 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00005741 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00005742 unsigned Opc;
5743 if (VT == MVT::v8i16)
5744 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00005745 else if (VT == MVT::v16i8)
5746 Opc = X86ISD::PINSRB;
5747 else
5748 Opc = X86ISD::PINSRB;
5749
Nate Begeman14d12ca2008-02-11 04:19:36 +00005750 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
5751 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00005752 if (N1.getValueType() != MVT::i32)
5753 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
5754 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005755 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00005756 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00005757 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00005758 // Bits [7:6] of the constant are the source select. This will always be
5759 // zero here. The DAG Combiner may combine an extract_elt index into these
5760 // bits. For example (insert (extract, 3), 2) could be matched by putting
5761 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00005762 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00005763 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00005764 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00005765 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005766 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00005767 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00005768 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00005769 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00005770 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00005771 // PINSR* works with constant index.
5772 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00005773 }
Dan Gohman475871a2008-07-27 21:46:04 +00005774 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005775}
5776
Dan Gohman475871a2008-07-27 21:46:04 +00005777SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005778X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005779 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00005780 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005781
5782 if (Subtarget->hasSSE41())
5783 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
5784
Dan Gohman8a55ce42009-09-23 21:02:20 +00005785 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00005786 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00005787
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005788 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005789 SDValue N0 = Op.getOperand(0);
5790 SDValue N1 = Op.getOperand(1);
5791 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00005792
Dan Gohman8a55ce42009-09-23 21:02:20 +00005793 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00005794 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
5795 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00005796 if (N1.getValueType() != MVT::i32)
5797 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
5798 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005799 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00005800 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005801 }
Dan Gohman475871a2008-07-27 21:46:04 +00005802 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005803}
5804
Dan Gohman475871a2008-07-27 21:46:04 +00005805SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005806X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005807 DebugLoc dl = Op.getDebugLoc();
Chris Lattnerf172ecd2010-07-04 23:07:25 +00005808
5809 if (Op.getValueType() == MVT::v1i64 &&
5810 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00005811 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00005812
Owen Anderson825b72b2009-08-11 20:47:22 +00005813 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00005814 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
5815 "Expected an SSE type!");
Dale Johannesenace16102009-02-03 19:33:06 +00005816 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00005817 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005818}
5819
Bill Wendling056292f2008-09-16 21:48:12 +00005820// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
5821// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
5822// one of the above mentioned nodes. It has to be wrapped because otherwise
5823// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
5824// be used to form addressing mode. These wrapped nodes will be selected
5825// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00005826SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005827X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005828 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00005829
Chris Lattner41621a22009-06-26 19:22:52 +00005830 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5831 // global base reg.
5832 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005833 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005834 CodeModel::Model M = getTargetMachine().getCodeModel();
5835
Chris Lattner4f066492009-07-11 20:29:19 +00005836 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005837 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005838 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005839 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005840 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005841 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005842 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005843
Evan Cheng1606e8e2009-03-13 07:51:59 +00005844 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00005845 CP->getAlignment(),
5846 CP->getOffset(), OpFlag);
5847 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00005848 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005849 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00005850 if (OpFlag) {
5851 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005852 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005853 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005854 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005855 }
5856
5857 return Result;
5858}
5859
Dan Gohmand858e902010-04-17 15:26:15 +00005860SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00005861 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00005862
Chris Lattner18c59872009-06-27 04:16:01 +00005863 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5864 // global base reg.
5865 unsigned char OpFlag = 0;
5866 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005867 CodeModel::Model M = getTargetMachine().getCodeModel();
5868
Chris Lattner4f066492009-07-11 20:29:19 +00005869 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005870 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005871 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005872 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005873 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005874 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005875 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005876
Chris Lattner18c59872009-06-27 04:16:01 +00005877 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
5878 OpFlag);
5879 DebugLoc DL = JT->getDebugLoc();
5880 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005881
Chris Lattner18c59872009-06-27 04:16:01 +00005882 // With PIC, the address is actually $g + Offset.
5883 if (OpFlag) {
5884 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5885 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005886 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00005887 Result);
5888 }
Eric Christopherfd179292009-08-27 18:07:15 +00005889
Chris Lattner18c59872009-06-27 04:16:01 +00005890 return Result;
5891}
5892
5893SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005894X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00005895 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00005896
Chris Lattner18c59872009-06-27 04:16:01 +00005897 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5898 // global base reg.
5899 unsigned char OpFlag = 0;
5900 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005901 CodeModel::Model M = getTargetMachine().getCodeModel();
5902
Chris Lattner4f066492009-07-11 20:29:19 +00005903 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005904 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005905 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005906 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005907 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005908 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005909 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005910
Chris Lattner18c59872009-06-27 04:16:01 +00005911 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00005912
Chris Lattner18c59872009-06-27 04:16:01 +00005913 DebugLoc DL = Op.getDebugLoc();
5914 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005915
5916
Chris Lattner18c59872009-06-27 04:16:01 +00005917 // With PIC, the address is actually $g + Offset.
5918 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00005919 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00005920 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5921 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005922 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00005923 Result);
5924 }
Eric Christopherfd179292009-08-27 18:07:15 +00005925
Chris Lattner18c59872009-06-27 04:16:01 +00005926 return Result;
5927}
5928
Dan Gohman475871a2008-07-27 21:46:04 +00005929SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005930X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00005931 // Create the TargetBlockAddressAddress node.
5932 unsigned char OpFlags =
5933 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00005934 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00005935 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00005936 DebugLoc dl = Op.getDebugLoc();
5937 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
5938 /*isTarget=*/true, OpFlags);
5939
Dan Gohmanf705adb2009-10-30 01:28:02 +00005940 if (Subtarget->isPICStyleRIPRel() &&
5941 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00005942 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
5943 else
5944 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00005945
Dan Gohman29cbade2009-11-20 23:18:13 +00005946 // With PIC, the address is actually $g + Offset.
5947 if (isGlobalRelativeToPICBase(OpFlags)) {
5948 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5949 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
5950 Result);
5951 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00005952
5953 return Result;
5954}
5955
5956SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00005957X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00005958 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00005959 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00005960 // Create the TargetGlobalAddress node, folding in the constant
5961 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00005962 unsigned char OpFlags =
5963 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005964 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00005965 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005966 if (OpFlags == X86II::MO_NO_FLAG &&
5967 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00005968 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00005969 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00005970 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005971 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00005972 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00005973 }
Eric Christopherfd179292009-08-27 18:07:15 +00005974
Chris Lattner4f066492009-07-11 20:29:19 +00005975 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005976 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00005977 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
5978 else
5979 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00005980
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005981 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00005982 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00005983 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5984 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005985 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005986 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005987
Chris Lattner36c25012009-07-10 07:34:39 +00005988 // For globals that require a load from a stub to get the address, emit the
5989 // load.
5990 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00005991 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00005992 MachinePointerInfo::getGOT(), false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005993
Dan Gohman6520e202008-10-18 02:06:02 +00005994 // If there was a non-zero offset that we didn't fold, create an explicit
5995 // addition for it.
5996 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00005997 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00005998 DAG.getConstant(Offset, getPointerTy()));
5999
Evan Cheng0db9fe62006-04-25 20:13:52 +00006000 return Result;
6001}
6002
Evan Chengda43bcf2008-09-24 00:05:32 +00006003SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006004X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00006005 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00006006 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006007 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00006008}
6009
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006010static SDValue
6011GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00006012 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00006013 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006014 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Owen Anderson825b72b2009-08-11 20:47:22 +00006015 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006016 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006017 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006018 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006019 GA->getOffset(),
6020 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006021 if (InFlag) {
6022 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006023 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006024 } else {
6025 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006026 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006027 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006028
6029 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00006030 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006031
Rafael Espindola15f1b662009-04-24 12:59:40 +00006032 SDValue Flag = Chain.getValue(1);
6033 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006034}
6035
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006036// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006037static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006038LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006039 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00006040 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00006041 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
6042 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006043 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006044 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006045 InFlag = Chain.getValue(1);
6046
Chris Lattnerb903bed2009-06-26 21:20:29 +00006047 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006048}
6049
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006050// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006051static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006052LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006053 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006054 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
6055 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006056}
6057
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006058// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
6059// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00006060static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006061 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00006062 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006063 DebugLoc dl = GA->getDebugLoc();
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006064
6065 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
6066 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
6067 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00006068
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006069 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
6070 DAG.getIntPtrConstant(0),
6071 MachinePointerInfo(Ptr), false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00006072
Chris Lattnerb903bed2009-06-26 21:20:29 +00006073 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006074 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
6075 // initialexec.
6076 unsigned WrapperKind = X86ISD::Wrapper;
6077 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006078 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00006079 } else if (is64Bit) {
6080 assert(model == TLSModel::InitialExec);
6081 OperandFlags = X86II::MO_GOTTPOFF;
6082 WrapperKind = X86ISD::WrapperRIP;
6083 } else {
6084 assert(model == TLSModel::InitialExec);
6085 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00006086 }
Eric Christopherfd179292009-08-27 18:07:15 +00006087
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006088 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
6089 // exec)
Devang Patel0d881da2010-07-06 22:08:15 +00006090 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
6091 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006092 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006093 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006094
Rafael Espindola9a580232009-02-27 13:37:18 +00006095 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006096 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006097 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006098
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006099 // The address of the thread local variable is the add of the thread
6100 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00006101 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006102}
6103
Dan Gohman475871a2008-07-27 21:46:04 +00006104SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006105X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Eric Christopher30ef0e52010-06-03 04:07:48 +00006106
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006107 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00006108 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00006109
Eric Christopher30ef0e52010-06-03 04:07:48 +00006110 if (Subtarget->isTargetELF()) {
6111 // TODO: implement the "local dynamic" model
6112 // TODO: implement the "initial exec"model for pic executables
6113
6114 // If GV is an alias then use the aliasee for determining
6115 // thread-localness.
6116 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
6117 GV = GA->resolveAliasedGlobal(false);
6118
6119 TLSModel::Model model
6120 = getTLSModel(GV, getTargetMachine().getRelocationModel());
6121
6122 switch (model) {
6123 case TLSModel::GeneralDynamic:
6124 case TLSModel::LocalDynamic: // not implemented
6125 if (Subtarget->is64Bit())
6126 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
6127 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
6128
6129 case TLSModel::InitialExec:
6130 case TLSModel::LocalExec:
6131 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
6132 Subtarget->is64Bit());
6133 }
6134 } else if (Subtarget->isTargetDarwin()) {
6135 // Darwin only has one model of TLS. Lower to that.
6136 unsigned char OpFlag = 0;
6137 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
6138 X86ISD::WrapperRIP : X86ISD::Wrapper;
6139
6140 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6141 // global base reg.
6142 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
6143 !Subtarget->is64Bit();
6144 if (PIC32)
6145 OpFlag = X86II::MO_TLVP_PIC_BASE;
6146 else
6147 OpFlag = X86II::MO_TLVP;
Devang Patel0d881da2010-07-06 22:08:15 +00006148 DebugLoc DL = Op.getDebugLoc();
6149 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopher30ef0e52010-06-03 04:07:48 +00006150 getPointerTy(),
6151 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00006152 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
6153
6154 // With PIC32, the address is actually $g + Offset.
6155 if (PIC32)
6156 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6157 DAG.getNode(X86ISD::GlobalBaseReg,
6158 DebugLoc(), getPointerTy()),
6159 Offset);
6160
6161 // Lowering the machine isd will make sure everything is in the right
6162 // location.
6163 SDValue Args[] = { Offset };
6164 SDValue Chain = DAG.getNode(X86ISD::TLSCALL, DL, MVT::Other, Args, 1);
6165
6166 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
6167 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6168 MFI->setAdjustsStack(true);
Eric Christopherfd179292009-08-27 18:07:15 +00006169
Eric Christopher30ef0e52010-06-03 04:07:48 +00006170 // And our return value (tls address) is in the standard call return value
6171 // location.
6172 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
6173 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006174 }
Eric Christopher30ef0e52010-06-03 04:07:48 +00006175
6176 assert(false &&
6177 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00006178
Torok Edwinc23197a2009-07-14 16:55:14 +00006179 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00006180 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006181}
6182
Evan Cheng0db9fe62006-04-25 20:13:52 +00006183
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006184/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00006185/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00006186SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00006187 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00006188 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006189 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006190 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006191 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00006192 SDValue ShOpLo = Op.getOperand(0);
6193 SDValue ShOpHi = Op.getOperand(1);
6194 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00006195 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00006196 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00006197 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00006198
Dan Gohman475871a2008-07-27 21:46:04 +00006199 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006200 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006201 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
6202 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006203 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006204 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
6205 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006206 }
Evan Chenge3413162006-01-09 18:33:28 +00006207
Owen Anderson825b72b2009-08-11 20:47:22 +00006208 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
6209 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00006210 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00006211 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00006212
Dan Gohman475871a2008-07-27 21:46:04 +00006213 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00006214 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00006215 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
6216 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00006217
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006218 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006219 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6220 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006221 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006222 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6223 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006224 }
6225
Dan Gohman475871a2008-07-27 21:46:04 +00006226 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00006227 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006228}
Evan Chenga3195e82006-01-12 22:54:21 +00006229
Dan Gohmand858e902010-04-17 15:26:15 +00006230SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
6231 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006232 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00006233
Dale Johannesen0488fb62010-09-30 23:57:10 +00006234 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006235 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006236
Owen Anderson825b72b2009-08-11 20:47:22 +00006237 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00006238 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00006239
Eli Friedman36df4992009-05-27 00:47:34 +00006240 // These are really Legal; return the operand so the caller accepts it as
6241 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006242 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00006243 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00006244 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00006245 Subtarget->is64Bit()) {
6246 return Op;
6247 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006248
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006249 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006250 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006251 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00006252 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006253 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00006254 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00006255 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006256 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006257 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006258 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
6259}
Evan Cheng0db9fe62006-04-25 20:13:52 +00006260
Owen Andersone50ed302009-08-10 22:56:29 +00006261SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006262 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00006263 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006264 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00006265 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00006266 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00006267 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006268 if (useSSE)
Owen Anderson825b72b2009-08-11 20:47:22 +00006269 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00006270 else
Owen Anderson825b72b2009-08-11 20:47:22 +00006271 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Chris Lattner492a43e2010-09-22 01:28:21 +00006272
6273 unsigned ByteSize = SrcVT.getSizeInBits()/8;
6274
6275 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6276 MachineMemOperand *MMO =
6277 DAG.getMachineFunction()
6278 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6279 MachineMemOperand::MOLoad, ByteSize, ByteSize);
6280
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006281 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006282 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
6283 X86ISD::FILD, DL,
6284 Tys, Ops, array_lengthof(Ops),
6285 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006286
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006287 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006288 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00006289 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006290
6291 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
6292 // shouldn't be necessary except that RFP cannot be live across
6293 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006294 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006295 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
6296 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006297 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00006298 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006299 SDValue Ops[] = {
6300 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
6301 };
Chris Lattner492a43e2010-09-22 01:28:21 +00006302 MachineMemOperand *MMO =
6303 DAG.getMachineFunction()
6304 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006305 MachineMemOperand::MOStore, SSFISize, SSFISize);
Chris Lattner492a43e2010-09-22 01:28:21 +00006306
6307 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
6308 Ops, array_lengthof(Ops),
6309 Op.getValueType(), MMO);
6310 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006311 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006312 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006313 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006314
Evan Cheng0db9fe62006-04-25 20:13:52 +00006315 return Result;
6316}
6317
Bill Wendling8b8a6362009-01-17 03:56:04 +00006318// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006319SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
6320 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00006321 // This algorithm is not obvious. Here it is in C code, more or less:
6322 /*
6323 double uint64_to_double( uint32_t hi, uint32_t lo ) {
6324 static const __m128i exp = { 0x4330000045300000ULL, 0 };
6325 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00006326
Bill Wendling8b8a6362009-01-17 03:56:04 +00006327 // Copy ints to xmm registers.
6328 __m128i xh = _mm_cvtsi32_si128( hi );
6329 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00006330
Bill Wendling8b8a6362009-01-17 03:56:04 +00006331 // Combine into low half of a single xmm register.
6332 __m128i x = _mm_unpacklo_epi32( xh, xl );
6333 __m128d d;
6334 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00006335
Bill Wendling8b8a6362009-01-17 03:56:04 +00006336 // Merge in appropriate exponents to give the integer bits the right
6337 // magnitude.
6338 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00006339
Bill Wendling8b8a6362009-01-17 03:56:04 +00006340 // Subtract away the biases to deal with the IEEE-754 double precision
6341 // implicit 1.
6342 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00006343
Bill Wendling8b8a6362009-01-17 03:56:04 +00006344 // All conversions up to here are exact. The correctly rounded result is
6345 // calculated using the current rounding mode using the following
6346 // horizontal add.
6347 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
6348 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
6349 // store doesn't really need to be here (except
6350 // maybe to zero the other double)
6351 return sd;
6352 }
6353 */
Dale Johannesen040225f2008-10-21 23:07:49 +00006354
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006355 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00006356 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00006357
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006358 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00006359 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00006360 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
6361 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
6362 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
6363 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006364 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006365 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006366
Bill Wendling8b8a6362009-01-17 03:56:04 +00006367 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00006368 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006369 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00006370 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006371 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006372 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006373 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006374
Owen Anderson825b72b2009-08-11 20:47:22 +00006375 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6376 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006377 Op.getOperand(0),
6378 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006379 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6380 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006381 Op.getOperand(0),
6382 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006383 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
6384 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00006385 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006386 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006387 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
6388 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Unpck2);
6389 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00006390 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006391 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006392 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006393
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006394 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00006395 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00006396 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
6397 DAG.getUNDEF(MVT::v2f64), ShufMask);
6398 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
6399 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006400 DAG.getIntPtrConstant(0));
6401}
6402
Bill Wendling8b8a6362009-01-17 03:56:04 +00006403// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006404SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
6405 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006406 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006407 // FP constant to bias correct the final result.
6408 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00006409 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006410
6411 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00006412 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6413 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00006414 Op.getOperand(0),
6415 DAG.getIntPtrConstant(0)));
6416
Owen Anderson825b72b2009-08-11 20:47:22 +00006417 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
6418 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006419 DAG.getIntPtrConstant(0));
6420
6421 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006422 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
6423 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006424 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006425 MVT::v2f64, Load)),
6426 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006427 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006428 MVT::v2f64, Bias)));
6429 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
6430 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006431 DAG.getIntPtrConstant(0));
6432
6433 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006434 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006435
6436 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00006437 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00006438
Owen Anderson825b72b2009-08-11 20:47:22 +00006439 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006440 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00006441 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00006442 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006443 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00006444 }
6445
6446 // Handle final rounding.
6447 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00006448}
6449
Dan Gohmand858e902010-04-17 15:26:15 +00006450SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
6451 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00006452 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006453 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006454
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006455 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00006456 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
6457 // the optimization here.
6458 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00006459 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00006460
Owen Andersone50ed302009-08-10 22:56:29 +00006461 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006462 EVT DstVT = Op.getValueType();
6463 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006464 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006465 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006466 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00006467
6468 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00006469 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006470 if (SrcVT == MVT::i32) {
6471 SDValue WordOff = DAG.getConstant(4, getPointerTy());
6472 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
6473 getPointerTy(), StackSlot, WordOff);
6474 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006475 StackSlot, MachinePointerInfo(),
6476 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006477 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006478 OffsetSlot, MachinePointerInfo(),
6479 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006480 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
6481 return Fild;
6482 }
6483
6484 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
6485 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006486 StackSlot, MachinePointerInfo(),
6487 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006488 // For i64 source, we need to add the appropriate power of 2 if the input
6489 // was negative. This is the same as the optimization in
6490 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
6491 // we must be careful to do the computation in x87 extended precision, not
6492 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00006493 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6494 MachineMemOperand *MMO =
6495 DAG.getMachineFunction()
6496 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6497 MachineMemOperand::MOLoad, 8, 8);
6498
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006499 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
6500 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006501 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
6502 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006503
6504 APInt FF(32, 0x5F800000ULL);
6505
6506 // Check whether the sign bit is set.
6507 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
6508 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
6509 ISD::SETLT);
6510
6511 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
6512 SDValue FudgePtr = DAG.getConstantPool(
6513 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
6514 getPointerTy());
6515
6516 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
6517 SDValue Zero = DAG.getIntPtrConstant(0);
6518 SDValue Four = DAG.getIntPtrConstant(4);
6519 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
6520 Zero, Four);
6521 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
6522
6523 // Load the value out, extending it from f32 to f80.
6524 // FIXME: Avoid the extend by constructing the right constant pool?
Evan Chengbcc80172010-07-07 22:15:37 +00006525 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, MVT::f80, dl, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00006526 FudgePtr, MachinePointerInfo::getConstantPool(),
6527 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006528 // Extend everything to 80 bits to force it to be done on x87.
6529 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
6530 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00006531}
6532
Dan Gohman475871a2008-07-27 21:46:04 +00006533std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00006534FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00006535 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00006536
Owen Andersone50ed302009-08-10 22:56:29 +00006537 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00006538
6539 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006540 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
6541 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00006542 }
6543
Owen Anderson825b72b2009-08-11 20:47:22 +00006544 assert(DstTy.getSimpleVT() <= MVT::i64 &&
6545 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00006546 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00006547
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006548 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006549 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00006550 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00006551 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00006552 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00006553 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00006554 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00006555 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006556
Evan Cheng87c89352007-10-15 20:11:21 +00006557 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
6558 // stack slot.
6559 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00006560 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00006561 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006562 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00006563
Chris Lattner07290932010-09-22 01:05:16 +00006564
6565
Evan Cheng0db9fe62006-04-25 20:13:52 +00006566 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00006567 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006568 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00006569 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
6570 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
6571 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006572 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006573
Dan Gohman475871a2008-07-27 21:46:04 +00006574 SDValue Chain = DAG.getEntryNode();
6575 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00006576 EVT TheVT = Op.getOperand(0).getValueType();
6577 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006578 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00006579 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006580 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006581 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00006582 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00006583 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00006584 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00006585 };
Chris Lattner492a43e2010-09-22 01:28:21 +00006586
6587 MachineMemOperand *MMO =
6588 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6589 MachineMemOperand::MOLoad, MemSize, MemSize);
6590 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
6591 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006592 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00006593 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006594 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
6595 }
Chris Lattner07290932010-09-22 01:05:16 +00006596
6597 MachineMemOperand *MMO =
6598 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6599 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006600
Evan Cheng0db9fe62006-04-25 20:13:52 +00006601 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00006602 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00006603 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
6604 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00006605
Chris Lattner27a6c732007-11-24 07:07:01 +00006606 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006607}
6608
Dan Gohmand858e902010-04-17 15:26:15 +00006609SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
6610 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00006611 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006612 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006613
Eli Friedman948e95a2009-05-23 09:59:16 +00006614 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00006615 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00006616 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
6617 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00006618
Chris Lattner27a6c732007-11-24 07:07:01 +00006619 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006620 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00006621 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00006622}
6623
Dan Gohmand858e902010-04-17 15:26:15 +00006624SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
6625 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00006626 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
6627 SDValue FIST = Vals.first, StackSlot = Vals.second;
6628 assert(FIST.getNode() && "Unexpected failure");
6629
6630 // Load the result.
6631 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00006632 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006633}
6634
Dan Gohmand858e902010-04-17 15:26:15 +00006635SDValue X86TargetLowering::LowerFABS(SDValue Op,
6636 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00006637 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006638 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006639 EVT VT = Op.getValueType();
6640 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006641 if (VT.isVector())
6642 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006643 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00006644 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006645 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00006646 CV.push_back(C);
6647 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006648 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006649 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00006650 CV.push_back(C);
6651 CV.push_back(C);
6652 CV.push_back(C);
6653 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006654 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006655 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006656 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006657 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006658 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006659 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006660 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006661}
6662
Dan Gohmand858e902010-04-17 15:26:15 +00006663SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00006664 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006665 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006666 EVT VT = Op.getValueType();
6667 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00006668 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00006669 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006670 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00006671 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006672 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00006673 CV.push_back(C);
6674 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006675 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006676 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00006677 CV.push_back(C);
6678 CV.push_back(C);
6679 CV.push_back(C);
6680 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006681 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006682 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006683 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006684 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006685 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006686 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006687 if (VT.isVector()) {
Dale Johannesenace16102009-02-03 19:33:06 +00006688 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00006689 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
6690 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006691 Op.getOperand(0)),
Owen Anderson825b72b2009-08-11 20:47:22 +00006692 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00006693 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006694 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00006695 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006696}
6697
Dan Gohmand858e902010-04-17 15:26:15 +00006698SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00006699 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00006700 SDValue Op0 = Op.getOperand(0);
6701 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006702 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006703 EVT VT = Op.getValueType();
6704 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00006705
6706 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00006707 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006708 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00006709 SrcVT = VT;
6710 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00006711 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00006712 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006713 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00006714 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00006715 }
6716
6717 // At this point the operands and the result should have the same
6718 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00006719
Evan Cheng68c47cb2007-01-05 07:55:56 +00006720 // First get the sign bit of second operand.
6721 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00006722 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006723 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
6724 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00006725 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006726 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
6727 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6728 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6729 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00006730 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006731 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006732 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006733 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006734 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006735 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006736 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00006737
6738 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00006739 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006740 // Op0 is MVT::f32, Op1 is MVT::f64.
6741 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
6742 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
6743 DAG.getConstant(32, MVT::i32));
6744 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, SignBit);
6745 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00006746 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00006747 }
6748
Evan Cheng73d6cf12007-01-05 21:37:56 +00006749 // Clear first operand sign bit.
6750 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00006751 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006752 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
6753 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00006754 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006755 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
6756 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6757 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6758 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00006759 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006760 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006761 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006762 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006763 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006764 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006765 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00006766
6767 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00006768 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00006769}
6770
Dan Gohman076aee32009-03-04 19:44:21 +00006771/// Emit nodes that will be selected as "test Op0,Op0", or something
6772/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00006773SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00006774 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00006775 DebugLoc dl = Op.getDebugLoc();
6776
Dan Gohman31125812009-03-07 01:58:32 +00006777 // CF and OF aren't always set the way we want. Determine which
6778 // of these we need.
6779 bool NeedCF = false;
6780 bool NeedOF = false;
6781 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006782 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00006783 case X86::COND_A: case X86::COND_AE:
6784 case X86::COND_B: case X86::COND_BE:
6785 NeedCF = true;
6786 break;
6787 case X86::COND_G: case X86::COND_GE:
6788 case X86::COND_L: case X86::COND_LE:
6789 case X86::COND_O: case X86::COND_NO:
6790 NeedOF = true;
6791 break;
Dan Gohman31125812009-03-07 01:58:32 +00006792 }
6793
Dan Gohman076aee32009-03-04 19:44:21 +00006794 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00006795 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
6796 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006797 if (Op.getResNo() != 0 || NeedOF || NeedCF)
6798 // Emit a CMP with 0, which is the TEST pattern.
6799 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
6800 DAG.getConstant(0, Op.getValueType()));
6801
6802 unsigned Opcode = 0;
6803 unsigned NumOperands = 0;
6804 switch (Op.getNode()->getOpcode()) {
6805 case ISD::ADD:
6806 // Due to an isel shortcoming, be conservative if this add is likely to be
6807 // selected as part of a load-modify-store instruction. When the root node
6808 // in a match is a store, isel doesn't know how to remap non-chain non-flag
6809 // uses of other nodes in the match, such as the ADD in this case. This
6810 // leads to the ADD being left around and reselected, with the result being
6811 // two adds in the output. Alas, even if none our users are stores, that
6812 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
6813 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
6814 // climbing the DAG back to the root, and it doesn't seem to be worth the
6815 // effort.
6816 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00006817 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006818 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
6819 goto default_case;
6820
6821 if (ConstantSDNode *C =
6822 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
6823 // An add of one will be selected as an INC.
6824 if (C->getAPIntValue() == 1) {
6825 Opcode = X86ISD::INC;
6826 NumOperands = 1;
6827 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00006828 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006829
6830 // An add of negative one (subtract of one) will be selected as a DEC.
6831 if (C->getAPIntValue().isAllOnesValue()) {
6832 Opcode = X86ISD::DEC;
6833 NumOperands = 1;
6834 break;
6835 }
Dan Gohman076aee32009-03-04 19:44:21 +00006836 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006837
6838 // Otherwise use a regular EFLAGS-setting add.
6839 Opcode = X86ISD::ADD;
6840 NumOperands = 2;
6841 break;
6842 case ISD::AND: {
6843 // If the primary and result isn't used, don't bother using X86ISD::AND,
6844 // because a TEST instruction will be better.
6845 bool NonFlagUse = false;
6846 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
6847 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
6848 SDNode *User = *UI;
6849 unsigned UOpNo = UI.getOperandNo();
6850 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
6851 // Look pass truncate.
6852 UOpNo = User->use_begin().getOperandNo();
6853 User = *User->use_begin();
6854 }
6855
6856 if (User->getOpcode() != ISD::BRCOND &&
6857 User->getOpcode() != ISD::SETCC &&
6858 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
6859 NonFlagUse = true;
6860 break;
6861 }
Dan Gohman076aee32009-03-04 19:44:21 +00006862 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006863
6864 if (!NonFlagUse)
6865 break;
6866 }
6867 // FALL THROUGH
6868 case ISD::SUB:
6869 case ISD::OR:
6870 case ISD::XOR:
6871 // Due to the ISEL shortcoming noted above, be conservative if this op is
6872 // likely to be selected as part of a load-modify-store instruction.
6873 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
6874 UE = Op.getNode()->use_end(); UI != UE; ++UI)
6875 if (UI->getOpcode() == ISD::STORE)
6876 goto default_case;
6877
6878 // Otherwise use a regular EFLAGS-setting instruction.
6879 switch (Op.getNode()->getOpcode()) {
6880 default: llvm_unreachable("unexpected operator!");
6881 case ISD::SUB: Opcode = X86ISD::SUB; break;
6882 case ISD::OR: Opcode = X86ISD::OR; break;
6883 case ISD::XOR: Opcode = X86ISD::XOR; break;
6884 case ISD::AND: Opcode = X86ISD::AND; break;
6885 }
6886
6887 NumOperands = 2;
6888 break;
6889 case X86ISD::ADD:
6890 case X86ISD::SUB:
6891 case X86ISD::INC:
6892 case X86ISD::DEC:
6893 case X86ISD::OR:
6894 case X86ISD::XOR:
6895 case X86ISD::AND:
6896 return SDValue(Op.getNode(), 1);
6897 default:
6898 default_case:
6899 break;
Dan Gohman076aee32009-03-04 19:44:21 +00006900 }
6901
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006902 if (Opcode == 0)
6903 // Emit a CMP with 0, which is the TEST pattern.
6904 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
6905 DAG.getConstant(0, Op.getValueType()));
6906
6907 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
6908 SmallVector<SDValue, 4> Ops;
6909 for (unsigned i = 0; i != NumOperands; ++i)
6910 Ops.push_back(Op.getOperand(i));
6911
6912 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
6913 DAG.ReplaceAllUsesWith(Op, New);
6914 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00006915}
6916
6917/// Emit nodes that will be selected as "cmp Op0,Op1", or something
6918/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00006919SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00006920 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00006921 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
6922 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00006923 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00006924
6925 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00006926 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00006927}
6928
Evan Chengd40d03e2010-01-06 19:38:29 +00006929/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
6930/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00006931SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
6932 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00006933 SDValue Op0 = And.getOperand(0);
6934 SDValue Op1 = And.getOperand(1);
6935 if (Op0.getOpcode() == ISD::TRUNCATE)
6936 Op0 = Op0.getOperand(0);
6937 if (Op1.getOpcode() == ISD::TRUNCATE)
6938 Op1 = Op1.getOperand(0);
6939
Evan Chengd40d03e2010-01-06 19:38:29 +00006940 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00006941 if (Op1.getOpcode() == ISD::SHL)
6942 std::swap(Op0, Op1);
6943 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00006944 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
6945 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00006946 // If we looked past a truncate, check that it's only truncating away
6947 // known zeros.
6948 unsigned BitWidth = Op0.getValueSizeInBits();
6949 unsigned AndBitWidth = And.getValueSizeInBits();
6950 if (BitWidth > AndBitWidth) {
6951 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
6952 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
6953 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
6954 return SDValue();
6955 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00006956 LHS = Op1;
6957 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00006958 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00006959 } else if (Op1.getOpcode() == ISD::Constant) {
6960 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
6961 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00006962 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
6963 LHS = AndLHS.getOperand(0);
6964 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00006965 }
Evan Chengd40d03e2010-01-06 19:38:29 +00006966 }
Evan Cheng0488db92007-09-25 01:57:46 +00006967
Evan Chengd40d03e2010-01-06 19:38:29 +00006968 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00006969 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00006970 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00006971 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00006972 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00006973 // Also promote i16 to i32 for performance / code size reason.
6974 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00006975 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00006976 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00006977
Evan Chengd40d03e2010-01-06 19:38:29 +00006978 // If the operand types disagree, extend the shift amount to match. Since
6979 // BT ignores high bits (like shifts) we can use anyextend.
6980 if (LHS.getValueType() != RHS.getValueType())
6981 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00006982
Evan Chengd40d03e2010-01-06 19:38:29 +00006983 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
6984 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
6985 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6986 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00006987 }
6988
Evan Cheng54de3ea2010-01-05 06:52:31 +00006989 return SDValue();
6990}
6991
Dan Gohmand858e902010-04-17 15:26:15 +00006992SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng54de3ea2010-01-05 06:52:31 +00006993 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
6994 SDValue Op0 = Op.getOperand(0);
6995 SDValue Op1 = Op.getOperand(1);
6996 DebugLoc dl = Op.getDebugLoc();
6997 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
6998
6999 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00007000 // Lower (X & (1 << N)) == 0 to BT(X, N).
7001 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
7002 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
7003 if (Op0.getOpcode() == ISD::AND &&
7004 Op0.hasOneUse() &&
7005 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00007006 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007007 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
7008 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
7009 if (NewSetCC.getNode())
7010 return NewSetCC;
7011 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00007012
Evan Cheng2c755ba2010-02-27 07:36:59 +00007013 // Look for "(setcc) == / != 1" to avoid unncessary setcc.
7014 if (Op0.getOpcode() == X86ISD::SETCC &&
7015 Op1.getOpcode() == ISD::Constant &&
7016 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
7017 cast<ConstantSDNode>(Op1)->isNullValue()) &&
7018 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
7019 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
7020 bool Invert = (CC == ISD::SETNE) ^
7021 cast<ConstantSDNode>(Op1)->isNullValue();
7022 if (Invert)
7023 CCode = X86::GetOppositeBranchCondition(CCode);
7024 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7025 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
7026 }
7027
Evan Chenge5b51ac2010-04-17 06:13:15 +00007028 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00007029 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007030 if (X86CC == X86::COND_INVALID)
7031 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007032
Evan Cheng552f09a2010-04-26 19:06:11 +00007033 SDValue Cond = EmitCmp(Op0, Op1, X86CC, DAG);
Evan Chengad9c0a32009-12-15 00:53:42 +00007034
7035 // Use sbb x, x to materialize carry bit into a GPR.
Evan Cheng2e489c42009-12-16 00:53:11 +00007036 if (X86CC == X86::COND_B)
Evan Chengad9c0a32009-12-15 00:53:42 +00007037 return DAG.getNode(ISD::AND, dl, MVT::i8,
7038 DAG.getNode(X86ISD::SETCC_CARRY, dl, MVT::i8,
7039 DAG.getConstant(X86CC, MVT::i8), Cond),
7040 DAG.getConstant(1, MVT::i8));
Evan Chengad9c0a32009-12-15 00:53:42 +00007041
Owen Anderson825b72b2009-08-11 20:47:22 +00007042 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7043 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00007044}
7045
Dan Gohmand858e902010-04-17 15:26:15 +00007046SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007047 SDValue Cond;
7048 SDValue Op0 = Op.getOperand(0);
7049 SDValue Op1 = Op.getOperand(1);
7050 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00007051 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00007052 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
7053 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007054 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00007055
7056 if (isFP) {
7057 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00007058 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007059 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
7060 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00007061 bool Swap = false;
7062
7063 switch (SetCCOpcode) {
7064 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007065 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00007066 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00007067 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00007068 case ISD::SETGT: Swap = true; // Fallthrough
7069 case ISD::SETLT:
7070 case ISD::SETOLT: SSECC = 1; break;
7071 case ISD::SETOGE:
7072 case ISD::SETGE: Swap = true; // Fallthrough
7073 case ISD::SETLE:
7074 case ISD::SETOLE: SSECC = 2; break;
7075 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007076 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00007077 case ISD::SETNE: SSECC = 4; break;
7078 case ISD::SETULE: Swap = true;
7079 case ISD::SETUGE: SSECC = 5; break;
7080 case ISD::SETULT: Swap = true;
7081 case ISD::SETUGT: SSECC = 6; break;
7082 case ISD::SETO: SSECC = 7; break;
7083 }
7084 if (Swap)
7085 std::swap(Op0, Op1);
7086
Nate Begemanfb8ead02008-07-25 19:05:58 +00007087 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00007088 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00007089 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00007090 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007091 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
7092 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007093 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007094 }
7095 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00007096 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007097 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
7098 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007099 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007100 }
Torok Edwinc23197a2009-07-14 16:55:14 +00007101 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00007102 }
7103 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00007104 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00007105 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007106
Nate Begeman30a0de92008-07-17 16:51:19 +00007107 // We are handling one of the integer comparisons here. Since SSE only has
7108 // GT and EQ comparisons for integer, swapping operands and multiple
7109 // operations may be required for some comparisons.
7110 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
7111 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007112
Owen Anderson825b72b2009-08-11 20:47:22 +00007113 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00007114 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007115 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007116 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007117 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
7118 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00007119 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007120
Nate Begeman30a0de92008-07-17 16:51:19 +00007121 switch (SetCCOpcode) {
7122 default: break;
7123 case ISD::SETNE: Invert = true;
7124 case ISD::SETEQ: Opc = EQOpc; break;
7125 case ISD::SETLT: Swap = true;
7126 case ISD::SETGT: Opc = GTOpc; break;
7127 case ISD::SETGE: Swap = true;
7128 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
7129 case ISD::SETULT: Swap = true;
7130 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
7131 case ISD::SETUGE: Swap = true;
7132 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
7133 }
7134 if (Swap)
7135 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007136
Nate Begeman30a0de92008-07-17 16:51:19 +00007137 // Since SSE has no unsigned integer comparisons, we need to flip the sign
7138 // bits of the inputs before performing those operations.
7139 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00007140 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00007141 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
7142 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00007143 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00007144 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
7145 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00007146 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
7147 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00007148 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007149
Dale Johannesenace16102009-02-03 19:33:06 +00007150 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00007151
7152 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00007153 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00007154 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00007155
Nate Begeman30a0de92008-07-17 16:51:19 +00007156 return Result;
7157}
Evan Cheng0488db92007-09-25 01:57:46 +00007158
Evan Cheng370e5342008-12-03 08:38:43 +00007159// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00007160static bool isX86LogicalCmp(SDValue Op) {
7161 unsigned Opc = Op.getNode()->getOpcode();
7162 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
7163 return true;
7164 if (Op.getResNo() == 1 &&
7165 (Opc == X86ISD::ADD ||
7166 Opc == X86ISD::SUB ||
7167 Opc == X86ISD::SMUL ||
7168 Opc == X86ISD::UMUL ||
7169 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00007170 Opc == X86ISD::DEC ||
7171 Opc == X86ISD::OR ||
7172 Opc == X86ISD::XOR ||
7173 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00007174 return true;
7175
7176 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00007177}
7178
Dan Gohmand858e902010-04-17 15:26:15 +00007179SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007180 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007181 SDValue Cond = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007182 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007183 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00007184
Dan Gohman1a492952009-10-20 16:22:37 +00007185 if (Cond.getOpcode() == ISD::SETCC) {
7186 SDValue NewCond = LowerSETCC(Cond, DAG);
7187 if (NewCond.getNode())
7188 Cond = NewCond;
7189 }
Evan Cheng734503b2006-09-11 02:19:56 +00007190
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007191 // (select (x == 0), -1, 0) -> (sign_bit (x - 1))
7192 SDValue Op1 = Op.getOperand(1);
7193 SDValue Op2 = Op.getOperand(2);
7194 if (Cond.getOpcode() == X86ISD::SETCC &&
7195 cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue() == X86::COND_E) {
7196 SDValue Cmp = Cond.getOperand(1);
7197 if (Cmp.getOpcode() == X86ISD::CMP) {
7198 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op1);
7199 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
7200 ConstantSDNode *RHSC =
7201 dyn_cast<ConstantSDNode>(Cmp.getOperand(1).getNode());
7202 if (N1C && N1C->isAllOnesValue() &&
7203 N2C && N2C->isNullValue() &&
7204 RHSC && RHSC->isNullValue()) {
7205 SDValue CmpOp0 = Cmp.getOperand(0);
Chris Lattnerda0688e2010-03-14 18:44:35 +00007206 Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007207 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
7208 return DAG.getNode(X86ISD::SETCC_CARRY, dl, Op.getValueType(),
7209 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
7210 }
7211 }
7212 }
7213
Evan Chengad9c0a32009-12-15 00:53:42 +00007214 // Look pass (and (setcc_carry (cmp ...)), 1).
7215 if (Cond.getOpcode() == ISD::AND &&
7216 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7217 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
7218 if (C && C->getAPIntValue() == 1)
7219 Cond = Cond.getOperand(0);
7220 }
7221
Evan Cheng3f41d662007-10-08 22:16:29 +00007222 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7223 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007224 if (Cond.getOpcode() == X86ISD::SETCC ||
7225 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007226 CC = Cond.getOperand(0);
7227
Dan Gohman475871a2008-07-27 21:46:04 +00007228 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007229 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00007230 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00007231
Evan Cheng3f41d662007-10-08 22:16:29 +00007232 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007233 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00007234 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00007235 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00007236
Chris Lattnerd1980a52009-03-12 06:52:53 +00007237 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
7238 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00007239 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007240 addTest = false;
7241 }
7242 }
7243
7244 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007245 // Look pass the truncate.
7246 if (Cond.getOpcode() == ISD::TRUNCATE)
7247 Cond = Cond.getOperand(0);
7248
7249 // We know the result of AND is compared against zero. Try to match
7250 // it to BT.
7251 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
7252 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
7253 if (NewSetCC.getNode()) {
7254 CC = NewSetCC.getOperand(0);
7255 Cond = NewSetCC.getOperand(1);
7256 addTest = false;
7257 }
7258 }
7259 }
7260
7261 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007262 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007263 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007264 }
7265
Evan Cheng0488db92007-09-25 01:57:46 +00007266 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
7267 // condition is true.
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007268 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
7269 SDValue Ops[] = { Op2, Op1, CC, Cond };
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007270 return DAG.getNode(X86ISD::CMOV, dl, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00007271}
7272
Evan Cheng370e5342008-12-03 08:38:43 +00007273// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
7274// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
7275// from the AND / OR.
7276static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
7277 Opc = Op.getOpcode();
7278 if (Opc != ISD::OR && Opc != ISD::AND)
7279 return false;
7280 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7281 Op.getOperand(0).hasOneUse() &&
7282 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
7283 Op.getOperand(1).hasOneUse());
7284}
7285
Evan Cheng961d6d42009-02-02 08:19:07 +00007286// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
7287// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00007288static bool isXor1OfSetCC(SDValue Op) {
7289 if (Op.getOpcode() != ISD::XOR)
7290 return false;
7291 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7292 if (N1C && N1C->getAPIntValue() == 1) {
7293 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7294 Op.getOperand(0).hasOneUse();
7295 }
7296 return false;
7297}
7298
Dan Gohmand858e902010-04-17 15:26:15 +00007299SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007300 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007301 SDValue Chain = Op.getOperand(0);
7302 SDValue Cond = Op.getOperand(1);
7303 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007304 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007305 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00007306
Dan Gohman1a492952009-10-20 16:22:37 +00007307 if (Cond.getOpcode() == ISD::SETCC) {
7308 SDValue NewCond = LowerSETCC(Cond, DAG);
7309 if (NewCond.getNode())
7310 Cond = NewCond;
7311 }
Chris Lattnere55484e2008-12-25 05:34:37 +00007312#if 0
7313 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00007314 else if (Cond.getOpcode() == X86ISD::ADD ||
7315 Cond.getOpcode() == X86ISD::SUB ||
7316 Cond.getOpcode() == X86ISD::SMUL ||
7317 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00007318 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00007319#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00007320
Evan Chengad9c0a32009-12-15 00:53:42 +00007321 // Look pass (and (setcc_carry (cmp ...)), 1).
7322 if (Cond.getOpcode() == ISD::AND &&
7323 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7324 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
7325 if (C && C->getAPIntValue() == 1)
7326 Cond = Cond.getOperand(0);
7327 }
7328
Evan Cheng3f41d662007-10-08 22:16:29 +00007329 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7330 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007331 if (Cond.getOpcode() == X86ISD::SETCC ||
7332 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007333 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007334
Dan Gohman475871a2008-07-27 21:46:04 +00007335 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007336 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00007337 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00007338 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00007339 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007340 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00007341 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00007342 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007343 default: break;
7344 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00007345 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00007346 // These can only come from an arithmetic instruction with overflow,
7347 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007348 Cond = Cond.getNode()->getOperand(1);
7349 addTest = false;
7350 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007351 }
Evan Cheng0488db92007-09-25 01:57:46 +00007352 }
Evan Cheng370e5342008-12-03 08:38:43 +00007353 } else {
7354 unsigned CondOpc;
7355 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
7356 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00007357 if (CondOpc == ISD::OR) {
7358 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
7359 // two branches instead of an explicit OR instruction with a
7360 // separate test.
7361 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007362 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00007363 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007364 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007365 Chain, Dest, CC, Cmp);
7366 CC = Cond.getOperand(1).getOperand(0);
7367 Cond = Cmp;
7368 addTest = false;
7369 }
7370 } else { // ISD::AND
7371 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
7372 // two branches instead of an explicit AND instruction with a
7373 // separate test. However, we only do this if this block doesn't
7374 // have a fall-through edge, because this requires an explicit
7375 // jmp when the condition is false.
7376 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007377 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00007378 Op.getNode()->hasOneUse()) {
7379 X86::CondCode CCode =
7380 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7381 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007382 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00007383 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00007384 // Look for an unconditional branch following this conditional branch.
7385 // We need this because we need to reverse the successors in order
7386 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00007387 if (User->getOpcode() == ISD::BR) {
7388 SDValue FalseBB = User->getOperand(1);
7389 SDNode *NewBR =
7390 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00007391 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00007392 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00007393 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00007394
Dale Johannesene4d209d2009-02-03 20:21:25 +00007395 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007396 Chain, Dest, CC, Cmp);
7397 X86::CondCode CCode =
7398 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
7399 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007400 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00007401 Cond = Cmp;
7402 addTest = false;
7403 }
7404 }
Dan Gohman279c22e2008-10-21 03:29:32 +00007405 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00007406 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
7407 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
7408 // It should be transformed during dag combiner except when the condition
7409 // is set by a arithmetics with overflow node.
7410 X86::CondCode CCode =
7411 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7412 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007413 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00007414 Cond = Cond.getOperand(0).getOperand(1);
7415 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00007416 }
Evan Cheng0488db92007-09-25 01:57:46 +00007417 }
7418
7419 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007420 // Look pass the truncate.
7421 if (Cond.getOpcode() == ISD::TRUNCATE)
7422 Cond = Cond.getOperand(0);
7423
7424 // We know the result of AND is compared against zero. Try to match
7425 // it to BT.
7426 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
7427 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
7428 if (NewSetCC.getNode()) {
7429 CC = NewSetCC.getOperand(0);
7430 Cond = NewSetCC.getOperand(1);
7431 addTest = false;
7432 }
7433 }
7434 }
7435
7436 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007437 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007438 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007439 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00007440 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00007441 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00007442}
7443
Anton Korobeynikove060b532007-04-17 19:34:00 +00007444
7445// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
7446// Calls to _alloca is needed to probe the stack when allocating more than 4k
7447// bytes in one go. Touching the stack at 4K increments is necessary to ensure
7448// that the guard pages used by the OS virtual memory manager are allocated in
7449// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00007450SDValue
7451X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007452 SelectionDAG &DAG) const {
Anton Korobeynikove060b532007-04-17 19:34:00 +00007453 assert(Subtarget->isTargetCygMing() &&
7454 "This should be used only on Cygwin/Mingw targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007455 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007456
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007457 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00007458 SDValue Chain = Op.getOperand(0);
7459 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007460 // FIXME: Ensure alignment here
7461
Dan Gohman475871a2008-07-27 21:46:04 +00007462 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007463
Owen Anderson825b72b2009-08-11 20:47:22 +00007464 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007465
Dale Johannesendd64c412009-02-04 00:33:20 +00007466 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007467 Flag = Chain.getValue(1);
7468
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00007469 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007470
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00007471 Chain = DAG.getNode(X86ISD::MINGW_ALLOCA, dl, NodeTys, Chain, Flag);
7472 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007473
Dale Johannesendd64c412009-02-04 00:33:20 +00007474 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007475
Dan Gohman475871a2008-07-27 21:46:04 +00007476 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007477 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007478}
7479
Dan Gohmand858e902010-04-17 15:26:15 +00007480SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00007481 MachineFunction &MF = DAG.getMachineFunction();
7482 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
7483
Dan Gohman69de1932008-02-06 22:27:42 +00007484 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00007485 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00007486
Evan Cheng25ab6902006-09-08 06:48:29 +00007487 if (!Subtarget->is64Bit()) {
7488 // vastart just stores the address of the VarArgsFrameIndex slot into the
7489 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00007490 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
7491 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007492 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
7493 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007494 }
7495
7496 // __va_list_tag:
7497 // gp_offset (0 - 6 * 8)
7498 // fp_offset (48 - 48 + 8 * 16)
7499 // overflow_arg_area (point to parameters coming in memory).
7500 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00007501 SmallVector<SDValue, 8> MemOps;
7502 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00007503 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00007504 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00007505 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
7506 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007507 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007508 MemOps.push_back(Store);
7509
7510 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00007511 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007512 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00007513 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00007514 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
7515 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007516 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007517 MemOps.push_back(Store);
7518
7519 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00007520 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007521 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00007522 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
7523 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007524 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
7525 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00007526 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007527 MemOps.push_back(Store);
7528
7529 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00007530 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007531 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00007532 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
7533 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007534 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
7535 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007536 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00007537 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00007538 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00007539}
7540
Dan Gohmand858e902010-04-17 15:26:15 +00007541SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman9018e832008-05-10 01:26:14 +00007542 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
7543 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman9018e832008-05-10 01:26:14 +00007544
Chris Lattner75361b62010-04-07 22:58:41 +00007545 report_fatal_error("VAArgInst is not yet implemented for x86-64!");
Dan Gohman475871a2008-07-27 21:46:04 +00007546 return SDValue();
Dan Gohman9018e832008-05-10 01:26:14 +00007547}
7548
Dan Gohmand858e902010-04-17 15:26:15 +00007549SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00007550 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00007551 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00007552 SDValue Chain = Op.getOperand(0);
7553 SDValue DstPtr = Op.getOperand(1);
7554 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00007555 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
7556 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00007557 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00007558
Chris Lattnere72f2022010-09-21 05:40:29 +00007559 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00007560 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Chris Lattnere72f2022010-09-21 05:40:29 +00007561 false,
7562 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00007563}
7564
Dan Gohman475871a2008-07-27 21:46:04 +00007565SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007566X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007567 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007568 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007569 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00007570 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00007571 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00007572 case Intrinsic::x86_sse_comieq_ss:
7573 case Intrinsic::x86_sse_comilt_ss:
7574 case Intrinsic::x86_sse_comile_ss:
7575 case Intrinsic::x86_sse_comigt_ss:
7576 case Intrinsic::x86_sse_comige_ss:
7577 case Intrinsic::x86_sse_comineq_ss:
7578 case Intrinsic::x86_sse_ucomieq_ss:
7579 case Intrinsic::x86_sse_ucomilt_ss:
7580 case Intrinsic::x86_sse_ucomile_ss:
7581 case Intrinsic::x86_sse_ucomigt_ss:
7582 case Intrinsic::x86_sse_ucomige_ss:
7583 case Intrinsic::x86_sse_ucomineq_ss:
7584 case Intrinsic::x86_sse2_comieq_sd:
7585 case Intrinsic::x86_sse2_comilt_sd:
7586 case Intrinsic::x86_sse2_comile_sd:
7587 case Intrinsic::x86_sse2_comigt_sd:
7588 case Intrinsic::x86_sse2_comige_sd:
7589 case Intrinsic::x86_sse2_comineq_sd:
7590 case Intrinsic::x86_sse2_ucomieq_sd:
7591 case Intrinsic::x86_sse2_ucomilt_sd:
7592 case Intrinsic::x86_sse2_ucomile_sd:
7593 case Intrinsic::x86_sse2_ucomigt_sd:
7594 case Intrinsic::x86_sse2_ucomige_sd:
7595 case Intrinsic::x86_sse2_ucomineq_sd: {
7596 unsigned Opc = 0;
7597 ISD::CondCode CC = ISD::SETCC_INVALID;
7598 switch (IntNo) {
7599 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007600 case Intrinsic::x86_sse_comieq_ss:
7601 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007602 Opc = X86ISD::COMI;
7603 CC = ISD::SETEQ;
7604 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00007605 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007606 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007607 Opc = X86ISD::COMI;
7608 CC = ISD::SETLT;
7609 break;
7610 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007611 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007612 Opc = X86ISD::COMI;
7613 CC = ISD::SETLE;
7614 break;
7615 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007616 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007617 Opc = X86ISD::COMI;
7618 CC = ISD::SETGT;
7619 break;
7620 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007621 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007622 Opc = X86ISD::COMI;
7623 CC = ISD::SETGE;
7624 break;
7625 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007626 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007627 Opc = X86ISD::COMI;
7628 CC = ISD::SETNE;
7629 break;
7630 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007631 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007632 Opc = X86ISD::UCOMI;
7633 CC = ISD::SETEQ;
7634 break;
7635 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007636 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007637 Opc = X86ISD::UCOMI;
7638 CC = ISD::SETLT;
7639 break;
7640 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007641 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007642 Opc = X86ISD::UCOMI;
7643 CC = ISD::SETLE;
7644 break;
7645 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007646 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007647 Opc = X86ISD::UCOMI;
7648 CC = ISD::SETGT;
7649 break;
7650 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007651 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007652 Opc = X86ISD::UCOMI;
7653 CC = ISD::SETGE;
7654 break;
7655 case Intrinsic::x86_sse_ucomineq_ss:
7656 case Intrinsic::x86_sse2_ucomineq_sd:
7657 Opc = X86ISD::UCOMI;
7658 CC = ISD::SETNE;
7659 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00007660 }
Evan Cheng734503b2006-09-11 02:19:56 +00007661
Dan Gohman475871a2008-07-27 21:46:04 +00007662 SDValue LHS = Op.getOperand(1);
7663 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00007664 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007665 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007666 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
7667 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7668 DAG.getConstant(X86CC, MVT::i8), Cond);
7669 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00007670 }
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007671 // ptest and testp intrinsics. The intrinsic these come from are designed to
7672 // return an integer value, not just an instruction so lower it to the ptest
7673 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00007674 case Intrinsic::x86_sse41_ptestz:
7675 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007676 case Intrinsic::x86_sse41_ptestnzc:
7677 case Intrinsic::x86_avx_ptestz_256:
7678 case Intrinsic::x86_avx_ptestc_256:
7679 case Intrinsic::x86_avx_ptestnzc_256:
7680 case Intrinsic::x86_avx_vtestz_ps:
7681 case Intrinsic::x86_avx_vtestc_ps:
7682 case Intrinsic::x86_avx_vtestnzc_ps:
7683 case Intrinsic::x86_avx_vtestz_pd:
7684 case Intrinsic::x86_avx_vtestc_pd:
7685 case Intrinsic::x86_avx_vtestnzc_pd:
7686 case Intrinsic::x86_avx_vtestz_ps_256:
7687 case Intrinsic::x86_avx_vtestc_ps_256:
7688 case Intrinsic::x86_avx_vtestnzc_ps_256:
7689 case Intrinsic::x86_avx_vtestz_pd_256:
7690 case Intrinsic::x86_avx_vtestc_pd_256:
7691 case Intrinsic::x86_avx_vtestnzc_pd_256: {
7692 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00007693 unsigned X86CC = 0;
7694 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00007695 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007696 case Intrinsic::x86_avx_vtestz_ps:
7697 case Intrinsic::x86_avx_vtestz_pd:
7698 case Intrinsic::x86_avx_vtestz_ps_256:
7699 case Intrinsic::x86_avx_vtestz_pd_256:
7700 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00007701 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007702 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00007703 // ZF = 1
7704 X86CC = X86::COND_E;
7705 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007706 case Intrinsic::x86_avx_vtestc_ps:
7707 case Intrinsic::x86_avx_vtestc_pd:
7708 case Intrinsic::x86_avx_vtestc_ps_256:
7709 case Intrinsic::x86_avx_vtestc_pd_256:
7710 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00007711 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007712 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00007713 // CF = 1
7714 X86CC = X86::COND_B;
7715 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007716 case Intrinsic::x86_avx_vtestnzc_ps:
7717 case Intrinsic::x86_avx_vtestnzc_pd:
7718 case Intrinsic::x86_avx_vtestnzc_ps_256:
7719 case Intrinsic::x86_avx_vtestnzc_pd_256:
7720 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00007721 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007722 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00007723 // ZF and CF = 0
7724 X86CC = X86::COND_A;
7725 break;
7726 }
Eric Christopherfd179292009-08-27 18:07:15 +00007727
Eric Christopher71c67532009-07-29 00:28:05 +00007728 SDValue LHS = Op.getOperand(1);
7729 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007730 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
7731 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00007732 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
7733 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
7734 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00007735 }
Evan Cheng5759f972008-05-04 09:15:50 +00007736
7737 // Fix vector shift instructions where the last operand is a non-immediate
7738 // i32 value.
7739 case Intrinsic::x86_sse2_pslli_w:
7740 case Intrinsic::x86_sse2_pslli_d:
7741 case Intrinsic::x86_sse2_pslli_q:
7742 case Intrinsic::x86_sse2_psrli_w:
7743 case Intrinsic::x86_sse2_psrli_d:
7744 case Intrinsic::x86_sse2_psrli_q:
7745 case Intrinsic::x86_sse2_psrai_w:
7746 case Intrinsic::x86_sse2_psrai_d:
7747 case Intrinsic::x86_mmx_pslli_w:
7748 case Intrinsic::x86_mmx_pslli_d:
7749 case Intrinsic::x86_mmx_pslli_q:
7750 case Intrinsic::x86_mmx_psrli_w:
7751 case Intrinsic::x86_mmx_psrli_d:
7752 case Intrinsic::x86_mmx_psrli_q:
7753 case Intrinsic::x86_mmx_psrai_w:
7754 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00007755 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00007756 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00007757 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00007758
7759 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00007760 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00007761 switch (IntNo) {
7762 case Intrinsic::x86_sse2_pslli_w:
7763 NewIntNo = Intrinsic::x86_sse2_psll_w;
7764 break;
7765 case Intrinsic::x86_sse2_pslli_d:
7766 NewIntNo = Intrinsic::x86_sse2_psll_d;
7767 break;
7768 case Intrinsic::x86_sse2_pslli_q:
7769 NewIntNo = Intrinsic::x86_sse2_psll_q;
7770 break;
7771 case Intrinsic::x86_sse2_psrli_w:
7772 NewIntNo = Intrinsic::x86_sse2_psrl_w;
7773 break;
7774 case Intrinsic::x86_sse2_psrli_d:
7775 NewIntNo = Intrinsic::x86_sse2_psrl_d;
7776 break;
7777 case Intrinsic::x86_sse2_psrli_q:
7778 NewIntNo = Intrinsic::x86_sse2_psrl_q;
7779 break;
7780 case Intrinsic::x86_sse2_psrai_w:
7781 NewIntNo = Intrinsic::x86_sse2_psra_w;
7782 break;
7783 case Intrinsic::x86_sse2_psrai_d:
7784 NewIntNo = Intrinsic::x86_sse2_psra_d;
7785 break;
7786 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00007787 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00007788 switch (IntNo) {
7789 case Intrinsic::x86_mmx_pslli_w:
7790 NewIntNo = Intrinsic::x86_mmx_psll_w;
7791 break;
7792 case Intrinsic::x86_mmx_pslli_d:
7793 NewIntNo = Intrinsic::x86_mmx_psll_d;
7794 break;
7795 case Intrinsic::x86_mmx_pslli_q:
7796 NewIntNo = Intrinsic::x86_mmx_psll_q;
7797 break;
7798 case Intrinsic::x86_mmx_psrli_w:
7799 NewIntNo = Intrinsic::x86_mmx_psrl_w;
7800 break;
7801 case Intrinsic::x86_mmx_psrli_d:
7802 NewIntNo = Intrinsic::x86_mmx_psrl_d;
7803 break;
7804 case Intrinsic::x86_mmx_psrli_q:
7805 NewIntNo = Intrinsic::x86_mmx_psrl_q;
7806 break;
7807 case Intrinsic::x86_mmx_psrai_w:
7808 NewIntNo = Intrinsic::x86_mmx_psra_w;
7809 break;
7810 case Intrinsic::x86_mmx_psrai_d:
7811 NewIntNo = Intrinsic::x86_mmx_psra_d;
7812 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00007813 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00007814 }
7815 break;
7816 }
7817 }
Mon P Wangefa42202009-09-03 19:56:25 +00007818
7819 // The vector shift intrinsics with scalars uses 32b shift amounts but
7820 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
7821 // to be zero.
7822 SDValue ShOps[4];
7823 ShOps[0] = ShAmt;
7824 ShOps[1] = DAG.getConstant(0, MVT::i32);
7825 if (ShAmtVT == MVT::v4i32) {
7826 ShOps[2] = DAG.getUNDEF(MVT::i32);
7827 ShOps[3] = DAG.getUNDEF(MVT::i32);
7828 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
7829 } else {
7830 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00007831// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00007832 }
7833
Owen Andersone50ed302009-08-10 22:56:29 +00007834 EVT VT = Op.getValueType();
Mon P Wangefa42202009-09-03 19:56:25 +00007835 ShAmt = DAG.getNode(ISD::BIT_CONVERT, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007836 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007837 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00007838 Op.getOperand(1), ShAmt);
7839 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00007840 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007841}
Evan Cheng72261582005-12-20 06:22:03 +00007842
Dan Gohmand858e902010-04-17 15:26:15 +00007843SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
7844 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00007845 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7846 MFI->setReturnAddressIsTaken(true);
7847
Bill Wendling64e87322009-01-16 19:25:27 +00007848 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007849 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00007850
7851 if (Depth > 0) {
7852 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
7853 SDValue Offset =
7854 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00007855 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007856 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00007857 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007858 FrameAddr, Offset),
Chris Lattner51abfe42010-09-21 06:02:19 +00007859 MachinePointerInfo(), false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00007860 }
7861
7862 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00007863 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00007864 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007865 RetAddrFI, MachinePointerInfo(), false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00007866}
7867
Dan Gohmand858e902010-04-17 15:26:15 +00007868SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00007869 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7870 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00007871
Owen Andersone50ed302009-08-10 22:56:29 +00007872 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007873 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00007874 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
7875 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00007876 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00007877 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00007878 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
7879 MachinePointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +00007880 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00007881 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00007882}
7883
Dan Gohman475871a2008-07-27 21:46:04 +00007884SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007885 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00007886 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007887}
7888
Dan Gohmand858e902010-04-17 15:26:15 +00007889SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007890 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00007891 SDValue Chain = Op.getOperand(0);
7892 SDValue Offset = Op.getOperand(1);
7893 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007894 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007895
Dan Gohmand8816272010-08-11 18:14:00 +00007896 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
7897 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
7898 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007899 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007900
Dan Gohmand8816272010-08-11 18:14:00 +00007901 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
7902 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007903 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00007904 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
7905 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00007906 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007907 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007908
Dale Johannesene4d209d2009-02-03 20:21:25 +00007909 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007910 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007911 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007912}
7913
Dan Gohman475871a2008-07-27 21:46:04 +00007914SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007915 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007916 SDValue Root = Op.getOperand(0);
7917 SDValue Trmp = Op.getOperand(1); // trampoline
7918 SDValue FPtr = Op.getOperand(2); // nested function
7919 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007920 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00007921
Dan Gohman69de1932008-02-06 22:27:42 +00007922 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00007923
7924 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00007925 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00007926
7927 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00007928 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
7929 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00007930
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00007931 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
7932 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00007933
7934 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
7935
7936 // Load the pointer to the nested function into R11.
7937 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00007938 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00007939 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007940 Addr, MachinePointerInfo(TrmpAddr),
7941 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007942
Owen Anderson825b72b2009-08-11 20:47:22 +00007943 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7944 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00007945 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
7946 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00007947 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00007948
7949 // Load the 'nest' parameter value into R10.
7950 // R10 is specified in X86CallingConv.td
7951 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00007952 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7953 DAG.getConstant(10, MVT::i64));
7954 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007955 Addr, MachinePointerInfo(TrmpAddr, 10),
7956 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007957
Owen Anderson825b72b2009-08-11 20:47:22 +00007958 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7959 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00007960 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
7961 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00007962 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00007963
7964 // Jump to the nested function.
7965 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00007966 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7967 DAG.getConstant(20, MVT::i64));
7968 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007969 Addr, MachinePointerInfo(TrmpAddr, 20),
7970 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007971
7972 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00007973 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7974 DAG.getConstant(22, MVT::i64));
7975 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00007976 MachinePointerInfo(TrmpAddr, 22),
7977 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007978
Dan Gohman475871a2008-07-27 21:46:04 +00007979 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00007980 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007981 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007982 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00007983 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00007984 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00007985 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00007986 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007987
7988 switch (CC) {
7989 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00007990 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00007991 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00007992 case CallingConv::X86_StdCall: {
7993 // Pass 'nest' parameter in ECX.
7994 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00007995 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007996
7997 // Check that ECX wasn't needed by an 'inreg' parameter.
7998 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00007999 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008000
Chris Lattner58d74912008-03-12 17:45:29 +00008001 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00008002 unsigned InRegCount = 0;
8003 unsigned Idx = 1;
8004
8005 for (FunctionType::param_iterator I = FTy->param_begin(),
8006 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00008007 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00008008 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008009 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008010
8011 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00008012 report_fatal_error("Nest register in use - reduce number of inreg"
8013 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008014 }
8015 }
8016 break;
8017 }
8018 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00008019 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00008020 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008021 // Pass 'nest' parameter in EAX.
8022 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008023 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008024 break;
8025 }
8026
Dan Gohman475871a2008-07-27 21:46:04 +00008027 SDValue OutChains[4];
8028 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008029
Owen Anderson825b72b2009-08-11 20:47:22 +00008030 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8031 DAG.getConstant(10, MVT::i32));
8032 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008033
Chris Lattnera62fe662010-02-05 19:20:30 +00008034 // This is storing the opcode for MOV32ri.
8035 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008036 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00008037 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008038 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008039 Trmp, MachinePointerInfo(TrmpAddr),
8040 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008041
Owen Anderson825b72b2009-08-11 20:47:22 +00008042 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8043 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008044 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
8045 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00008046 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008047
Chris Lattnera62fe662010-02-05 19:20:30 +00008048 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00008049 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8050 DAG.getConstant(5, MVT::i32));
8051 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008052 MachinePointerInfo(TrmpAddr, 5),
8053 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008054
Owen Anderson825b72b2009-08-11 20:47:22 +00008055 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8056 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008057 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
8058 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00008059 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008060
Dan Gohman475871a2008-07-27 21:46:04 +00008061 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008062 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008063 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008064 }
8065}
8066
Dan Gohmand858e902010-04-17 15:26:15 +00008067SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
8068 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008069 /*
8070 The rounding mode is in bits 11:10 of FPSR, and has the following
8071 settings:
8072 00 Round to nearest
8073 01 Round to -inf
8074 10 Round to +inf
8075 11 Round to 0
8076
8077 FLT_ROUNDS, on the other hand, expects the following:
8078 -1 Undefined
8079 0 Round to 0
8080 1 Round to nearest
8081 2 Round to +inf
8082 3 Round to -inf
8083
8084 To perform the conversion, we do:
8085 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
8086 */
8087
8088 MachineFunction &MF = DAG.getMachineFunction();
8089 const TargetMachine &TM = MF.getTarget();
8090 const TargetFrameInfo &TFI = *TM.getFrameInfo();
8091 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00008092 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00008093 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008094
8095 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00008096 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008097 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008098
Chris Lattner2156b792010-09-22 01:11:26 +00008099
8100 MachineMemOperand *MMO =
8101 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8102 MachineMemOperand::MOStore, 2, 2);
8103
8104 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
8105 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
8106 DAG.getVTList(MVT::Other),
8107 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008108
8109 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00008110 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Chris Lattner51abfe42010-09-21 06:02:19 +00008111 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008112
8113 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00008114 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00008115 DAG.getNode(ISD::SRL, DL, MVT::i16,
8116 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008117 CWD, DAG.getConstant(0x800, MVT::i16)),
8118 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00008119 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00008120 DAG.getNode(ISD::SRL, DL, MVT::i16,
8121 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008122 CWD, DAG.getConstant(0x400, MVT::i16)),
8123 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008124
Dan Gohman475871a2008-07-27 21:46:04 +00008125 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00008126 DAG.getNode(ISD::AND, DL, MVT::i16,
8127 DAG.getNode(ISD::ADD, DL, MVT::i16,
8128 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00008129 DAG.getConstant(1, MVT::i16)),
8130 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008131
8132
Duncan Sands83ec4b62008-06-06 12:08:01 +00008133 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00008134 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008135}
8136
Dan Gohmand858e902010-04-17 15:26:15 +00008137SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008138 EVT VT = Op.getValueType();
8139 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008140 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008141 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008142
8143 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008144 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00008145 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00008146 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008147 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008148 }
Evan Cheng18efe262007-12-14 02:13:44 +00008149
Evan Cheng152804e2007-12-14 08:30:15 +00008150 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008151 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008152 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008153
8154 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008155 SDValue Ops[] = {
8156 Op,
8157 DAG.getConstant(NumBits+NumBits-1, OpVT),
8158 DAG.getConstant(X86::COND_E, MVT::i8),
8159 Op.getValue(1)
8160 };
8161 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008162
8163 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00008164 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00008165
Owen Anderson825b72b2009-08-11 20:47:22 +00008166 if (VT == MVT::i8)
8167 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008168 return Op;
8169}
8170
Dan Gohmand858e902010-04-17 15:26:15 +00008171SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008172 EVT VT = Op.getValueType();
8173 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008174 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008175 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008176
8177 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008178 if (VT == MVT::i8) {
8179 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008180 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008181 }
Evan Cheng152804e2007-12-14 08:30:15 +00008182
8183 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008184 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008185 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008186
8187 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008188 SDValue Ops[] = {
8189 Op,
8190 DAG.getConstant(NumBits, OpVT),
8191 DAG.getConstant(X86::COND_E, MVT::i8),
8192 Op.getValue(1)
8193 };
8194 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008195
Owen Anderson825b72b2009-08-11 20:47:22 +00008196 if (VT == MVT::i8)
8197 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008198 return Op;
8199}
8200
Dan Gohmand858e902010-04-17 15:26:15 +00008201SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008202 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00008203 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008204 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00008205
Mon P Wangaf9b9522008-12-18 21:42:19 +00008206 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
8207 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
8208 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
8209 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
8210 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
8211 //
8212 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
8213 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
8214 // return AloBlo + AloBhi + AhiBlo;
8215
8216 SDValue A = Op.getOperand(0);
8217 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008218
Dale Johannesene4d209d2009-02-03 20:21:25 +00008219 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008220 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8221 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008222 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008223 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8224 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008225 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008226 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008227 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008228 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008229 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008230 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008231 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008232 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008233 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008234 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008235 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8236 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008237 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008238 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8239 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008240 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
8241 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008242 return Res;
8243}
8244
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008245SDValue X86TargetLowering::LowerSHL(SDValue Op, SelectionDAG &DAG) const {
8246 EVT VT = Op.getValueType();
8247 DebugLoc dl = Op.getDebugLoc();
8248 SDValue R = Op.getOperand(0);
8249
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008250 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008251
Nate Begeman51409212010-07-28 00:21:48 +00008252 assert(Subtarget->hasSSE41() && "Cannot lower SHL without SSE4.1 or later");
8253
8254 if (VT == MVT::v4i32) {
8255 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8256 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8257 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
8258
8259 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
8260
8261 std::vector<Constant*> CV(4, CI);
8262 Constant *C = ConstantVector::get(CV);
8263 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8264 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008265 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008266 false, false, 16);
8267
8268 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
8269 Op = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, Op);
8270 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
8271 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
8272 }
8273 if (VT == MVT::v16i8) {
8274 // a = a << 5;
8275 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8276 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8277 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
8278
8279 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
8280 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
8281
8282 std::vector<Constant*> CVM1(16, CM1);
8283 std::vector<Constant*> CVM2(16, CM2);
8284 Constant *C = ConstantVector::get(CVM1);
8285 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8286 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008287 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008288 false, false, 16);
8289
8290 // r = pblendv(r, psllw(r & (char16)15, 4), a);
8291 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8292 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8293 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8294 DAG.getConstant(4, MVT::i32));
8295 R = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8296 DAG.getConstant(Intrinsic::x86_sse41_pblendvb, MVT::i32),
8297 R, M, Op);
8298 // a += a
8299 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
8300
8301 C = ConstantVector::get(CVM2);
8302 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8303 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008304 MachinePointerInfo::getConstantPool(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008305 false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +00008306
8307 // r = pblendv(r, psllw(r & (char16)63, 2), a);
8308 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8309 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8310 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8311 DAG.getConstant(2, MVT::i32));
8312 R = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8313 DAG.getConstant(Intrinsic::x86_sse41_pblendvb, MVT::i32),
8314 R, M, Op);
8315 // a += a
8316 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
8317
8318 // return pblendv(r, r+r, a);
8319 R = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8320 DAG.getConstant(Intrinsic::x86_sse41_pblendvb, MVT::i32),
8321 R, DAG.getNode(ISD::ADD, dl, VT, R, R), Op);
8322 return R;
8323 }
8324 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008325}
Mon P Wangaf9b9522008-12-18 21:42:19 +00008326
Dan Gohmand858e902010-04-17 15:26:15 +00008327SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +00008328 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
8329 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00008330 // looks for this combo and may remove the "setcc" instruction if the "setcc"
8331 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00008332 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00008333 SDValue LHS = N->getOperand(0);
8334 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00008335 unsigned BaseOp = 0;
8336 unsigned Cond = 0;
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008337 DebugLoc dl = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00008338
8339 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008340 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00008341 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00008342 // A subtract of one will be selected as a INC. Note that INC doesn't
8343 // set CF, so we can't do this for UADDO.
8344 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
8345 if (C->getAPIntValue() == 1) {
8346 BaseOp = X86ISD::INC;
8347 Cond = X86::COND_O;
8348 break;
8349 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008350 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00008351 Cond = X86::COND_O;
8352 break;
8353 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008354 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00008355 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008356 break;
8357 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00008358 // A subtract of one will be selected as a DEC. Note that DEC doesn't
8359 // set CF, so we can't do this for USUBO.
8360 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
8361 if (C->getAPIntValue() == 1) {
8362 BaseOp = X86ISD::DEC;
8363 Cond = X86::COND_O;
8364 break;
8365 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008366 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00008367 Cond = X86::COND_O;
8368 break;
8369 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008370 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00008371 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008372 break;
8373 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00008374 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00008375 Cond = X86::COND_O;
8376 break;
8377 case ISD::UMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00008378 BaseOp = X86ISD::UMUL;
Dan Gohman653456c2009-01-07 00:15:08 +00008379 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008380 break;
8381 }
Bill Wendling3fafd932008-11-26 22:37:40 +00008382
Bill Wendling61edeb52008-12-02 01:06:39 +00008383 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008384 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008385 SDValue Sum = DAG.getNode(BaseOp, dl, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00008386
Bill Wendling61edeb52008-12-02 01:06:39 +00008387 SDValue SetCC =
Dale Johannesene4d209d2009-02-03 20:21:25 +00008388 DAG.getNode(X86ISD::SETCC, dl, N->getValueType(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00008389 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00008390
Bill Wendling61edeb52008-12-02 01:06:39 +00008391 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
8392 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00008393}
8394
Eric Christopher9a9d2752010-07-22 02:48:34 +00008395SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
8396 DebugLoc dl = Op.getDebugLoc();
8397
Eric Christopherb6729dc2010-08-04 23:03:04 +00008398 if (!Subtarget->hasSSE2()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +00008399 SDValue Chain = Op.getOperand(0);
8400 SDValue Zero = DAG.getConstant(0,
Eric Christopherb6729dc2010-08-04 23:03:04 +00008401 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +00008402 SDValue Ops[] = {
8403 DAG.getRegister(X86::ESP, MVT::i32), // Base
8404 DAG.getTargetConstant(1, MVT::i8), // Scale
8405 DAG.getRegister(0, MVT::i32), // Index
8406 DAG.getTargetConstant(0, MVT::i32), // Disp
8407 DAG.getRegister(0, MVT::i32), // Segment.
8408 Zero,
8409 Chain
8410 };
8411 SDNode *Res =
8412 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
8413 array_lengthof(Ops));
8414 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +00008415 }
Eric Christopher9a9d2752010-07-22 02:48:34 +00008416
8417 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +00008418 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +00008419 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Chris Lattner132929a2010-08-14 17:26:09 +00008420
8421 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
8422 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
8423 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
8424 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
8425
8426 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
8427 if (!Op1 && !Op2 && !Op3 && Op4)
8428 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
8429
8430 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
8431 if (Op1 && !Op2 && !Op3 && !Op4)
8432 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
8433
8434 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
8435 // (MFENCE)>;
8436 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +00008437}
8438
Dan Gohmand858e902010-04-17 15:26:15 +00008439SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008440 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008441 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00008442 unsigned Reg = 0;
8443 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00008444 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008445 default:
8446 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008447 case MVT::i8: Reg = X86::AL; size = 1; break;
8448 case MVT::i16: Reg = X86::AX; size = 2; break;
8449 case MVT::i32: Reg = X86::EAX; size = 4; break;
8450 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00008451 assert(Subtarget->is64Bit() && "Node not type legal!");
8452 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00008453 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008454 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008455 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00008456 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00008457 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00008458 Op.getOperand(1),
8459 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00008460 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00008461 cpIn.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00008462 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008463 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
8464 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
8465 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +00008466 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008467 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00008468 return cpOut;
8469}
8470
Duncan Sands1607f052008-12-01 11:39:25 +00008471SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008472 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +00008473 assert(Subtarget->is64Bit() && "Result not type legalized?");
Owen Anderson825b72b2009-08-11 20:47:22 +00008474 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00008475 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008476 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00008477 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008478 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
8479 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00008480 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00008481 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
8482 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00008483 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00008484 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00008485 rdx.getValue(1)
8486 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008487 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008488}
8489
Dale Johannesen7d07b482010-05-21 00:52:33 +00008490SDValue X86TargetLowering::LowerBIT_CONVERT(SDValue Op,
8491 SelectionDAG &DAG) const {
8492 EVT SrcVT = Op.getOperand(0).getValueType();
8493 EVT DstVT = Op.getValueType();
8494 assert((Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
8495 Subtarget->hasMMX() && !DisableMMX) &&
8496 "Unexpected custom BIT_CONVERT");
8497 assert((DstVT == MVT::i64 ||
8498 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
8499 "Unexpected custom BIT_CONVERT");
8500 // i64 <=> MMX conversions are Legal.
8501 if (SrcVT==MVT::i64 && DstVT.isVector())
8502 return Op;
8503 if (DstVT==MVT::i64 && SrcVT.isVector())
8504 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +00008505 // MMX <=> MMX conversions are Legal.
8506 if (SrcVT.isVector() && DstVT.isVector())
8507 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +00008508 // All other conversions need to be expanded.
8509 return SDValue();
8510}
Dan Gohmand858e902010-04-17 15:26:15 +00008511SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +00008512 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00008513 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008514 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008515 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00008516 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008517 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008518 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00008519 Node->getOperand(0),
8520 Node->getOperand(1), negOp,
8521 cast<AtomicSDNode>(Node)->getSrcValue(),
8522 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00008523}
8524
Evan Cheng0db9fe62006-04-25 20:13:52 +00008525/// LowerOperation - Provide custom lowering hooks for some operations.
8526///
Dan Gohmand858e902010-04-17 15:26:15 +00008527SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00008528 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008529 default: llvm_unreachable("Should not custom lower this!");
Eric Christopher9a9d2752010-07-22 02:48:34 +00008530 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008531 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
8532 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008533 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00008534 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008535 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
8536 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
8537 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
8538 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
8539 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
8540 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00008541 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00008542 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00008543 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008544 case ISD::SHL_PARTS:
8545 case ISD::SRA_PARTS:
8546 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
8547 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008548 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008549 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00008550 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008551 case ISD::FABS: return LowerFABS(Op, DAG);
8552 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008553 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00008554 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00008555 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00008556 case ISD::SELECT: return LowerSELECT(Op, DAG);
8557 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008558 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008559 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00008560 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00008561 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008562 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00008563 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
8564 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008565 case ISD::FRAME_TO_ARGS_OFFSET:
8566 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008567 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008568 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008569 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00008570 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00008571 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
8572 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008573 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008574 case ISD::SHL: return LowerSHL(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00008575 case ISD::SADDO:
8576 case ISD::UADDO:
8577 case ISD::SSUBO:
8578 case ISD::USUBO:
8579 case ISD::SMULO:
8580 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00008581 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Dale Johannesen7d07b482010-05-21 00:52:33 +00008582 case ISD::BIT_CONVERT: return LowerBIT_CONVERT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008583 }
Chris Lattner27a6c732007-11-24 07:07:01 +00008584}
8585
Duncan Sands1607f052008-12-01 11:39:25 +00008586void X86TargetLowering::
8587ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00008588 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008589 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008590 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00008591 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00008592
8593 SDValue Chain = Node->getOperand(0);
8594 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008595 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00008596 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00008597 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00008598 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00008599 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00008600 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00008601 SDValue Result =
8602 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
8603 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00008604 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00008605 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00008606 Results.push_back(Result.getValue(2));
8607}
8608
Duncan Sands126d9072008-07-04 11:47:58 +00008609/// ReplaceNodeResults - Replace a node with an illegal result type
8610/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00008611void X86TargetLowering::ReplaceNodeResults(SDNode *N,
8612 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00008613 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00008614 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00008615 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00008616 default:
Duncan Sands1607f052008-12-01 11:39:25 +00008617 assert(false && "Do not know how to custom type legalize this operation!");
8618 return;
8619 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00008620 std::pair<SDValue,SDValue> Vals =
8621 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00008622 SDValue FIST = Vals.first, StackSlot = Vals.second;
8623 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00008624 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00008625 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +00008626 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
8627 MachinePointerInfo(), false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00008628 }
8629 return;
8630 }
8631 case ISD::READCYCLECOUNTER: {
Owen Anderson825b72b2009-08-11 20:47:22 +00008632 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00008633 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008634 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008635 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00008636 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008637 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00008638 eax.getValue(2));
8639 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
8640 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00008641 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00008642 Results.push_back(edx.getValue(1));
8643 return;
8644 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008645 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00008646 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008647 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00008648 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00008649 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
8650 DAG.getConstant(0, MVT::i32));
8651 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
8652 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00008653 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
8654 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00008655 cpInL.getValue(1));
8656 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00008657 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
8658 DAG.getConstant(0, MVT::i32));
8659 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
8660 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00008661 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00008662 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00008663 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00008664 swapInL.getValue(1));
8665 SDValue Ops[] = { swapInH.getValue(0),
8666 N->getOperand(1),
8667 swapInH.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00008668 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008669 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, dl, Tys, Ops, 3);
Dale Johannesendd64c412009-02-04 00:33:20 +00008670 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00008671 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00008672 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00008673 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00008674 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00008675 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00008676 Results.push_back(cpOutH.getValue(1));
8677 return;
8678 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008679 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00008680 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
8681 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008682 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00008683 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
8684 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008685 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00008686 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
8687 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008688 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00008689 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
8690 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008691 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00008692 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
8693 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008694 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00008695 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
8696 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008697 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00008698 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
8699 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00008700 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008701}
8702
Evan Cheng72261582005-12-20 06:22:03 +00008703const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
8704 switch (Opcode) {
8705 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00008706 case X86ISD::BSF: return "X86ISD::BSF";
8707 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00008708 case X86ISD::SHLD: return "X86ISD::SHLD";
8709 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00008710 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00008711 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00008712 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00008713 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00008714 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00008715 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00008716 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
8717 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
8718 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00008719 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00008720 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00008721 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00008722 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00008723 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00008724 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00008725 case X86ISD::COMI: return "X86ISD::COMI";
8726 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00008727 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00008728 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Evan Cheng72261582005-12-20 06:22:03 +00008729 case X86ISD::CMOV: return "X86ISD::CMOV";
8730 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00008731 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00008732 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
8733 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00008734 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00008735 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00008736 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00008737 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00008738 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00008739 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
8740 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00008741 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00008742 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Evan Cheng8ca29322006-11-10 21:43:37 +00008743 case X86ISD::FMAX: return "X86ISD::FMAX";
8744 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00008745 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
8746 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00008747 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +00008748 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008749 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00008750 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008751 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00008752 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
8753 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008754 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
8755 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
8756 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
8757 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
8758 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
8759 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00008760 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
8761 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00008762 case X86ISD::VSHL: return "X86ISD::VSHL";
8763 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00008764 case X86ISD::CMPPD: return "X86ISD::CMPPD";
8765 case X86ISD::CMPPS: return "X86ISD::CMPPS";
8766 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
8767 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
8768 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
8769 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
8770 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
8771 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
8772 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
8773 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008774 case X86ISD::ADD: return "X86ISD::ADD";
8775 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingd350e022008-12-12 21:15:41 +00008776 case X86ISD::SMUL: return "X86ISD::SMUL";
8777 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00008778 case X86ISD::INC: return "X86ISD::INC";
8779 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00008780 case X86ISD::OR: return "X86ISD::OR";
8781 case X86ISD::XOR: return "X86ISD::XOR";
8782 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00008783 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00008784 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008785 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00008786 case X86ISD::PALIGN: return "X86ISD::PALIGN";
8787 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
8788 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
8789 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
8790 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
8791 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
8792 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
8793 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
8794 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00008795 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00008796 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00008797 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00008798 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
8799 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00008800 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
8801 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
8802 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
8803 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
8804 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
8805 case X86ISD::MOVSD: return "X86ISD::MOVSD";
8806 case X86ISD::MOVSS: return "X86ISD::MOVSS";
8807 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
8808 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
8809 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
8810 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
8811 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
8812 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
8813 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
8814 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
8815 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
8816 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
8817 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
8818 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +00008819 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008820 case X86ISD::MINGW_ALLOCA: return "X86ISD::MINGW_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00008821 }
8822}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008823
Chris Lattnerc9addb72007-03-30 23:15:24 +00008824// isLegalAddressingMode - Return true if the addressing mode represented
8825// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00008826bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00008827 const Type *Ty) const {
8828 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008829 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +00008830 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00008831
Chris Lattnerc9addb72007-03-30 23:15:24 +00008832 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008833 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00008834 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00008835
Chris Lattnerc9addb72007-03-30 23:15:24 +00008836 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00008837 unsigned GVFlags =
8838 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008839
Chris Lattnerdfed4132009-07-10 07:38:24 +00008840 // If a reference to this global requires an extra load, we can't fold it.
8841 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00008842 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008843
Chris Lattnerdfed4132009-07-10 07:38:24 +00008844 // If BaseGV requires a register for the PIC base, we cannot also have a
8845 // BaseReg specified.
8846 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00008847 return false;
Evan Cheng52787842007-08-01 23:46:47 +00008848
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008849 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +00008850 if ((M != CodeModel::Small || R != Reloc::Static) &&
8851 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008852 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00008853 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008854
Chris Lattnerc9addb72007-03-30 23:15:24 +00008855 switch (AM.Scale) {
8856 case 0:
8857 case 1:
8858 case 2:
8859 case 4:
8860 case 8:
8861 // These scales always work.
8862 break;
8863 case 3:
8864 case 5:
8865 case 9:
8866 // These scales are formed with basereg+scalereg. Only accept if there is
8867 // no basereg yet.
8868 if (AM.HasBaseReg)
8869 return false;
8870 break;
8871 default: // Other stuff never works.
8872 return false;
8873 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008874
Chris Lattnerc9addb72007-03-30 23:15:24 +00008875 return true;
8876}
8877
8878
Evan Cheng2bd122c2007-10-26 01:56:11 +00008879bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00008880 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00008881 return false;
Evan Chenge127a732007-10-29 07:57:50 +00008882 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
8883 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00008884 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00008885 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00008886 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00008887}
8888
Owen Andersone50ed302009-08-10 22:56:29 +00008889bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008890 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00008891 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008892 unsigned NumBits1 = VT1.getSizeInBits();
8893 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00008894 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00008895 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00008896 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00008897}
Evan Cheng2bd122c2007-10-26 01:56:11 +00008898
Dan Gohman97121ba2009-04-08 00:15:30 +00008899bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00008900 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00008901 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00008902}
8903
Owen Andersone50ed302009-08-10 22:56:29 +00008904bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00008905 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00008906 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00008907}
8908
Owen Andersone50ed302009-08-10 22:56:29 +00008909bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00008910 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00008911 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00008912}
8913
Evan Cheng60c07e12006-07-05 22:17:51 +00008914/// isShuffleMaskLegal - Targets can use this to indicate that they only
8915/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
8916/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
8917/// are assumed to be legal.
8918bool
Eric Christopherfd179292009-08-27 18:07:15 +00008919X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00008920 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +00008921 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +00008922 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +00008923 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +00008924
Nate Begemana09008b2009-10-19 02:17:23 +00008925 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00008926 return (VT.getVectorNumElements() == 2 ||
8927 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
8928 isMOVLMask(M, VT) ||
8929 isSHUFPMask(M, VT) ||
8930 isPSHUFDMask(M, VT) ||
8931 isPSHUFHWMask(M, VT) ||
8932 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00008933 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00008934 isUNPCKLMask(M, VT) ||
8935 isUNPCKHMask(M, VT) ||
8936 isUNPCKL_v_undef_Mask(M, VT) ||
8937 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00008938}
8939
Dan Gohman7d8143f2008-04-09 20:09:42 +00008940bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00008941X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00008942 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00008943 unsigned NumElts = VT.getVectorNumElements();
8944 // FIXME: This collection of masks seems suspect.
8945 if (NumElts == 2)
8946 return true;
8947 if (NumElts == 4 && VT.getSizeInBits() == 128) {
8948 return (isMOVLMask(Mask, VT) ||
8949 isCommutedMOVLMask(Mask, VT, true) ||
8950 isSHUFPMask(Mask, VT) ||
8951 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00008952 }
8953 return false;
8954}
8955
8956//===----------------------------------------------------------------------===//
8957// X86 Scheduler Hooks
8958//===----------------------------------------------------------------------===//
8959
Mon P Wang63307c32008-05-05 19:05:59 +00008960// private utility function
8961MachineBasicBlock *
8962X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
8963 MachineBasicBlock *MBB,
8964 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008965 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008966 unsigned LoadOpc,
8967 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008968 unsigned notOpc,
8969 unsigned EAXreg,
8970 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00008971 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00008972 // For the atomic bitwise operator, we generate
8973 // thisMBB:
8974 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00008975 // ld t1 = [bitinstr.addr]
8976 // op t2 = t1, [bitinstr.val]
8977 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00008978 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
8979 // bz newMBB
8980 // fallthrough -->nextMBB
8981 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8982 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008983 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00008984 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00008985
Mon P Wang63307c32008-05-05 19:05:59 +00008986 /// First build the CFG
8987 MachineFunction *F = MBB->getParent();
8988 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008989 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
8990 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
8991 F->insert(MBBIter, newMBB);
8992 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008993
Dan Gohman14152b42010-07-06 20:24:04 +00008994 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
8995 nextMBB->splice(nextMBB->begin(), thisMBB,
8996 llvm::next(MachineBasicBlock::iterator(bInstr)),
8997 thisMBB->end());
8998 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008999
Mon P Wang63307c32008-05-05 19:05:59 +00009000 // Update thisMBB to fall through to newMBB
9001 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009002
Mon P Wang63307c32008-05-05 19:05:59 +00009003 // newMBB jumps to itself and fall through to nextMBB
9004 newMBB->addSuccessor(nextMBB);
9005 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009006
Mon P Wang63307c32008-05-05 19:05:59 +00009007 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009008 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009009 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00009010 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009011 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009012 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009013 int numArgs = bInstr->getNumOperands() - 1;
9014 for (int i=0; i < numArgs; ++i)
9015 argOpers[i] = &bInstr->getOperand(i+1);
9016
9017 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009018 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009019 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009020
Dale Johannesen140be2d2008-08-19 18:47:28 +00009021 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009022 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009023 for (int i=0; i <= lastAddrIndx; ++i)
9024 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009025
Dale Johannesen140be2d2008-08-19 18:47:28 +00009026 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009027 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009028 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009029 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009030 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009031 tt = t1;
9032
Dale Johannesen140be2d2008-08-19 18:47:28 +00009033 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00009034 assert((argOpers[valArgIndx]->isReg() ||
9035 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009036 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00009037 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009038 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009039 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009040 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009041 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00009042 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009043
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009044 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00009045 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009046
Dale Johannesene4d209d2009-02-03 20:21:25 +00009047 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00009048 for (int i=0; i <= lastAddrIndx; ++i)
9049 (*MIB).addOperand(*argOpers[i]);
9050 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00009051 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009052 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9053 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00009054
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009055 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00009056 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009057
Mon P Wang63307c32008-05-05 19:05:59 +00009058 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009059 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009060
Dan Gohman14152b42010-07-06 20:24:04 +00009061 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009062 return nextMBB;
9063}
9064
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00009065// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00009066MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009067X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
9068 MachineBasicBlock *MBB,
9069 unsigned regOpcL,
9070 unsigned regOpcH,
9071 unsigned immOpcL,
9072 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009073 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009074 // For the atomic bitwise operator, we generate
9075 // thisMBB (instructions are in pairs, except cmpxchg8b)
9076 // ld t1,t2 = [bitinstr.addr]
9077 // newMBB:
9078 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
9079 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00009080 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009081 // mov ECX, EBX <- t5, t6
9082 // mov EAX, EDX <- t1, t2
9083 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
9084 // mov t3, t4 <- EAX, EDX
9085 // bz newMBB
9086 // result in out1, out2
9087 // fallthrough -->nextMBB
9088
9089 const TargetRegisterClass *RC = X86::GR32RegisterClass;
9090 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009091 const unsigned NotOpc = X86::NOT32r;
9092 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9093 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9094 MachineFunction::iterator MBBIter = MBB;
9095 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009096
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009097 /// First build the CFG
9098 MachineFunction *F = MBB->getParent();
9099 MachineBasicBlock *thisMBB = MBB;
9100 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9101 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9102 F->insert(MBBIter, newMBB);
9103 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009104
Dan Gohman14152b42010-07-06 20:24:04 +00009105 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9106 nextMBB->splice(nextMBB->begin(), thisMBB,
9107 llvm::next(MachineBasicBlock::iterator(bInstr)),
9108 thisMBB->end());
9109 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009110
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009111 // Update thisMBB to fall through to newMBB
9112 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009113
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009114 // newMBB jumps to itself and fall through to nextMBB
9115 newMBB->addSuccessor(nextMBB);
9116 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009117
Dale Johannesene4d209d2009-02-03 20:21:25 +00009118 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009119 // Insert instructions into newMBB based on incoming instruction
9120 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009121 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009122 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009123 MachineOperand& dest1Oper = bInstr->getOperand(0);
9124 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009125 MachineOperand* argOpers[2 + X86::AddrNumOperands];
9126 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009127 argOpers[i] = &bInstr->getOperand(i+2);
9128
Dan Gohman71ea4e52010-05-14 21:01:44 +00009129 // We use some of the operands multiple times, so conservatively just
9130 // clear any kill flags that might be present.
9131 if (argOpers[i]->isReg() && argOpers[i]->isUse())
9132 argOpers[i]->setIsKill(false);
9133 }
9134
Evan Chengad5b52f2010-01-08 19:14:57 +00009135 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009136 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00009137
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009138 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009139 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009140 for (int i=0; i <= lastAddrIndx; ++i)
9141 (*MIB).addOperand(*argOpers[i]);
9142 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009143 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00009144 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00009145 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009146 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00009147 MachineOperand newOp3 = *(argOpers[3]);
9148 if (newOp3.isImm())
9149 newOp3.setImm(newOp3.getImm()+4);
9150 else
9151 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009152 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00009153 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009154
9155 // t3/4 are defined later, at the bottom of the loop
9156 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
9157 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009158 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009159 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009160 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009161 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
9162
Evan Cheng306b4ca2010-01-08 23:41:50 +00009163 // The subsequent operations should be using the destination registers of
9164 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00009165 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009166 t1 = F->getRegInfo().createVirtualRegister(RC);
9167 t2 = F->getRegInfo().createVirtualRegister(RC);
9168 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
9169 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009170 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009171 t1 = dest1Oper.getReg();
9172 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009173 }
9174
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009175 int valArgIndx = lastAddrIndx + 1;
9176 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00009177 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009178 "invalid operand");
9179 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
9180 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009181 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009182 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009183 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009184 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00009185 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009186 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009187 (*MIB).addOperand(*argOpers[valArgIndx]);
9188 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009189 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009190 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009191 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009192 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009193 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009194 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009195 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00009196 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009197 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009198 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009199
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009200 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009201 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009202 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009203 MIB.addReg(t2);
9204
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009205 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009206 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009207 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009208 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00009209
Dale Johannesene4d209d2009-02-03 20:21:25 +00009210 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009211 for (int i=0; i <= lastAddrIndx; ++i)
9212 (*MIB).addOperand(*argOpers[i]);
9213
9214 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009215 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9216 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009217
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009218 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009219 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009220 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009221 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009222
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009223 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009224 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009225
Dan Gohman14152b42010-07-06 20:24:04 +00009226 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009227 return nextMBB;
9228}
9229
9230// private utility function
9231MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00009232X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
9233 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009234 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009235 // For the atomic min/max operator, we generate
9236 // thisMBB:
9237 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009238 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00009239 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00009240 // cmp t1, t2
9241 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00009242 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009243 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9244 // bz newMBB
9245 // fallthrough -->nextMBB
9246 //
9247 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9248 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009249 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009250 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009251
Mon P Wang63307c32008-05-05 19:05:59 +00009252 /// First build the CFG
9253 MachineFunction *F = MBB->getParent();
9254 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009255 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9256 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9257 F->insert(MBBIter, newMBB);
9258 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009259
Dan Gohman14152b42010-07-06 20:24:04 +00009260 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9261 nextMBB->splice(nextMBB->begin(), thisMBB,
9262 llvm::next(MachineBasicBlock::iterator(mInstr)),
9263 thisMBB->end());
9264 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009265
Mon P Wang63307c32008-05-05 19:05:59 +00009266 // Update thisMBB to fall through to newMBB
9267 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009268
Mon P Wang63307c32008-05-05 19:05:59 +00009269 // newMBB jumps to newMBB and fall through to nextMBB
9270 newMBB->addSuccessor(nextMBB);
9271 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009272
Dale Johannesene4d209d2009-02-03 20:21:25 +00009273 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009274 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009275 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009276 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00009277 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009278 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009279 int numArgs = mInstr->getNumOperands() - 1;
9280 for (int i=0; i < numArgs; ++i)
9281 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009282
Mon P Wang63307c32008-05-05 19:05:59 +00009283 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009284 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009285 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009286
Mon P Wangab3e7472008-05-05 22:56:23 +00009287 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009288 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009289 for (int i=0; i <= lastAddrIndx; ++i)
9290 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00009291
Mon P Wang63307c32008-05-05 19:05:59 +00009292 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00009293 assert((argOpers[valArgIndx]->isReg() ||
9294 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009295 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00009296
9297 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00009298 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009299 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00009300 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009301 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009302 (*MIB).addOperand(*argOpers[valArgIndx]);
9303
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009304 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00009305 MIB.addReg(t1);
9306
Dale Johannesene4d209d2009-02-03 20:21:25 +00009307 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00009308 MIB.addReg(t1);
9309 MIB.addReg(t2);
9310
9311 // Generate movc
9312 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009313 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00009314 MIB.addReg(t2);
9315 MIB.addReg(t1);
9316
9317 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00009318 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00009319 for (int i=0; i <= lastAddrIndx; ++i)
9320 (*MIB).addOperand(*argOpers[i]);
9321 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00009322 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009323 (*MIB).setMemRefs(mInstr->memoperands_begin(),
9324 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +00009325
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009326 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00009327 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009328
Mon P Wang63307c32008-05-05 19:05:59 +00009329 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009330 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009331
Dan Gohman14152b42010-07-06 20:24:04 +00009332 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009333 return nextMBB;
9334}
9335
Eric Christopherf83a5de2009-08-27 18:08:16 +00009336// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009337// or XMM0_V32I8 in AVX all of this code can be replaced with that
9338// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +00009339MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +00009340X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +00009341 unsigned numArgs, bool memArg) const {
Eric Christopherb120ab42009-08-18 22:50:32 +00009342
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009343 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
9344 "Target must have SSE4.2 or AVX features enabled");
9345
Eric Christopherb120ab42009-08-18 22:50:32 +00009346 DebugLoc dl = MI->getDebugLoc();
9347 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9348
9349 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009350
9351 if (!Subtarget->hasAVX()) {
9352 if (memArg)
9353 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
9354 else
9355 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
9356 } else {
9357 if (memArg)
9358 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
9359 else
9360 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
9361 }
Eric Christopherb120ab42009-08-18 22:50:32 +00009362
9363 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(Opc));
9364
9365 for (unsigned i = 0; i < numArgs; ++i) {
9366 MachineOperand &Op = MI->getOperand(i+1);
9367
9368 if (!(Op.isReg() && Op.isImplicit()))
9369 MIB.addOperand(Op);
9370 }
9371
9372 BuildMI(BB, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
9373 .addReg(X86::XMM0);
9374
Dan Gohman14152b42010-07-06 20:24:04 +00009375 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +00009376
9377 return BB;
9378}
9379
9380MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +00009381X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
9382 MachineInstr *MI,
9383 MachineBasicBlock *MBB) const {
9384 // Emit code to save XMM registers to the stack. The ABI says that the
9385 // number of registers to save is given in %al, so it's theoretically
9386 // possible to do an indirect jump trick to avoid saving all of them,
9387 // however this code takes a simpler approach and just executes all
9388 // of the stores if %al is non-zero. It's less code, and it's probably
9389 // easier on the hardware branch predictor, and stores aren't all that
9390 // expensive anyway.
9391
9392 // Create the new basic blocks. One block contains all the XMM stores,
9393 // and one block is the final destination regardless of whether any
9394 // stores were performed.
9395 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9396 MachineFunction *F = MBB->getParent();
9397 MachineFunction::iterator MBBIter = MBB;
9398 ++MBBIter;
9399 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
9400 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
9401 F->insert(MBBIter, XMMSaveMBB);
9402 F->insert(MBBIter, EndMBB);
9403
Dan Gohman14152b42010-07-06 20:24:04 +00009404 // Transfer the remainder of MBB and its successor edges to EndMBB.
9405 EndMBB->splice(EndMBB->begin(), MBB,
9406 llvm::next(MachineBasicBlock::iterator(MI)),
9407 MBB->end());
9408 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
9409
Dan Gohmand6708ea2009-08-15 01:38:56 +00009410 // The original block will now fall through to the XMM save block.
9411 MBB->addSuccessor(XMMSaveMBB);
9412 // The XMMSaveMBB will fall through to the end block.
9413 XMMSaveMBB->addSuccessor(EndMBB);
9414
9415 // Now add the instructions.
9416 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9417 DebugLoc DL = MI->getDebugLoc();
9418
9419 unsigned CountReg = MI->getOperand(0).getReg();
9420 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
9421 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
9422
9423 if (!Subtarget->isTargetWin64()) {
9424 // If %al is 0, branch around the XMM save block.
9425 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009426 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +00009427 MBB->addSuccessor(EndMBB);
9428 }
9429
9430 // In the XMM save block, save all the XMM argument registers.
9431 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
9432 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +00009433 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +00009434 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +00009435 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +00009436 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +00009437 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +00009438 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
9439 .addFrameIndex(RegSaveFrameIndex)
9440 .addImm(/*Scale=*/1)
9441 .addReg(/*IndexReg=*/0)
9442 .addImm(/*Disp=*/Offset)
9443 .addReg(/*Segment=*/0)
9444 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +00009445 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +00009446 }
9447
Dan Gohman14152b42010-07-06 20:24:04 +00009448 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +00009449
9450 return EndMBB;
9451}
Mon P Wang63307c32008-05-05 19:05:59 +00009452
Evan Cheng60c07e12006-07-05 22:17:51 +00009453MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +00009454X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00009455 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +00009456 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9457 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +00009458
Chris Lattner52600972009-09-02 05:57:00 +00009459 // To "insert" a SELECT_CC instruction, we actually have to insert the
9460 // diamond control-flow pattern. The incoming instruction knows the
9461 // destination vreg to set, the condition code register to branch on, the
9462 // true/false values to select between, and a branch opcode to use.
9463 const BasicBlock *LLVM_BB = BB->getBasicBlock();
9464 MachineFunction::iterator It = BB;
9465 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +00009466
Chris Lattner52600972009-09-02 05:57:00 +00009467 // thisMBB:
9468 // ...
9469 // TrueVal = ...
9470 // cmpTY ccX, r1, r2
9471 // bCC copy1MBB
9472 // fallthrough --> copy0MBB
9473 MachineBasicBlock *thisMBB = BB;
9474 MachineFunction *F = BB->getParent();
9475 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
9476 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +00009477 F->insert(It, copy0MBB);
9478 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +00009479
Bill Wendling730c07e2010-06-25 20:48:10 +00009480 // If the EFLAGS register isn't dead in the terminator, then claim that it's
9481 // live into the sink and copy blocks.
9482 const MachineFunction *MF = BB->getParent();
9483 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
9484 BitVector ReservedRegs = TRI->getReservedRegs(*MF);
Bill Wendling730c07e2010-06-25 20:48:10 +00009485
Dan Gohman14152b42010-07-06 20:24:04 +00009486 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
9487 const MachineOperand &MO = MI->getOperand(I);
9488 if (!MO.isReg() || !MO.isUse() || MO.isKill()) continue;
Bill Wendling730c07e2010-06-25 20:48:10 +00009489 unsigned Reg = MO.getReg();
9490 if (Reg != X86::EFLAGS) continue;
9491 copy0MBB->addLiveIn(Reg);
9492 sinkMBB->addLiveIn(Reg);
9493 }
9494
Dan Gohman14152b42010-07-06 20:24:04 +00009495 // Transfer the remainder of BB and its successor edges to sinkMBB.
9496 sinkMBB->splice(sinkMBB->begin(), BB,
9497 llvm::next(MachineBasicBlock::iterator(MI)),
9498 BB->end());
9499 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
9500
9501 // Add the true and fallthrough blocks as its successors.
9502 BB->addSuccessor(copy0MBB);
9503 BB->addSuccessor(sinkMBB);
9504
9505 // Create the conditional branch instruction.
9506 unsigned Opc =
9507 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
9508 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
9509
Chris Lattner52600972009-09-02 05:57:00 +00009510 // copy0MBB:
9511 // %FalseValue = ...
9512 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +00009513 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00009514
Chris Lattner52600972009-09-02 05:57:00 +00009515 // sinkMBB:
9516 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
9517 // ...
Dan Gohman14152b42010-07-06 20:24:04 +00009518 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
9519 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +00009520 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
9521 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
9522
Dan Gohman14152b42010-07-06 20:24:04 +00009523 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +00009524 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +00009525}
9526
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00009527MachineBasicBlock *
9528X86TargetLowering::EmitLoweredMingwAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00009529 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00009530 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9531 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00009532
9533 // The lowering is pretty easy: we're just emitting the call to _alloca. The
9534 // non-trivial part is impdef of ESP.
9535 // FIXME: The code should be tweaked as soon as we'll try to do codegen for
9536 // mingw-w64.
9537
Dan Gohman14152b42010-07-06 20:24:04 +00009538 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00009539 .addExternalSymbol("_alloca")
9540 .addReg(X86::EAX, RegState::Implicit)
9541 .addReg(X86::ESP, RegState::Implicit)
9542 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
Anton Korobeynikov9f7f83b2010-08-25 07:50:11 +00009543 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
9544 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00009545
Dan Gohman14152b42010-07-06 20:24:04 +00009546 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00009547 return BB;
9548}
Chris Lattner52600972009-09-02 05:57:00 +00009549
9550MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +00009551X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
9552 MachineBasicBlock *BB) const {
9553 // This is pretty easy. We're taking the value that we received from
9554 // our load from the relocation, sticking it in either RDI (x86-64)
9555 // or EAX and doing an indirect call. The return value will then
9556 // be in the normal return register.
Eric Christopher54415362010-06-08 22:04:25 +00009557 const X86InstrInfo *TII
9558 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +00009559 DebugLoc DL = MI->getDebugLoc();
9560 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +00009561
9562 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +00009563 assert(MI->getOperand(3).isGlobal() && "This should be a global");
9564
Eric Christopher30ef0e52010-06-03 04:07:48 +00009565 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +00009566 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
9567 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +00009568 .addReg(X86::RIP)
9569 .addImm(0).addReg(0)
9570 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
9571 MI->getOperand(3).getTargetFlags())
9572 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +00009573 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +00009574 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +00009575 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +00009576 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
9577 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +00009578 .addReg(0)
9579 .addImm(0).addReg(0)
9580 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
9581 MI->getOperand(3).getTargetFlags())
9582 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +00009583 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +00009584 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +00009585 } else {
Dan Gohman14152b42010-07-06 20:24:04 +00009586 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
9587 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +00009588 .addReg(TII->getGlobalBaseReg(F))
9589 .addImm(0).addReg(0)
9590 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
9591 MI->getOperand(3).getTargetFlags())
9592 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +00009593 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +00009594 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +00009595 }
9596
Dan Gohman14152b42010-07-06 20:24:04 +00009597 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +00009598 return BB;
9599}
9600
9601MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00009602X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00009603 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00009604 switch (MI->getOpcode()) {
9605 default: assert(false && "Unexpected instr type to insert");
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00009606 case X86::MINGW_ALLOCA:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00009607 return EmitLoweredMingwAlloca(MI, BB);
Eric Christopher30ef0e52010-06-03 04:07:48 +00009608 case X86::TLSCall_32:
9609 case X86::TLSCall_64:
9610 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +00009611 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +00009612 case X86::CMOV_FR32:
9613 case X86::CMOV_FR64:
9614 case X86::CMOV_V4F32:
9615 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +00009616 case X86::CMOV_V2I64:
Chris Lattner314a1132010-03-14 18:31:44 +00009617 case X86::CMOV_GR16:
9618 case X86::CMOV_GR32:
9619 case X86::CMOV_RFP32:
9620 case X86::CMOV_RFP64:
9621 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00009622 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +00009623
Dale Johannesen849f2142007-07-03 00:53:03 +00009624 case X86::FP32_TO_INT16_IN_MEM:
9625 case X86::FP32_TO_INT32_IN_MEM:
9626 case X86::FP32_TO_INT64_IN_MEM:
9627 case X86::FP64_TO_INT16_IN_MEM:
9628 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00009629 case X86::FP64_TO_INT64_IN_MEM:
9630 case X86::FP80_TO_INT16_IN_MEM:
9631 case X86::FP80_TO_INT32_IN_MEM:
9632 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +00009633 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9634 DebugLoc DL = MI->getDebugLoc();
9635
Evan Cheng60c07e12006-07-05 22:17:51 +00009636 // Change the floating point control register to use "round towards zero"
9637 // mode when truncating to an integer value.
9638 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +00009639 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +00009640 addFrameReference(BuildMI(*BB, MI, DL,
9641 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00009642
9643 // Load the old value of the high byte of the control word...
9644 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +00009645 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +00009646 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009647 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00009648
9649 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +00009650 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00009651 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00009652
9653 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +00009654 addFrameReference(BuildMI(*BB, MI, DL,
9655 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00009656
9657 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +00009658 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00009659 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00009660
9661 // Get the X86 opcode to use.
9662 unsigned Opc;
9663 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009664 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00009665 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
9666 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
9667 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
9668 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
9669 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
9670 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00009671 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
9672 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
9673 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00009674 }
9675
9676 X86AddressMode AM;
9677 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +00009678 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00009679 AM.BaseType = X86AddressMode::RegBase;
9680 AM.Base.Reg = Op.getReg();
9681 } else {
9682 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +00009683 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +00009684 }
9685 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +00009686 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00009687 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00009688 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +00009689 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00009690 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00009691 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +00009692 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00009693 AM.GV = Op.getGlobal();
9694 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00009695 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00009696 }
Dan Gohman14152b42010-07-06 20:24:04 +00009697 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009698 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00009699
9700 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +00009701 addFrameReference(BuildMI(*BB, MI, DL,
9702 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00009703
Dan Gohman14152b42010-07-06 20:24:04 +00009704 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00009705 return BB;
9706 }
Eric Christopherb120ab42009-08-18 22:50:32 +00009707 // String/text processing lowering.
9708 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009709 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +00009710 return EmitPCMP(MI, BB, 3, false /* in-mem */);
9711 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009712 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +00009713 return EmitPCMP(MI, BB, 3, true /* in-mem */);
9714 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009715 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +00009716 return EmitPCMP(MI, BB, 5, false /* in mem */);
9717 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009718 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +00009719 return EmitPCMP(MI, BB, 5, true /* in mem */);
9720
9721 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +00009722 case X86::ATOMAND32:
9723 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00009724 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009725 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009726 X86::NOT32r, X86::EAX,
9727 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00009728 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +00009729 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
9730 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009731 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009732 X86::NOT32r, X86::EAX,
9733 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00009734 case X86::ATOMXOR32:
9735 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00009736 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009737 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009738 X86::NOT32r, X86::EAX,
9739 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009740 case X86::ATOMNAND32:
9741 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009742 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009743 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009744 X86::NOT32r, X86::EAX,
9745 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +00009746 case X86::ATOMMIN32:
9747 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
9748 case X86::ATOMMAX32:
9749 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
9750 case X86::ATOMUMIN32:
9751 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
9752 case X86::ATOMUMAX32:
9753 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +00009754
9755 case X86::ATOMAND16:
9756 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
9757 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009758 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009759 X86::NOT16r, X86::AX,
9760 X86::GR16RegisterClass);
9761 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +00009762 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009763 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009764 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009765 X86::NOT16r, X86::AX,
9766 X86::GR16RegisterClass);
9767 case X86::ATOMXOR16:
9768 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
9769 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009770 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009771 X86::NOT16r, X86::AX,
9772 X86::GR16RegisterClass);
9773 case X86::ATOMNAND16:
9774 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
9775 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009776 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009777 X86::NOT16r, X86::AX,
9778 X86::GR16RegisterClass, true);
9779 case X86::ATOMMIN16:
9780 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
9781 case X86::ATOMMAX16:
9782 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
9783 case X86::ATOMUMIN16:
9784 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
9785 case X86::ATOMUMAX16:
9786 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
9787
9788 case X86::ATOMAND8:
9789 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
9790 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009791 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009792 X86::NOT8r, X86::AL,
9793 X86::GR8RegisterClass);
9794 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +00009795 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009796 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009797 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009798 X86::NOT8r, X86::AL,
9799 X86::GR8RegisterClass);
9800 case X86::ATOMXOR8:
9801 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
9802 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009803 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009804 X86::NOT8r, X86::AL,
9805 X86::GR8RegisterClass);
9806 case X86::ATOMNAND8:
9807 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
9808 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009809 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009810 X86::NOT8r, X86::AL,
9811 X86::GR8RegisterClass, true);
9812 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009813 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +00009814 case X86::ATOMAND64:
9815 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00009816 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009817 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +00009818 X86::NOT64r, X86::RAX,
9819 X86::GR64RegisterClass);
9820 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +00009821 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
9822 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009823 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +00009824 X86::NOT64r, X86::RAX,
9825 X86::GR64RegisterClass);
9826 case X86::ATOMXOR64:
9827 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00009828 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009829 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +00009830 X86::NOT64r, X86::RAX,
9831 X86::GR64RegisterClass);
9832 case X86::ATOMNAND64:
9833 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
9834 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009835 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +00009836 X86::NOT64r, X86::RAX,
9837 X86::GR64RegisterClass, true);
9838 case X86::ATOMMIN64:
9839 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
9840 case X86::ATOMMAX64:
9841 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
9842 case X86::ATOMUMIN64:
9843 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
9844 case X86::ATOMUMAX64:
9845 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009846
9847 // This group does 64-bit operations on a 32-bit host.
9848 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00009849 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009850 X86::AND32rr, X86::AND32rr,
9851 X86::AND32ri, X86::AND32ri,
9852 false);
9853 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00009854 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009855 X86::OR32rr, X86::OR32rr,
9856 X86::OR32ri, X86::OR32ri,
9857 false);
9858 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00009859 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009860 X86::XOR32rr, X86::XOR32rr,
9861 X86::XOR32ri, X86::XOR32ri,
9862 false);
9863 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00009864 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009865 X86::AND32rr, X86::AND32rr,
9866 X86::AND32ri, X86::AND32ri,
9867 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009868 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00009869 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009870 X86::ADD32rr, X86::ADC32rr,
9871 X86::ADD32ri, X86::ADC32ri,
9872 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009873 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00009874 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009875 X86::SUB32rr, X86::SBB32rr,
9876 X86::SUB32ri, X86::SBB32ri,
9877 false);
Dale Johannesen880ae362008-10-03 22:25:52 +00009878 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00009879 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +00009880 X86::MOV32rr, X86::MOV32rr,
9881 X86::MOV32ri, X86::MOV32ri,
9882 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +00009883 case X86::VASTART_SAVE_XMM_REGS:
9884 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +00009885 }
9886}
9887
9888//===----------------------------------------------------------------------===//
9889// X86 Optimization Hooks
9890//===----------------------------------------------------------------------===//
9891
Dan Gohman475871a2008-07-27 21:46:04 +00009892void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00009893 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00009894 APInt &KnownZero,
9895 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00009896 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00009897 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009898 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00009899 assert((Opc >= ISD::BUILTIN_OP_END ||
9900 Opc == ISD::INTRINSIC_WO_CHAIN ||
9901 Opc == ISD::INTRINSIC_W_CHAIN ||
9902 Opc == ISD::INTRINSIC_VOID) &&
9903 "Should use MaskedValueIsZero if you don't know whether Op"
9904 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009905
Dan Gohmanf4f92f52008-02-13 23:07:24 +00009906 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009907 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00009908 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +00009909 case X86ISD::ADD:
9910 case X86ISD::SUB:
9911 case X86ISD::SMUL:
9912 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +00009913 case X86ISD::INC:
9914 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +00009915 case X86ISD::OR:
9916 case X86ISD::XOR:
9917 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +00009918 // These nodes' second result is a boolean.
9919 if (Op.getResNo() == 0)
9920 break;
9921 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009922 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00009923 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
9924 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +00009925 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009926 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009927}
Chris Lattner259e97c2006-01-31 19:43:35 +00009928
Owen Andersonbc146b02010-09-21 20:42:50 +00009929unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
9930 unsigned Depth) const {
9931 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
9932 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
9933 return Op.getValueType().getScalarType().getSizeInBits();
9934
9935 // Fallback case.
9936 return 1;
9937}
9938
Evan Cheng206ee9d2006-07-07 08:33:52 +00009939/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +00009940/// node is a GlobalAddress + offset.
9941bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +00009942 const GlobalValue* &GA,
9943 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +00009944 if (N->getOpcode() == X86ISD::Wrapper) {
9945 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00009946 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00009947 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +00009948 return true;
9949 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00009950 }
Evan Chengad4196b2008-05-12 19:56:52 +00009951 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +00009952}
9953
Evan Cheng206ee9d2006-07-07 08:33:52 +00009954/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
9955/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
9956/// if the load addresses are consecutive, non-overlapping, and in the right
Nate Begemanfdea31a2010-03-24 20:49:50 +00009957/// order.
Dan Gohman475871a2008-07-27 21:46:04 +00009958static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00009959 const TargetLowering &TLI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009960 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00009961 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +00009962
Eli Friedman7a5e5552009-06-07 06:52:44 +00009963 if (VT.getSizeInBits() != 128)
9964 return SDValue();
9965
Nate Begemanfdea31a2010-03-24 20:49:50 +00009966 SmallVector<SDValue, 16> Elts;
9967 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00009968 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00009969
Nate Begemanfdea31a2010-03-24 20:49:50 +00009970 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00009971}
Evan Chengd880b972008-05-09 21:53:03 +00009972
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +00009973/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
9974/// generation and convert it from being a bunch of shuffles and extracts
9975/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +00009976static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
9977 const TargetLowering &TLI) {
9978 SDValue InputVector = N->getOperand(0);
9979
9980 // Only operate on vectors of 4 elements, where the alternative shuffling
9981 // gets to be more expensive.
9982 if (InputVector.getValueType() != MVT::v4i32)
9983 return SDValue();
9984
9985 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
9986 // single use which is a sign-extend or zero-extend, and all elements are
9987 // used.
9988 SmallVector<SDNode *, 4> Uses;
9989 unsigned ExtractedElements = 0;
9990 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
9991 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
9992 if (UI.getUse().getResNo() != InputVector.getResNo())
9993 return SDValue();
9994
9995 SDNode *Extract = *UI;
9996 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
9997 return SDValue();
9998
9999 if (Extract->getValueType(0) != MVT::i32)
10000 return SDValue();
10001 if (!Extract->hasOneUse())
10002 return SDValue();
10003 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
10004 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
10005 return SDValue();
10006 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
10007 return SDValue();
10008
10009 // Record which element was extracted.
10010 ExtractedElements |=
10011 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
10012
10013 Uses.push_back(Extract);
10014 }
10015
10016 // If not all the elements were used, this may not be worthwhile.
10017 if (ExtractedElements != 15)
10018 return SDValue();
10019
10020 // Ok, we've now decided to do the transformation.
10021 DebugLoc dl = InputVector.getDebugLoc();
10022
10023 // Store the value to a temporary stack slot.
10024 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010025 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
10026 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010027
10028 // Replace each use (extract) with a load of the appropriate element.
10029 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
10030 UE = Uses.end(); UI != UE; ++UI) {
10031 SDNode *Extract = *UI;
10032
10033 // Compute the element's address.
10034 SDValue Idx = Extract->getOperand(1);
10035 unsigned EltSize =
10036 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
10037 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
10038 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
10039
Eric Christopher90eb4022010-07-22 00:26:08 +000010040 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(),
Chris Lattner51abfe42010-09-21 06:02:19 +000010041 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010042
10043 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000010044 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000010045 ScalarAddr, MachinePointerInfo(),
10046 false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010047
10048 // Replace the exact with the load.
10049 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
10050 }
10051
10052 // The replacement was made in place; don't return anything.
10053 return SDValue();
10054}
10055
Chris Lattner83e6c992006-10-04 06:57:07 +000010056/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000010057static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000010058 const X86Subtarget *Subtarget) {
10059 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000010060 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000010061 // Get the LHS/RHS of the select.
10062 SDValue LHS = N->getOperand(1);
10063 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +000010064
Dan Gohman670e5392009-09-21 18:03:22 +000010065 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000010066 // instructions match the semantics of the common C idiom x<y?x:y but not
10067 // x<=y?x:y, because of how they handle negative zero (which can be
10068 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +000010069 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +000010070 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +000010071 Cond.getOpcode() == ISD::SETCC) {
10072 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010073
Chris Lattner47b4ce82009-03-11 05:48:52 +000010074 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000010075 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000010076 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
10077 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000010078 switch (CC) {
10079 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000010080 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000010081 // Converting this to a min would handle NaNs incorrectly, and swapping
10082 // the operands would cause it to handle comparisons between positive
10083 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010084 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000010085 if (!UnsafeFPMath &&
10086 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
10087 break;
10088 std::swap(LHS, RHS);
10089 }
Dan Gohman670e5392009-09-21 18:03:22 +000010090 Opcode = X86ISD::FMIN;
10091 break;
10092 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000010093 // Converting this to a min would handle comparisons between positive
10094 // and negative zero incorrectly.
10095 if (!UnsafeFPMath &&
10096 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
10097 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010098 Opcode = X86ISD::FMIN;
10099 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000010100 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000010101 // Converting this to a min would handle both negative zeros and NaNs
10102 // incorrectly, but we can swap the operands to fix both.
10103 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010104 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010105 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000010106 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010107 Opcode = X86ISD::FMIN;
10108 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010109
Dan Gohman670e5392009-09-21 18:03:22 +000010110 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010111 // Converting this to a max would handle comparisons between positive
10112 // and negative zero incorrectly.
10113 if (!UnsafeFPMath &&
10114 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
10115 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010116 Opcode = X86ISD::FMAX;
10117 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000010118 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000010119 // Converting this to a max would handle NaNs incorrectly, and swapping
10120 // the operands would cause it to handle comparisons between positive
10121 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010122 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000010123 if (!UnsafeFPMath &&
10124 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
10125 break;
10126 std::swap(LHS, RHS);
10127 }
Dan Gohman670e5392009-09-21 18:03:22 +000010128 Opcode = X86ISD::FMAX;
10129 break;
10130 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010131 // Converting this to a max would handle both negative zeros and NaNs
10132 // incorrectly, but we can swap the operands to fix both.
10133 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010134 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010135 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010136 case ISD::SETGE:
10137 Opcode = X86ISD::FMAX;
10138 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000010139 }
Dan Gohman670e5392009-09-21 18:03:22 +000010140 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000010141 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
10142 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000010143 switch (CC) {
10144 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000010145 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010146 // Converting this to a min would handle comparisons between positive
10147 // and negative zero incorrectly, and swapping the operands would
10148 // cause it to handle NaNs incorrectly.
10149 if (!UnsafeFPMath &&
10150 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000010151 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000010152 break;
10153 std::swap(LHS, RHS);
10154 }
Dan Gohman670e5392009-09-21 18:03:22 +000010155 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000010156 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010157 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000010158 // Converting this to a min would handle NaNs incorrectly.
10159 if (!UnsafeFPMath &&
10160 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
10161 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010162 Opcode = X86ISD::FMIN;
10163 break;
10164 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010165 // Converting this to a min would handle both negative zeros and NaNs
10166 // incorrectly, but we can swap the operands to fix both.
10167 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010168 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010169 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010170 case ISD::SETGE:
10171 Opcode = X86ISD::FMIN;
10172 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010173
Dan Gohman670e5392009-09-21 18:03:22 +000010174 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000010175 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010176 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000010177 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010178 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000010179 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010180 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000010181 // Converting this to a max would handle comparisons between positive
10182 // and negative zero incorrectly, and swapping the operands would
10183 // cause it to handle NaNs incorrectly.
10184 if (!UnsafeFPMath &&
10185 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000010186 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000010187 break;
10188 std::swap(LHS, RHS);
10189 }
Dan Gohman670e5392009-09-21 18:03:22 +000010190 Opcode = X86ISD::FMAX;
10191 break;
10192 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000010193 // Converting this to a max would handle both negative zeros and NaNs
10194 // incorrectly, but we can swap the operands to fix both.
10195 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010196 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010197 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000010198 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010199 Opcode = X86ISD::FMAX;
10200 break;
10201 }
Chris Lattner83e6c992006-10-04 06:57:07 +000010202 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010203
Chris Lattner47b4ce82009-03-11 05:48:52 +000010204 if (Opcode)
10205 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000010206 }
Eric Christopherfd179292009-08-27 18:07:15 +000010207
Chris Lattnerd1980a52009-03-12 06:52:53 +000010208 // If this is a select between two integer constants, try to do some
10209 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000010210 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
10211 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000010212 // Don't do this for crazy integer types.
10213 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
10214 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000010215 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000010216 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000010217
Chris Lattnercee56e72009-03-13 05:53:31 +000010218 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000010219 // Efficiently invertible.
10220 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
10221 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
10222 isa<ConstantSDNode>(Cond.getOperand(1))))) {
10223 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000010224 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000010225 }
Eric Christopherfd179292009-08-27 18:07:15 +000010226
Chris Lattnerd1980a52009-03-12 06:52:53 +000010227 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000010228 if (FalseC->getAPIntValue() == 0 &&
10229 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000010230 if (NeedsCondInvert) // Invert the condition if needed.
10231 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
10232 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010233
Chris Lattnerd1980a52009-03-12 06:52:53 +000010234 // Zero extend the condition if needed.
10235 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010236
Chris Lattnercee56e72009-03-13 05:53:31 +000010237 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000010238 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000010239 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000010240 }
Eric Christopherfd179292009-08-27 18:07:15 +000010241
Chris Lattner97a29a52009-03-13 05:22:11 +000010242 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000010243 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000010244 if (NeedsCondInvert) // Invert the condition if needed.
10245 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
10246 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010247
Chris Lattner97a29a52009-03-13 05:22:11 +000010248 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000010249 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
10250 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000010251 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000010252 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000010253 }
Eric Christopherfd179292009-08-27 18:07:15 +000010254
Chris Lattnercee56e72009-03-13 05:53:31 +000010255 // Optimize cases that will turn into an LEA instruction. This requires
10256 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000010257 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000010258 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000010259 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000010260
Chris Lattnercee56e72009-03-13 05:53:31 +000010261 bool isFastMultiplier = false;
10262 if (Diff < 10) {
10263 switch ((unsigned char)Diff) {
10264 default: break;
10265 case 1: // result = add base, cond
10266 case 2: // result = lea base( , cond*2)
10267 case 3: // result = lea base(cond, cond*2)
10268 case 4: // result = lea base( , cond*4)
10269 case 5: // result = lea base(cond, cond*4)
10270 case 8: // result = lea base( , cond*8)
10271 case 9: // result = lea base(cond, cond*8)
10272 isFastMultiplier = true;
10273 break;
10274 }
10275 }
Eric Christopherfd179292009-08-27 18:07:15 +000010276
Chris Lattnercee56e72009-03-13 05:53:31 +000010277 if (isFastMultiplier) {
10278 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
10279 if (NeedsCondInvert) // Invert the condition if needed.
10280 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
10281 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010282
Chris Lattnercee56e72009-03-13 05:53:31 +000010283 // Zero extend the condition if needed.
10284 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
10285 Cond);
10286 // Scale the condition by the difference.
10287 if (Diff != 1)
10288 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
10289 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010290
Chris Lattnercee56e72009-03-13 05:53:31 +000010291 // Add the base if non-zero.
10292 if (FalseC->getAPIntValue() != 0)
10293 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
10294 SDValue(FalseC, 0));
10295 return Cond;
10296 }
Eric Christopherfd179292009-08-27 18:07:15 +000010297 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000010298 }
10299 }
Eric Christopherfd179292009-08-27 18:07:15 +000010300
Dan Gohman475871a2008-07-27 21:46:04 +000010301 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000010302}
10303
Chris Lattnerd1980a52009-03-12 06:52:53 +000010304/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
10305static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
10306 TargetLowering::DAGCombinerInfo &DCI) {
10307 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000010308
Chris Lattnerd1980a52009-03-12 06:52:53 +000010309 // If the flag operand isn't dead, don't touch this CMOV.
10310 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
10311 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000010312
Chris Lattnerd1980a52009-03-12 06:52:53 +000010313 // If this is a select between two integer constants, try to do some
10314 // optimizations. Note that the operands are ordered the opposite of SELECT
10315 // operands.
10316 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
10317 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
10318 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
10319 // larger than FalseC (the false value).
10320 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
Eric Christopherfd179292009-08-27 18:07:15 +000010321
Chris Lattnerd1980a52009-03-12 06:52:53 +000010322 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
10323 CC = X86::GetOppositeBranchCondition(CC);
10324 std::swap(TrueC, FalseC);
10325 }
Eric Christopherfd179292009-08-27 18:07:15 +000010326
Chris Lattnerd1980a52009-03-12 06:52:53 +000010327 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000010328 // This is efficient for any integer data type (including i8/i16) and
10329 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000010330 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
10331 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000010332 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10333 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010334
Chris Lattnerd1980a52009-03-12 06:52:53 +000010335 // Zero extend the condition if needed.
10336 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010337
Chris Lattnerd1980a52009-03-12 06:52:53 +000010338 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
10339 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000010340 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000010341 if (N->getNumValues() == 2) // Dead flag value?
10342 return DCI.CombineTo(N, Cond, SDValue());
10343 return Cond;
10344 }
Eric Christopherfd179292009-08-27 18:07:15 +000010345
Chris Lattnercee56e72009-03-13 05:53:31 +000010346 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
10347 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000010348 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
10349 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000010350 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10351 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010352
Chris Lattner97a29a52009-03-13 05:22:11 +000010353 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000010354 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
10355 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000010356 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
10357 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000010358
Chris Lattner97a29a52009-03-13 05:22:11 +000010359 if (N->getNumValues() == 2) // Dead flag value?
10360 return DCI.CombineTo(N, Cond, SDValue());
10361 return Cond;
10362 }
Eric Christopherfd179292009-08-27 18:07:15 +000010363
Chris Lattnercee56e72009-03-13 05:53:31 +000010364 // Optimize cases that will turn into an LEA instruction. This requires
10365 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000010366 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000010367 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000010368 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000010369
Chris Lattnercee56e72009-03-13 05:53:31 +000010370 bool isFastMultiplier = false;
10371 if (Diff < 10) {
10372 switch ((unsigned char)Diff) {
10373 default: break;
10374 case 1: // result = add base, cond
10375 case 2: // result = lea base( , cond*2)
10376 case 3: // result = lea base(cond, cond*2)
10377 case 4: // result = lea base( , cond*4)
10378 case 5: // result = lea base(cond, cond*4)
10379 case 8: // result = lea base( , cond*8)
10380 case 9: // result = lea base(cond, cond*8)
10381 isFastMultiplier = true;
10382 break;
10383 }
10384 }
Eric Christopherfd179292009-08-27 18:07:15 +000010385
Chris Lattnercee56e72009-03-13 05:53:31 +000010386 if (isFastMultiplier) {
10387 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
10388 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000010389 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10390 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000010391 // Zero extend the condition if needed.
10392 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
10393 Cond);
10394 // Scale the condition by the difference.
10395 if (Diff != 1)
10396 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
10397 DAG.getConstant(Diff, Cond.getValueType()));
10398
10399 // Add the base if non-zero.
10400 if (FalseC->getAPIntValue() != 0)
10401 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
10402 SDValue(FalseC, 0));
10403 if (N->getNumValues() == 2) // Dead flag value?
10404 return DCI.CombineTo(N, Cond, SDValue());
10405 return Cond;
10406 }
Eric Christopherfd179292009-08-27 18:07:15 +000010407 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000010408 }
10409 }
10410 return SDValue();
10411}
10412
10413
Evan Cheng0b0cd912009-03-28 05:57:29 +000010414/// PerformMulCombine - Optimize a single multiply with constant into two
10415/// in order to implement it with two cheaper instructions, e.g.
10416/// LEA + SHL, LEA + LEA.
10417static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
10418 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000010419 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
10420 return SDValue();
10421
Owen Andersone50ed302009-08-10 22:56:29 +000010422 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010423 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000010424 return SDValue();
10425
10426 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
10427 if (!C)
10428 return SDValue();
10429 uint64_t MulAmt = C->getZExtValue();
10430 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
10431 return SDValue();
10432
10433 uint64_t MulAmt1 = 0;
10434 uint64_t MulAmt2 = 0;
10435 if ((MulAmt % 9) == 0) {
10436 MulAmt1 = 9;
10437 MulAmt2 = MulAmt / 9;
10438 } else if ((MulAmt % 5) == 0) {
10439 MulAmt1 = 5;
10440 MulAmt2 = MulAmt / 5;
10441 } else if ((MulAmt % 3) == 0) {
10442 MulAmt1 = 3;
10443 MulAmt2 = MulAmt / 3;
10444 }
10445 if (MulAmt2 &&
10446 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
10447 DebugLoc DL = N->getDebugLoc();
10448
10449 if (isPowerOf2_64(MulAmt2) &&
10450 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
10451 // If second multiplifer is pow2, issue it first. We want the multiply by
10452 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
10453 // is an add.
10454 std::swap(MulAmt1, MulAmt2);
10455
10456 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000010457 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000010458 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000010459 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000010460 else
Evan Cheng73f24c92009-03-30 21:36:47 +000010461 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000010462 DAG.getConstant(MulAmt1, VT));
10463
Eric Christopherfd179292009-08-27 18:07:15 +000010464 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000010465 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000010466 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000010467 else
Evan Cheng73f24c92009-03-30 21:36:47 +000010468 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000010469 DAG.getConstant(MulAmt2, VT));
10470
10471 // Do not add new nodes to DAG combiner worklist.
10472 DCI.CombineTo(N, NewMul, false);
10473 }
10474 return SDValue();
10475}
10476
Evan Chengad9c0a32009-12-15 00:53:42 +000010477static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
10478 SDValue N0 = N->getOperand(0);
10479 SDValue N1 = N->getOperand(1);
10480 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
10481 EVT VT = N0.getValueType();
10482
10483 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
10484 // since the result of setcc_c is all zero's or all ones.
10485 if (N1C && N0.getOpcode() == ISD::AND &&
10486 N0.getOperand(1).getOpcode() == ISD::Constant) {
10487 SDValue N00 = N0.getOperand(0);
10488 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
10489 ((N00.getOpcode() == ISD::ANY_EXTEND ||
10490 N00.getOpcode() == ISD::ZERO_EXTEND) &&
10491 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
10492 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
10493 APInt ShAmt = N1C->getAPIntValue();
10494 Mask = Mask.shl(ShAmt);
10495 if (Mask != 0)
10496 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
10497 N00, DAG.getConstant(Mask, VT));
10498 }
10499 }
10500
10501 return SDValue();
10502}
Evan Cheng0b0cd912009-03-28 05:57:29 +000010503
Nate Begeman740ab032009-01-26 00:52:55 +000010504/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
10505/// when possible.
10506static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
10507 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000010508 EVT VT = N->getValueType(0);
10509 if (!VT.isVector() && VT.isInteger() &&
10510 N->getOpcode() == ISD::SHL)
10511 return PerformSHLCombine(N, DAG);
10512
Nate Begeman740ab032009-01-26 00:52:55 +000010513 // On X86 with SSE2 support, we can transform this to a vector shift if
10514 // all elements are shifted by the same amount. We can't do this in legalize
10515 // because the a constant vector is typically transformed to a constant pool
10516 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +000010517 if (!Subtarget->hasSSE2())
10518 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000010519
Owen Anderson825b72b2009-08-11 20:47:22 +000010520 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +000010521 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000010522
Mon P Wang3becd092009-01-28 08:12:05 +000010523 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000010524 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000010525 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000010526 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000010527 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
10528 unsigned NumElts = VT.getVectorNumElements();
10529 unsigned i = 0;
10530 for (; i != NumElts; ++i) {
10531 SDValue Arg = ShAmtOp.getOperand(i);
10532 if (Arg.getOpcode() == ISD::UNDEF) continue;
10533 BaseShAmt = Arg;
10534 break;
10535 }
10536 for (; i != NumElts; ++i) {
10537 SDValue Arg = ShAmtOp.getOperand(i);
10538 if (Arg.getOpcode() == ISD::UNDEF) continue;
10539 if (Arg != BaseShAmt) {
10540 return SDValue();
10541 }
10542 }
10543 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000010544 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000010545 SDValue InVec = ShAmtOp.getOperand(0);
10546 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
10547 unsigned NumElts = InVec.getValueType().getVectorNumElements();
10548 unsigned i = 0;
10549 for (; i != NumElts; ++i) {
10550 SDValue Arg = InVec.getOperand(i);
10551 if (Arg.getOpcode() == ISD::UNDEF) continue;
10552 BaseShAmt = Arg;
10553 break;
10554 }
10555 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
10556 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000010557 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000010558 if (C->getZExtValue() == SplatIdx)
10559 BaseShAmt = InVec.getOperand(1);
10560 }
10561 }
10562 if (BaseShAmt.getNode() == 0)
10563 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
10564 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000010565 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000010566 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000010567
Mon P Wangefa42202009-09-03 19:56:25 +000010568 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000010569 if (EltVT.bitsGT(MVT::i32))
10570 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
10571 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000010572 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000010573
Nate Begemanc2fd67f2009-01-26 03:15:31 +000010574 // The shift amount is identical so we can do a vector shift.
10575 SDValue ValOp = N->getOperand(0);
10576 switch (N->getOpcode()) {
10577 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000010578 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000010579 break;
10580 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000010581 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000010582 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010583 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000010584 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000010585 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000010586 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010587 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000010588 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000010589 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000010590 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010591 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000010592 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000010593 break;
10594 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000010595 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000010596 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010597 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000010598 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000010599 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000010600 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010601 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000010602 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000010603 break;
10604 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000010605 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000010606 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010607 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000010608 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000010609 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000010610 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010611 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000010612 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000010613 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000010614 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010615 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000010616 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000010617 break;
Nate Begeman740ab032009-01-26 00:52:55 +000010618 }
10619 return SDValue();
10620}
10621
Evan Cheng760d1942010-01-04 21:22:48 +000010622static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000010623 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000010624 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000010625 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000010626 return SDValue();
10627
Evan Cheng760d1942010-01-04 21:22:48 +000010628 EVT VT = N->getValueType(0);
Evan Cheng8b1190a2010-04-28 01:18:01 +000010629 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
Evan Cheng760d1942010-01-04 21:22:48 +000010630 return SDValue();
10631
10632 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
10633 SDValue N0 = N->getOperand(0);
10634 SDValue N1 = N->getOperand(1);
10635 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
10636 std::swap(N0, N1);
10637 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
10638 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000010639 if (!N0.hasOneUse() || !N1.hasOneUse())
10640 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000010641
10642 SDValue ShAmt0 = N0.getOperand(1);
10643 if (ShAmt0.getValueType() != MVT::i8)
10644 return SDValue();
10645 SDValue ShAmt1 = N1.getOperand(1);
10646 if (ShAmt1.getValueType() != MVT::i8)
10647 return SDValue();
10648 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
10649 ShAmt0 = ShAmt0.getOperand(0);
10650 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
10651 ShAmt1 = ShAmt1.getOperand(0);
10652
10653 DebugLoc DL = N->getDebugLoc();
10654 unsigned Opc = X86ISD::SHLD;
10655 SDValue Op0 = N0.getOperand(0);
10656 SDValue Op1 = N1.getOperand(0);
10657 if (ShAmt0.getOpcode() == ISD::SUB) {
10658 Opc = X86ISD::SHRD;
10659 std::swap(Op0, Op1);
10660 std::swap(ShAmt0, ShAmt1);
10661 }
10662
Evan Cheng8b1190a2010-04-28 01:18:01 +000010663 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000010664 if (ShAmt1.getOpcode() == ISD::SUB) {
10665 SDValue Sum = ShAmt1.getOperand(0);
10666 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000010667 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
10668 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
10669 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
10670 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000010671 return DAG.getNode(Opc, DL, VT,
10672 Op0, Op1,
10673 DAG.getNode(ISD::TRUNCATE, DL,
10674 MVT::i8, ShAmt0));
10675 }
10676 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
10677 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
10678 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000010679 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000010680 return DAG.getNode(Opc, DL, VT,
10681 N0.getOperand(0), N1.getOperand(0),
10682 DAG.getNode(ISD::TRUNCATE, DL,
10683 MVT::i8, ShAmt0));
10684 }
10685
10686 return SDValue();
10687}
10688
Chris Lattner149a4e52008-02-22 02:09:43 +000010689/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000010690static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000010691 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +000010692 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
10693 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000010694 // A preferable solution to the general problem is to figure out the right
10695 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000010696
10697 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +000010698 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +000010699 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +000010700 if (VT.getSizeInBits() != 64)
10701 return SDValue();
10702
Devang Patel578efa92009-06-05 21:57:13 +000010703 const Function *F = DAG.getMachineFunction().getFunction();
10704 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000010705 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +000010706 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000010707 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000010708 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000010709 isa<LoadSDNode>(St->getValue()) &&
10710 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
10711 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000010712 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000010713 LoadSDNode *Ld = 0;
10714 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000010715 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000010716 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000010717 // Must be a store of a load. We currently handle two cases: the load
10718 // is a direct child, and it's under an intervening TokenFactor. It is
10719 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000010720 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000010721 Ld = cast<LoadSDNode>(St->getChain());
10722 else if (St->getValue().hasOneUse() &&
10723 ChainVal->getOpcode() == ISD::TokenFactor) {
10724 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000010725 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000010726 TokenFactorIndex = i;
10727 Ld = cast<LoadSDNode>(St->getValue());
10728 } else
10729 Ops.push_back(ChainVal->getOperand(i));
10730 }
10731 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000010732
Evan Cheng536e6672009-03-12 05:59:15 +000010733 if (!Ld || !ISD::isNormalLoad(Ld))
10734 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000010735
Evan Cheng536e6672009-03-12 05:59:15 +000010736 // If this is not the MMX case, i.e. we are just turning i64 load/store
10737 // into f64 load/store, avoid the transformation if there are multiple
10738 // uses of the loaded value.
10739 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
10740 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000010741
Evan Cheng536e6672009-03-12 05:59:15 +000010742 DebugLoc LdDL = Ld->getDebugLoc();
10743 DebugLoc StDL = N->getDebugLoc();
10744 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
10745 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
10746 // pair instead.
10747 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000010748 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000010749 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
10750 Ld->getPointerInfo(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000010751 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000010752 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000010753 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000010754 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000010755 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000010756 Ops.size());
10757 }
Evan Cheng536e6672009-03-12 05:59:15 +000010758 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000010759 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000010760 St->isVolatile(), St->isNonTemporal(),
10761 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000010762 }
Evan Cheng536e6672009-03-12 05:59:15 +000010763
10764 // Otherwise, lower to two pairs of 32-bit loads / stores.
10765 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000010766 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
10767 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000010768
Owen Anderson825b72b2009-08-11 20:47:22 +000010769 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000010770 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000010771 Ld->isVolatile(), Ld->isNonTemporal(),
10772 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000010773 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000010774 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000010775 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000010776 MinAlign(Ld->getAlignment(), 4));
10777
10778 SDValue NewChain = LoLd.getValue(1);
10779 if (TokenFactorIndex != -1) {
10780 Ops.push_back(LoLd);
10781 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000010782 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000010783 Ops.size());
10784 }
10785
10786 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000010787 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
10788 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000010789
10790 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000010791 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000010792 St->isVolatile(), St->isNonTemporal(),
10793 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000010794 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000010795 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000010796 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000010797 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000010798 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000010799 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000010800 }
Dan Gohman475871a2008-07-27 21:46:04 +000010801 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000010802}
10803
Chris Lattner6cf73262008-01-25 06:14:17 +000010804/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
10805/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000010806static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000010807 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
10808 // F[X]OR(0.0, x) -> x
10809 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000010810 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
10811 if (C->getValueAPF().isPosZero())
10812 return N->getOperand(1);
10813 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
10814 if (C->getValueAPF().isPosZero())
10815 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000010816 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000010817}
10818
10819/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000010820static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000010821 // FAND(0.0, x) -> 0.0
10822 // FAND(x, 0.0) -> 0.0
10823 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
10824 if (C->getValueAPF().isPosZero())
10825 return N->getOperand(0);
10826 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
10827 if (C->getValueAPF().isPosZero())
10828 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000010829 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000010830}
10831
Dan Gohmane5af2d32009-01-29 01:59:02 +000010832static SDValue PerformBTCombine(SDNode *N,
10833 SelectionDAG &DAG,
10834 TargetLowering::DAGCombinerInfo &DCI) {
10835 // BT ignores high bits in the bit index operand.
10836 SDValue Op1 = N->getOperand(1);
10837 if (Op1.hasOneUse()) {
10838 unsigned BitWidth = Op1.getValueSizeInBits();
10839 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
10840 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000010841 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
10842 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000010843 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000010844 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
10845 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
10846 DCI.CommitTargetLoweringOpt(TLO);
10847 }
10848 return SDValue();
10849}
Chris Lattner83e6c992006-10-04 06:57:07 +000010850
Eli Friedman7a5e5552009-06-07 06:52:44 +000010851static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
10852 SDValue Op = N->getOperand(0);
10853 if (Op.getOpcode() == ISD::BIT_CONVERT)
10854 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000010855 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000010856 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000010857 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000010858 OpVT.getVectorElementType().getSizeInBits()) {
10859 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT, Op);
10860 }
10861 return SDValue();
10862}
10863
Evan Cheng2e489c42009-12-16 00:53:11 +000010864static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
10865 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
10866 // (and (i32 x86isd::setcc_carry), 1)
10867 // This eliminates the zext. This transformation is necessary because
10868 // ISD::SETCC is always legalized to i8.
10869 DebugLoc dl = N->getDebugLoc();
10870 SDValue N0 = N->getOperand(0);
10871 EVT VT = N->getValueType(0);
10872 if (N0.getOpcode() == ISD::AND &&
10873 N0.hasOneUse() &&
10874 N0.getOperand(0).hasOneUse()) {
10875 SDValue N00 = N0.getOperand(0);
10876 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
10877 return SDValue();
10878 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
10879 if (!C || C->getZExtValue() != 1)
10880 return SDValue();
10881 return DAG.getNode(ISD::AND, dl, VT,
10882 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
10883 N00.getOperand(0), N00.getOperand(1)),
10884 DAG.getConstant(1, VT));
10885 }
10886
10887 return SDValue();
10888}
10889
Dan Gohman475871a2008-07-27 21:46:04 +000010890SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000010891 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000010892 SelectionDAG &DAG = DCI.DAG;
10893 switch (N->getOpcode()) {
10894 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010895 case ISD::EXTRACT_VECTOR_ELT:
10896 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +000010897 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000010898 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000010899 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000010900 case ISD::SHL:
10901 case ISD::SRA:
10902 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000010903 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000010904 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000010905 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000010906 case X86ISD::FOR: return PerformFORCombine(N, DAG);
10907 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000010908 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000010909 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000010910 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000010911 case X86ISD::SHUFPS: // Handle all target specific shuffles
10912 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000010913 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000010914 case X86ISD::PUNPCKHBW:
10915 case X86ISD::PUNPCKHWD:
10916 case X86ISD::PUNPCKHDQ:
10917 case X86ISD::PUNPCKHQDQ:
10918 case X86ISD::UNPCKHPS:
10919 case X86ISD::UNPCKHPD:
10920 case X86ISD::PUNPCKLBW:
10921 case X86ISD::PUNPCKLWD:
10922 case X86ISD::PUNPCKLDQ:
10923 case X86ISD::PUNPCKLQDQ:
10924 case X86ISD::UNPCKLPS:
10925 case X86ISD::UNPCKLPD:
10926 case X86ISD::MOVHLPS:
10927 case X86ISD::MOVLHPS:
10928 case X86ISD::PSHUFD:
10929 case X86ISD::PSHUFHW:
10930 case X86ISD::PSHUFLW:
10931 case X86ISD::MOVSS:
10932 case X86ISD::MOVSD:
10933 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
Evan Cheng206ee9d2006-07-07 08:33:52 +000010934 }
10935
Dan Gohman475871a2008-07-27 21:46:04 +000010936 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000010937}
10938
Evan Chenge5b51ac2010-04-17 06:13:15 +000010939/// isTypeDesirableForOp - Return true if the target has native support for
10940/// the specified value type and it is 'desirable' to use the type for the
10941/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
10942/// instruction encodings are longer and some i16 instructions are slow.
10943bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
10944 if (!isTypeLegal(VT))
10945 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000010946 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000010947 return true;
10948
10949 switch (Opc) {
10950 default:
10951 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000010952 case ISD::LOAD:
10953 case ISD::SIGN_EXTEND:
10954 case ISD::ZERO_EXTEND:
10955 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000010956 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000010957 case ISD::SRL:
10958 case ISD::SUB:
10959 case ISD::ADD:
10960 case ISD::MUL:
10961 case ISD::AND:
10962 case ISD::OR:
10963 case ISD::XOR:
10964 return false;
10965 }
10966}
10967
10968/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000010969/// beneficial for dag combiner to promote the specified node. If true, it
10970/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000010971bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000010972 EVT VT = Op.getValueType();
10973 if (VT != MVT::i16)
10974 return false;
10975
Evan Cheng4c26e932010-04-19 19:29:22 +000010976 bool Promote = false;
10977 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000010978 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000010979 default: break;
10980 case ISD::LOAD: {
10981 LoadSDNode *LD = cast<LoadSDNode>(Op);
10982 // If the non-extending load has a single use and it's not live out, then it
10983 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000010984 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
10985 Op.hasOneUse()*/) {
10986 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
10987 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
10988 // The only case where we'd want to promote LOAD (rather then it being
10989 // promoted as an operand is when it's only use is liveout.
10990 if (UI->getOpcode() != ISD::CopyToReg)
10991 return false;
10992 }
10993 }
Evan Cheng4c26e932010-04-19 19:29:22 +000010994 Promote = true;
10995 break;
10996 }
10997 case ISD::SIGN_EXTEND:
10998 case ISD::ZERO_EXTEND:
10999 case ISD::ANY_EXTEND:
11000 Promote = true;
11001 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000011002 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000011003 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000011004 SDValue N0 = Op.getOperand(0);
11005 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000011006 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000011007 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000011008 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000011009 break;
11010 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000011011 case ISD::ADD:
11012 case ISD::MUL:
11013 case ISD::AND:
11014 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000011015 case ISD::XOR:
11016 Commute = true;
11017 // fallthrough
11018 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000011019 SDValue N0 = Op.getOperand(0);
11020 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000011021 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000011022 return false;
11023 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000011024 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000011025 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000011026 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000011027 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000011028 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000011029 }
11030 }
11031
11032 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000011033 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000011034}
11035
Evan Cheng60c07e12006-07-05 22:17:51 +000011036//===----------------------------------------------------------------------===//
11037// X86 Inline Assembly Support
11038//===----------------------------------------------------------------------===//
11039
Chris Lattnerb8105652009-07-20 17:51:36 +000011040static bool LowerToBSwap(CallInst *CI) {
11041 // FIXME: this should verify that we are targetting a 486 or better. If not,
11042 // we will turn this bswap into something that will be lowered to logical ops
11043 // instead of emitting the bswap asm. For now, we don't support 486 or lower
11044 // so don't worry about this.
Eric Christopherfd179292009-08-27 18:07:15 +000011045
Chris Lattnerb8105652009-07-20 17:51:36 +000011046 // Verify this is a simple bswap.
Gabor Greife1c2b9c2010-06-30 13:03:37 +000011047 if (CI->getNumArgOperands() != 1 ||
Gabor Greif1cfe44a2010-06-26 11:51:52 +000011048 CI->getType() != CI->getArgOperand(0)->getType() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011049 !CI->getType()->isIntegerTy())
Chris Lattnerb8105652009-07-20 17:51:36 +000011050 return false;
Eric Christopherfd179292009-08-27 18:07:15 +000011051
Chris Lattnerb8105652009-07-20 17:51:36 +000011052 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
11053 if (!Ty || Ty->getBitWidth() % 16 != 0)
11054 return false;
Eric Christopherfd179292009-08-27 18:07:15 +000011055
Chris Lattnerb8105652009-07-20 17:51:36 +000011056 // Okay, we can do this xform, do so now.
11057 const Type *Tys[] = { Ty };
11058 Module *M = CI->getParent()->getParent()->getParent();
11059 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
Eric Christopherfd179292009-08-27 18:07:15 +000011060
Gabor Greif1cfe44a2010-06-26 11:51:52 +000011061 Value *Op = CI->getArgOperand(0);
Chris Lattnerb8105652009-07-20 17:51:36 +000011062 Op = CallInst::Create(Int, Op, CI->getName(), CI);
Eric Christopherfd179292009-08-27 18:07:15 +000011063
Chris Lattnerb8105652009-07-20 17:51:36 +000011064 CI->replaceAllUsesWith(Op);
11065 CI->eraseFromParent();
11066 return true;
11067}
11068
11069bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
11070 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
11071 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
11072
11073 std::string AsmStr = IA->getAsmString();
11074
11075 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000011076 SmallVector<StringRef, 4> AsmPieces;
Chris Lattnerb8105652009-07-20 17:51:36 +000011077 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
11078
11079 switch (AsmPieces.size()) {
11080 default: return false;
11081 case 1:
11082 AsmStr = AsmPieces[0];
11083 AsmPieces.clear();
11084 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
11085
11086 // bswap $0
11087 if (AsmPieces.size() == 2 &&
11088 (AsmPieces[0] == "bswap" ||
11089 AsmPieces[0] == "bswapq" ||
11090 AsmPieces[0] == "bswapl") &&
11091 (AsmPieces[1] == "$0" ||
11092 AsmPieces[1] == "${0:q}")) {
11093 // No need to check constraints, nothing other than the equivalent of
11094 // "=r,0" would be valid here.
11095 return LowerToBSwap(CI);
11096 }
11097 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011098 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000011099 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000011100 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000011101 AsmPieces[1] == "$$8," &&
11102 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000011103 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
11104 AsmPieces.clear();
Benjamin Kramer018cbd52010-03-12 13:54:59 +000011105 const std::string &Constraints = IA->getConstraintString();
11106 SplitString(StringRef(Constraints).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000011107 std::sort(AsmPieces.begin(), AsmPieces.end());
11108 if (AsmPieces.size() == 4 &&
11109 AsmPieces[0] == "~{cc}" &&
11110 AsmPieces[1] == "~{dirflag}" &&
11111 AsmPieces[2] == "~{flags}" &&
11112 AsmPieces[3] == "~{fpsr}") {
11113 return LowerToBSwap(CI);
11114 }
Chris Lattnerb8105652009-07-20 17:51:36 +000011115 }
11116 break;
11117 case 3:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011118 if (CI->getType()->isIntegerTy(64) &&
Owen Anderson1d0be152009-08-13 21:58:54 +000011119 Constraints.size() >= 2 &&
Chris Lattnerb8105652009-07-20 17:51:36 +000011120 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
11121 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
11122 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramerd4f19592010-01-11 18:03:24 +000011123 SmallVector<StringRef, 4> Words;
Chris Lattnerb8105652009-07-20 17:51:36 +000011124 SplitString(AsmPieces[0], Words, " \t");
11125 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
11126 Words.clear();
11127 SplitString(AsmPieces[1], Words, " \t");
11128 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
11129 Words.clear();
11130 SplitString(AsmPieces[2], Words, " \t,");
11131 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
11132 Words[2] == "%edx") {
11133 return LowerToBSwap(CI);
11134 }
11135 }
11136 }
11137 }
11138 break;
11139 }
11140 return false;
11141}
11142
11143
11144
Chris Lattnerf4dff842006-07-11 02:54:03 +000011145/// getConstraintType - Given a constraint letter, return the type of
11146/// constraint it is for this target.
11147X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000011148X86TargetLowering::getConstraintType(const std::string &Constraint) const {
11149 if (Constraint.size() == 1) {
11150 switch (Constraint[0]) {
11151 case 'A':
Dale Johannesen330169f2008-11-13 21:52:36 +000011152 return C_Register;
Chris Lattnerfce84ac2008-03-11 19:06:29 +000011153 case 'f':
Chris Lattner4234f572007-03-25 02:14:49 +000011154 case 'r':
11155 case 'R':
11156 case 'l':
11157 case 'q':
11158 case 'Q':
11159 case 'x':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000011160 case 'y':
Chris Lattner4234f572007-03-25 02:14:49 +000011161 case 'Y':
11162 return C_RegisterClass;
Dale Johannesen78e3e522009-02-12 20:58:09 +000011163 case 'e':
11164 case 'Z':
11165 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000011166 default:
11167 break;
11168 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000011169 }
Chris Lattner4234f572007-03-25 02:14:49 +000011170 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000011171}
11172
John Thompsoneac6e1d2010-09-13 18:15:37 +000011173/// Examine constraint type and operand type and determine a weight value,
11174/// where: -1 = invalid match, and 0 = so-so match to 3 = good match.
11175/// This object must already have been set up with the operand type
11176/// and the current alternative constraint selected.
11177int X86TargetLowering::getSingleConstraintMatchWeight(
11178 AsmOperandInfo &info, const char *constraint) const {
11179 int weight = -1;
11180 Value *CallOperandVal = info.CallOperandVal;
11181 // If we don't have a value, we can't do a match,
11182 // but allow it at the lowest weight.
11183 if (CallOperandVal == NULL)
11184 return 0;
11185 // Look at the constraint type.
11186 switch (*constraint) {
11187 default:
11188 return TargetLowering::getSingleConstraintMatchWeight(info, constraint);
11189 break;
11190 case 'I':
11191 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
11192 if (C->getZExtValue() <= 31)
11193 weight = 3;
11194 }
11195 break;
11196 // etc.
11197 }
11198 return weight;
11199}
11200
Dale Johannesenba2a0b92008-01-29 02:21:21 +000011201/// LowerXConstraint - try to replace an X constraint, which matches anything,
11202/// with another that has more specific requirements based on the type of the
11203/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000011204const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000011205LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000011206 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
11207 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000011208 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesenba2a0b92008-01-29 02:21:21 +000011209 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +000011210 return "Y";
11211 if (Subtarget->hasSSE1())
11212 return "x";
11213 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011214
Chris Lattner5e764232008-04-26 23:02:14 +000011215 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000011216}
11217
Chris Lattner48884cd2007-08-25 00:47:38 +000011218/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
11219/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000011220void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +000011221 char Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000011222 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000011223 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000011224 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000011225
Chris Lattner22aaf1d2006-10-31 20:13:11 +000011226 switch (Constraint) {
11227 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000011228 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000011229 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000011230 if (C->getZExtValue() <= 31) {
11231 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000011232 break;
11233 }
Devang Patel84f7fd22007-03-17 00:13:28 +000011234 }
Chris Lattner48884cd2007-08-25 00:47:38 +000011235 return;
Evan Cheng364091e2008-09-22 23:57:37 +000011236 case 'J':
11237 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000011238 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000011239 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
11240 break;
11241 }
11242 }
11243 return;
11244 case 'K':
11245 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000011246 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000011247 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
11248 break;
11249 }
11250 }
11251 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000011252 case 'N':
11253 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000011254 if (C->getZExtValue() <= 255) {
11255 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000011256 break;
11257 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000011258 }
Chris Lattner48884cd2007-08-25 00:47:38 +000011259 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000011260 case 'e': {
11261 // 32-bit signed value
11262 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000011263 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
11264 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000011265 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000011266 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000011267 break;
11268 }
11269 // FIXME gcc accepts some relocatable values here too, but only in certain
11270 // memory models; it's complicated.
11271 }
11272 return;
11273 }
11274 case 'Z': {
11275 // 32-bit unsigned value
11276 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000011277 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
11278 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000011279 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
11280 break;
11281 }
11282 }
11283 // FIXME gcc accepts some relocatable values here too, but only in certain
11284 // memory models; it's complicated.
11285 return;
11286 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000011287 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000011288 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000011289 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000011290 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000011291 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000011292 break;
11293 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011294
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000011295 // In any sort of PIC mode addresses need to be computed at runtime by
11296 // adding in a register or some sort of table lookup. These can't
11297 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000011298 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000011299 return;
11300
Chris Lattnerdc43a882007-05-03 16:52:29 +000011301 // If we are in non-pic codegen mode, we allow the address of a global (with
11302 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000011303 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000011304 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000011305
Chris Lattner49921962009-05-08 18:23:14 +000011306 // Match either (GA), (GA+C), (GA+C1+C2), etc.
11307 while (1) {
11308 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
11309 Offset += GA->getOffset();
11310 break;
11311 } else if (Op.getOpcode() == ISD::ADD) {
11312 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
11313 Offset += C->getZExtValue();
11314 Op = Op.getOperand(0);
11315 continue;
11316 }
11317 } else if (Op.getOpcode() == ISD::SUB) {
11318 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
11319 Offset += -C->getZExtValue();
11320 Op = Op.getOperand(0);
11321 continue;
11322 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000011323 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000011324
Chris Lattner49921962009-05-08 18:23:14 +000011325 // Otherwise, this isn't something we can handle, reject it.
11326 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000011327 }
Eric Christopherfd179292009-08-27 18:07:15 +000011328
Dan Gohman46510a72010-04-15 01:51:59 +000011329 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000011330 // If we require an extra load to get this address, as in PIC mode, we
11331 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000011332 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
11333 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000011334 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000011335
Devang Patel0d881da2010-07-06 22:08:15 +000011336 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
11337 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000011338 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000011339 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000011340 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011341
Gabor Greifba36cb52008-08-28 21:40:38 +000011342 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000011343 Ops.push_back(Result);
11344 return;
11345 }
Dale Johannesen1784d162010-06-25 21:55:36 +000011346 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000011347}
11348
Chris Lattner259e97c2006-01-31 19:43:35 +000011349std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +000011350getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000011351 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +000011352 if (Constraint.size() == 1) {
11353 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +000011354 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +000011355 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +000011356 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
11357 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011358 if (VT == MVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +000011359 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
11360 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
11361 X86::R10D,X86::R11D,X86::R12D,
11362 X86::R13D,X86::R14D,X86::R15D,
11363 X86::EBP, X86::ESP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011364 else if (VT == MVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +000011365 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
11366 X86::SI, X86::DI, X86::R8W,X86::R9W,
11367 X86::R10W,X86::R11W,X86::R12W,
11368 X86::R13W,X86::R14W,X86::R15W,
11369 X86::BP, X86::SP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011370 else if (VT == MVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +000011371 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
11372 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
11373 X86::R10B,X86::R11B,X86::R12B,
11374 X86::R13B,X86::R14B,X86::R15B,
11375 X86::BPL, X86::SPL, 0);
11376
Owen Anderson825b72b2009-08-11 20:47:22 +000011377 else if (VT == MVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +000011378 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
11379 X86::RSI, X86::RDI, X86::R8, X86::R9,
11380 X86::R10, X86::R11, X86::R12,
11381 X86::R13, X86::R14, X86::R15,
11382 X86::RBP, X86::RSP, 0);
11383
11384 break;
11385 }
Eric Christopherfd179292009-08-27 18:07:15 +000011386 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +000011387 case 'Q': // Q_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000011388 if (VT == MVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000011389 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011390 else if (VT == MVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000011391 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011392 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +000011393 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011394 else if (VT == MVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +000011395 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
11396 break;
Chris Lattner259e97c2006-01-31 19:43:35 +000011397 }
11398 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011399
Chris Lattner1efa40f2006-02-22 00:56:39 +000011400 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +000011401}
Chris Lattnerf76d1802006-07-31 23:26:50 +000011402
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011403std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000011404X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000011405 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000011406 // First, see if this is a constraint that directly corresponds to an LLVM
11407 // register class.
11408 if (Constraint.size() == 1) {
11409 // GCC Constraint Letters
11410 switch (Constraint[0]) {
11411 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000011412 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000011413 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000011414 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +000011415 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000011416 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000011417 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000011418 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000011419 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000011420 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000011421 case 'R': // LEGACY_REGS
11422 if (VT == MVT::i8)
11423 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
11424 if (VT == MVT::i16)
11425 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
11426 if (VT == MVT::i32 || !Subtarget->is64Bit())
11427 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
11428 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000011429 case 'f': // FP Stack registers.
11430 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
11431 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000011432 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000011433 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000011434 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000011435 return std::make_pair(0U, X86::RFP64RegisterClass);
11436 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000011437 case 'y': // MMX_REGS if MMX allowed.
11438 if (!Subtarget->hasMMX()) break;
11439 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000011440 case 'Y': // SSE_REGS if SSE2 allowed
11441 if (!Subtarget->hasSSE2()) break;
11442 // FALL THROUGH.
11443 case 'x': // SSE_REGS if SSE1 allowed
11444 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000011445
Owen Anderson825b72b2009-08-11 20:47:22 +000011446 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000011447 default: break;
11448 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000011449 case MVT::f32:
11450 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000011451 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000011452 case MVT::f64:
11453 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000011454 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000011455 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000011456 case MVT::v16i8:
11457 case MVT::v8i16:
11458 case MVT::v4i32:
11459 case MVT::v2i64:
11460 case MVT::v4f32:
11461 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000011462 return std::make_pair(0U, X86::VR128RegisterClass);
11463 }
Chris Lattnerad043e82007-04-09 05:11:28 +000011464 break;
11465 }
11466 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011467
Chris Lattnerf76d1802006-07-31 23:26:50 +000011468 // Use the default implementation in TargetLowering to convert the register
11469 // constraint into a member of a register class.
11470 std::pair<unsigned, const TargetRegisterClass*> Res;
11471 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000011472
11473 // Not found as a standard register?
11474 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000011475 // Map st(0) -> st(7) -> ST0
11476 if (Constraint.size() == 7 && Constraint[0] == '{' &&
11477 tolower(Constraint[1]) == 's' &&
11478 tolower(Constraint[2]) == 't' &&
11479 Constraint[3] == '(' &&
11480 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
11481 Constraint[5] == ')' &&
11482 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000011483
Chris Lattner56d77c72009-09-13 22:41:48 +000011484 Res.first = X86::ST0+Constraint[4]-'0';
11485 Res.second = X86::RFP80RegisterClass;
11486 return Res;
11487 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000011488
Chris Lattner56d77c72009-09-13 22:41:48 +000011489 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000011490 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000011491 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000011492 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000011493 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000011494 }
Chris Lattner56d77c72009-09-13 22:41:48 +000011495
11496 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000011497 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000011498 Res.first = X86::EFLAGS;
11499 Res.second = X86::CCRRegisterClass;
11500 return Res;
11501 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000011502
Dale Johannesen330169f2008-11-13 21:52:36 +000011503 // 'A' means EAX + EDX.
11504 if (Constraint == "A") {
11505 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000011506 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000011507 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000011508 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000011509 return Res;
11510 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011511
Chris Lattnerf76d1802006-07-31 23:26:50 +000011512 // Otherwise, check to see if this is a register class of the wrong value
11513 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
11514 // turn into {ax},{dx}.
11515 if (Res.second->hasType(VT))
11516 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011517
Chris Lattnerf76d1802006-07-31 23:26:50 +000011518 // All of the single-register GCC register classes map their values onto
11519 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
11520 // really want an 8-bit or 32-bit register, map to the appropriate register
11521 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000011522 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011523 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000011524 unsigned DestReg = 0;
11525 switch (Res.first) {
11526 default: break;
11527 case X86::AX: DestReg = X86::AL; break;
11528 case X86::DX: DestReg = X86::DL; break;
11529 case X86::CX: DestReg = X86::CL; break;
11530 case X86::BX: DestReg = X86::BL; break;
11531 }
11532 if (DestReg) {
11533 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000011534 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000011535 }
Owen Anderson825b72b2009-08-11 20:47:22 +000011536 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000011537 unsigned DestReg = 0;
11538 switch (Res.first) {
11539 default: break;
11540 case X86::AX: DestReg = X86::EAX; break;
11541 case X86::DX: DestReg = X86::EDX; break;
11542 case X86::CX: DestReg = X86::ECX; break;
11543 case X86::BX: DestReg = X86::EBX; break;
11544 case X86::SI: DestReg = X86::ESI; break;
11545 case X86::DI: DestReg = X86::EDI; break;
11546 case X86::BP: DestReg = X86::EBP; break;
11547 case X86::SP: DestReg = X86::ESP; break;
11548 }
11549 if (DestReg) {
11550 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000011551 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000011552 }
Owen Anderson825b72b2009-08-11 20:47:22 +000011553 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000011554 unsigned DestReg = 0;
11555 switch (Res.first) {
11556 default: break;
11557 case X86::AX: DestReg = X86::RAX; break;
11558 case X86::DX: DestReg = X86::RDX; break;
11559 case X86::CX: DestReg = X86::RCX; break;
11560 case X86::BX: DestReg = X86::RBX; break;
11561 case X86::SI: DestReg = X86::RSI; break;
11562 case X86::DI: DestReg = X86::RDI; break;
11563 case X86::BP: DestReg = X86::RBP; break;
11564 case X86::SP: DestReg = X86::RSP; break;
11565 }
11566 if (DestReg) {
11567 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000011568 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000011569 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000011570 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000011571 } else if (Res.second == X86::FR32RegisterClass ||
11572 Res.second == X86::FR64RegisterClass ||
11573 Res.second == X86::VR128RegisterClass) {
11574 // Handle references to XMM physical registers that got mapped into the
11575 // wrong class. This can happen with constraints like {xmm0} where the
11576 // target independent register mapper will just pick the first match it can
11577 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000011578 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000011579 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000011580 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000011581 Res.second = X86::FR64RegisterClass;
11582 else if (X86::VR128RegisterClass->hasType(VT))
11583 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000011584 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011585
Chris Lattnerf76d1802006-07-31 23:26:50 +000011586 return Res;
11587}