blob: 0593f4ce887887a5702f73fd026ea206f0aac679 [file] [log] [blame]
Arnold Schwaighofer92226dd2007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Evan Chengb1712452010-01-27 06:25:16 +000015#define DEBUG_TYPE "x86-isel"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000016#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000017#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000018#include "X86ISelLowering.h"
19#include "X86TargetMachine.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000020#include "X86TargetObjectFile.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000021#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000022#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000023#include "llvm/DerivedTypes.h"
Chris Lattnerb903bed2009-06-26 21:20:29 +000024#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000025#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000026#include "llvm/Function.h"
Chris Lattnerb8105652009-07-20 17:51:36 +000027#include "llvm/Instructions.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000028#include "llvm/Intrinsics.h"
Owen Andersona90b3dc2009-07-15 21:51:10 +000029#include "llvm/LLVMContext.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000030#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000031#include "llvm/CodeGen/MachineFunction.h"
32#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner5e1df8d2010-01-25 23:38:14 +000033#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000034#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000035#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000036#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner589c6f62010-01-26 06:28:43 +000037#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000038#include "llvm/MC/MCContext.h"
Daniel Dunbar4e815f82010-03-15 23:51:06 +000039#include "llvm/MC/MCExpr.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000040#include "llvm/MC/MCSymbol.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000041#include "llvm/ADT/BitVector.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000042#include "llvm/ADT/SmallSet.h"
Evan Chengb1712452010-01-27 06:25:16 +000043#include "llvm/ADT/Statistic.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000044#include "llvm/ADT/StringExtras.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000045#include "llvm/ADT/VectorExtras.h"
Mon P Wang3c81d352008-11-23 04:37:22 +000046#include "llvm/Support/CommandLine.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000047#include "llvm/Support/Debug.h"
Bill Wendlingec041eb2010-03-12 19:20:40 +000048#include "llvm/Support/Dwarf.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000049#include "llvm/Support/ErrorHandling.h"
50#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000051#include "llvm/Support/raw_ostream.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000052using namespace llvm;
Bill Wendlingec041eb2010-03-12 19:20:40 +000053using namespace dwarf;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000054
Evan Chengb1712452010-01-27 06:25:16 +000055STATISTIC(NumTailCalls, "Number of tail calls");
56
Mon P Wang3c81d352008-11-23 04:37:22 +000057static cl::opt<bool>
Mon P Wang9f22a4a2008-11-24 02:10:43 +000058DisableMMX("disable-mmx", cl::Hidden, cl::desc("Disable use of MMX"));
Mon P Wang3c81d352008-11-23 04:37:22 +000059
Evan Cheng10e86422008-04-25 19:11:04 +000060// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000061static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000062 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000063
Chris Lattnerf0144122009-07-28 03:13:23 +000064static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
65 switch (TM.getSubtarget<X86Subtarget>().TargetType) {
66 default: llvm_unreachable("unknown subtarget type");
67 case X86Subtarget::isDarwin:
Bill Wendling757e75b2010-03-15 19:04:37 +000068 if (TM.getSubtarget<X86Subtarget>().is64Bit())
69 return new X8664_MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +000070 return new TargetLoweringObjectFileMachO();
Chris Lattnerf0144122009-07-28 03:13:23 +000071 case X86Subtarget::isELF:
Anton Korobeynikov9184b252010-02-15 22:35:59 +000072 if (TM.getSubtarget<X86Subtarget>().is64Bit())
73 return new X8664_ELFTargetObjectFile(TM);
74 return new X8632_ELFTargetObjectFile(TM);
Chris Lattnerf0144122009-07-28 03:13:23 +000075 case X86Subtarget::isMingw:
76 case X86Subtarget::isCygwin:
77 case X86Subtarget::isWindows:
78 return new TargetLoweringObjectFileCOFF();
79 }
Chris Lattnerf0144122009-07-28 03:13:23 +000080}
81
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +000082X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +000083 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +000084 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesenf1fc3a82007-09-23 14:52:20 +000085 X86ScalarSSEf64 = Subtarget->hasSSE2();
86 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng25ab6902006-09-08 06:48:29 +000087 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000088
Anton Korobeynikov2365f512007-07-14 14:06:15 +000089 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000090 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000091
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000092 // Set up the TargetLowering object.
93
94 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Owen Anderson825b72b2009-08-11 20:47:22 +000095 setShiftAmountType(MVT::i8);
Duncan Sands03228082008-11-23 15:47:28 +000096 setBooleanContents(ZeroOrOneBooleanContent);
Evan Cheng0b2afbd2006-01-25 09:15:17 +000097 setSchedulingPreference(SchedulingForRegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +000098 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +000099
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000100 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000101 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000102 setUseUnderscoreSetJmp(false);
103 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000104 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000105 // MS runtime is weird: it exports _setjmp, but longjmp!
106 setUseUnderscoreSetJmp(true);
107 setUseUnderscoreLongJmp(false);
108 } else {
109 setUseUnderscoreSetJmp(true);
110 setUseUnderscoreLongJmp(true);
111 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000112
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000113 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000114 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman71edb242010-04-30 18:30:26 +0000115 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000116 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000117 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000118 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000119
Owen Anderson825b72b2009-08-11 20:47:22 +0000120 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000121
Scott Michelfdc40a02009-02-17 22:15:04 +0000122 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000123 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000124 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000125 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000126 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000127 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
128 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000129
130 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000131 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
132 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
133 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
134 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
135 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
136 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000137
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000138 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
139 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000140 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
141 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
142 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000143
Evan Cheng25ab6902006-09-08 06:48:29 +0000144 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000145 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
146 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000147 } else if (!UseSoftFloat) {
148 if (X86ScalarSSEf64) {
Dale Johannesen1c15bf52008-10-21 20:50:01 +0000149 // We have an impenetrably clever algorithm for ui64->double only.
Owen Anderson825b72b2009-08-11 20:47:22 +0000150 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000151 }
Eli Friedman948e95a2009-05-23 09:59:16 +0000152 // We have an algorithm for SSE2, and we turn this into a 64-bit
153 // FILD for other targets.
Owen Anderson825b72b2009-08-11 20:47:22 +0000154 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000155 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000156
157 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
158 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000159 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
160 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000161
Devang Patel6a784892009-06-05 18:48:29 +0000162 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000163 // SSE has no i16 to fp conversion, only i32
164 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000165 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000166 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000167 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000168 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000169 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
170 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000171 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000172 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000173 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
174 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000175 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000176
Dale Johannesen73328d12007-09-19 23:55:34 +0000177 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
178 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000179 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
180 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000181
Evan Cheng02568ff2006-01-30 22:13:22 +0000182 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
183 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000184 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
185 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000186
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000187 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000188 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000189 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000190 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000191 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000192 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
193 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000194 }
195
196 // Handle FP_TO_UINT by promoting the destination to a larger signed
197 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000198 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
199 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
200 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000201
Evan Cheng25ab6902006-09-08 06:48:29 +0000202 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000203 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
204 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000205 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000206 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000207 // Expand FP_TO_UINT into a select.
208 // FIXME: We would like to use a Custom expander here eventually to do
209 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000210 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000211 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000212 // With SSE3 we can use fisttpll to convert to a signed i64; without
213 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000214 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000215 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000216
Chris Lattner399610a2006-12-05 18:22:22 +0000217 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000218 if (!X86ScalarSSEf64) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000219 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
220 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
Chris Lattnerf3597a12006-12-05 18:45:06 +0000221 }
Chris Lattner21f66852005-12-23 05:15:23 +0000222
Dan Gohmanb00ee212008-02-18 19:34:53 +0000223 // Scalar integer divide and remainder are lowered to use operations that
224 // produce two results, to match the available instructions. This exposes
225 // the two-result form to trivial CSE, which is able to combine x/y and x%y
226 // into a single instruction.
227 //
228 // Scalar integer multiply-high is also lowered to use two-result
229 // operations, to match the available instructions. However, plain multiply
230 // (low) operations are left as Legal, as there are single-result
231 // instructions for this in x86. Using the two-result multiply instructions
232 // when both high and low results are needed must be arranged by dagcombine.
Owen Anderson825b72b2009-08-11 20:47:22 +0000233 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
234 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
235 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
236 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
237 setOperationAction(ISD::SREM , MVT::i8 , Expand);
238 setOperationAction(ISD::UREM , MVT::i8 , Expand);
239 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
240 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
241 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
242 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
243 setOperationAction(ISD::SREM , MVT::i16 , Expand);
244 setOperationAction(ISD::UREM , MVT::i16 , Expand);
245 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
246 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
247 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
248 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
249 setOperationAction(ISD::SREM , MVT::i32 , Expand);
250 setOperationAction(ISD::UREM , MVT::i32 , Expand);
251 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
252 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
253 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
254 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
255 setOperationAction(ISD::SREM , MVT::i64 , Expand);
256 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohmana37c9f72007-09-25 18:23:27 +0000257
Owen Anderson825b72b2009-08-11 20:47:22 +0000258 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
259 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
260 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
261 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000262 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000263 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
264 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
265 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
266 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
267 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
268 setOperationAction(ISD::FREM , MVT::f32 , Expand);
269 setOperationAction(ISD::FREM , MVT::f64 , Expand);
270 setOperationAction(ISD::FREM , MVT::f80 , Expand);
271 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000272
Owen Anderson825b72b2009-08-11 20:47:22 +0000273 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
274 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
275 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
276 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000277 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
278 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000279 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
280 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
281 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000282 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000283 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
284 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
285 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000286 }
287
Owen Anderson825b72b2009-08-11 20:47:22 +0000288 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
289 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000290
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000291 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000292 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000293 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000294 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000295 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000296 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
297 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
298 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
299 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
300 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000301 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000302 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
303 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
304 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
305 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000306 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000307 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
308 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000309 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000310 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000311
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000312 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000313 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
314 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
315 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
316 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000317 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000318 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
319 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000320 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000321 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000322 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
323 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
324 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
325 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000326 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000327 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000328 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000329 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
330 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
331 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000332 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000333 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
334 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
335 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000336 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000337
Evan Chengd2cde682008-03-10 19:38:10 +0000338 if (Subtarget->hasSSE1())
Owen Anderson825b72b2009-08-11 20:47:22 +0000339 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000340
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000341 if (!Subtarget->hasSSE2())
Owen Anderson825b72b2009-08-11 20:47:22 +0000342 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000343
Mon P Wang63307c32008-05-05 19:05:59 +0000344 // Expand certain atomics
Owen Anderson825b72b2009-08-11 20:47:22 +0000345 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Custom);
346 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Custom);
347 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
348 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Bill Wendling5bf1b4e2008-08-20 00:28:16 +0000349
Owen Anderson825b72b2009-08-11 20:47:22 +0000350 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Custom);
351 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Custom);
352 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
353 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000354
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000355 if (!Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000356 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
357 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
358 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
359 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
360 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
361 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
362 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000363 }
364
Evan Cheng3c992d22006-03-07 02:02:57 +0000365 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000366 if (!Subtarget->isTargetDarwin() &&
367 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000368 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000369 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000370 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000371
Owen Anderson825b72b2009-08-11 20:47:22 +0000372 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
373 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
374 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
375 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000376 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000377 setExceptionPointerRegister(X86::RAX);
378 setExceptionSelectorRegister(X86::RDX);
379 } else {
380 setExceptionPointerRegister(X86::EAX);
381 setExceptionSelectorRegister(X86::EDX);
382 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000383 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
384 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000385
Owen Anderson825b72b2009-08-11 20:47:22 +0000386 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000387
Owen Anderson825b72b2009-08-11 20:47:22 +0000388 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000389
Nate Begemanacc398c2006-01-25 18:21:52 +0000390 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000391 setOperationAction(ISD::VASTART , MVT::Other, Custom);
392 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000393 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000394 setOperationAction(ISD::VAARG , MVT::Other, Custom);
395 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000396 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000397 setOperationAction(ISD::VAARG , MVT::Other, Expand);
398 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000399 }
Evan Chengae642192007-03-02 23:16:35 +0000400
Owen Anderson825b72b2009-08-11 20:47:22 +0000401 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
402 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000403 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000404 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000405 if (Subtarget->isTargetCygMing())
Owen Anderson825b72b2009-08-11 20:47:22 +0000406 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000407 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000408 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000409
Evan Chengc7ce29b2009-02-13 22:36:38 +0000410 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000411 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000412 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000413 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
414 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000415
Evan Cheng223547a2006-01-31 22:28:30 +0000416 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000417 setOperationAction(ISD::FABS , MVT::f64, Custom);
418 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000419
420 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000421 setOperationAction(ISD::FNEG , MVT::f64, Custom);
422 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000423
Evan Cheng68c47cb2007-01-05 07:55:56 +0000424 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000425 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
426 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000427
Evan Chengd25e9e82006-02-02 00:28:23 +0000428 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000429 setOperationAction(ISD::FSIN , MVT::f64, Expand);
430 setOperationAction(ISD::FCOS , MVT::f64, Expand);
431 setOperationAction(ISD::FSIN , MVT::f32, Expand);
432 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000433
Chris Lattnera54aa942006-01-29 06:26:08 +0000434 // Expand FP immediates into loads from the stack, except for the special
435 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000436 addLegalFPImmediate(APFloat(+0.0)); // xorpd
437 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000438 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000439 // Use SSE for f32, x87 for f64.
440 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000441 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
442 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000443
444 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000445 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000446
447 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000448 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000449
Owen Anderson825b72b2009-08-11 20:47:22 +0000450 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000451
452 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000453 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
454 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000455
456 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000457 setOperationAction(ISD::FSIN , MVT::f32, Expand);
458 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000459
Nate Begemane1795842008-02-14 08:57:00 +0000460 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000461 addLegalFPImmediate(APFloat(+0.0f)); // xorps
462 addLegalFPImmediate(APFloat(+0.0)); // FLD0
463 addLegalFPImmediate(APFloat(+1.0)); // FLD1
464 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
465 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
466
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000467 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000468 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
469 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000470 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000471 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000472 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000473 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000474 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
475 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000476
Owen Anderson825b72b2009-08-11 20:47:22 +0000477 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
478 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
479 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
480 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000481
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000482 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000483 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
484 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000485 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000486 addLegalFPImmediate(APFloat(+0.0)); // FLD0
487 addLegalFPImmediate(APFloat(+1.0)); // FLD1
488 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
489 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000490 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
491 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
492 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
493 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000494 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000495
Dale Johannesen59a58732007-08-05 18:49:15 +0000496 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000497 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000498 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
499 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
500 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000501 {
502 bool ignored;
503 APFloat TmpFlt(+0.0);
504 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
505 &ignored);
506 addLegalFPImmediate(TmpFlt); // FLD0
507 TmpFlt.changeSign();
508 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
509 APFloat TmpFlt2(+1.0);
510 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
511 &ignored);
512 addLegalFPImmediate(TmpFlt2); // FLD1
513 TmpFlt2.changeSign();
514 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
515 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000516
Evan Chengc7ce29b2009-02-13 22:36:38 +0000517 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000518 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
519 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000520 }
Dale Johannesen2f429012007-09-26 21:10:55 +0000521 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000522
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000523 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000524 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
525 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
526 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000527
Owen Anderson825b72b2009-08-11 20:47:22 +0000528 setOperationAction(ISD::FLOG, MVT::f80, Expand);
529 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
530 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
531 setOperationAction(ISD::FEXP, MVT::f80, Expand);
532 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000533
Mon P Wangf007a8b2008-11-06 05:31:54 +0000534 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000535 // (for widening) or expand (for scalarization). Then we will selectively
536 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000537 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
538 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
539 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
540 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
541 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
542 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
543 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
544 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
545 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
546 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
547 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
548 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
549 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
550 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
551 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
552 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
553 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
554 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
555 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
556 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
557 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
558 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
559 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
560 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
561 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
562 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
563 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
564 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
565 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
566 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
567 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
568 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
569 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
570 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
571 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
572 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
573 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
574 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
575 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
576 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
577 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
578 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
579 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
580 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
581 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
582 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
583 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
584 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
585 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
586 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000587 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000588 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
589 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
590 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
591 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
592 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
593 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
594 setTruncStoreAction((MVT::SimpleValueType)VT,
595 (MVT::SimpleValueType)InnerVT, Expand);
596 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
597 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
598 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000599 }
600
Evan Chengc7ce29b2009-02-13 22:36:38 +0000601 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
602 // with -msoft-float, disable use of MMX as well.
Evan Cheng92722532009-03-26 23:06:32 +0000603 if (!UseSoftFloat && !DisableMMX && Subtarget->hasMMX()) {
Dale Johannesen76090172010-04-20 22:34:09 +0000604 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass, false);
605 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass, false);
606 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass, false);
607 addRegisterClass(MVT::v2f32, X86::VR64RegisterClass, false);
608 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass, false);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000609
Owen Anderson825b72b2009-08-11 20:47:22 +0000610 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
611 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
612 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
613 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000614
Owen Anderson825b72b2009-08-11 20:47:22 +0000615 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
616 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
617 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
618 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000619
Owen Anderson825b72b2009-08-11 20:47:22 +0000620 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
621 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
Bill Wendling74027e92007-03-15 21:24:36 +0000622
Owen Anderson825b72b2009-08-11 20:47:22 +0000623 setOperationAction(ISD::AND, MVT::v8i8, Promote);
624 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
625 setOperationAction(ISD::AND, MVT::v4i16, Promote);
626 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
627 setOperationAction(ISD::AND, MVT::v2i32, Promote);
628 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
629 setOperationAction(ISD::AND, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000630
Owen Anderson825b72b2009-08-11 20:47:22 +0000631 setOperationAction(ISD::OR, MVT::v8i8, Promote);
632 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
633 setOperationAction(ISD::OR, MVT::v4i16, Promote);
634 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
635 setOperationAction(ISD::OR, MVT::v2i32, Promote);
636 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
637 setOperationAction(ISD::OR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000638
Owen Anderson825b72b2009-08-11 20:47:22 +0000639 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
640 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
641 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
642 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
643 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
644 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
645 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000646
Owen Anderson825b72b2009-08-11 20:47:22 +0000647 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
648 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
649 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
650 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
651 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
652 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
653 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
654 AddPromotedToType (ISD::LOAD, MVT::v2f32, MVT::v1i64);
655 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000656
Owen Anderson825b72b2009-08-11 20:47:22 +0000657 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
658 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
659 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
660 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom);
661 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
Bill Wendlinga348c562007-03-22 18:42:45 +0000662
Owen Anderson825b72b2009-08-11 20:47:22 +0000663 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
664 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
665 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
666 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000667
Owen Anderson825b72b2009-08-11 20:47:22 +0000668 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f32, Custom);
669 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
670 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
671 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Bill Wendling3180e202008-07-20 02:32:23 +0000672
Owen Anderson825b72b2009-08-11 20:47:22 +0000673 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
Mon P Wang9e5ecb82008-12-12 01:25:51 +0000674
Owen Anderson825b72b2009-08-11 20:47:22 +0000675 setOperationAction(ISD::SELECT, MVT::v8i8, Promote);
676 setOperationAction(ISD::SELECT, MVT::v4i16, Promote);
677 setOperationAction(ISD::SELECT, MVT::v2i32, Promote);
678 setOperationAction(ISD::SELECT, MVT::v1i64, Custom);
679 setOperationAction(ISD::VSETCC, MVT::v8i8, Custom);
680 setOperationAction(ISD::VSETCC, MVT::v4i16, Custom);
681 setOperationAction(ISD::VSETCC, MVT::v2i32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000682 }
683
Evan Cheng92722532009-03-26 23:06:32 +0000684 if (!UseSoftFloat && Subtarget->hasSSE1()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000685 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000686
Owen Anderson825b72b2009-08-11 20:47:22 +0000687 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
688 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
689 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
690 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
691 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
692 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
693 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
694 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
695 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
696 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
697 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
698 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000699 }
700
Evan Cheng92722532009-03-26 23:06:32 +0000701 if (!UseSoftFloat && Subtarget->hasSSE2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000702 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000703
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000704 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
705 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000706 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
707 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
708 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
709 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000710
Owen Anderson825b72b2009-08-11 20:47:22 +0000711 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
712 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
713 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
714 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
715 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
716 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
717 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
718 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
719 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
720 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
721 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
722 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
723 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
724 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
725 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
726 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000727
Owen Anderson825b72b2009-08-11 20:47:22 +0000728 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
729 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
730 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
731 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000732
Owen Anderson825b72b2009-08-11 20:47:22 +0000733 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
734 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
735 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
736 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
737 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000738
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000739 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
740 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
741 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
742 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
743 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
744
Evan Cheng2c3ae372006-04-12 21:21:57 +0000745 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000746 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
747 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000748 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000749 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000750 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000751 // Do not attempt to custom lower non-128-bit vectors
752 if (!VT.is128BitVector())
753 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000754 setOperationAction(ISD::BUILD_VECTOR,
755 VT.getSimpleVT().SimpleTy, Custom);
756 setOperationAction(ISD::VECTOR_SHUFFLE,
757 VT.getSimpleVT().SimpleTy, Custom);
758 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
759 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000760 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000761
Owen Anderson825b72b2009-08-11 20:47:22 +0000762 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
763 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
764 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
765 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
766 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
767 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000768
Nate Begemancdd1eec2008-02-12 22:51:28 +0000769 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000770 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
771 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000772 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000773
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000774 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000775 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
776 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000777 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000778
779 // Do not attempt to promote non-128-bit vectors
780 if (!VT.is128BitVector()) {
781 continue;
782 }
Eric Christopher4bd24c22010-03-30 01:04:59 +0000783
Owen Andersond6662ad2009-08-10 20:46:15 +0000784 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000785 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000786 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000787 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000788 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000789 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000790 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000791 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000792 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000793 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000794 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000795
Owen Anderson825b72b2009-08-11 20:47:22 +0000796 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000797
Evan Cheng2c3ae372006-04-12 21:21:57 +0000798 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000799 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
800 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
801 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
802 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000803
Owen Anderson825b72b2009-08-11 20:47:22 +0000804 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
805 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Eli Friedman23ef1052009-06-06 03:57:58 +0000806 if (!DisableMMX && Subtarget->hasMMX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000807 setOperationAction(ISD::FP_TO_SINT, MVT::v2i32, Custom);
808 setOperationAction(ISD::SINT_TO_FP, MVT::v2i32, Custom);
Eli Friedman23ef1052009-06-06 03:57:58 +0000809 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000810 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000811
Nate Begeman14d12ca2008-02-11 04:19:36 +0000812 if (Subtarget->hasSSE41()) {
813 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000814 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000815
816 // i8 and i16 vectors are custom , because the source register and source
817 // source memory operand types are not the same width. f32 vectors are
818 // custom since the immediate controlling the insert encodes additional
819 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000820 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
821 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
822 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
823 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000824
Owen Anderson825b72b2009-08-11 20:47:22 +0000825 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
826 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
827 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
828 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000829
830 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000831 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
832 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000833 }
834 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000835
Nate Begeman30a0de92008-07-17 16:51:19 +0000836 if (Subtarget->hasSSE42()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000837 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
Nate Begeman30a0de92008-07-17 16:51:19 +0000838 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000839
David Greene9b9838d2009-06-29 16:47:10 +0000840 if (!UseSoftFloat && Subtarget->hasAVX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000841 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
842 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
843 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
844 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000845
Owen Anderson825b72b2009-08-11 20:47:22 +0000846 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
847 setOperationAction(ISD::LOAD, MVT::v8i32, Legal);
848 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
849 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
850 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
851 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
852 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
853 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
854 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
855 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
856 //setOperationAction(ISD::BUILD_VECTOR, MVT::v8f32, Custom);
857 //setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Custom);
858 //setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8f32, Custom);
859 //setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
860 //setOperationAction(ISD::VSETCC, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000861
862 // Operations to consider commented out -v16i16 v32i8
Owen Anderson825b72b2009-08-11 20:47:22 +0000863 //setOperationAction(ISD::ADD, MVT::v16i16, Legal);
864 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
865 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
866 //setOperationAction(ISD::SUB, MVT::v32i8, Legal);
867 //setOperationAction(ISD::SUB, MVT::v16i16, Legal);
868 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
869 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
870 //setOperationAction(ISD::MUL, MVT::v16i16, Legal);
871 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
872 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
873 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
874 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
875 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
876 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000877
Owen Anderson825b72b2009-08-11 20:47:22 +0000878 setOperationAction(ISD::VSETCC, MVT::v4f64, Custom);
879 // setOperationAction(ISD::VSETCC, MVT::v32i8, Custom);
880 // setOperationAction(ISD::VSETCC, MVT::v16i16, Custom);
881 setOperationAction(ISD::VSETCC, MVT::v8i32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000882
Owen Anderson825b72b2009-08-11 20:47:22 +0000883 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v32i8, Custom);
884 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i16, Custom);
885 // setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i16, Custom);
886 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i32, Custom);
887 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000888
Owen Anderson825b72b2009-08-11 20:47:22 +0000889 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f64, Custom);
890 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i64, Custom);
891 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f64, Custom);
892 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i64, Custom);
893 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f64, Custom);
894 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000895
896#if 0
897 // Not sure we want to do this since there are no 256-bit integer
898 // operations in AVX
899
900 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
901 // This includes 256-bit vectors
Owen Anderson825b72b2009-08-11 20:47:22 +0000902 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; ++i) {
903 EVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000904
905 // Do not attempt to custom lower non-power-of-2 vectors
906 if (!isPowerOf2_32(VT.getVectorNumElements()))
907 continue;
908
909 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
910 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
911 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
912 }
913
914 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000915 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i64, Custom);
916 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i64, Custom);
Eric Christopherfd179292009-08-27 18:07:15 +0000917 }
David Greene9b9838d2009-06-29 16:47:10 +0000918#endif
919
920#if 0
921 // Not sure we want to do this since there are no 256-bit integer
922 // operations in AVX
923
924 // Promote v32i8, v16i16, v8i32 load, select, and, or, xor to v4i64.
925 // Including 256-bit vectors
Owen Anderson825b72b2009-08-11 20:47:22 +0000926 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; i++) {
927 EVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000928
929 if (!VT.is256BitVector()) {
930 continue;
931 }
932 setOperationAction(ISD::AND, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000933 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000934 setOperationAction(ISD::OR, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000935 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000936 setOperationAction(ISD::XOR, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000937 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000938 setOperationAction(ISD::LOAD, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000939 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000940 setOperationAction(ISD::SELECT, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000941 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000942 }
943
Owen Anderson825b72b2009-08-11 20:47:22 +0000944 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
David Greene9b9838d2009-06-29 16:47:10 +0000945#endif
946 }
947
Evan Cheng6be2c582006-04-05 23:38:46 +0000948 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000949 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +0000950
Bill Wendling74c37652008-12-09 22:08:41 +0000951 // Add/Sub/Mul with overflow operations are custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000952 setOperationAction(ISD::SADDO, MVT::i32, Custom);
953 setOperationAction(ISD::SADDO, MVT::i64, Custom);
954 setOperationAction(ISD::UADDO, MVT::i32, Custom);
955 setOperationAction(ISD::UADDO, MVT::i64, Custom);
956 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
957 setOperationAction(ISD::SSUBO, MVT::i64, Custom);
958 setOperationAction(ISD::USUBO, MVT::i32, Custom);
959 setOperationAction(ISD::USUBO, MVT::i64, Custom);
960 setOperationAction(ISD::SMULO, MVT::i32, Custom);
961 setOperationAction(ISD::SMULO, MVT::i64, Custom);
Bill Wendling41ea7e72008-11-24 19:21:46 +0000962
Evan Chengd54f2d52009-03-31 19:38:51 +0000963 if (!Subtarget->is64Bit()) {
964 // These libcalls are not available in 32-bit.
965 setLibcallName(RTLIB::SHL_I128, 0);
966 setLibcallName(RTLIB::SRL_I128, 0);
967 setLibcallName(RTLIB::SRA_I128, 0);
968 }
969
Evan Cheng206ee9d2006-07-07 08:33:52 +0000970 // We have target-specific dag combine patterns for the following nodes:
971 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +0000972 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +0000973 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +0000974 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +0000975 setTargetDAGCombine(ISD::SHL);
976 setTargetDAGCombine(ISD::SRA);
977 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +0000978 setTargetDAGCombine(ISD::OR);
Chris Lattner149a4e52008-02-22 02:09:43 +0000979 setTargetDAGCombine(ISD::STORE);
Owen Anderson99177002009-06-29 18:04:45 +0000980 setTargetDAGCombine(ISD::MEMBARRIER);
Evan Cheng2e489c42009-12-16 00:53:11 +0000981 setTargetDAGCombine(ISD::ZERO_EXTEND);
Evan Cheng0b0cd912009-03-28 05:57:29 +0000982 if (Subtarget->is64Bit())
983 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +0000984
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000985 computeRegisterProperties();
986
Evan Cheng87ed7162006-02-14 08:25:08 +0000987 // FIXME: These should be based on subtarget info. Plus, the values should
988 // be smaller when we are in optimizing for size mode.
Dan Gohman87060f52008-06-30 21:00:56 +0000989 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng255f20f2010-04-01 06:04:33 +0000990 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Dan Gohman87060f52008-06-30 21:00:56 +0000991 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Evan Chengfb8075d2008-02-28 00:43:03 +0000992 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +0000993 benefitFromCodePlacementOpt = true;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000994}
995
Scott Michel5b8f82e2008-03-10 15:42:14 +0000996
Owen Anderson825b72b2009-08-11 20:47:22 +0000997MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
998 return MVT::i8;
Scott Michel5b8f82e2008-03-10 15:42:14 +0000999}
1000
1001
Evan Cheng29286502008-01-23 23:17:41 +00001002/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1003/// the desired ByVal argument alignment.
1004static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
1005 if (MaxAlign == 16)
1006 return;
1007 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
1008 if (VTy->getBitWidth() == 128)
1009 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +00001010 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1011 unsigned EltAlign = 0;
1012 getMaxByValAlign(ATy->getElementType(), EltAlign);
1013 if (EltAlign > MaxAlign)
1014 MaxAlign = EltAlign;
1015 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1016 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1017 unsigned EltAlign = 0;
1018 getMaxByValAlign(STy->getElementType(i), EltAlign);
1019 if (EltAlign > MaxAlign)
1020 MaxAlign = EltAlign;
1021 if (MaxAlign == 16)
1022 break;
1023 }
1024 }
1025 return;
1026}
1027
1028/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1029/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001030/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1031/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +00001032unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001033 if (Subtarget->is64Bit()) {
1034 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001035 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001036 if (TyAlign > 8)
1037 return TyAlign;
1038 return 8;
1039 }
1040
Evan Cheng29286502008-01-23 23:17:41 +00001041 unsigned Align = 4;
Dale Johannesen0c191872008-02-08 19:48:20 +00001042 if (Subtarget->hasSSE1())
1043 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001044 return Align;
1045}
Chris Lattner2b02a442007-02-25 08:29:00 +00001046
Evan Chengf0df0312008-05-15 08:39:06 +00001047/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001048/// and store operations as a result of memset, memcpy, and memmove
1049/// lowering. If DstAlign is zero that means it's safe to destination
1050/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1051/// means there isn't a need to check it against alignment requirement,
1052/// probably because the source does not need to be loaded. If
1053/// 'NonScalarIntSafe' is true, that means it's safe to return a
1054/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1055/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1056/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001057/// It returns EVT::Other if the type should be determined using generic
1058/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001059EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001060X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1061 unsigned DstAlign, unsigned SrcAlign,
Evan Chengf28f8bc2010-04-02 19:36:14 +00001062 bool NonScalarIntSafe,
Evan Chengc3b0c342010-04-08 07:37:57 +00001063 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001064 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001065 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1066 // linux. This is because the stack realignment code can't handle certain
1067 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001068 const Function *F = MF.getFunction();
Evan Chengf28f8bc2010-04-02 19:36:14 +00001069 if (NonScalarIntSafe &&
1070 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001071 if (Size >= 16 &&
1072 (Subtarget->isUnalignedMemAccessFast() ||
Chandler Carruthae1d41c2010-04-02 01:31:24 +00001073 ((DstAlign == 0 || DstAlign >= 16) &&
1074 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001075 Subtarget->getStackAlignment() >= 16) {
1076 if (Subtarget->hasSSE2())
1077 return MVT::v4i32;
Evan Chengf28f8bc2010-04-02 19:36:14 +00001078 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001079 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001080 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001081 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001082 Subtarget->getStackAlignment() >= 8 &&
Evan Chengc3b0c342010-04-08 07:37:57 +00001083 Subtarget->hasSSE2()) {
1084 // Do not use f64 to lower memcpy if source is string constant. It's
1085 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001086 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001087 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001088 }
Evan Chengf0df0312008-05-15 08:39:06 +00001089 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001090 return MVT::i64;
1091 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001092}
1093
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001094/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1095/// current function. The returned value is a member of the
1096/// MachineJumpTableInfo::JTEntryKind enum.
1097unsigned X86TargetLowering::getJumpTableEncoding() const {
1098 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1099 // symbol.
1100 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1101 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001102 return MachineJumpTableInfo::EK_Custom32;
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001103
1104 // Otherwise, use the normal jump table encoding heuristics.
1105 return TargetLowering::getJumpTableEncoding();
1106}
1107
Chris Lattner589c6f62010-01-26 06:28:43 +00001108/// getPICBaseSymbol - Return the X86-32 PIC base.
1109MCSymbol *
1110X86TargetLowering::getPICBaseSymbol(const MachineFunction *MF,
1111 MCContext &Ctx) const {
1112 const MCAsmInfo &MAI = *getTargetMachine().getMCAsmInfo();
Chris Lattner9b97a732010-03-30 18:10:53 +00001113 return Ctx.GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix())+
1114 Twine(MF->getFunctionNumber())+"$pb");
Chris Lattner589c6f62010-01-26 06:28:43 +00001115}
1116
1117
Chris Lattnerc64daab2010-01-26 05:02:42 +00001118const MCExpr *
1119X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1120 const MachineBasicBlock *MBB,
1121 unsigned uid,MCContext &Ctx) const{
1122 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1123 Subtarget->isPICStyleGOT());
1124 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1125 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001126 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1127 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001128}
1129
Evan Chengcc415862007-11-09 01:32:10 +00001130/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1131/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001132SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001133 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001134 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001135 // This doesn't have DebugLoc associated with it, but is not really the
1136 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001137 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001138 return Table;
1139}
1140
Chris Lattner589c6f62010-01-26 06:28:43 +00001141/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1142/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1143/// MCExpr.
1144const MCExpr *X86TargetLowering::
1145getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1146 MCContext &Ctx) const {
1147 // X86-64 uses RIP relative addressing based on the jump table label.
1148 if (Subtarget->isPICStyleRIPRel())
1149 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1150
1151 // Otherwise, the reference is relative to the PIC base.
1152 return MCSymbolRefExpr::Create(getPICBaseSymbol(MF, Ctx), Ctx);
1153}
1154
Bill Wendlingb4202b82009-07-01 18:50:55 +00001155/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +00001156unsigned X86TargetLowering::getFunctionAlignment(const Function *F) const {
Dan Gohman25103a22009-08-18 00:20:06 +00001157 return F->hasFnAttr(Attribute::OptimizeForSize) ? 0 : 4;
Bill Wendling20c568f2009-06-30 22:38:32 +00001158}
1159
Chris Lattner2b02a442007-02-25 08:29:00 +00001160//===----------------------------------------------------------------------===//
1161// Return Value Calling Convention Implementation
1162//===----------------------------------------------------------------------===//
1163
Chris Lattner59ed56b2007-02-28 04:55:35 +00001164#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001165
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001166bool
1167X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv, bool isVarArg,
1168 const SmallVectorImpl<EVT> &OutTys,
1169 const SmallVectorImpl<ISD::ArgFlagsTy> &ArgsFlags,
Dan Gohmand858e902010-04-17 15:26:15 +00001170 SelectionDAG &DAG) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001171 SmallVector<CCValAssign, 16> RVLocs;
1172 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1173 RVLocs, *DAG.getContext());
1174 return CCInfo.CheckReturn(OutTys, ArgsFlags, RetCC_X86);
1175}
1176
Dan Gohman98ca4f22009-08-05 01:29:28 +00001177SDValue
1178X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001179 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001180 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmand858e902010-04-17 15:26:15 +00001181 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001182 MachineFunction &MF = DAG.getMachineFunction();
1183 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001184
Chris Lattner9774c912007-02-27 05:28:59 +00001185 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001186 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1187 RVLocs, *DAG.getContext());
1188 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001189
Evan Chengdcea1632010-02-04 02:40:39 +00001190 // Add the regs to the liveout set for the function.
1191 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1192 for (unsigned i = 0; i != RVLocs.size(); ++i)
1193 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1194 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001195
Dan Gohman475871a2008-07-27 21:46:04 +00001196 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001197
Dan Gohman475871a2008-07-27 21:46:04 +00001198 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001199 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1200 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001201 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1202 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001203
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001204 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001205 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1206 CCValAssign &VA = RVLocs[i];
1207 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman98ca4f22009-08-05 01:29:28 +00001208 SDValue ValToCopy = Outs[i].Val;
Scott Michelfdc40a02009-02-17 22:15:04 +00001209
Chris Lattner447ff682008-03-11 03:23:40 +00001210 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1211 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001212 if (VA.getLocReg() == X86::ST0 ||
1213 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001214 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1215 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001216 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001217 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001218 RetOps.push_back(ValToCopy);
1219 // Don't emit a copytoreg.
1220 continue;
1221 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001222
Evan Cheng242b38b2009-02-23 09:03:22 +00001223 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1224 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001225 if (Subtarget->is64Bit()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001226 EVT ValVT = ValToCopy.getValueType();
Evan Cheng242b38b2009-02-23 09:03:22 +00001227 if (ValVT.isVector() && ValVT.getSizeInBits() == 64) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001228 ValToCopy = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, ValToCopy);
Evan Cheng242b38b2009-02-23 09:03:22 +00001229 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1)
Owen Anderson825b72b2009-08-11 20:47:22 +00001230 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, ValToCopy);
Evan Cheng242b38b2009-02-23 09:03:22 +00001231 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001232 }
1233
Dale Johannesendd64c412009-02-04 00:33:20 +00001234 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001235 Flag = Chain.getValue(1);
1236 }
Dan Gohman61a92132008-04-21 23:59:07 +00001237
1238 // The x86-64 ABI for returning structs by value requires that we copy
1239 // the sret argument into %rax for the return. We saved the argument into
1240 // a virtual register in the entry block, so now we copy the value out
1241 // and into %rax.
1242 if (Subtarget->is64Bit() &&
1243 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1244 MachineFunction &MF = DAG.getMachineFunction();
1245 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1246 unsigned Reg = FuncInfo->getSRetReturnReg();
1247 if (!Reg) {
Evan Chengdcea1632010-02-04 02:40:39 +00001248 Reg = MRI.createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001249 FuncInfo->setSRetReturnReg(Reg);
1250 }
Dale Johannesendd64c412009-02-04 00:33:20 +00001251 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001252
Dale Johannesendd64c412009-02-04 00:33:20 +00001253 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001254 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001255
1256 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001257 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001258 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001259
Chris Lattner447ff682008-03-11 03:23:40 +00001260 RetOps[0] = Chain; // Update chain.
1261
1262 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001263 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001264 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001265
1266 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001267 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001268}
1269
Dan Gohman98ca4f22009-08-05 01:29:28 +00001270/// LowerCallResult - Lower the result values of a call into the
1271/// appropriate copies out of appropriate physical registers.
1272///
1273SDValue
1274X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001275 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001276 const SmallVectorImpl<ISD::InputArg> &Ins,
1277 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001278 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001279
Chris Lattnere32bbf62007-02-28 07:09:55 +00001280 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001281 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001282 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001283 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001284 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001285 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001286
Chris Lattner3085e152007-02-25 08:59:22 +00001287 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001288 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001289 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001290 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001291
Torok Edwin3f142c32009-02-01 18:15:56 +00001292 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001293 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Dan Gohman98ca4f22009-08-05 01:29:28 +00001294 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001295 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001296 }
1297
Chris Lattner8e6da152008-03-10 21:08:41 +00001298 // If this is a call to a function that returns an fp value on the floating
1299 // point stack, but where we prefer to use the value in xmm registers, copy
1300 // it out as F80 and use a truncate to move it from fp stack reg to xmm reg.
Dan Gohman37eed792009-02-04 17:28:58 +00001301 if ((VA.getLocReg() == X86::ST0 ||
1302 VA.getLocReg() == X86::ST1) &&
1303 isScalarFPTypeInSSEReg(VA.getValVT())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001304 CopyVT = MVT::f80;
Chris Lattner3085e152007-02-25 08:59:22 +00001305 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001306
Evan Cheng79fb3b42009-02-20 20:43:02 +00001307 SDValue Val;
1308 if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001309 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1310 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1311 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001312 MVT::v2i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001313 Val = Chain.getValue(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001314 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1315 Val, DAG.getConstant(0, MVT::i64));
Evan Cheng242b38b2009-02-23 09:03:22 +00001316 } else {
1317 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001318 MVT::i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001319 Val = Chain.getValue(0);
1320 }
Evan Cheng79fb3b42009-02-20 20:43:02 +00001321 Val = DAG.getNode(ISD::BIT_CONVERT, dl, CopyVT, Val);
1322 } else {
1323 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1324 CopyVT, InFlag).getValue(1);
1325 Val = Chain.getValue(0);
1326 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001327 InFlag = Chain.getValue(2);
Chris Lattner112dedc2007-12-29 06:41:28 +00001328
Dan Gohman37eed792009-02-04 17:28:58 +00001329 if (CopyVT != VA.getValVT()) {
Chris Lattner8e6da152008-03-10 21:08:41 +00001330 // Round the F80 the right size, which also moves to the appropriate xmm
1331 // register.
Dan Gohman37eed792009-02-04 17:28:58 +00001332 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
Chris Lattner8e6da152008-03-10 21:08:41 +00001333 // This truncation won't change the value.
1334 DAG.getIntPtrConstant(1));
1335 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001336
Dan Gohman98ca4f22009-08-05 01:29:28 +00001337 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001338 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001339
Dan Gohman98ca4f22009-08-05 01:29:28 +00001340 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001341}
1342
1343
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001344//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001345// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001346//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001347// StdCall calling convention seems to be standard for many Windows' API
1348// routines and around. It differs from C calling convention just a little:
1349// callee should clean up the stack, not caller. Symbols should be also
1350// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001351// For info on fast calling convention see Fast Calling Convention (tail call)
1352// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001353
Dan Gohman98ca4f22009-08-05 01:29:28 +00001354/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001355/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001356static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1357 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001358 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001359
Dan Gohman98ca4f22009-08-05 01:29:28 +00001360 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001361}
1362
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001363/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001364/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001365static bool
1366ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1367 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001368 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001369
Dan Gohman98ca4f22009-08-05 01:29:28 +00001370 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001371}
1372
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001373/// IsCalleePop - Determines whether the callee is required to pop its
1374/// own arguments. Callee pop is necessary to support tail calls.
Dan Gohmand858e902010-04-17 15:26:15 +00001375bool X86TargetLowering::IsCalleePop(bool IsVarArg,
1376 CallingConv::ID CallingConv) const {
Gordon Henriksen86737662008-01-05 16:56:59 +00001377 if (IsVarArg)
1378 return false;
1379
Dan Gohman095cc292008-09-13 01:54:27 +00001380 switch (CallingConv) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001381 default:
1382 return false;
1383 case CallingConv::X86_StdCall:
1384 return !Subtarget->is64Bit();
1385 case CallingConv::X86_FastCall:
1386 return !Subtarget->is64Bit();
1387 case CallingConv::Fast:
Dan Gohman1797ed52010-02-08 20:27:50 +00001388 return GuaranteedTailCallOpt;
Chris Lattner29689432010-03-11 00:22:57 +00001389 case CallingConv::GHC:
1390 return GuaranteedTailCallOpt;
Gordon Henriksen86737662008-01-05 16:56:59 +00001391 }
1392}
1393
Dan Gohman095cc292008-09-13 01:54:27 +00001394/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1395/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001396CCAssignFn *X86TargetLowering::CCAssignFnForNode(CallingConv::ID CC) const {
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001397 if (Subtarget->is64Bit()) {
Chris Lattner29689432010-03-11 00:22:57 +00001398 if (CC == CallingConv::GHC)
1399 return CC_X86_64_GHC;
1400 else if (Subtarget->isTargetWin64())
Anton Korobeynikov8f88cb02008-03-22 20:37:30 +00001401 return CC_X86_Win64_C;
Evan Chenge9ac9e62008-09-07 09:07:23 +00001402 else
1403 return CC_X86_64_C;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001404 }
1405
Gordon Henriksen86737662008-01-05 16:56:59 +00001406 if (CC == CallingConv::X86_FastCall)
1407 return CC_X86_32_FastCall;
Evan Chengb188dd92008-09-10 18:25:29 +00001408 else if (CC == CallingConv::Fast)
1409 return CC_X86_32_FastCC;
Chris Lattner29689432010-03-11 00:22:57 +00001410 else if (CC == CallingConv::GHC)
1411 return CC_X86_32_GHC;
Gordon Henriksen86737662008-01-05 16:56:59 +00001412 else
1413 return CC_X86_32_C;
1414}
1415
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001416/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1417/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001418/// the specific parameter attribute. The copy will be passed as a byval
1419/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001420static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001421CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001422 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1423 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001424 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesendd64c412009-02-04 00:33:20 +00001425 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001426 /*isVolatile*/false, /*AlwaysInline=*/true,
1427 NULL, 0, NULL, 0);
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001428}
1429
Chris Lattner29689432010-03-11 00:22:57 +00001430/// IsTailCallConvention - Return true if the calling convention is one that
1431/// supports tail call optimization.
1432static bool IsTailCallConvention(CallingConv::ID CC) {
1433 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1434}
1435
Evan Cheng0c439eb2010-01-27 00:07:07 +00001436/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1437/// a tailcall target by changing its ABI.
1438static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001439 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001440}
1441
Dan Gohman98ca4f22009-08-05 01:29:28 +00001442SDValue
1443X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001444 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001445 const SmallVectorImpl<ISD::InputArg> &Ins,
1446 DebugLoc dl, SelectionDAG &DAG,
1447 const CCValAssign &VA,
1448 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001449 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001450 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001451 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001452 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001453 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001454 EVT ValVT;
1455
1456 // If value is passed by pointer we have address passed instead of the value
1457 // itself.
1458 if (VA.getLocInfo() == CCValAssign::Indirect)
1459 ValVT = VA.getLocVT();
1460 else
1461 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001462
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001463 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001464 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001465 // In case of tail call optimization mark all arguments mutable. Since they
1466 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001467 if (Flags.isByVal()) {
1468 int FI = MFI->CreateFixedObject(Flags.getByValSize(),
1469 VA.getLocMemOffset(), isImmutable, false);
1470 return DAG.getFrameIndex(FI, getPointerTy());
1471 } else {
1472 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
1473 VA.getLocMemOffset(), isImmutable, false);
1474 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1475 return DAG.getLoad(ValVT, dl, Chain, FIN,
David Greene67c9d422010-02-15 16:53:33 +00001476 PseudoSourceValue::getFixedStack(FI), 0,
1477 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001478 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001479}
1480
Dan Gohman475871a2008-07-27 21:46:04 +00001481SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001482X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001483 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001484 bool isVarArg,
1485 const SmallVectorImpl<ISD::InputArg> &Ins,
1486 DebugLoc dl,
1487 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001488 SmallVectorImpl<SDValue> &InVals)
1489 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001490 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001491 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001492
Gordon Henriksen86737662008-01-05 16:56:59 +00001493 const Function* Fn = MF.getFunction();
1494 if (Fn->hasExternalLinkage() &&
1495 Subtarget->isTargetCygMing() &&
1496 Fn->getName() == "main")
1497 FuncInfo->setForceFramePointer(true);
1498
Evan Cheng1bc78042006-04-26 01:20:17 +00001499 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001500 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001501 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001502
Chris Lattner29689432010-03-11 00:22:57 +00001503 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1504 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001505
Chris Lattner638402b2007-02-28 07:00:42 +00001506 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001507 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001508 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1509 ArgLocs, *DAG.getContext());
1510 CCInfo.AnalyzeFormalArguments(Ins, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001511
Chris Lattnerf39f7712007-02-28 05:46:49 +00001512 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001513 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001514 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1515 CCValAssign &VA = ArgLocs[i];
1516 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1517 // places.
1518 assert(VA.getValNo() != LastVal &&
1519 "Don't support value assigned to multiple locs yet");
1520 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001521
Chris Lattnerf39f7712007-02-28 05:46:49 +00001522 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001523 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001524 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001525 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001526 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001527 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001528 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001529 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001530 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001531 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001532 RC = X86::FR64RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001533 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001534 RC = X86::VR128RegisterClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001535 else if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
1536 RC = X86::VR64RegisterClass;
1537 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001538 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001539
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001540 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001541 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001542
Chris Lattnerf39f7712007-02-28 05:46:49 +00001543 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1544 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1545 // right size.
1546 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001547 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001548 DAG.getValueType(VA.getValVT()));
1549 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001550 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001551 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001552 else if (VA.getLocInfo() == CCValAssign::BCvt)
Anton Korobeynikov6dde14b2009-08-03 08:14:14 +00001553 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001554
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001555 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001556 // Handle MMX values passed in XMM regs.
1557 if (RegVT.isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001558 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1559 ArgValue, DAG.getConstant(0, MVT::i64));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001560 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1561 } else
1562 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001563 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001564 } else {
1565 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001566 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001567 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001568
1569 // If value is passed via pointer - do a load.
1570 if (VA.getLocInfo() == CCValAssign::Indirect)
David Greene67c9d422010-02-15 16:53:33 +00001571 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue, NULL, 0,
1572 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001573
Dan Gohman98ca4f22009-08-05 01:29:28 +00001574 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001575 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001576
Dan Gohman61a92132008-04-21 23:59:07 +00001577 // The x86-64 ABI for returning structs by value requires that we copy
1578 // the sret argument into %rax for the return. Save the argument into
1579 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001580 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001581 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1582 unsigned Reg = FuncInfo->getSRetReturnReg();
1583 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001584 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001585 FuncInfo->setSRetReturnReg(Reg);
1586 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001587 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001588 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001589 }
1590
Chris Lattnerf39f7712007-02-28 05:46:49 +00001591 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001592 // Align stack specially for tail calls.
1593 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001594 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001595
Evan Cheng1bc78042006-04-26 01:20:17 +00001596 // If the function takes variable number of arguments, make a frame index for
1597 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001598 if (isVarArg) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001599 if (Is64Bit || CallConv != CallingConv::X86_FastCall) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001600 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,
1601 true, false));
Gordon Henriksen86737662008-01-05 16:56:59 +00001602 }
1603 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001604 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1605
1606 // FIXME: We should really autogenerate these arrays
1607 static const unsigned GPR64ArgRegsWin64[] = {
1608 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001609 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001610 static const unsigned XMMArgRegsWin64[] = {
1611 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1612 };
1613 static const unsigned GPR64ArgRegs64Bit[] = {
1614 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1615 };
1616 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001617 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1618 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1619 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001620 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1621
1622 if (IsWin64) {
1623 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1624 GPR64ArgRegs = GPR64ArgRegsWin64;
1625 XMMArgRegs = XMMArgRegsWin64;
1626 } else {
1627 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1628 GPR64ArgRegs = GPR64ArgRegs64Bit;
1629 XMMArgRegs = XMMArgRegs64Bit;
1630 }
1631 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1632 TotalNumIntRegs);
1633 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1634 TotalNumXMMRegs);
1635
Devang Patel578efa92009-06-05 21:57:13 +00001636 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Evan Chengc7ce29b2009-02-13 22:36:38 +00001637 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001638 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001639 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001640 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001641 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00001642 // Kernel mode asks for SSE to be disabled, so don't push them
1643 // on the stack.
1644 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001645
Gordon Henriksen86737662008-01-05 16:56:59 +00001646 // For X86-64, if there are vararg parameters that are passed via
1647 // registers, then we must store them to their spots on the stack so they
1648 // may be loaded by deferencing the result of va_next.
Dan Gohman1e93df62010-04-17 14:41:14 +00001649 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1650 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1651 FuncInfo->setRegSaveFrameIndex(
1652 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
1653 false));
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001654
Gordon Henriksen86737662008-01-05 16:56:59 +00001655 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001656 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001657 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1658 getPointerTy());
1659 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001660 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001661 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1662 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001663 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
1664 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001665 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001666 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001667 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Dan Gohman1e93df62010-04-17 14:41:14 +00001668 PseudoSourceValue::getFixedStack(
1669 FuncInfo->getRegSaveFrameIndex()),
David Greene67c9d422010-02-15 16:53:33 +00001670 Offset, false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001671 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001672 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001673 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001674
Dan Gohmanface41a2009-08-16 21:24:25 +00001675 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1676 // Now store the XMM (fp + vector) parameter registers.
1677 SmallVector<SDValue, 11> SaveXMMOps;
1678 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001679
Dan Gohmanface41a2009-08-16 21:24:25 +00001680 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
1681 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1682 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001683
Dan Gohman1e93df62010-04-17 14:41:14 +00001684 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1685 FuncInfo->getRegSaveFrameIndex()));
1686 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1687 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001688
Dan Gohmanface41a2009-08-16 21:24:25 +00001689 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
1690 unsigned VReg = MF.addLiveIn(XMMArgRegs[NumXMMRegs],
1691 X86::VR128RegisterClass);
1692 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1693 SaveXMMOps.push_back(Val);
1694 }
1695 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1696 MVT::Other,
1697 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001698 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001699
1700 if (!MemOps.empty())
1701 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1702 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001703 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001704 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001705
Gordon Henriksen86737662008-01-05 16:56:59 +00001706 // Some CCs need callee pop.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001707 if (IsCalleePop(isVarArg, CallConv)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001708 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001709 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001710 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001711 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001712 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001713 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001714 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001715
Gordon Henriksen86737662008-01-05 16:56:59 +00001716 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001717 // RegSaveFrameIndex is X86-64 only.
1718 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001719 if (CallConv == CallingConv::X86_FastCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001720 // fastcc functions can't have varargs.
1721 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001722 }
Evan Cheng25caf632006-05-23 21:06:34 +00001723
Dan Gohman98ca4f22009-08-05 01:29:28 +00001724 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001725}
1726
Dan Gohman475871a2008-07-27 21:46:04 +00001727SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001728X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1729 SDValue StackPtr, SDValue Arg,
1730 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001731 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001732 ISD::ArgFlagsTy Flags) const {
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +00001733 const unsigned FirstStackArgOffset = (Subtarget->isTargetWin64() ? 32 : 0);
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +00001734 unsigned LocMemOffset = FirstStackArgOffset + VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001735 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001736 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001737 if (Flags.isByVal()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001738 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Evan Chengdffbd832008-01-10 00:09:10 +00001739 }
Dale Johannesenace16102009-02-03 19:33:06 +00001740 return DAG.getStore(Chain, dl, Arg, PtrOff,
David Greene67c9d422010-02-15 16:53:33 +00001741 PseudoSourceValue::getStack(), LocMemOffset,
1742 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001743}
1744
Bill Wendling64e87322009-01-16 19:25:27 +00001745/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001746/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001747SDValue
1748X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001749 SDValue &OutRetAddr, SDValue Chain,
1750 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001751 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001752 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001753 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001754 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001755
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001756 // Load the "old" Return address.
David Greene67c9d422010-02-15 16:53:33 +00001757 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, NULL, 0, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001758 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001759}
1760
1761/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1762/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001763static SDValue
1764EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001765 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001766 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001767 // Store the return address to the appropriate stack slot.
1768 if (!FPDiff) return Chain;
1769 // Calculate the new stack slot for the return address.
1770 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001771 int NewReturnAddrFI =
Arnold Schwaighofer92652752010-02-22 16:18:09 +00001772 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001773 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001774 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001775 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
David Greene67c9d422010-02-15 16:53:33 +00001776 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0,
1777 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001778 return Chain;
1779}
1780
Dan Gohman98ca4f22009-08-05 01:29:28 +00001781SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001782X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001783 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001784 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001785 const SmallVectorImpl<ISD::OutputArg> &Outs,
1786 const SmallVectorImpl<ISD::InputArg> &Ins,
1787 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001788 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001789 MachineFunction &MF = DAG.getMachineFunction();
1790 bool Is64Bit = Subtarget->is64Bit();
1791 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00001792 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001793
Evan Cheng5f941932010-02-05 02:21:12 +00001794 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001795 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00001796 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1797 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Evan Cheng022d9e12010-02-02 23:55:14 +00001798 Outs, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00001799
1800 // Sibcalls are automatically detected tailcalls which do not require
1801 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00001802 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00001803 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00001804
1805 if (isTailCall)
1806 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00001807 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00001808
Chris Lattner29689432010-03-11 00:22:57 +00001809 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1810 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001811
Chris Lattner638402b2007-02-28 07:00:42 +00001812 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001813 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001814 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1815 ArgLocs, *DAG.getContext());
1816 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001817
Chris Lattner423c5f42007-02-28 05:31:48 +00001818 // Get a count of how many bytes are to be pushed on the stack.
1819 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00001820 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00001821 // This is a sibcall. The memory operands are available in caller's
1822 // own caller's stack.
1823 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00001824 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00001825 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001826
Gordon Henriksen86737662008-01-05 16:56:59 +00001827 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00001828 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001829 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00001830 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00001831 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1832 FPDiff = NumBytesCallerPushed - NumBytes;
1833
1834 // Set the delta of movement of the returnaddr stackslot.
1835 // But only set if delta is greater than previous delta.
1836 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1837 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1838 }
1839
Evan Chengf22f9b32010-02-06 03:28:46 +00001840 if (!IsSibcall)
1841 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001842
Dan Gohman475871a2008-07-27 21:46:04 +00001843 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001844 // Load return adress for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00001845 if (isTailCall && FPDiff)
1846 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
1847 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001848
Dan Gohman475871a2008-07-27 21:46:04 +00001849 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1850 SmallVector<SDValue, 8> MemOpChains;
1851 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001852
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001853 // Walk the register/memloc assignments, inserting copies/loads. In the case
1854 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00001855 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1856 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001857 EVT RegVT = VA.getLocVT();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001858 SDValue Arg = Outs[i].Val;
1859 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00001860 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00001861
Chris Lattner423c5f42007-02-28 05:31:48 +00001862 // Promote the value if needed.
1863 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001864 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00001865 case CCValAssign::Full: break;
1866 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001867 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001868 break;
1869 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001870 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001871 break;
1872 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001873 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
1874 // Special case: passing MMX values in XMM registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001875 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
1876 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
1877 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001878 } else
1879 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
1880 break;
1881 case CCValAssign::BCvt:
1882 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001883 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001884 case CCValAssign::Indirect: {
1885 // Store the argument.
1886 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00001887 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001888 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
David Greene67c9d422010-02-15 16:53:33 +00001889 PseudoSourceValue::getFixedStack(FI), 0,
1890 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001891 Arg = SpillSlot;
1892 break;
1893 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00001894 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001895
Chris Lattner423c5f42007-02-28 05:31:48 +00001896 if (VA.isRegLoc()) {
1897 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Evan Chengf22f9b32010-02-06 03:28:46 +00001898 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00001899 assert(VA.isMemLoc());
1900 if (StackPtr.getNode() == 0)
1901 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
1902 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1903 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001904 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001905 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001906
Evan Cheng32fe1032006-05-25 00:59:30 +00001907 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001908 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001909 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001910
Evan Cheng347d5f72006-04-28 21:29:37 +00001911 // Build a sequence of copy-to-reg nodes chained together with token chain
1912 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001913 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001914 // Tail call byval lowering might overwrite argument registers so in case of
1915 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001916 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001917 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001918 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001919 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001920 InFlag = Chain.getValue(1);
1921 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001922
Chris Lattner88e1fd52009-07-09 04:24:46 +00001923 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001924 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1925 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001926 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001927 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
1928 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001929 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001930 InFlag);
1931 InFlag = Chain.getValue(1);
1932 } else {
1933 // If we are tail calling and generating PIC/GOT style code load the
1934 // address of the callee into ECX. The value in ecx is used as target of
1935 // the tail jump. This is done to circumvent the ebx/callee-saved problem
1936 // for tail calls on PIC/GOT architectures. Normally we would just put the
1937 // address of GOT into ebx and then call target@PLT. But for tail calls
1938 // ebx would be restored (since ebx is callee saved) before jumping to the
1939 // target@PLT.
1940
1941 // Note: The actual moving to ECX is done further down.
1942 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1943 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1944 !G->getGlobal()->hasProtectedVisibility())
1945 Callee = LowerGlobalAddress(Callee, DAG);
1946 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00001947 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001948 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001949 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001950
Gordon Henriksen86737662008-01-05 16:56:59 +00001951 if (Is64Bit && isVarArg) {
1952 // From AMD64 ABI document:
1953 // For calls that may call functions that use varargs or stdargs
1954 // (prototype-less calls or calls to functions containing ellipsis (...) in
1955 // the declaration) %al is used as hidden argument to specify the number
1956 // of SSE registers used. The contents of %al do not need to match exactly
1957 // the number of registers, but must be an ubound on the number of SSE
1958 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001959
1960 // FIXME: Verify this on Win64
Gordon Henriksen86737662008-01-05 16:56:59 +00001961 // Count the number of XMM registers allocated.
1962 static const unsigned XMMArgRegs[] = {
1963 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1964 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1965 };
1966 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Scott Michelfdc40a02009-02-17 22:15:04 +00001967 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00001968 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001969
Dale Johannesendd64c412009-02-04 00:33:20 +00001970 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00001971 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00001972 InFlag = Chain.getValue(1);
1973 }
1974
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001975
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001976 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001977 if (isTailCall) {
1978 // Force all the incoming stack arguments to be loaded from the stack
1979 // before any new outgoing arguments are stored to the stack, because the
1980 // outgoing stack slots may alias the incoming argument stack slots, and
1981 // the alias isn't otherwise explicit. This is slightly more conservative
1982 // than necessary, because it means that each store effectively depends
1983 // on every argument instead of just those arguments it would clobber.
1984 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
1985
Dan Gohman475871a2008-07-27 21:46:04 +00001986 SmallVector<SDValue, 8> MemOpChains2;
1987 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00001988 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001989 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00001990 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00001991 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00001992 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1993 CCValAssign &VA = ArgLocs[i];
1994 if (VA.isRegLoc())
1995 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001996 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001997 SDValue Arg = Outs[i].Val;
1998 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00001999 // Create frame index.
2000 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002001 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
David Greene3f2bf852009-11-12 20:49:22 +00002002 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true, false);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002003 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002004
Duncan Sands276dcbd2008-03-21 09:14:45 +00002005 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002006 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002007 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002008 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002009 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002010 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002011 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002012
Dan Gohman98ca4f22009-08-05 01:29:28 +00002013 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2014 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002015 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002016 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002017 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002018 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002019 DAG.getStore(ArgChain, dl, Arg, FIN,
David Greene67c9d422010-02-15 16:53:33 +00002020 PseudoSourceValue::getFixedStack(FI), 0,
2021 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002022 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002023 }
2024 }
2025
2026 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002027 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002028 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002029
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002030 // Copy arguments to their registers.
2031 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002032 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002033 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002034 InFlag = Chain.getValue(1);
2035 }
Dan Gohman475871a2008-07-27 21:46:04 +00002036 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002037
Gordon Henriksen86737662008-01-05 16:56:59 +00002038 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002039 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002040 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002041 }
2042
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002043 bool WasGlobalOrExternal = false;
2044 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2045 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2046 // In the 64-bit large code model, we have to make all calls
2047 // through a register, since the call instruction's 32-bit
2048 // pc-relative offset may not be large enough to hold the whole
2049 // address.
2050 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
2051 WasGlobalOrExternal = true;
2052 // If the callee is a GlobalAddress node (quite common, every direct call
2053 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2054 // it.
2055
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002056 // We should use extra load for direct calls to dllimported functions in
2057 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002058 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002059 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002060 unsigned char OpFlags = 0;
Eric Christopherfd179292009-08-27 18:07:15 +00002061
Chris Lattner48a7d022009-07-09 05:02:21 +00002062 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2063 // external symbols most go through the PLT in PIC mode. If the symbol
2064 // has hidden or protected visibility, or if it is static or local, then
2065 // we don't need to use the PLT - we can directly call it.
2066 if (Subtarget->isTargetELF() &&
2067 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002068 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002069 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002070 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002071 (GV->isDeclaration() || GV->isWeakForLinker()) &&
2072 Subtarget->getDarwinVers() < 9) {
2073 // PC-relative references to external symbols should go through $stub,
2074 // unless we're building with the leopard linker or later, which
2075 // automatically synthesizes these stubs.
2076 OpFlags = X86II::MO_DARWIN_STUB;
2077 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002078
Chris Lattner74e726e2009-07-09 05:27:35 +00002079 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002080 G->getOffset(), OpFlags);
2081 }
Bill Wendling056292f2008-09-16 21:48:12 +00002082 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002083 WasGlobalOrExternal = true;
Chris Lattner48a7d022009-07-09 05:02:21 +00002084 unsigned char OpFlags = 0;
2085
2086 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to external
2087 // symbols should go through the PLT.
2088 if (Subtarget->isTargetELF() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002089 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002090 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002091 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002092 Subtarget->getDarwinVers() < 9) {
2093 // PC-relative references to external symbols should go through $stub,
2094 // unless we're building with the leopard linker or later, which
2095 // automatically synthesizes these stubs.
2096 OpFlags = X86II::MO_DARWIN_STUB;
2097 }
Eric Christopherfd179292009-08-27 18:07:15 +00002098
Chris Lattner48a7d022009-07-09 05:02:21 +00002099 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2100 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002101 }
2102
Chris Lattnerd96d0722007-02-25 06:40:16 +00002103 // Returns a chain & a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +00002104 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00002105 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002106
Evan Chengf22f9b32010-02-06 03:28:46 +00002107 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002108 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2109 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002110 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002111 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002112
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002113 Ops.push_back(Chain);
2114 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002115
Dan Gohman98ca4f22009-08-05 01:29:28 +00002116 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002117 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002118
Gordon Henriksen86737662008-01-05 16:56:59 +00002119 // Add argument registers to the end of the list so that they are known live
2120 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002121 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2122 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2123 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002124
Evan Cheng586ccac2008-03-18 23:36:35 +00002125 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002126 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002127 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2128
2129 // Add an implicit use of AL for x86 vararg functions.
2130 if (Is64Bit && isVarArg)
Owen Anderson825b72b2009-08-11 20:47:22 +00002131 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002132
Gabor Greifba36cb52008-08-28 21:40:38 +00002133 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002134 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002135
Dan Gohman98ca4f22009-08-05 01:29:28 +00002136 if (isTailCall) {
2137 // If this is the first return lowered for this function, add the regs
2138 // to the liveout set for the function.
2139 if (MF.getRegInfo().liveout_empty()) {
2140 SmallVector<CCValAssign, 16> RVLocs;
2141 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
2142 *DAG.getContext());
2143 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
2144 for (unsigned i = 0; i != RVLocs.size(); ++i)
2145 if (RVLocs[i].isRegLoc())
2146 MF.getRegInfo().addLiveOut(RVLocs[i].getLocReg());
2147 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002148 return DAG.getNode(X86ISD::TC_RETURN, dl,
2149 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002150 }
2151
Dale Johannesenace16102009-02-03 19:33:06 +00002152 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002153 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002154
Chris Lattner2d297092006-05-23 18:50:38 +00002155 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002156 unsigned NumBytesForCalleeToPush;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002157 if (IsCalleePop(isVarArg, CallConv))
Gordon Henriksen86737662008-01-05 16:56:59 +00002158 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002159 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002160 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002161 // pops the hidden struct pointer, so we have to push it back.
2162 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002163 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002164 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002165 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002166
Gordon Henriksenae636f82008-01-03 16:47:34 +00002167 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002168 if (!IsSibcall) {
2169 Chain = DAG.getCALLSEQ_END(Chain,
2170 DAG.getIntPtrConstant(NumBytes, true),
2171 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2172 true),
2173 InFlag);
2174 InFlag = Chain.getValue(1);
2175 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002176
Chris Lattner3085e152007-02-25 08:59:22 +00002177 // Handle result values, copying them out of physregs into vregs that we
2178 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002179 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2180 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002181}
2182
Evan Cheng25ab6902006-09-08 06:48:29 +00002183
2184//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002185// Fast Calling Convention (tail call) implementation
2186//===----------------------------------------------------------------------===//
2187
2188// Like std call, callee cleans arguments, convention except that ECX is
2189// reserved for storing the tail called function address. Only 2 registers are
2190// free for argument passing (inreg). Tail call optimization is performed
2191// provided:
2192// * tailcallopt is enabled
2193// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002194// On X86_64 architecture with GOT-style position independent code only local
2195// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002196// To keep the stack aligned according to platform abi the function
2197// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2198// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002199// If a tail called function callee has more arguments than the caller the
2200// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002201// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002202// original REtADDR, but before the saved framepointer or the spilled registers
2203// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2204// stack layout:
2205// arg1
2206// arg2
2207// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002208// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002209// move area ]
2210// (possible EBP)
2211// ESI
2212// EDI
2213// local1 ..
2214
2215/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2216/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002217unsigned
2218X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2219 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002220 MachineFunction &MF = DAG.getMachineFunction();
2221 const TargetMachine &TM = MF.getTarget();
2222 const TargetFrameInfo &TFI = *TM.getFrameInfo();
2223 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002224 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002225 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002226 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002227 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2228 // Number smaller than 12 so just add the difference.
2229 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2230 } else {
2231 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002232 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002233 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002234 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002235 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002236}
2237
Evan Cheng5f941932010-02-05 02:21:12 +00002238/// MatchingStackOffset - Return true if the given stack call argument is
2239/// already available in the same position (relatively) of the caller's
2240/// incoming argument stack.
2241static
2242bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2243 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2244 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002245 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2246 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002247 if (Arg.getOpcode() == ISD::CopyFromReg) {
2248 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
2249 if (!VR || TargetRegisterInfo::isPhysicalRegister(VR))
2250 return false;
2251 MachineInstr *Def = MRI->getVRegDef(VR);
2252 if (!Def)
2253 return false;
2254 if (!Flags.isByVal()) {
2255 if (!TII->isLoadFromStackSlot(Def, FI))
2256 return false;
2257 } else {
2258 unsigned Opcode = Def->getOpcode();
2259 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2260 Def->getOperand(1).isFI()) {
2261 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002262 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002263 } else
2264 return false;
2265 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002266 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2267 if (Flags.isByVal())
2268 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002269 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002270 // define @foo(%struct.X* %A) {
2271 // tail call @bar(%struct.X* byval %A)
2272 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002273 return false;
2274 SDValue Ptr = Ld->getBasePtr();
2275 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2276 if (!FINode)
2277 return false;
2278 FI = FINode->getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002279 } else
2280 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002281
Evan Cheng4cae1332010-03-05 08:38:04 +00002282 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002283 if (!MFI->isFixedObjectIndex(FI))
2284 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002285 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002286}
2287
Dan Gohman98ca4f22009-08-05 01:29:28 +00002288/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2289/// for tail call optimization. Targets which want to do tail call
2290/// optimization should implement this function.
2291bool
2292X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002293 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002294 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002295 bool isCalleeStructRet,
2296 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002297 const SmallVectorImpl<ISD::OutputArg> &Outs,
2298 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002299 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002300 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002301 CalleeCC != CallingConv::C)
2302 return false;
2303
Evan Cheng7096ae42010-01-29 06:45:59 +00002304 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002305 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002306 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002307 CallingConv::ID CallerCC = CallerF->getCallingConv();
2308 bool CCMatch = CallerCC == CalleeCC;
2309
Dan Gohman1797ed52010-02-08 20:27:50 +00002310 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002311 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002312 return true;
2313 return false;
2314 }
2315
Evan Chengb2c92902010-02-02 02:22:50 +00002316 // Look for obvious safe cases to perform tail call optimization that does not
2317 // requite ABI changes. This is what gcc calls sibcall.
2318
Evan Cheng2c12cb42010-03-26 16:26:03 +00002319 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2320 // emit a special epilogue.
2321 if (RegInfo->needsStackRealignment(MF))
2322 return false;
2323
Evan Cheng3c262ee2010-03-26 02:13:13 +00002324 // Do not sibcall optimize vararg calls unless the call site is not passing any
2325 // arguments.
2326 if (isVarArg && !Outs.empty())
Evan Cheng843bd692010-01-31 06:44:49 +00002327 return false;
2328
Evan Chenga375d472010-03-15 18:54:48 +00002329 // Also avoid sibcall optimization if either caller or callee uses struct
2330 // return semantics.
2331 if (isCalleeStructRet || isCallerStructRet)
2332 return false;
2333
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002334 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2335 // Therefore if it's not used by the call it is not safe to optimize this into
2336 // a sibcall.
2337 bool Unused = false;
2338 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2339 if (!Ins[i].Used) {
2340 Unused = true;
2341 break;
2342 }
2343 }
2344 if (Unused) {
2345 SmallVector<CCValAssign, 16> RVLocs;
2346 CCState CCInfo(CalleeCC, false, getTargetMachine(),
2347 RVLocs, *DAG.getContext());
2348 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002349 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002350 CCValAssign &VA = RVLocs[i];
2351 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2352 return false;
2353 }
2354 }
2355
Evan Cheng13617962010-04-30 01:12:32 +00002356 // If the calling conventions do not match, then we'd better make sure the
2357 // results are returned in the same way as what the caller expects.
2358 if (!CCMatch) {
2359 SmallVector<CCValAssign, 16> RVLocs1;
2360 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
2361 RVLocs1, *DAG.getContext());
2362 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2363
2364 SmallVector<CCValAssign, 16> RVLocs2;
2365 CCState CCInfo2(CallerCC, false, getTargetMachine(),
2366 RVLocs2, *DAG.getContext());
2367 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2368
2369 if (RVLocs1.size() != RVLocs2.size())
2370 return false;
2371 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2372 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2373 return false;
2374 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2375 return false;
2376 if (RVLocs1[i].isRegLoc()) {
2377 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2378 return false;
2379 } else {
2380 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2381 return false;
2382 }
2383 }
2384 }
2385
Evan Chenga6bff982010-01-30 01:22:00 +00002386 // If the callee takes no arguments then go on to check the results of the
2387 // call.
2388 if (!Outs.empty()) {
2389 // Check if stack adjustment is needed. For now, do not do this if any
2390 // argument is passed on the stack.
2391 SmallVector<CCValAssign, 16> ArgLocs;
2392 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
2393 ArgLocs, *DAG.getContext());
2394 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CalleeCC));
Evan Chengb2c92902010-02-02 02:22:50 +00002395 if (CCInfo.getNextStackOffset()) {
2396 MachineFunction &MF = DAG.getMachineFunction();
2397 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2398 return false;
2399 if (Subtarget->isTargetWin64())
2400 // Win64 ABI has additional complications.
2401 return false;
2402
2403 // Check if the arguments are already laid out in the right way as
2404 // the caller's fixed stack objects.
2405 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002406 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2407 const X86InstrInfo *TII =
2408 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002409 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2410 CCValAssign &VA = ArgLocs[i];
2411 EVT RegVT = VA.getLocVT();
2412 SDValue Arg = Outs[i].Val;
2413 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002414 if (VA.getLocInfo() == CCValAssign::Indirect)
2415 return false;
2416 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002417 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2418 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002419 return false;
2420 }
2421 }
2422 }
Evan Chenga6bff982010-01-30 01:22:00 +00002423 }
Evan Chengb1712452010-01-27 06:25:16 +00002424
Evan Cheng86809cc2010-02-03 03:28:02 +00002425 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002426}
2427
Dan Gohman3df24e62008-09-03 23:12:08 +00002428FastISel *
Chris Lattnered3a8062010-04-05 06:05:26 +00002429X86TargetLowering::createFastISel(MachineFunction &mf,
Evan Chengddc419c2010-01-26 19:04:47 +00002430 DenseMap<const Value *, unsigned> &vm,
2431 DenseMap<const BasicBlock*, MachineBasicBlock*> &bm,
Dan Gohmanf81eca02010-04-22 20:46:50 +00002432 DenseMap<const AllocaInst *, int> &am,
2433 std::vector<std::pair<MachineInstr*, unsigned> > &pn
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002434#ifndef NDEBUG
Dan Gohman25208642010-04-14 19:53:31 +00002435 , SmallSet<const Instruction *, 8> &cil
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002436#endif
Dan Gohmand858e902010-04-17 15:26:15 +00002437 ) const {
Dan Gohmanf81eca02010-04-22 20:46:50 +00002438 return X86::createFastISel(mf, vm, bm, am, pn
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002439#ifndef NDEBUG
2440 , cil
2441#endif
2442 );
Dan Gohmand9f3c482008-08-19 21:32:53 +00002443}
2444
2445
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002446//===----------------------------------------------------------------------===//
2447// Other Lowering Hooks
2448//===----------------------------------------------------------------------===//
2449
2450
Dan Gohmand858e902010-04-17 15:26:15 +00002451SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002452 MachineFunction &MF = DAG.getMachineFunction();
2453 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2454 int ReturnAddrIndex = FuncInfo->getRAIndex();
2455
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002456 if (ReturnAddrIndex == 0) {
2457 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002458 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002459 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Arnold Schwaighofer92652752010-02-22 16:18:09 +00002460 false, false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002461 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002462 }
2463
Evan Cheng25ab6902006-09-08 06:48:29 +00002464 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002465}
2466
2467
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002468bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2469 bool hasSymbolicDisplacement) {
2470 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002471 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002472 return false;
2473
2474 // If we don't have a symbolic displacement - we don't have any extra
2475 // restrictions.
2476 if (!hasSymbolicDisplacement)
2477 return true;
2478
2479 // FIXME: Some tweaks might be needed for medium code model.
2480 if (M != CodeModel::Small && M != CodeModel::Kernel)
2481 return false;
2482
2483 // For small code model we assume that latest object is 16MB before end of 31
2484 // bits boundary. We may also accept pretty large negative constants knowing
2485 // that all objects are in the positive half of address space.
2486 if (M == CodeModel::Small && Offset < 16*1024*1024)
2487 return true;
2488
2489 // For kernel code model we know that all object resist in the negative half
2490 // of 32bits address space. We may not accept negative offsets, since they may
2491 // be just off and we may accept pretty large positive ones.
2492 if (M == CodeModel::Kernel && Offset > 0)
2493 return true;
2494
2495 return false;
2496}
2497
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002498/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2499/// specific condition code, returning the condition code and the LHS/RHS of the
2500/// comparison to make.
2501static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2502 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002503 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002504 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2505 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2506 // X > -1 -> X == 0, jump !sign.
2507 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002508 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002509 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2510 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002511 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002512 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002513 // X < 1 -> X <= 0
2514 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002515 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002516 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002517 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002518
Evan Chengd9558e02006-01-06 00:43:03 +00002519 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002520 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002521 case ISD::SETEQ: return X86::COND_E;
2522 case ISD::SETGT: return X86::COND_G;
2523 case ISD::SETGE: return X86::COND_GE;
2524 case ISD::SETLT: return X86::COND_L;
2525 case ISD::SETLE: return X86::COND_LE;
2526 case ISD::SETNE: return X86::COND_NE;
2527 case ISD::SETULT: return X86::COND_B;
2528 case ISD::SETUGT: return X86::COND_A;
2529 case ISD::SETULE: return X86::COND_BE;
2530 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002531 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002532 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002533
Chris Lattner4c78e022008-12-23 23:42:27 +00002534 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002535
Chris Lattner4c78e022008-12-23 23:42:27 +00002536 // If LHS is a foldable load, but RHS is not, flip the condition.
2537 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2538 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2539 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2540 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002541 }
2542
Chris Lattner4c78e022008-12-23 23:42:27 +00002543 switch (SetCCOpcode) {
2544 default: break;
2545 case ISD::SETOLT:
2546 case ISD::SETOLE:
2547 case ISD::SETUGT:
2548 case ISD::SETUGE:
2549 std::swap(LHS, RHS);
2550 break;
2551 }
2552
2553 // On a floating point condition, the flags are set as follows:
2554 // ZF PF CF op
2555 // 0 | 0 | 0 | X > Y
2556 // 0 | 0 | 1 | X < Y
2557 // 1 | 0 | 0 | X == Y
2558 // 1 | 1 | 1 | unordered
2559 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002560 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002561 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002562 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002563 case ISD::SETOLT: // flipped
2564 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002565 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002566 case ISD::SETOLE: // flipped
2567 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002568 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002569 case ISD::SETUGT: // flipped
2570 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002571 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002572 case ISD::SETUGE: // flipped
2573 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002574 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002575 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002576 case ISD::SETNE: return X86::COND_NE;
2577 case ISD::SETUO: return X86::COND_P;
2578 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002579 case ISD::SETOEQ:
2580 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002581 }
Evan Chengd9558e02006-01-06 00:43:03 +00002582}
2583
Evan Cheng4a460802006-01-11 00:33:36 +00002584/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2585/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002586/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002587static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002588 switch (X86CC) {
2589 default:
2590 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002591 case X86::COND_B:
2592 case X86::COND_BE:
2593 case X86::COND_E:
2594 case X86::COND_P:
2595 case X86::COND_A:
2596 case X86::COND_AE:
2597 case X86::COND_NE:
2598 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002599 return true;
2600 }
2601}
2602
Evan Chengeb2f9692009-10-27 19:56:55 +00002603/// isFPImmLegal - Returns true if the target can instruction select the
2604/// specified FP immediate natively. If false, the legalizer will
2605/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002606bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00002607 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
2608 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
2609 return true;
2610 }
2611 return false;
2612}
2613
Nate Begeman9008ca62009-04-27 18:41:29 +00002614/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2615/// the specified range (L, H].
2616static bool isUndefOrInRange(int Val, int Low, int Hi) {
2617 return (Val < 0) || (Val >= Low && Val < Hi);
2618}
2619
2620/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2621/// specified value.
2622static bool isUndefOrEqual(int Val, int CmpVal) {
2623 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002624 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002625 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002626}
2627
Nate Begeman9008ca62009-04-27 18:41:29 +00002628/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2629/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2630/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00002631static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002632 if (VT == MVT::v4f32 || VT == MVT::v4i32 || VT == MVT::v4i16)
Nate Begeman9008ca62009-04-27 18:41:29 +00002633 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002634 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00002635 return (Mask[0] < 2 && Mask[1] < 2);
2636 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002637}
2638
Nate Begeman9008ca62009-04-27 18:41:29 +00002639bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002640 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002641 N->getMask(M);
2642 return ::isPSHUFDMask(M, N->getValueType(0));
2643}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002644
Nate Begeman9008ca62009-04-27 18:41:29 +00002645/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2646/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00002647static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002648 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002649 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002650
Nate Begeman9008ca62009-04-27 18:41:29 +00002651 // Lower quadword copied in order or undef.
2652 for (int i = 0; i != 4; ++i)
2653 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002654 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002655
Evan Cheng506d3df2006-03-29 23:07:14 +00002656 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002657 for (int i = 4; i != 8; ++i)
2658 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002659 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002660
Evan Cheng506d3df2006-03-29 23:07:14 +00002661 return true;
2662}
2663
Nate Begeman9008ca62009-04-27 18:41:29 +00002664bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002665 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002666 N->getMask(M);
2667 return ::isPSHUFHWMask(M, N->getValueType(0));
2668}
Evan Cheng506d3df2006-03-29 23:07:14 +00002669
Nate Begeman9008ca62009-04-27 18:41:29 +00002670/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2671/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00002672static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002673 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002674 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002675
Rafael Espindola15684b22009-04-24 12:40:33 +00002676 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00002677 for (int i = 4; i != 8; ++i)
2678 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00002679 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002680
Rafael Espindola15684b22009-04-24 12:40:33 +00002681 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002682 for (int i = 0; i != 4; ++i)
2683 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00002684 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002685
Rafael Espindola15684b22009-04-24 12:40:33 +00002686 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002687}
2688
Nate Begeman9008ca62009-04-27 18:41:29 +00002689bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002690 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002691 N->getMask(M);
2692 return ::isPSHUFLWMask(M, N->getValueType(0));
2693}
2694
Nate Begemana09008b2009-10-19 02:17:23 +00002695/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
2696/// is suitable for input to PALIGNR.
2697static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
2698 bool hasSSSE3) {
2699 int i, e = VT.getVectorNumElements();
2700
2701 // Do not handle v2i64 / v2f64 shuffles with palignr.
2702 if (e < 4 || !hasSSSE3)
2703 return false;
2704
2705 for (i = 0; i != e; ++i)
2706 if (Mask[i] >= 0)
2707 break;
2708
2709 // All undef, not a palignr.
2710 if (i == e)
2711 return false;
2712
2713 // Determine if it's ok to perform a palignr with only the LHS, since we
2714 // don't have access to the actual shuffle elements to see if RHS is undef.
2715 bool Unary = Mask[i] < (int)e;
2716 bool NeedsUnary = false;
2717
2718 int s = Mask[i] - i;
2719
2720 // Check the rest of the elements to see if they are consecutive.
2721 for (++i; i != e; ++i) {
2722 int m = Mask[i];
2723 if (m < 0)
2724 continue;
2725
2726 Unary = Unary && (m < (int)e);
2727 NeedsUnary = NeedsUnary || (m < s);
2728
2729 if (NeedsUnary && !Unary)
2730 return false;
2731 if (Unary && m != ((s+i) & (e-1)))
2732 return false;
2733 if (!Unary && m != (s+i))
2734 return false;
2735 }
2736 return true;
2737}
2738
2739bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
2740 SmallVector<int, 8> M;
2741 N->getMask(M);
2742 return ::isPALIGNRMask(M, N->getValueType(0), true);
2743}
2744
Evan Cheng14aed5e2006-03-24 01:18:28 +00002745/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2746/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00002747static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002748 int NumElems = VT.getVectorNumElements();
2749 if (NumElems != 2 && NumElems != 4)
2750 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002751
Nate Begeman9008ca62009-04-27 18:41:29 +00002752 int Half = NumElems / 2;
2753 for (int i = 0; i < Half; ++i)
2754 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002755 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002756 for (int i = Half; i < NumElems; ++i)
2757 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002758 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002759
Evan Cheng14aed5e2006-03-24 01:18:28 +00002760 return true;
2761}
2762
Nate Begeman9008ca62009-04-27 18:41:29 +00002763bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
2764 SmallVector<int, 8> M;
2765 N->getMask(M);
2766 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002767}
2768
Evan Cheng213d2cf2007-05-17 18:45:50 +00002769/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002770/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2771/// half elements to come from vector 1 (which would equal the dest.) and
2772/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00002773static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002774 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002775
2776 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00002777 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002778
Nate Begeman9008ca62009-04-27 18:41:29 +00002779 int Half = NumElems / 2;
2780 for (int i = 0; i < Half; ++i)
2781 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002782 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002783 for (int i = Half; i < NumElems; ++i)
2784 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002785 return false;
2786 return true;
2787}
2788
Nate Begeman9008ca62009-04-27 18:41:29 +00002789static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
2790 SmallVector<int, 8> M;
2791 N->getMask(M);
2792 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002793}
2794
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002795/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2796/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002797bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
2798 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002799 return false;
2800
Evan Cheng2064a2b2006-03-28 06:50:32 +00002801 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00002802 return isUndefOrEqual(N->getMaskElt(0), 6) &&
2803 isUndefOrEqual(N->getMaskElt(1), 7) &&
2804 isUndefOrEqual(N->getMaskElt(2), 2) &&
2805 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002806}
2807
Nate Begeman0b10b912009-11-07 23:17:15 +00002808/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2809/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2810/// <2, 3, 2, 3>
2811bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
2812 unsigned NumElems = N->getValueType(0).getVectorNumElements();
2813
2814 if (NumElems != 4)
2815 return false;
2816
2817 return isUndefOrEqual(N->getMaskElt(0), 2) &&
2818 isUndefOrEqual(N->getMaskElt(1), 3) &&
2819 isUndefOrEqual(N->getMaskElt(2), 2) &&
2820 isUndefOrEqual(N->getMaskElt(3), 3);
2821}
2822
Evan Cheng5ced1d82006-04-06 23:23:56 +00002823/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2824/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00002825bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
2826 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002827
Evan Cheng5ced1d82006-04-06 23:23:56 +00002828 if (NumElems != 2 && NumElems != 4)
2829 return false;
2830
Evan Chengc5cdff22006-04-07 21:53:05 +00002831 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002832 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002833 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002834
Evan Chengc5cdff22006-04-07 21:53:05 +00002835 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002836 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002837 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002838
2839 return true;
2840}
2841
Nate Begeman0b10b912009-11-07 23:17:15 +00002842/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
2843/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
2844bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002845 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002846
Evan Cheng5ced1d82006-04-06 23:23:56 +00002847 if (NumElems != 2 && NumElems != 4)
2848 return false;
2849
Evan Chengc5cdff22006-04-07 21:53:05 +00002850 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002851 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002852 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002853
Nate Begeman9008ca62009-04-27 18:41:29 +00002854 for (unsigned i = 0; i < NumElems/2; ++i)
2855 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002856 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002857
2858 return true;
2859}
2860
Evan Cheng0038e592006-03-28 00:39:58 +00002861/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2862/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00002863static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002864 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002865 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002866 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00002867 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002868
Nate Begeman9008ca62009-04-27 18:41:29 +00002869 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2870 int BitI = Mask[i];
2871 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002872 if (!isUndefOrEqual(BitI, j))
2873 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002874 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002875 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002876 return false;
2877 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002878 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002879 return false;
2880 }
Evan Cheng0038e592006-03-28 00:39:58 +00002881 }
Evan Cheng0038e592006-03-28 00:39:58 +00002882 return true;
2883}
2884
Nate Begeman9008ca62009-04-27 18:41:29 +00002885bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2886 SmallVector<int, 8> M;
2887 N->getMask(M);
2888 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002889}
2890
Evan Cheng4fcb9222006-03-28 02:43:26 +00002891/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2892/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00002893static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002894 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002895 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002896 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00002897 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002898
Nate Begeman9008ca62009-04-27 18:41:29 +00002899 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2900 int BitI = Mask[i];
2901 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00002902 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00002903 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002904 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002905 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002906 return false;
2907 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002908 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002909 return false;
2910 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002911 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002912 return true;
2913}
2914
Nate Begeman9008ca62009-04-27 18:41:29 +00002915bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2916 SmallVector<int, 8> M;
2917 N->getMask(M);
2918 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002919}
2920
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002921/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2922/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2923/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00002924static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002925 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002926 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002927 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002928
Nate Begeman9008ca62009-04-27 18:41:29 +00002929 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
2930 int BitI = Mask[i];
2931 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002932 if (!isUndefOrEqual(BitI, j))
2933 return false;
2934 if (!isUndefOrEqual(BitI1, j))
2935 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002936 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002937 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002938}
2939
Nate Begeman9008ca62009-04-27 18:41:29 +00002940bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
2941 SmallVector<int, 8> M;
2942 N->getMask(M);
2943 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
2944}
2945
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002946/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2947/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2948/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00002949static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002950 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002951 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2952 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002953
Nate Begeman9008ca62009-04-27 18:41:29 +00002954 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2955 int BitI = Mask[i];
2956 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002957 if (!isUndefOrEqual(BitI, j))
2958 return false;
2959 if (!isUndefOrEqual(BitI1, j))
2960 return false;
2961 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002962 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002963}
2964
Nate Begeman9008ca62009-04-27 18:41:29 +00002965bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
2966 SmallVector<int, 8> M;
2967 N->getMask(M);
2968 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
2969}
2970
Evan Cheng017dcc62006-04-21 01:05:10 +00002971/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2972/// specifies a shuffle of elements that is suitable for input to MOVSS,
2973/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00002974static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00002975 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002976 return false;
Eli Friedman10415532009-06-06 06:05:10 +00002977
2978 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002979
Nate Begeman9008ca62009-04-27 18:41:29 +00002980 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002981 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002982
Nate Begeman9008ca62009-04-27 18:41:29 +00002983 for (int i = 1; i < NumElts; ++i)
2984 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002985 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002986
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002987 return true;
2988}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002989
Nate Begeman9008ca62009-04-27 18:41:29 +00002990bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
2991 SmallVector<int, 8> M;
2992 N->getMask(M);
2993 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002994}
2995
Evan Cheng017dcc62006-04-21 01:05:10 +00002996/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2997/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00002998/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00002999static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003000 bool V2IsSplat = false, bool V2IsUndef = false) {
3001 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003002 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003003 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003004
Nate Begeman9008ca62009-04-27 18:41:29 +00003005 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003006 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003007
Nate Begeman9008ca62009-04-27 18:41:29 +00003008 for (int i = 1; i < NumOps; ++i)
3009 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3010 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3011 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003012 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003013
Evan Cheng39623da2006-04-20 08:58:49 +00003014 return true;
3015}
3016
Nate Begeman9008ca62009-04-27 18:41:29 +00003017static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003018 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003019 SmallVector<int, 8> M;
3020 N->getMask(M);
3021 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003022}
3023
Evan Chengd9539472006-04-14 21:59:03 +00003024/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3025/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003026bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
3027 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003028 return false;
3029
3030 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00003031 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003032 int Elt = N->getMaskElt(i);
3033 if (Elt >= 0 && Elt != 1)
3034 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00003035 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003036
3037 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003038 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003039 int Elt = N->getMaskElt(i);
3040 if (Elt >= 0 && Elt != 3)
3041 return false;
3042 if (Elt == 3)
3043 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003044 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003045 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00003046 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003047 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003048}
3049
3050/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3051/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003052bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
3053 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003054 return false;
3055
3056 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00003057 for (unsigned i = 0; i < 2; ++i)
3058 if (N->getMaskElt(i) > 0)
3059 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003060
3061 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003062 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003063 int Elt = N->getMaskElt(i);
3064 if (Elt >= 0 && Elt != 2)
3065 return false;
3066 if (Elt == 2)
3067 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003068 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003069 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003070 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003071}
3072
Evan Cheng0b457f02008-09-25 20:50:48 +00003073/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3074/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003075bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3076 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003077
Nate Begeman9008ca62009-04-27 18:41:29 +00003078 for (int i = 0; i < e; ++i)
3079 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003080 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003081 for (int i = 0; i < e; ++i)
3082 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003083 return false;
3084 return true;
3085}
3086
Evan Cheng63d33002006-03-22 08:01:21 +00003087/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003088/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003089unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003090 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3091 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3092
Evan Chengb9df0ca2006-03-22 02:53:00 +00003093 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3094 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003095 for (int i = 0; i < NumOperands; ++i) {
3096 int Val = SVOp->getMaskElt(NumOperands-i-1);
3097 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003098 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003099 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003100 if (i != NumOperands - 1)
3101 Mask <<= Shift;
3102 }
Evan Cheng63d33002006-03-22 08:01:21 +00003103 return Mask;
3104}
3105
Evan Cheng506d3df2006-03-29 23:07:14 +00003106/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003107/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003108unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003109 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003110 unsigned Mask = 0;
3111 // 8 nodes, but we only care about the last 4.
3112 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003113 int Val = SVOp->getMaskElt(i);
3114 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003115 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003116 if (i != 4)
3117 Mask <<= 2;
3118 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003119 return Mask;
3120}
3121
3122/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003123/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003124unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003125 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003126 unsigned Mask = 0;
3127 // 8 nodes, but we only care about the first 4.
3128 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003129 int Val = SVOp->getMaskElt(i);
3130 if (Val >= 0)
3131 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003132 if (i != 0)
3133 Mask <<= 2;
3134 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003135 return Mask;
3136}
3137
Nate Begemana09008b2009-10-19 02:17:23 +00003138/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3139/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3140unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3141 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3142 EVT VVT = N->getValueType(0);
3143 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3144 int Val = 0;
3145
3146 unsigned i, e;
3147 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3148 Val = SVOp->getMaskElt(i);
3149 if (Val >= 0)
3150 break;
3151 }
3152 return (Val - i) * EltSize;
3153}
3154
Evan Cheng37b73872009-07-30 08:33:02 +00003155/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3156/// constant +0.0.
3157bool X86::isZeroNode(SDValue Elt) {
3158 return ((isa<ConstantSDNode>(Elt) &&
3159 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
3160 (isa<ConstantFPSDNode>(Elt) &&
3161 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3162}
3163
Nate Begeman9008ca62009-04-27 18:41:29 +00003164/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3165/// their permute mask.
3166static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3167 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003168 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003169 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003170 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003171
Nate Begeman5a5ca152009-04-29 05:20:52 +00003172 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003173 int idx = SVOp->getMaskElt(i);
3174 if (idx < 0)
3175 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003176 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003177 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003178 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003179 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003180 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003181 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3182 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003183}
3184
Evan Cheng779ccea2007-12-07 21:30:01 +00003185/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3186/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003187static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003188 unsigned NumElems = VT.getVectorNumElements();
3189 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003190 int idx = Mask[i];
3191 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003192 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003193 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003194 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003195 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003196 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003197 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003198}
3199
Evan Cheng533a0aa2006-04-19 20:35:22 +00003200/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3201/// match movhlps. The lower half elements should come from upper half of
3202/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003203/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003204static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3205 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003206 return false;
3207 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003208 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003209 return false;
3210 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003211 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003212 return false;
3213 return true;
3214}
3215
Evan Cheng5ced1d82006-04-06 23:23:56 +00003216/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003217/// is promoted to a vector. It also returns the LoadSDNode by reference if
3218/// required.
3219static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003220 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3221 return false;
3222 N = N->getOperand(0).getNode();
3223 if (!ISD::isNON_EXTLoad(N))
3224 return false;
3225 if (LD)
3226 *LD = cast<LoadSDNode>(N);
3227 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003228}
3229
Evan Cheng533a0aa2006-04-19 20:35:22 +00003230/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3231/// match movlp{s|d}. The lower half elements should come from lower half of
3232/// V1 (and in order), and the upper half elements should come from the upper
3233/// half of V2 (and in order). And since V1 will become the source of the
3234/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003235static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3236 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003237 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003238 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003239 // Is V2 is a vector load, don't do this transformation. We will try to use
3240 // load folding shufps op.
3241 if (ISD::isNON_EXTLoad(V2))
3242 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003243
Nate Begeman5a5ca152009-04-29 05:20:52 +00003244 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003245
Evan Cheng533a0aa2006-04-19 20:35:22 +00003246 if (NumElems != 2 && NumElems != 4)
3247 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003248 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003249 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003250 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003251 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003252 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003253 return false;
3254 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003255}
3256
Evan Cheng39623da2006-04-20 08:58:49 +00003257/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3258/// all the same.
3259static bool isSplatVector(SDNode *N) {
3260 if (N->getOpcode() != ISD::BUILD_VECTOR)
3261 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003262
Dan Gohman475871a2008-07-27 21:46:04 +00003263 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003264 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3265 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003266 return false;
3267 return true;
3268}
3269
Evan Cheng213d2cf2007-05-17 18:45:50 +00003270/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003271/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003272/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003273static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003274 SDValue V1 = N->getOperand(0);
3275 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003276 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3277 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003278 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003279 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003280 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003281 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3282 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003283 if (Opc != ISD::BUILD_VECTOR ||
3284 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003285 return false;
3286 } else if (Idx >= 0) {
3287 unsigned Opc = V1.getOpcode();
3288 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3289 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003290 if (Opc != ISD::BUILD_VECTOR ||
3291 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003292 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003293 }
3294 }
3295 return true;
3296}
3297
3298/// getZeroVector - Returns a vector of specified type with all zero elements.
3299///
Owen Andersone50ed302009-08-10 22:56:29 +00003300static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003301 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003302 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003303
Chris Lattner8a594482007-11-25 00:24:49 +00003304 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3305 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003306 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003307 if (VT.getSizeInBits() == 64) { // MMX
Owen Anderson825b72b2009-08-11 20:47:22 +00003308 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3309 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003310 } else if (HasSSE2) { // SSE2
Owen Anderson825b72b2009-08-11 20:47:22 +00003311 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3312 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003313 } else { // SSE1
Owen Anderson825b72b2009-08-11 20:47:22 +00003314 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3315 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003316 }
Dale Johannesenace16102009-02-03 19:33:06 +00003317 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003318}
3319
Chris Lattner8a594482007-11-25 00:24:49 +00003320/// getOnesVector - Returns a vector of specified type with all bits set.
3321///
Owen Andersone50ed302009-08-10 22:56:29 +00003322static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003323 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003324
Chris Lattner8a594482007-11-25 00:24:49 +00003325 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3326 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003327 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003328 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003329 if (VT.getSizeInBits() == 64) // MMX
Owen Anderson825b72b2009-08-11 20:47:22 +00003330 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Chris Lattner8a594482007-11-25 00:24:49 +00003331 else // SSE
Owen Anderson825b72b2009-08-11 20:47:22 +00003332 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Dale Johannesenace16102009-02-03 19:33:06 +00003333 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003334}
3335
3336
Evan Cheng39623da2006-04-20 08:58:49 +00003337/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3338/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003339static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003340 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003341 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003342
Evan Cheng39623da2006-04-20 08:58:49 +00003343 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003344 SmallVector<int, 8> MaskVec;
3345 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003346
Nate Begeman5a5ca152009-04-29 05:20:52 +00003347 for (unsigned i = 0; i != NumElems; ++i) {
3348 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003349 MaskVec[i] = NumElems;
3350 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003351 }
Evan Cheng39623da2006-04-20 08:58:49 +00003352 }
Evan Cheng39623da2006-04-20 08:58:49 +00003353 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003354 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3355 SVOp->getOperand(1), &MaskVec[0]);
3356 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003357}
3358
Evan Cheng017dcc62006-04-21 01:05:10 +00003359/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3360/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003361static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003362 SDValue V2) {
3363 unsigned NumElems = VT.getVectorNumElements();
3364 SmallVector<int, 8> Mask;
3365 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003366 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003367 Mask.push_back(i);
3368 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003369}
3370
Nate Begeman9008ca62009-04-27 18:41:29 +00003371/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003372static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003373 SDValue V2) {
3374 unsigned NumElems = VT.getVectorNumElements();
3375 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003376 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003377 Mask.push_back(i);
3378 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003379 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003380 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003381}
3382
Nate Begeman9008ca62009-04-27 18:41:29 +00003383/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003384static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003385 SDValue V2) {
3386 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003387 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003388 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003389 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003390 Mask.push_back(i + Half);
3391 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003392 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003393 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003394}
3395
Evan Cheng0c0f83f2008-04-05 00:30:36 +00003396/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Eric Christopherfd179292009-08-27 18:07:15 +00003397static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00003398 bool HasSSE2) {
3399 if (SV->getValueType(0).getVectorNumElements() <= 4)
3400 return SDValue(SV, 0);
Eric Christopherfd179292009-08-27 18:07:15 +00003401
Owen Anderson825b72b2009-08-11 20:47:22 +00003402 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003403 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003404 DebugLoc dl = SV->getDebugLoc();
3405 SDValue V1 = SV->getOperand(0);
3406 int NumElems = VT.getVectorNumElements();
3407 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003408
Nate Begeman9008ca62009-04-27 18:41:29 +00003409 // unpack elements to the correct location
3410 while (NumElems > 4) {
3411 if (EltNo < NumElems/2) {
3412 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3413 } else {
3414 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3415 EltNo -= NumElems/2;
3416 }
3417 NumElems >>= 1;
3418 }
Eric Christopherfd179292009-08-27 18:07:15 +00003419
Nate Begeman9008ca62009-04-27 18:41:29 +00003420 // Perform the splat.
3421 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Dale Johannesenace16102009-02-03 19:33:06 +00003422 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003423 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
3424 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003425}
3426
Evan Chengba05f722006-04-21 23:03:30 +00003427/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003428/// vector of zero or undef vector. This produces a shuffle where the low
3429/// element of V2 is swizzled into the zero/undef vector, landing at element
3430/// 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 +00003431static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003432 bool isZero, bool HasSSE2,
3433 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003434 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003435 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003436 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3437 unsigned NumElems = VT.getVectorNumElements();
3438 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003439 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003440 // If this is the insertion idx, put the low elt of V2 here.
3441 MaskVec.push_back(i == Idx ? NumElems : i);
3442 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003443}
3444
Evan Chengf26ffe92008-05-29 08:22:04 +00003445/// getNumOfConsecutiveZeros - Return the number of elements in a result of
3446/// a shuffle that is zero.
3447static
Nate Begeman9008ca62009-04-27 18:41:29 +00003448unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, int NumElems,
3449 bool Low, SelectionDAG &DAG) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003450 unsigned NumZeros = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003451 for (int i = 0; i < NumElems; ++i) {
Evan Chengab262272008-06-25 20:52:59 +00003452 unsigned Index = Low ? i : NumElems-i-1;
Nate Begeman9008ca62009-04-27 18:41:29 +00003453 int Idx = SVOp->getMaskElt(Index);
3454 if (Idx < 0) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003455 ++NumZeros;
3456 continue;
3457 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003458 SDValue Elt = DAG.getShuffleScalarElt(SVOp, Index);
Evan Cheng37b73872009-07-30 08:33:02 +00003459 if (Elt.getNode() && X86::isZeroNode(Elt))
Evan Chengf26ffe92008-05-29 08:22:04 +00003460 ++NumZeros;
3461 else
3462 break;
3463 }
3464 return NumZeros;
3465}
3466
3467/// isVectorShift - Returns true if the shuffle can be implemented as a
3468/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003469/// FIXME: split into pslldqi, psrldqi, palignr variants.
3470static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00003471 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
John McCallb1fb4492010-04-07 01:49:15 +00003472 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
Evan Chengf26ffe92008-05-29 08:22:04 +00003473
3474 isLeft = true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003475 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, true, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003476 if (!NumZeros) {
3477 isLeft = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003478 NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, false, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003479 if (!NumZeros)
3480 return false;
3481 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003482 bool SeenV1 = false;
3483 bool SeenV2 = false;
John McCallb1fb4492010-04-07 01:49:15 +00003484 for (unsigned i = NumZeros; i < NumElems; ++i) {
3485 unsigned Val = isLeft ? (i - NumZeros) : i;
3486 int Idx_ = SVOp->getMaskElt(isLeft ? i : (i - NumZeros));
3487 if (Idx_ < 0)
Evan Chengf26ffe92008-05-29 08:22:04 +00003488 continue;
John McCallb1fb4492010-04-07 01:49:15 +00003489 unsigned Idx = (unsigned) Idx_;
Nate Begeman9008ca62009-04-27 18:41:29 +00003490 if (Idx < NumElems)
Evan Chengf26ffe92008-05-29 08:22:04 +00003491 SeenV1 = true;
3492 else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003493 Idx -= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00003494 SeenV2 = true;
3495 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003496 if (Idx != Val)
Evan Chengf26ffe92008-05-29 08:22:04 +00003497 return false;
3498 }
3499 if (SeenV1 && SeenV2)
3500 return false;
3501
Nate Begeman9008ca62009-04-27 18:41:29 +00003502 ShVal = SeenV1 ? SVOp->getOperand(0) : SVOp->getOperand(1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003503 ShAmt = NumZeros;
3504 return true;
3505}
3506
3507
Evan Chengc78d3b42006-04-24 18:01:45 +00003508/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3509///
Dan Gohman475871a2008-07-27 21:46:04 +00003510static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003511 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00003512 SelectionDAG &DAG,
3513 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003514 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00003515 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003516
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003517 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003518 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003519 bool First = true;
3520 for (unsigned i = 0; i < 16; ++i) {
3521 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3522 if (ThisIsNonZero && First) {
3523 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003524 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003525 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003526 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003527 First = false;
3528 }
3529
3530 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00003531 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003532 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3533 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003534 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003535 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00003536 }
3537 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003538 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
3539 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
3540 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00003541 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003542 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00003543 } else
3544 ThisElt = LastElt;
3545
Gabor Greifba36cb52008-08-28 21:40:38 +00003546 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00003547 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00003548 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00003549 }
3550 }
3551
Owen Anderson825b72b2009-08-11 20:47:22 +00003552 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00003553}
3554
Bill Wendlinga348c562007-03-22 18:42:45 +00003555/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003556///
Dan Gohman475871a2008-07-27 21:46:04 +00003557static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00003558 unsigned NumNonZero, unsigned NumZero,
3559 SelectionDAG &DAG,
3560 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003561 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00003562 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003563
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003564 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003565 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003566 bool First = true;
3567 for (unsigned i = 0; i < 8; ++i) {
3568 bool isNonZero = (NonZeros & (1 << i)) != 0;
3569 if (isNonZero) {
3570 if (First) {
3571 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003572 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003573 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003574 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003575 First = false;
3576 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003577 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003578 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00003579 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00003580 }
3581 }
3582
3583 return V;
3584}
3585
Evan Chengf26ffe92008-05-29 08:22:04 +00003586/// getVShift - Return a vector logical shift node.
3587///
Owen Andersone50ed302009-08-10 22:56:29 +00003588static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00003589 unsigned NumBits, SelectionDAG &DAG,
3590 const TargetLowering &TLI, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003591 bool isMMX = VT.getSizeInBits() == 64;
Owen Anderson825b72b2009-08-11 20:47:22 +00003592 EVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00003593 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Dale Johannesenace16102009-02-03 19:33:06 +00003594 SrcOp = DAG.getNode(ISD::BIT_CONVERT, dl, ShVT, SrcOp);
3595 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3596 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00003597 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00003598}
3599
Dan Gohman475871a2008-07-27 21:46:04 +00003600SDValue
Evan Chengc3630942009-12-09 21:00:30 +00003601X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00003602 SelectionDAG &DAG) const {
Evan Chengc3630942009-12-09 21:00:30 +00003603
3604 // Check if the scalar load can be widened into a vector load. And if
3605 // the address is "base + cst" see if the cst can be "absorbed" into
3606 // the shuffle mask.
3607 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
3608 SDValue Ptr = LD->getBasePtr();
3609 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
3610 return SDValue();
3611 EVT PVT = LD->getValueType(0);
3612 if (PVT != MVT::i32 && PVT != MVT::f32)
3613 return SDValue();
3614
3615 int FI = -1;
3616 int64_t Offset = 0;
3617 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
3618 FI = FINode->getIndex();
3619 Offset = 0;
3620 } else if (Ptr.getOpcode() == ISD::ADD &&
3621 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
3622 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
3623 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
3624 Offset = Ptr.getConstantOperandVal(1);
3625 Ptr = Ptr.getOperand(0);
3626 } else {
3627 return SDValue();
3628 }
3629
3630 SDValue Chain = LD->getChain();
3631 // Make sure the stack object alignment is at least 16.
3632 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3633 if (DAG.InferPtrAlignment(Ptr) < 16) {
3634 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00003635 // Can't change the alignment. FIXME: It's possible to compute
3636 // the exact stack offset and reference FI + adjust offset instead.
3637 // If someone *really* cares about this. That's the way to implement it.
3638 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00003639 } else {
3640 MFI->setObjectAlignment(FI, 16);
3641 }
3642 }
3643
3644 // (Offset % 16) must be multiple of 4. Then address is then
3645 // Ptr + (Offset & ~15).
3646 if (Offset < 0)
3647 return SDValue();
3648 if ((Offset % 16) & 3)
3649 return SDValue();
3650 int64_t StartOffset = Offset & ~15;
3651 if (StartOffset)
3652 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
3653 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
3654
3655 int EltNo = (Offset - StartOffset) >> 2;
3656 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
3657 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
David Greene67c9d422010-02-15 16:53:33 +00003658 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,LD->getSrcValue(),0,
3659 false, false, 0);
Evan Chengc3630942009-12-09 21:00:30 +00003660 // Canonicalize it to a v4i32 shuffle.
3661 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32, V1);
3662 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3663 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
3664 DAG.getUNDEF(MVT::v4i32), &Mask[0]));
3665 }
3666
3667 return SDValue();
3668}
3669
Nate Begeman1449f292010-03-24 22:19:06 +00003670/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
3671/// vector of type 'VT', see if the elements can be replaced by a single large
3672/// load which has the same value as a build_vector whose operands are 'elts'.
3673///
3674/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
3675///
3676/// FIXME: we'd also like to handle the case where the last elements are zero
3677/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
3678/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00003679static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
3680 DebugLoc &dl, SelectionDAG &DAG) {
3681 EVT EltVT = VT.getVectorElementType();
3682 unsigned NumElems = Elts.size();
3683
Nate Begemanfdea31a2010-03-24 20:49:50 +00003684 LoadSDNode *LDBase = NULL;
3685 unsigned LastLoadedElt = -1U;
Nate Begeman1449f292010-03-24 22:19:06 +00003686
3687 // For each element in the initializer, see if we've found a load or an undef.
3688 // If we don't find an initial load element, or later load elements are
3689 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00003690 for (unsigned i = 0; i < NumElems; ++i) {
3691 SDValue Elt = Elts[i];
3692
3693 if (!Elt.getNode() ||
3694 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
3695 return SDValue();
3696 if (!LDBase) {
3697 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
3698 return SDValue();
3699 LDBase = cast<LoadSDNode>(Elt.getNode());
3700 LastLoadedElt = i;
3701 continue;
3702 }
3703 if (Elt.getOpcode() == ISD::UNDEF)
3704 continue;
3705
3706 LoadSDNode *LD = cast<LoadSDNode>(Elt);
3707 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
3708 return SDValue();
3709 LastLoadedElt = i;
3710 }
Nate Begeman1449f292010-03-24 22:19:06 +00003711
3712 // If we have found an entire vector of loads and undefs, then return a large
3713 // load of the entire vector width starting at the base pointer. If we found
3714 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00003715 if (LastLoadedElt == NumElems - 1) {
3716 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
3717 return DAG.getLoad(VT, dl, LDBase->getChain(), LDBase->getBasePtr(),
3718 LDBase->getSrcValue(), LDBase->getSrcValueOffset(),
3719 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
3720 return DAG.getLoad(VT, dl, LDBase->getChain(), LDBase->getBasePtr(),
3721 LDBase->getSrcValue(), LDBase->getSrcValueOffset(),
3722 LDBase->isVolatile(), LDBase->isNonTemporal(),
3723 LDBase->getAlignment());
3724 } else if (NumElems == 4 && LastLoadedElt == 1) {
3725 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
3726 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
3727 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2);
3728 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, ResNode);
3729 }
3730 return SDValue();
3731}
3732
Evan Chengc3630942009-12-09 21:00:30 +00003733SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00003734X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003735 DebugLoc dl = Op.getDebugLoc();
Chris Lattner8a594482007-11-25 00:24:49 +00003736 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif327ef032008-08-28 23:19:51 +00003737 if (ISD::isBuildVectorAllZeros(Op.getNode())
3738 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattner8a594482007-11-25 00:24:49 +00003739 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3740 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3741 // eliminated on x86-32 hosts.
Owen Anderson825b72b2009-08-11 20:47:22 +00003742 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
Chris Lattner8a594482007-11-25 00:24:49 +00003743 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003744
Gabor Greifba36cb52008-08-28 21:40:38 +00003745 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00003746 return getOnesVector(Op.getValueType(), DAG, dl);
3747 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00003748 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003749
Owen Andersone50ed302009-08-10 22:56:29 +00003750 EVT VT = Op.getValueType();
3751 EVT ExtVT = VT.getVectorElementType();
3752 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003753
3754 unsigned NumElems = Op.getNumOperands();
3755 unsigned NumZero = 0;
3756 unsigned NumNonZero = 0;
3757 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003758 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00003759 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003760 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003761 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00003762 if (Elt.getOpcode() == ISD::UNDEF)
3763 continue;
3764 Values.insert(Elt);
3765 if (Elt.getOpcode() != ISD::Constant &&
3766 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003767 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00003768 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00003769 NumZero++;
3770 else {
3771 NonZeros |= (1 << i);
3772 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003773 }
3774 }
3775
Dan Gohman7f321562007-06-25 16:23:39 +00003776 if (NumNonZero == 0) {
Chris Lattner8a594482007-11-25 00:24:49 +00003777 // All undef vector. Return an UNDEF. All zero vectors were handled above.
Dale Johannesene8d72302009-02-06 23:05:02 +00003778 return DAG.getUNDEF(VT);
Dan Gohman7f321562007-06-25 16:23:39 +00003779 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003780
Chris Lattner67f453a2008-03-09 05:42:06 +00003781 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00003782 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003783 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00003784 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00003785
Chris Lattner62098042008-03-09 01:05:04 +00003786 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3787 // the value are obviously zero, truncate the value to i32 and do the
3788 // insertion that way. Only do this if the value is non-constant or if the
3789 // value is a constant being inserted into element 0. It is cheaper to do
3790 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00003791 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00003792 (!IsAllConstants || Idx == 0)) {
3793 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3794 // Handle MMX and SSE both.
Owen Anderson825b72b2009-08-11 20:47:22 +00003795 EVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3796 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Scott Michelfdc40a02009-02-17 22:15:04 +00003797
Chris Lattner62098042008-03-09 01:05:04 +00003798 // Truncate the value (which may itself be a constant) to i32, and
3799 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00003800 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00003801 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00003802 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3803 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00003804
Chris Lattner62098042008-03-09 01:05:04 +00003805 // Now we have our 32-bit value zero extended in the low element of
3806 // a vector. If Idx != 0, swizzle it into place.
3807 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003808 SmallVector<int, 4> Mask;
3809 Mask.push_back(Idx);
3810 for (unsigned i = 1; i != VecElts; ++i)
3811 Mask.push_back(i);
3812 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00003813 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00003814 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003815 }
Dale Johannesenace16102009-02-03 19:33:06 +00003816 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00003817 }
3818 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003819
Chris Lattner19f79692008-03-08 22:59:52 +00003820 // If we have a constant or non-constant insertion into the low element of
3821 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3822 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00003823 // depending on what the source datatype is.
3824 if (Idx == 0) {
3825 if (NumZero == 0) {
3826 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00003827 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
3828 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00003829 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
3830 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
3831 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
3832 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00003833 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
3834 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
3835 EVT MiddleVT = VT.getSizeInBits() == 64 ? MVT::v2i32 : MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00003836 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
3837 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3838 Subtarget->hasSSE2(), DAG);
3839 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Item);
3840 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003841 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003842
3843 // Is it a vector logical left shift?
3844 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00003845 X86::isZeroNode(Op.getOperand(0)) &&
3846 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003847 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00003848 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00003849 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00003850 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00003851 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00003852 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003853
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003854 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00003855 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003856
Chris Lattner19f79692008-03-08 22:59:52 +00003857 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3858 // is a non-constant being inserted into an element other than the low one,
3859 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3860 // movd/movss) to move this into the low element, then shuffle it into
3861 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003862 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00003863 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00003864
Evan Cheng0db9fe62006-04-25 20:13:52 +00003865 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003866 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3867 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00003868 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003869 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00003870 MaskVec.push_back(i == Idx ? 0 : 1);
3871 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003872 }
3873 }
3874
Chris Lattner67f453a2008-03-09 05:42:06 +00003875 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00003876 if (Values.size() == 1) {
3877 if (EVTBits == 32) {
3878 // Instead of a shuffle like this:
3879 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
3880 // Check if it's possible to issue this instead.
3881 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
3882 unsigned Idx = CountTrailingZeros_32(NonZeros);
3883 SDValue Item = Op.getOperand(Idx);
3884 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
3885 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
3886 }
Dan Gohman475871a2008-07-27 21:46:04 +00003887 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00003888 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003889
Dan Gohmana3941172007-07-24 22:55:08 +00003890 // A vector full of immediates; various special cases are already
3891 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003892 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00003893 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00003894
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003895 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003896 if (EVTBits == 64) {
3897 if (NumNonZero == 1) {
3898 // One half is zero or undef.
3899 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00003900 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003901 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00003902 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3903 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00003904 }
Dan Gohman475871a2008-07-27 21:46:04 +00003905 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00003906 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003907
3908 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00003909 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00003910 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003911 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003912 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003913 }
3914
Bill Wendling826f36f2007-03-28 00:57:11 +00003915 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00003916 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003917 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003918 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003919 }
3920
3921 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003922 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00003923 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003924 if (NumElems == 4 && NumZero > 0) {
3925 for (unsigned i = 0; i < 4; ++i) {
3926 bool isZero = !(NonZeros & (1 << i));
3927 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003928 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003929 else
Dale Johannesenace16102009-02-03 19:33:06 +00003930 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003931 }
3932
3933 for (unsigned i = 0; i < 2; ++i) {
3934 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3935 default: break;
3936 case 0:
3937 V[i] = V[i*2]; // Must be a zero vector.
3938 break;
3939 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00003940 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003941 break;
3942 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00003943 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003944 break;
3945 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00003946 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003947 break;
3948 }
3949 }
3950
Nate Begeman9008ca62009-04-27 18:41:29 +00003951 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003952 bool Reverse = (NonZeros & 0x3) == 2;
3953 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003954 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003955 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3956 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003957 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
3958 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003959 }
3960
Nate Begemanfdea31a2010-03-24 20:49:50 +00003961 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
3962 // Check for a build vector of consecutive loads.
3963 for (unsigned i = 0; i < NumElems; ++i)
3964 V[i] = Op.getOperand(i);
3965
3966 // Check for elements which are consecutive loads.
3967 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
3968 if (LD.getNode())
3969 return LD;
3970
3971 // For SSE 4.1, use inserts into undef.
3972 if (getSubtarget()->hasSSE41()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003973 V[0] = DAG.getUNDEF(VT);
3974 for (unsigned i = 0; i < NumElems; ++i)
3975 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
3976 V[0] = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, V[0],
3977 Op.getOperand(i), DAG.getIntPtrConstant(i));
3978 return V[0];
3979 }
Nate Begemanfdea31a2010-03-24 20:49:50 +00003980
3981 // Otherwise, expand into a number of unpckl*
Evan Cheng0db9fe62006-04-25 20:13:52 +00003982 // e.g. for v4f32
3983 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3984 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3985 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Evan Cheng0db9fe62006-04-25 20:13:52 +00003986 for (unsigned i = 0; i < NumElems; ++i)
Dale Johannesenace16102009-02-03 19:33:06 +00003987 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003988 NumElems >>= 1;
3989 while (NumElems != 0) {
3990 for (unsigned i = 0; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003991 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + NumElems]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003992 NumElems >>= 1;
3993 }
3994 return V[0];
3995 }
Dan Gohman475871a2008-07-27 21:46:04 +00003996 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003997}
3998
Mon P Wangeb38ebf2010-01-24 00:05:03 +00003999SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004000X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004001 // We support concatenate two MMX registers and place them in a MMX
4002 // register. This is better than doing a stack convert.
4003 DebugLoc dl = Op.getDebugLoc();
4004 EVT ResVT = Op.getValueType();
4005 assert(Op.getNumOperands() == 2);
4006 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
4007 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
4008 int Mask[2];
4009 SDValue InVec = DAG.getNode(ISD::BIT_CONVERT,dl, MVT::v1i64, Op.getOperand(0));
4010 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4011 InVec = Op.getOperand(1);
4012 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4013 unsigned NumElts = ResVT.getVectorNumElements();
4014 VecOp = DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
4015 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
4016 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
4017 } else {
4018 InVec = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v1i64, InVec);
4019 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4020 Mask[0] = 0; Mask[1] = 2;
4021 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
4022 }
4023 return DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
4024}
4025
Nate Begemanb9a47b82009-02-23 08:49:38 +00004026// v8i16 shuffles - Prefer shuffles in the following order:
4027// 1. [all] pshuflw, pshufhw, optional move
4028// 2. [ssse3] 1 x pshufb
4029// 3. [ssse3] 2 x pshufb + 1 x por
4030// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004031static
Nate Begeman9008ca62009-04-27 18:41:29 +00004032SDValue LowerVECTOR_SHUFFLEv8i16(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00004033 SelectionDAG &DAG,
4034 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004035 SDValue V1 = SVOp->getOperand(0);
4036 SDValue V2 = SVOp->getOperand(1);
4037 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004038 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00004039
Nate Begemanb9a47b82009-02-23 08:49:38 +00004040 // Determine if more than 1 of the words in each of the low and high quadwords
4041 // of the result come from the same quadword of one of the two inputs. Undef
4042 // mask values count as coming from any quadword, for better codegen.
4043 SmallVector<unsigned, 4> LoQuad(4);
4044 SmallVector<unsigned, 4> HiQuad(4);
4045 BitVector InputQuads(4);
4046 for (unsigned i = 0; i < 8; ++i) {
4047 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00004048 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004049 MaskVals.push_back(EltIdx);
4050 if (EltIdx < 0) {
4051 ++Quad[0];
4052 ++Quad[1];
4053 ++Quad[2];
4054 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00004055 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004056 }
4057 ++Quad[EltIdx / 4];
4058 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00004059 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004060
Nate Begemanb9a47b82009-02-23 08:49:38 +00004061 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004062 unsigned MaxQuad = 1;
4063 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004064 if (LoQuad[i] > MaxQuad) {
4065 BestLoQuad = i;
4066 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004067 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004068 }
4069
Nate Begemanb9a47b82009-02-23 08:49:38 +00004070 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004071 MaxQuad = 1;
4072 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004073 if (HiQuad[i] > MaxQuad) {
4074 BestHiQuad = i;
4075 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004076 }
4077 }
4078
Nate Begemanb9a47b82009-02-23 08:49:38 +00004079 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00004080 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00004081 // single pshufb instruction is necessary. If There are more than 2 input
4082 // quads, disable the next transformation since it does not help SSSE3.
4083 bool V1Used = InputQuads[0] || InputQuads[1];
4084 bool V2Used = InputQuads[2] || InputQuads[3];
4085 if (TLI.getSubtarget()->hasSSSE3()) {
4086 if (InputQuads.count() == 2 && V1Used && V2Used) {
4087 BestLoQuad = InputQuads.find_first();
4088 BestHiQuad = InputQuads.find_next(BestLoQuad);
4089 }
4090 if (InputQuads.count() > 2) {
4091 BestLoQuad = -1;
4092 BestHiQuad = -1;
4093 }
4094 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004095
Nate Begemanb9a47b82009-02-23 08:49:38 +00004096 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
4097 // the shuffle mask. If a quad is scored as -1, that means that it contains
4098 // words from all 4 input quadwords.
4099 SDValue NewV;
4100 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004101 SmallVector<int, 8> MaskV;
4102 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
4103 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00004104 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004105 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V1),
4106 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V2), &MaskV[0]);
4107 NewV = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004108
Nate Begemanb9a47b82009-02-23 08:49:38 +00004109 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
4110 // source words for the shuffle, to aid later transformations.
4111 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00004112 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00004113 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004114 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00004115 if (idx != (int)i)
4116 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004117 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00004118 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004119 AllWordsInNewV = false;
4120 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00004121 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004122
Nate Begemanb9a47b82009-02-23 08:49:38 +00004123 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
4124 if (AllWordsInNewV) {
4125 for (int i = 0; i != 8; ++i) {
4126 int idx = MaskVals[i];
4127 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004128 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004129 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004130 if ((idx != i) && idx < 4)
4131 pshufhw = false;
4132 if ((idx != i) && idx > 3)
4133 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00004134 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00004135 V1 = NewV;
4136 V2Used = false;
4137 BestLoQuad = 0;
4138 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004139 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004140
Nate Begemanb9a47b82009-02-23 08:49:38 +00004141 // If we've eliminated the use of V2, and the new mask is a pshuflw or
4142 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00004143 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Eric Christopherfd179292009-08-27 18:07:15 +00004144 return DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00004145 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Evan Cheng14b32e12007-12-11 01:46:18 +00004146 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004147 }
Eric Christopherfd179292009-08-27 18:07:15 +00004148
Nate Begemanb9a47b82009-02-23 08:49:38 +00004149 // If we have SSSE3, and all words of the result are from 1 input vector,
4150 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
4151 // is present, fall back to case 4.
4152 if (TLI.getSubtarget()->hasSSSE3()) {
4153 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004154
Nate Begemanb9a47b82009-02-23 08:49:38 +00004155 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00004156 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00004157 // mask, and elements that come from V1 in the V2 mask, so that the two
4158 // results can be OR'd together.
4159 bool TwoInputs = V1Used && V2Used;
4160 for (unsigned i = 0; i != 8; ++i) {
4161 int EltIdx = MaskVals[i] * 2;
4162 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004163 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4164 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004165 continue;
4166 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004167 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
4168 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004169 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004170 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004171 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004172 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004173 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004174 if (!TwoInputs)
Owen Anderson825b72b2009-08-11 20:47:22 +00004175 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004176
Nate Begemanb9a47b82009-02-23 08:49:38 +00004177 // Calculate the shuffle mask for the second input, shuffle it, and
4178 // OR it with the first shuffled input.
4179 pshufbMask.clear();
4180 for (unsigned i = 0; i != 8; ++i) {
4181 int EltIdx = MaskVals[i] * 2;
4182 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004183 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4184 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004185 continue;
4186 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004187 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
4188 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004189 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004190 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00004191 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004192 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004193 MVT::v16i8, &pshufbMask[0], 16));
4194 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
4195 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004196 }
4197
4198 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
4199 // and update MaskVals with new element order.
4200 BitVector InOrder(8);
4201 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004202 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004203 for (int i = 0; i != 4; ++i) {
4204 int idx = MaskVals[i];
4205 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004206 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004207 InOrder.set(i);
4208 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004209 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004210 InOrder.set(i);
4211 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004212 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004213 }
4214 }
4215 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004216 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00004217 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004218 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004219 }
Eric Christopherfd179292009-08-27 18:07:15 +00004220
Nate Begemanb9a47b82009-02-23 08:49:38 +00004221 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
4222 // and update MaskVals with the new element order.
4223 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004224 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004225 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004226 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004227 for (unsigned i = 4; i != 8; ++i) {
4228 int idx = MaskVals[i];
4229 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004230 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004231 InOrder.set(i);
4232 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004233 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004234 InOrder.set(i);
4235 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004236 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004237 }
4238 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004239 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004240 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004241 }
Eric Christopherfd179292009-08-27 18:07:15 +00004242
Nate Begemanb9a47b82009-02-23 08:49:38 +00004243 // In case BestHi & BestLo were both -1, which means each quadword has a word
4244 // from each of the four input quadwords, calculate the InOrder bitvector now
4245 // before falling through to the insert/extract cleanup.
4246 if (BestLoQuad == -1 && BestHiQuad == -1) {
4247 NewV = V1;
4248 for (int i = 0; i != 8; ++i)
4249 if (MaskVals[i] < 0 || MaskVals[i] == i)
4250 InOrder.set(i);
4251 }
Eric Christopherfd179292009-08-27 18:07:15 +00004252
Nate Begemanb9a47b82009-02-23 08:49:38 +00004253 // The other elements are put in the right place using pextrw and pinsrw.
4254 for (unsigned i = 0; i != 8; ++i) {
4255 if (InOrder[i])
4256 continue;
4257 int EltIdx = MaskVals[i];
4258 if (EltIdx < 0)
4259 continue;
4260 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004261 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004262 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00004263 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004264 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004265 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004266 DAG.getIntPtrConstant(i));
4267 }
4268 return NewV;
4269}
4270
4271// v16i8 shuffles - Prefer shuffles in the following order:
4272// 1. [ssse3] 1 x pshufb
4273// 2. [ssse3] 2 x pshufb + 1 x por
4274// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
4275static
Nate Begeman9008ca62009-04-27 18:41:29 +00004276SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00004277 SelectionDAG &DAG,
4278 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004279 SDValue V1 = SVOp->getOperand(0);
4280 SDValue V2 = SVOp->getOperand(1);
4281 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004282 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00004283 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00004284
Nate Begemanb9a47b82009-02-23 08:49:38 +00004285 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00004286 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00004287 // present, fall back to case 3.
4288 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
4289 bool V1Only = true;
4290 bool V2Only = true;
4291 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004292 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00004293 if (EltIdx < 0)
4294 continue;
4295 if (EltIdx < 16)
4296 V2Only = false;
4297 else
4298 V1Only = false;
4299 }
Eric Christopherfd179292009-08-27 18:07:15 +00004300
Nate Begemanb9a47b82009-02-23 08:49:38 +00004301 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
4302 if (TLI.getSubtarget()->hasSSSE3()) {
4303 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004304
Nate Begemanb9a47b82009-02-23 08:49:38 +00004305 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00004306 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004307 //
4308 // Otherwise, we have elements from both input vectors, and must zero out
4309 // elements that come from V2 in the first mask, and V1 in the second mask
4310 // so that we can OR them together.
4311 bool TwoInputs = !(V1Only || V2Only);
4312 for (unsigned i = 0; i != 16; ++i) {
4313 int EltIdx = MaskVals[i];
4314 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004315 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004316 continue;
4317 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004318 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004319 }
4320 // If all the elements are from V2, assign it to V1 and return after
4321 // building the first pshufb.
4322 if (V2Only)
4323 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00004324 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004325 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004326 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004327 if (!TwoInputs)
4328 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00004329
Nate Begemanb9a47b82009-02-23 08:49:38 +00004330 // Calculate the shuffle mask for the second input, shuffle it, and
4331 // OR it with the first shuffled input.
4332 pshufbMask.clear();
4333 for (unsigned i = 0; i != 16; ++i) {
4334 int EltIdx = MaskVals[i];
4335 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004336 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004337 continue;
4338 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004339 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004340 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004341 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004342 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004343 MVT::v16i8, &pshufbMask[0], 16));
4344 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004345 }
Eric Christopherfd179292009-08-27 18:07:15 +00004346
Nate Begemanb9a47b82009-02-23 08:49:38 +00004347 // No SSSE3 - Calculate in place words and then fix all out of place words
4348 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
4349 // the 16 different words that comprise the two doublequadword input vectors.
Owen Anderson825b72b2009-08-11 20:47:22 +00004350 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
4351 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004352 SDValue NewV = V2Only ? V2 : V1;
4353 for (int i = 0; i != 8; ++i) {
4354 int Elt0 = MaskVals[i*2];
4355 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00004356
Nate Begemanb9a47b82009-02-23 08:49:38 +00004357 // This word of the result is all undef, skip it.
4358 if (Elt0 < 0 && Elt1 < 0)
4359 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004360
Nate Begemanb9a47b82009-02-23 08:49:38 +00004361 // This word of the result is already in the correct place, skip it.
4362 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
4363 continue;
4364 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
4365 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004366
Nate Begemanb9a47b82009-02-23 08:49:38 +00004367 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
4368 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
4369 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00004370
4371 // If Elt0 and Elt1 are defined, are consecutive, and can be load
4372 // using a single extract together, load it and store it.
4373 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004374 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004375 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00004376 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004377 DAG.getIntPtrConstant(i));
4378 continue;
4379 }
4380
Nate Begemanb9a47b82009-02-23 08:49:38 +00004381 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00004382 // source byte is not also odd, shift the extracted word left 8 bits
4383 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004384 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004385 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004386 DAG.getIntPtrConstant(Elt1 / 2));
4387 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004388 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004389 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004390 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004391 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
4392 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004393 }
4394 // If Elt0 is defined, extract it from the appropriate source. If the
4395 // source byte is not also even, shift the extracted word right 8 bits. If
4396 // Elt1 was also defined, OR the extracted values together before
4397 // inserting them in the result.
4398 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004399 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004400 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
4401 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004402 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004403 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004404 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004405 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
4406 DAG.getConstant(0x00FF, MVT::i16));
4407 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00004408 : InsElt0;
4409 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004410 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004411 DAG.getIntPtrConstant(i));
4412 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004413 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004414}
4415
Evan Cheng7a831ce2007-12-15 03:00:47 +00004416/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
4417/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
4418/// done when every pair / quad of shuffle mask elements point to elements in
4419/// the right sequence. e.g.
Evan Cheng14b32e12007-12-11 01:46:18 +00004420/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
4421static
Nate Begeman9008ca62009-04-27 18:41:29 +00004422SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
4423 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00004424 const TargetLowering &TLI, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00004425 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00004426 SDValue V1 = SVOp->getOperand(0);
4427 SDValue V2 = SVOp->getOperand(1);
4428 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00004429 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Owen Anderson825b72b2009-08-11 20:47:22 +00004430 EVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Owen Andersone50ed302009-08-10 22:56:29 +00004431 EVT MaskEltVT = MaskVT.getVectorElementType();
4432 EVT NewVT = MaskVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00004433 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004434 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004435 case MVT::v4f32: NewVT = MVT::v2f64; break;
4436 case MVT::v4i32: NewVT = MVT::v2i64; break;
4437 case MVT::v8i16: NewVT = MVT::v4i32; break;
4438 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004439 }
4440
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004441 if (NewWidth == 2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004442 if (VT.isInteger())
Owen Anderson825b72b2009-08-11 20:47:22 +00004443 NewVT = MVT::v2i64;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004444 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004445 NewVT = MVT::v2f64;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004446 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004447 int Scale = NumElems / NewWidth;
4448 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00004449 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004450 int StartIdx = -1;
4451 for (int j = 0; j < Scale; ++j) {
4452 int EltIdx = SVOp->getMaskElt(i+j);
4453 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004454 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00004455 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00004456 StartIdx = EltIdx - (EltIdx % Scale);
4457 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00004458 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004459 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004460 if (StartIdx == -1)
4461 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00004462 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004463 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004464 }
4465
Dale Johannesenace16102009-02-03 19:33:06 +00004466 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V1);
4467 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00004468 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004469}
4470
Evan Chengd880b972008-05-09 21:53:03 +00004471/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004472///
Owen Andersone50ed302009-08-10 22:56:29 +00004473static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00004474 SDValue SrcOp, SelectionDAG &DAG,
4475 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004476 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004477 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00004478 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00004479 LD = dyn_cast<LoadSDNode>(SrcOp);
4480 if (!LD) {
4481 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
4482 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00004483 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
4484 if ((ExtVT.SimpleTy != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00004485 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4486 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00004487 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004488 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00004489 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Dale Johannesenace16102009-02-03 19:33:06 +00004490 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4491 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
4492 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
4493 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00004494 SrcOp.getOperand(0)
4495 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004496 }
4497 }
4498 }
4499
Dale Johannesenace16102009-02-03 19:33:06 +00004500 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4501 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004502 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00004503 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004504}
4505
Evan Chengace3c172008-07-22 21:13:36 +00004506/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
4507/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004508static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00004509LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
4510 SDValue V1 = SVOp->getOperand(0);
4511 SDValue V2 = SVOp->getOperand(1);
4512 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004513 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00004514
Evan Chengace3c172008-07-22 21:13:36 +00004515 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00004516 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00004517 SmallVector<int, 8> Mask1(4U, -1);
4518 SmallVector<int, 8> PermMask;
4519 SVOp->getMask(PermMask);
4520
Evan Chengace3c172008-07-22 21:13:36 +00004521 unsigned NumHi = 0;
4522 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00004523 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004524 int Idx = PermMask[i];
4525 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004526 Locs[i] = std::make_pair(-1, -1);
4527 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004528 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
4529 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004530 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00004531 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004532 NumLo++;
4533 } else {
4534 Locs[i] = std::make_pair(1, NumHi);
4535 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004536 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004537 NumHi++;
4538 }
4539 }
4540 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004541
Evan Chengace3c172008-07-22 21:13:36 +00004542 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004543 // If no more than two elements come from either vector. This can be
4544 // implemented with two shuffles. First shuffle gather the elements.
4545 // The second shuffle, which takes the first shuffle as both of its
4546 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00004547 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004548
Nate Begeman9008ca62009-04-27 18:41:29 +00004549 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00004550
Evan Chengace3c172008-07-22 21:13:36 +00004551 for (unsigned i = 0; i != 4; ++i) {
4552 if (Locs[i].first == -1)
4553 continue;
4554 else {
4555 unsigned Idx = (i < 2) ? 0 : 4;
4556 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004557 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004558 }
4559 }
4560
Nate Begeman9008ca62009-04-27 18:41:29 +00004561 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004562 } else if (NumLo == 3 || NumHi == 3) {
4563 // Otherwise, we must have three elements from one vector, call it X, and
4564 // one element from the other, call it Y. First, use a shufps to build an
4565 // intermediate vector with the one element from Y and the element from X
4566 // that will be in the same half in the final destination (the indexes don't
4567 // matter). Then, use a shufps to build the final vector, taking the half
4568 // containing the element from Y from the intermediate, and the other half
4569 // from X.
4570 if (NumHi == 3) {
4571 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00004572 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004573 std::swap(V1, V2);
4574 }
4575
4576 // Find the element from V2.
4577 unsigned HiIndex;
4578 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004579 int Val = PermMask[HiIndex];
4580 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004581 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004582 if (Val >= 4)
4583 break;
4584 }
4585
Nate Begeman9008ca62009-04-27 18:41:29 +00004586 Mask1[0] = PermMask[HiIndex];
4587 Mask1[1] = -1;
4588 Mask1[2] = PermMask[HiIndex^1];
4589 Mask1[3] = -1;
4590 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004591
4592 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004593 Mask1[0] = PermMask[0];
4594 Mask1[1] = PermMask[1];
4595 Mask1[2] = HiIndex & 1 ? 6 : 4;
4596 Mask1[3] = HiIndex & 1 ? 4 : 6;
4597 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004598 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004599 Mask1[0] = HiIndex & 1 ? 2 : 0;
4600 Mask1[1] = HiIndex & 1 ? 0 : 2;
4601 Mask1[2] = PermMask[2];
4602 Mask1[3] = PermMask[3];
4603 if (Mask1[2] >= 0)
4604 Mask1[2] += 4;
4605 if (Mask1[3] >= 0)
4606 Mask1[3] += 4;
4607 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004608 }
Evan Chengace3c172008-07-22 21:13:36 +00004609 }
4610
4611 // Break it into (shuffle shuffle_hi, shuffle_lo).
4612 Locs.clear();
Nate Begeman9008ca62009-04-27 18:41:29 +00004613 SmallVector<int,8> LoMask(4U, -1);
4614 SmallVector<int,8> HiMask(4U, -1);
4615
4616 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00004617 unsigned MaskIdx = 0;
4618 unsigned LoIdx = 0;
4619 unsigned HiIdx = 2;
4620 for (unsigned i = 0; i != 4; ++i) {
4621 if (i == 2) {
4622 MaskPtr = &HiMask;
4623 MaskIdx = 1;
4624 LoIdx = 0;
4625 HiIdx = 2;
4626 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004627 int Idx = PermMask[i];
4628 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004629 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00004630 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004631 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004632 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004633 LoIdx++;
4634 } else {
4635 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004636 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004637 HiIdx++;
4638 }
4639 }
4640
Nate Begeman9008ca62009-04-27 18:41:29 +00004641 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
4642 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
4643 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00004644 for (unsigned i = 0; i != 4; ++i) {
4645 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004646 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00004647 } else {
4648 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004649 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00004650 }
4651 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004652 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00004653}
4654
Dan Gohman475871a2008-07-27 21:46:04 +00004655SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004656X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00004657 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00004658 SDValue V1 = Op.getOperand(0);
4659 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00004660 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004661 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00004662 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004663 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004664 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
4665 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00004666 bool V1IsSplat = false;
4667 bool V2IsSplat = false;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004668
Nate Begeman9008ca62009-04-27 18:41:29 +00004669 if (isZeroShuffle(SVOp))
Dale Johannesenace16102009-02-03 19:33:06 +00004670 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004671
Nate Begeman9008ca62009-04-27 18:41:29 +00004672 // Promote splats to v4f32.
4673 if (SVOp->isSplat()) {
Eric Christopherfd179292009-08-27 18:07:15 +00004674 if (isMMX || NumElems < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004675 return Op;
4676 return PromoteSplat(SVOp, DAG, Subtarget->hasSSE2());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004677 }
4678
Evan Cheng7a831ce2007-12-15 03:00:47 +00004679 // If the shuffle can be profitably rewritten as a narrower shuffle, then
4680 // do it!
Owen Anderson825b72b2009-08-11 20:47:22 +00004681 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004682 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004683 if (NewOp.getNode())
Scott Michelfdc40a02009-02-17 22:15:04 +00004684 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004685 LowerVECTOR_SHUFFLE(NewOp, DAG));
Owen Anderson825b72b2009-08-11 20:47:22 +00004686 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Evan Cheng7a831ce2007-12-15 03:00:47 +00004687 // FIXME: Figure out a cleaner way to do this.
4688 // Try to make use of movq to zero out the top part.
Gabor Greifba36cb52008-08-28 21:40:38 +00004689 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004690 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004691 if (NewOp.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004692 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
4693 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
4694 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004695 }
Gabor Greifba36cb52008-08-28 21:40:38 +00004696 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004697 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
4698 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
Evan Chengd880b972008-05-09 21:53:03 +00004699 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Nate Begeman9008ca62009-04-27 18:41:29 +00004700 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004701 }
4702 }
Eric Christopherfd179292009-08-27 18:07:15 +00004703
Nate Begeman9008ca62009-04-27 18:41:29 +00004704 if (X86::isPSHUFDMask(SVOp))
4705 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004706
Evan Chengf26ffe92008-05-29 08:22:04 +00004707 // Check if this can be converted into a logical shift.
4708 bool isLeft = false;
4709 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00004710 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00004711 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00004712 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00004713 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004714 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00004715 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00004716 EVT EltVT = VT.getVectorElementType();
4717 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004718 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004719 }
Eric Christopherfd179292009-08-27 18:07:15 +00004720
Nate Begeman9008ca62009-04-27 18:41:29 +00004721 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004722 if (V1IsUndef)
4723 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00004724 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004725 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Nate Begemanfb8ead02008-07-25 19:05:58 +00004726 if (!isMMX)
4727 return Op;
Evan Cheng7e2ff772008-05-08 00:57:18 +00004728 }
Eric Christopherfd179292009-08-27 18:07:15 +00004729
Nate Begeman9008ca62009-04-27 18:41:29 +00004730 // FIXME: fold these into legal mask.
4731 if (!isMMX && (X86::isMOVSHDUPMask(SVOp) ||
4732 X86::isMOVSLDUPMask(SVOp) ||
4733 X86::isMOVHLPSMask(SVOp) ||
Nate Begeman0b10b912009-11-07 23:17:15 +00004734 X86::isMOVLHPSMask(SVOp) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00004735 X86::isMOVLPMask(SVOp)))
Evan Cheng9bbbb982006-10-25 20:48:19 +00004736 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004737
Nate Begeman9008ca62009-04-27 18:41:29 +00004738 if (ShouldXformToMOVHLPS(SVOp) ||
4739 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
4740 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004741
Evan Chengf26ffe92008-05-29 08:22:04 +00004742 if (isShift) {
4743 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00004744 EVT EltVT = VT.getVectorElementType();
4745 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004746 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004747 }
Eric Christopherfd179292009-08-27 18:07:15 +00004748
Evan Cheng9eca5e82006-10-25 21:49:50 +00004749 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00004750 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4751 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00004752 V1IsSplat = isSplatVector(V1.getNode());
4753 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00004754
Chris Lattner8a594482007-11-25 00:24:49 +00004755 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00004756 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004757 Op = CommuteVectorShuffle(SVOp, DAG);
4758 SVOp = cast<ShuffleVectorSDNode>(Op);
4759 V1 = SVOp->getOperand(0);
4760 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00004761 std::swap(V1IsSplat, V2IsSplat);
4762 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00004763 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00004764 }
4765
Nate Begeman9008ca62009-04-27 18:41:29 +00004766 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
4767 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00004768 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00004769 return V1;
4770 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
4771 // the instruction selector will not match, so get a canonical MOVL with
4772 // swapped operands to undo the commute.
4773 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00004774 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004775
Nate Begeman9008ca62009-04-27 18:41:29 +00004776 if (X86::isUNPCKL_v_undef_Mask(SVOp) ||
4777 X86::isUNPCKH_v_undef_Mask(SVOp) ||
4778 X86::isUNPCKLMask(SVOp) ||
4779 X86::isUNPCKHMask(SVOp))
Evan Chengd9b8e402006-10-16 06:36:00 +00004780 return Op;
Evan Chenge1113032006-10-04 18:33:38 +00004781
Evan Cheng9bbbb982006-10-25 20:48:19 +00004782 if (V2IsSplat) {
4783 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004784 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00004785 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00004786 SDValue NewMask = NormalizeMask(SVOp, DAG);
4787 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
4788 if (NSVOp != SVOp) {
4789 if (X86::isUNPCKLMask(NSVOp, true)) {
4790 return NewMask;
4791 } else if (X86::isUNPCKHMask(NSVOp, true)) {
4792 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004793 }
4794 }
4795 }
4796
Evan Cheng9eca5e82006-10-25 21:49:50 +00004797 if (Commuted) {
4798 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00004799 // FIXME: this seems wrong.
4800 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
4801 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
4802 if (X86::isUNPCKL_v_undef_Mask(NewSVOp) ||
4803 X86::isUNPCKH_v_undef_Mask(NewSVOp) ||
4804 X86::isUNPCKLMask(NewSVOp) ||
4805 X86::isUNPCKHMask(NewSVOp))
4806 return NewOp;
Evan Cheng9eca5e82006-10-25 21:49:50 +00004807 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004808
Nate Begemanb9a47b82009-02-23 08:49:38 +00004809 // FIXME: for mmx, bitcast v2i32 to v4i16 for shuffle.
Nate Begeman9008ca62009-04-27 18:41:29 +00004810
4811 // Normalize the node to match x86 shuffle ops if needed
4812 if (!isMMX && V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
4813 return CommuteVectorShuffle(SVOp, DAG);
4814
4815 // Check for legal shuffle and return?
4816 SmallVector<int, 16> PermMask;
4817 SVOp->getMask(PermMask);
4818 if (isShuffleMaskLegal(PermMask, VT))
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004819 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004820
Evan Cheng14b32e12007-12-11 01:46:18 +00004821 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00004822 if (VT == MVT::v8i16) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004823 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(SVOp, DAG, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004824 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00004825 return NewOp;
4826 }
4827
Owen Anderson825b72b2009-08-11 20:47:22 +00004828 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004829 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004830 if (NewOp.getNode())
4831 return NewOp;
4832 }
Eric Christopherfd179292009-08-27 18:07:15 +00004833
Evan Chengace3c172008-07-22 21:13:36 +00004834 // Handle all 4 wide cases with a number of shuffles except for MMX.
4835 if (NumElems == 4 && !isMMX)
Nate Begeman9008ca62009-04-27 18:41:29 +00004836 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004837
Dan Gohman475871a2008-07-27 21:46:04 +00004838 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004839}
4840
Dan Gohman475871a2008-07-27 21:46:04 +00004841SDValue
4842X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00004843 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00004844 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004845 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004846 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004847 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004848 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004849 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004850 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004851 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004852 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00004853 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
4854 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
4855 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004856 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4857 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004858 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004859 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00004860 Op.getOperand(0)),
4861 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00004862 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004863 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004864 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004865 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004866 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00004867 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00004868 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4869 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004870 // result has a single use which is a store or a bitcast to i32. And in
4871 // the case of a store, it's not worth it if the index is a constant 0,
4872 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00004873 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00004874 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004875 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004876 if ((User->getOpcode() != ISD::STORE ||
4877 (isa<ConstantSDNode>(Op.getOperand(1)) &&
4878 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman171c11e2008-04-16 02:32:24 +00004879 (User->getOpcode() != ISD::BIT_CONVERT ||
Owen Anderson825b72b2009-08-11 20:47:22 +00004880 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00004881 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00004882 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
4883 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004884 Op.getOperand(0)),
4885 Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004886 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Extract);
4887 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00004888 // ExtractPS works with constant index.
4889 if (isa<ConstantSDNode>(Op.getOperand(1)))
4890 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004891 }
Dan Gohman475871a2008-07-27 21:46:04 +00004892 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004893}
4894
4895
Dan Gohman475871a2008-07-27 21:46:04 +00004896SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004897X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
4898 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004899 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00004900 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004901
Evan Cheng62a3f152008-03-24 21:52:23 +00004902 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004903 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004904 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00004905 return Res;
4906 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00004907
Owen Andersone50ed302009-08-10 22:56:29 +00004908 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004909 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004910 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004911 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004912 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004913 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004914 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004915 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4916 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Scott Michelfdc40a02009-02-17 22:15:04 +00004917 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004918 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00004919 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004920 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00004921 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00004922 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004923 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00004924 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004925 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004926 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004927 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004928 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004929 if (Idx == 0)
4930 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004931
Evan Cheng0db9fe62006-04-25 20:13:52 +00004932 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00004933 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00004934 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00004935 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00004936 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004937 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004938 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004939 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004940 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4941 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4942 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004943 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004944 if (Idx == 0)
4945 return Op;
4946
4947 // UNPCKHPD the element to the lowest double word, then movsd.
4948 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4949 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00004950 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00004951 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00004952 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00004953 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004954 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004955 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004956 }
4957
Dan Gohman475871a2008-07-27 21:46:04 +00004958 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004959}
4960
Dan Gohman475871a2008-07-27 21:46:04 +00004961SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004962X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
4963 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00004964 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00004965 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004966 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004967
Dan Gohman475871a2008-07-27 21:46:04 +00004968 SDValue N0 = Op.getOperand(0);
4969 SDValue N1 = Op.getOperand(1);
4970 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004971
Dan Gohman8a55ce42009-09-23 21:02:20 +00004972 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00004973 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00004974 unsigned Opc;
4975 if (VT == MVT::v8i16)
4976 Opc = X86ISD::PINSRW;
4977 else if (VT == MVT::v4i16)
4978 Opc = X86ISD::MMX_PINSRW;
4979 else if (VT == MVT::v16i8)
4980 Opc = X86ISD::PINSRB;
4981 else
4982 Opc = X86ISD::PINSRB;
4983
Nate Begeman14d12ca2008-02-11 04:19:36 +00004984 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4985 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00004986 if (N1.getValueType() != MVT::i32)
4987 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
4988 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004989 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00004990 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004991 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004992 // Bits [7:6] of the constant are the source select. This will always be
4993 // zero here. The DAG Combiner may combine an extract_elt index into these
4994 // bits. For example (insert (extract, 3), 2) could be matched by putting
4995 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00004996 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00004997 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00004998 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00004999 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005000 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00005001 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00005002 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00005003 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00005004 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00005005 // PINSR* works with constant index.
5006 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00005007 }
Dan Gohman475871a2008-07-27 21:46:04 +00005008 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005009}
5010
Dan Gohman475871a2008-07-27 21:46:04 +00005011SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005012X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005013 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00005014 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005015
5016 if (Subtarget->hasSSE41())
5017 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
5018
Dan Gohman8a55ce42009-09-23 21:02:20 +00005019 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00005020 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00005021
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005022 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005023 SDValue N0 = Op.getOperand(0);
5024 SDValue N1 = Op.getOperand(1);
5025 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00005026
Dan Gohman8a55ce42009-09-23 21:02:20 +00005027 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00005028 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
5029 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00005030 if (N1.getValueType() != MVT::i32)
5031 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
5032 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005033 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00005034 return DAG.getNode(VT == MVT::v8i16 ? X86ISD::PINSRW : X86ISD::MMX_PINSRW,
5035 dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005036 }
Dan Gohman475871a2008-07-27 21:46:04 +00005037 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005038}
5039
Dan Gohman475871a2008-07-27 21:46:04 +00005040SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005041X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005042 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00005043 if (Op.getValueType() == MVT::v2f32)
5044 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f32,
5045 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i32,
5046 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32,
Evan Cheng52672b82008-07-22 18:39:19 +00005047 Op.getOperand(0))));
5048
Owen Anderson825b72b2009-08-11 20:47:22 +00005049 if (Op.getValueType() == MVT::v1i64 && Op.getOperand(0).getValueType() == MVT::i64)
5050 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00005051
Owen Anderson825b72b2009-08-11 20:47:22 +00005052 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
5053 EVT VT = MVT::v2i32;
5054 switch (Op.getValueType().getSimpleVT().SimpleTy) {
Evan Chengefec7512008-02-18 23:04:32 +00005055 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00005056 case MVT::v16i8:
5057 case MVT::v8i16:
5058 VT = MVT::v4i32;
Evan Chengefec7512008-02-18 23:04:32 +00005059 break;
5060 }
Dale Johannesenace16102009-02-03 19:33:06 +00005061 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(),
5062 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005063}
5064
Bill Wendling056292f2008-09-16 21:48:12 +00005065// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
5066// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
5067// one of the above mentioned nodes. It has to be wrapped because otherwise
5068// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
5069// be used to form addressing mode. These wrapped nodes will be selected
5070// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00005071SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005072X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005073 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00005074
Chris Lattner41621a22009-06-26 19:22:52 +00005075 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5076 // global base reg.
5077 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005078 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005079 CodeModel::Model M = getTargetMachine().getCodeModel();
5080
Chris Lattner4f066492009-07-11 20:29:19 +00005081 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005082 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005083 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005084 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005085 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005086 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005087 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005088
Evan Cheng1606e8e2009-03-13 07:51:59 +00005089 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00005090 CP->getAlignment(),
5091 CP->getOffset(), OpFlag);
5092 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00005093 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005094 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00005095 if (OpFlag) {
5096 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005097 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005098 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005099 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005100 }
5101
5102 return Result;
5103}
5104
Dan Gohmand858e902010-04-17 15:26:15 +00005105SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00005106 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00005107
Chris Lattner18c59872009-06-27 04:16:01 +00005108 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5109 // global base reg.
5110 unsigned char OpFlag = 0;
5111 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005112 CodeModel::Model M = getTargetMachine().getCodeModel();
5113
Chris Lattner4f066492009-07-11 20:29:19 +00005114 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005115 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005116 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005117 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005118 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005119 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005120 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005121
Chris Lattner18c59872009-06-27 04:16:01 +00005122 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
5123 OpFlag);
5124 DebugLoc DL = JT->getDebugLoc();
5125 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005126
Chris Lattner18c59872009-06-27 04:16:01 +00005127 // With PIC, the address is actually $g + Offset.
5128 if (OpFlag) {
5129 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5130 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005131 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00005132 Result);
5133 }
Eric Christopherfd179292009-08-27 18:07:15 +00005134
Chris Lattner18c59872009-06-27 04:16:01 +00005135 return Result;
5136}
5137
5138SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005139X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00005140 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00005141
Chris Lattner18c59872009-06-27 04:16:01 +00005142 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5143 // global base reg.
5144 unsigned char OpFlag = 0;
5145 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005146 CodeModel::Model M = getTargetMachine().getCodeModel();
5147
Chris Lattner4f066492009-07-11 20:29:19 +00005148 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005149 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005150 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005151 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005152 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005153 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005154 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005155
Chris Lattner18c59872009-06-27 04:16:01 +00005156 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00005157
Chris Lattner18c59872009-06-27 04:16:01 +00005158 DebugLoc DL = Op.getDebugLoc();
5159 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005160
5161
Chris Lattner18c59872009-06-27 04:16:01 +00005162 // With PIC, the address is actually $g + Offset.
5163 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00005164 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00005165 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5166 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005167 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00005168 Result);
5169 }
Eric Christopherfd179292009-08-27 18:07:15 +00005170
Chris Lattner18c59872009-06-27 04:16:01 +00005171 return Result;
5172}
5173
Dan Gohman475871a2008-07-27 21:46:04 +00005174SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005175X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00005176 // Create the TargetBlockAddressAddress node.
5177 unsigned char OpFlags =
5178 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00005179 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00005180 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00005181 DebugLoc dl = Op.getDebugLoc();
5182 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
5183 /*isTarget=*/true, OpFlags);
5184
Dan Gohmanf705adb2009-10-30 01:28:02 +00005185 if (Subtarget->isPICStyleRIPRel() &&
5186 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00005187 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
5188 else
5189 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00005190
Dan Gohman29cbade2009-11-20 23:18:13 +00005191 // With PIC, the address is actually $g + Offset.
5192 if (isGlobalRelativeToPICBase(OpFlags)) {
5193 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5194 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
5195 Result);
5196 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00005197
5198 return Result;
5199}
5200
5201SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00005202X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00005203 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00005204 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00005205 // Create the TargetGlobalAddress node, folding in the constant
5206 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00005207 unsigned char OpFlags =
5208 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005209 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00005210 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005211 if (OpFlags == X86II::MO_NO_FLAG &&
5212 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00005213 // A direct static reference to a global.
Dale Johannesen60b3ba02009-07-21 00:12:29 +00005214 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00005215 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005216 } else {
Chris Lattnerb1acd682009-06-27 05:39:56 +00005217 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00005218 }
Eric Christopherfd179292009-08-27 18:07:15 +00005219
Chris Lattner4f066492009-07-11 20:29:19 +00005220 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005221 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00005222 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
5223 else
5224 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00005225
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005226 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00005227 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00005228 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5229 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005230 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005231 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005232
Chris Lattner36c25012009-07-10 07:34:39 +00005233 // For globals that require a load from a stub to get the address, emit the
5234 // load.
5235 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00005236 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
David Greene67c9d422010-02-15 16:53:33 +00005237 PseudoSourceValue::getGOT(), 0, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005238
Dan Gohman6520e202008-10-18 02:06:02 +00005239 // If there was a non-zero offset that we didn't fold, create an explicit
5240 // addition for it.
5241 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00005242 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00005243 DAG.getConstant(Offset, getPointerTy()));
5244
Evan Cheng0db9fe62006-04-25 20:13:52 +00005245 return Result;
5246}
5247
Evan Chengda43bcf2008-09-24 00:05:32 +00005248SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005249X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00005250 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00005251 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005252 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00005253}
5254
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005255static SDValue
5256GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00005257 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00005258 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00005259 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Owen Anderson825b72b2009-08-11 20:47:22 +00005260 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005261 DebugLoc dl = GA->getDebugLoc();
5262 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
5263 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00005264 GA->getOffset(),
5265 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005266 if (InFlag) {
5267 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00005268 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005269 } else {
5270 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00005271 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005272 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00005273
5274 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
5275 MFI->setHasCalls(true);
5276
Rafael Espindola15f1b662009-04-24 12:59:40 +00005277 SDValue Flag = Chain.getValue(1);
5278 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005279}
5280
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005281// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00005282static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005283LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005284 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005285 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00005286 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
5287 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005288 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005289 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005290 InFlag = Chain.getValue(1);
5291
Chris Lattnerb903bed2009-06-26 21:20:29 +00005292 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005293}
5294
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005295// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00005296static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005297LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005298 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00005299 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
5300 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005301}
5302
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005303// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
5304// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00005305static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005306 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00005307 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00005308 DebugLoc dl = GA->getDebugLoc();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005309 // Get the Thread Pointer
Rafael Espindola094fad32009-04-08 21:14:34 +00005310 SDValue Base = DAG.getNode(X86ISD::SegmentBaseAddress,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005311 DebugLoc(), PtrVT,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00005312 DAG.getRegister(is64Bit? X86::FS : X86::GS,
Owen Anderson825b72b2009-08-11 20:47:22 +00005313 MVT::i32));
Rafael Espindola094fad32009-04-08 21:14:34 +00005314
5315 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Base,
David Greene67c9d422010-02-15 16:53:33 +00005316 NULL, 0, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00005317
Chris Lattnerb903bed2009-06-26 21:20:29 +00005318 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005319 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
5320 // initialexec.
5321 unsigned WrapperKind = X86ISD::Wrapper;
5322 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00005323 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00005324 } else if (is64Bit) {
5325 assert(model == TLSModel::InitialExec);
5326 OperandFlags = X86II::MO_GOTTPOFF;
5327 WrapperKind = X86ISD::WrapperRIP;
5328 } else {
5329 assert(model == TLSModel::InitialExec);
5330 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00005331 }
Eric Christopherfd179292009-08-27 18:07:15 +00005332
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005333 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
5334 // exec)
Chris Lattner4150c082009-06-21 02:22:34 +00005335 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00005336 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00005337 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00005338
Rafael Espindola9a580232009-02-27 13:37:18 +00005339 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00005340 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
David Greene67c9d422010-02-15 16:53:33 +00005341 PseudoSourceValue::getGOT(), 0, false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00005342
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005343 // The address of the thread local variable is the add of the thread
5344 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00005345 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005346}
5347
Dan Gohman475871a2008-07-27 21:46:04 +00005348SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005349X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005350 // TODO: implement the "local dynamic" model
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00005351 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005352 assert(Subtarget->isTargetELF() &&
5353 "TLS not implemented for non-ELF targets");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005354 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00005355 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00005356
Chris Lattnerb903bed2009-06-26 21:20:29 +00005357 // If GV is an alias then use the aliasee for determining
5358 // thread-localness.
5359 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
5360 GV = GA->resolveAliasedGlobal(false);
Eric Christopherfd179292009-08-27 18:07:15 +00005361
Chris Lattnerb903bed2009-06-26 21:20:29 +00005362 TLSModel::Model model = getTLSModel(GV,
5363 getTargetMachine().getRelocationModel());
Eric Christopherfd179292009-08-27 18:07:15 +00005364
Chris Lattnerb903bed2009-06-26 21:20:29 +00005365 switch (model) {
5366 case TLSModel::GeneralDynamic:
5367 case TLSModel::LocalDynamic: // not implemented
5368 if (Subtarget->is64Bit())
Rafael Espindola9a580232009-02-27 13:37:18 +00005369 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
Chris Lattnerb903bed2009-06-26 21:20:29 +00005370 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00005371
Chris Lattnerb903bed2009-06-26 21:20:29 +00005372 case TLSModel::InitialExec:
5373 case TLSModel::LocalExec:
5374 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
5375 Subtarget->is64Bit());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005376 }
Eric Christopherfd179292009-08-27 18:07:15 +00005377
Torok Edwinc23197a2009-07-14 16:55:14 +00005378 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00005379 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005380}
5381
Evan Cheng0db9fe62006-04-25 20:13:52 +00005382
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005383/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00005384/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00005385SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00005386 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00005387 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005388 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005389 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005390 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00005391 SDValue ShOpLo = Op.getOperand(0);
5392 SDValue ShOpHi = Op.getOperand(1);
5393 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00005394 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00005395 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00005396 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00005397
Dan Gohman475871a2008-07-27 21:46:04 +00005398 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005399 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00005400 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
5401 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005402 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005403 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
5404 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005405 }
Evan Chenge3413162006-01-09 18:33:28 +00005406
Owen Anderson825b72b2009-08-11 20:47:22 +00005407 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
5408 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00005409 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00005410 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00005411
Dan Gohman475871a2008-07-27 21:46:04 +00005412 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00005413 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00005414 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
5415 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00005416
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005417 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00005418 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
5419 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005420 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005421 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
5422 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005423 }
5424
Dan Gohman475871a2008-07-27 21:46:04 +00005425 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00005426 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005427}
Evan Chenga3195e82006-01-12 22:54:21 +00005428
Dan Gohmand858e902010-04-17 15:26:15 +00005429SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
5430 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005431 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00005432
5433 if (SrcVT.isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005434 if (SrcVT == MVT::v2i32 && Op.getValueType() == MVT::v2f64) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005435 return Op;
5436 }
5437 return SDValue();
5438 }
5439
Owen Anderson825b72b2009-08-11 20:47:22 +00005440 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00005441 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00005442
Eli Friedman36df4992009-05-27 00:47:34 +00005443 // These are really Legal; return the operand so the caller accepts it as
5444 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00005445 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00005446 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00005447 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00005448 Subtarget->is64Bit()) {
5449 return Op;
5450 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005451
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005452 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005453 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005454 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005455 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005456 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00005457 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00005458 StackSlot,
David Greene67c9d422010-02-15 16:53:33 +00005459 PseudoSourceValue::getFixedStack(SSFI), 0,
5460 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00005461 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
5462}
Evan Cheng0db9fe62006-04-25 20:13:52 +00005463
Owen Andersone50ed302009-08-10 22:56:29 +00005464SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Eli Friedman948e95a2009-05-23 09:59:16 +00005465 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00005466 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005467 // Build the FILD
Eli Friedman948e95a2009-05-23 09:59:16 +00005468 DebugLoc dl = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00005469 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00005470 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005471 if (useSSE)
Owen Anderson825b72b2009-08-11 20:47:22 +00005472 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00005473 else
Owen Anderson825b72b2009-08-11 20:47:22 +00005474 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005475 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Dale Johannesenace16102009-02-03 19:33:06 +00005476 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD, dl,
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005477 Tys, Ops, array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005478
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005479 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005480 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00005481 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005482
5483 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
5484 // shouldn't be necessary except that RFP cannot be live across
5485 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005486 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005487 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005488 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00005489 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005490 SDValue Ops[] = {
5491 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
5492 };
5493 Chain = DAG.getNode(X86ISD::FST, dl, Tys, Ops, array_lengthof(Ops));
Dale Johannesenace16102009-02-03 19:33:06 +00005494 Result = DAG.getLoad(Op.getValueType(), dl, Chain, StackSlot,
David Greene67c9d422010-02-15 16:53:33 +00005495 PseudoSourceValue::getFixedStack(SSFI), 0,
5496 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005497 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005498
Evan Cheng0db9fe62006-04-25 20:13:52 +00005499 return Result;
5500}
5501
Bill Wendling8b8a6362009-01-17 03:56:04 +00005502// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00005503SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
5504 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00005505 // This algorithm is not obvious. Here it is in C code, more or less:
5506 /*
5507 double uint64_to_double( uint32_t hi, uint32_t lo ) {
5508 static const __m128i exp = { 0x4330000045300000ULL, 0 };
5509 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00005510
Bill Wendling8b8a6362009-01-17 03:56:04 +00005511 // Copy ints to xmm registers.
5512 __m128i xh = _mm_cvtsi32_si128( hi );
5513 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00005514
Bill Wendling8b8a6362009-01-17 03:56:04 +00005515 // Combine into low half of a single xmm register.
5516 __m128i x = _mm_unpacklo_epi32( xh, xl );
5517 __m128d d;
5518 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00005519
Bill Wendling8b8a6362009-01-17 03:56:04 +00005520 // Merge in appropriate exponents to give the integer bits the right
5521 // magnitude.
5522 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00005523
Bill Wendling8b8a6362009-01-17 03:56:04 +00005524 // Subtract away the biases to deal with the IEEE-754 double precision
5525 // implicit 1.
5526 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00005527
Bill Wendling8b8a6362009-01-17 03:56:04 +00005528 // All conversions up to here are exact. The correctly rounded result is
5529 // calculated using the current rounding mode using the following
5530 // horizontal add.
5531 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
5532 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
5533 // store doesn't really need to be here (except
5534 // maybe to zero the other double)
5535 return sd;
5536 }
5537 */
Dale Johannesen040225f2008-10-21 23:07:49 +00005538
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005539 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00005540 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00005541
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005542 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00005543 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00005544 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
5545 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
5546 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
5547 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00005548 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005549 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005550
Bill Wendling8b8a6362009-01-17 03:56:04 +00005551 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00005552 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005553 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00005554 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005555 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00005556 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005557 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005558
Owen Anderson825b72b2009-08-11 20:47:22 +00005559 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5560 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00005561 Op.getOperand(0),
5562 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005563 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5564 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00005565 Op.getOperand(0),
5566 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005567 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
5568 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005569 PseudoSourceValue::getConstantPool(), 0,
David Greene67c9d422010-02-15 16:53:33 +00005570 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00005571 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
5572 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Unpck2);
5573 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005574 PseudoSourceValue::getConstantPool(), 0,
David Greene67c9d422010-02-15 16:53:33 +00005575 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00005576 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005577
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005578 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00005579 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00005580 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
5581 DAG.getUNDEF(MVT::v2f64), ShufMask);
5582 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
5583 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005584 DAG.getIntPtrConstant(0));
5585}
5586
Bill Wendling8b8a6362009-01-17 03:56:04 +00005587// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00005588SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
5589 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005590 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00005591 // FP constant to bias correct the final result.
5592 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00005593 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005594
5595 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00005596 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5597 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005598 Op.getOperand(0),
5599 DAG.getIntPtrConstant(0)));
5600
Owen Anderson825b72b2009-08-11 20:47:22 +00005601 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5602 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00005603 DAG.getIntPtrConstant(0));
5604
5605 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00005606 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
5607 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005608 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005609 MVT::v2f64, Load)),
5610 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005611 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005612 MVT::v2f64, Bias)));
5613 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5614 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00005615 DAG.getIntPtrConstant(0));
5616
5617 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00005618 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005619
5620 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00005621 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00005622
Owen Anderson825b72b2009-08-11 20:47:22 +00005623 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005624 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00005625 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00005626 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005627 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00005628 }
5629
5630 // Handle final rounding.
5631 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00005632}
5633
Dan Gohmand858e902010-04-17 15:26:15 +00005634SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
5635 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00005636 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005637 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00005638
Evan Chenga06ec9e2009-01-19 08:08:22 +00005639 // Now not UINT_TO_FP is legal (it's marked custom), dag combiner won't
5640 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
5641 // the optimization here.
5642 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00005643 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00005644
Owen Andersone50ed302009-08-10 22:56:29 +00005645 EVT SrcVT = N0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00005646 if (SrcVT == MVT::i64) {
Eli Friedman36df4992009-05-27 00:47:34 +00005647 // We only handle SSE2 f64 target here; caller can expand the rest.
Owen Anderson825b72b2009-08-11 20:47:22 +00005648 if (Op.getValueType() != MVT::f64 || !X86ScalarSSEf64)
Daniel Dunbar82205572009-05-26 21:27:02 +00005649 return SDValue();
Bill Wendling030939c2009-01-17 07:40:19 +00005650
Bill Wendling8b8a6362009-01-17 03:56:04 +00005651 return LowerUINT_TO_FP_i64(Op, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00005652 } else if (SrcVT == MVT::i32 && X86ScalarSSEf64) {
Bill Wendling8b8a6362009-01-17 03:56:04 +00005653 return LowerUINT_TO_FP_i32(Op, DAG);
5654 }
5655
Owen Anderson825b72b2009-08-11 20:47:22 +00005656 assert(SrcVT == MVT::i32 && "Unknown UINT_TO_FP to lower!");
Eli Friedman948e95a2009-05-23 09:59:16 +00005657
5658 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00005659 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Eli Friedman948e95a2009-05-23 09:59:16 +00005660 SDValue WordOff = DAG.getConstant(4, getPointerTy());
5661 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
5662 getPointerTy(), StackSlot, WordOff);
5663 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
David Greene67c9d422010-02-15 16:53:33 +00005664 StackSlot, NULL, 0, false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005665 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
David Greene67c9d422010-02-15 16:53:33 +00005666 OffsetSlot, NULL, 0, false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005667 return BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005668}
5669
Dan Gohman475871a2008-07-27 21:46:04 +00005670std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00005671FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005672 DebugLoc dl = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00005673
Owen Andersone50ed302009-08-10 22:56:29 +00005674 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00005675
5676 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005677 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
5678 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00005679 }
5680
Owen Anderson825b72b2009-08-11 20:47:22 +00005681 assert(DstTy.getSimpleVT() <= MVT::i64 &&
5682 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00005683 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00005684
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005685 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00005686 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00005687 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005688 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00005689 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00005690 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00005691 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005692 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005693
Evan Cheng87c89352007-10-15 20:11:21 +00005694 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
5695 // stack slot.
5696 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00005697 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00005698 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005699 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00005700
Evan Cheng0db9fe62006-04-25 20:13:52 +00005701 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00005702 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005703 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005704 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
5705 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
5706 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005707 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005708
Dan Gohman475871a2008-07-27 21:46:04 +00005709 SDValue Chain = DAG.getEntryNode();
5710 SDValue Value = Op.getOperand(0);
Chris Lattner78631162008-01-16 06:24:21 +00005711 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005712 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dale Johannesenace16102009-02-03 19:33:06 +00005713 Chain = DAG.getStore(Chain, dl, Value, StackSlot,
David Greene67c9d422010-02-15 16:53:33 +00005714 PseudoSourceValue::getFixedStack(SSFI), 0,
5715 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005716 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00005717 SDValue Ops[] = {
Chris Lattner5a88b832007-02-25 07:10:00 +00005718 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
5719 };
Dale Johannesenace16102009-02-03 19:33:06 +00005720 Value = DAG.getNode(X86ISD::FLD, dl, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005721 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00005722 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005723 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5724 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005725
Evan Cheng0db9fe62006-04-25 20:13:52 +00005726 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00005727 SDValue Ops[] = { Chain, Value, StackSlot };
Owen Anderson825b72b2009-08-11 20:47:22 +00005728 SDValue FIST = DAG.getNode(Opc, dl, MVT::Other, Ops, 3);
Evan Chengd9558e02006-01-06 00:43:03 +00005729
Chris Lattner27a6c732007-11-24 07:07:01 +00005730 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005731}
5732
Dan Gohmand858e902010-04-17 15:26:15 +00005733SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
5734 SelectionDAG &DAG) const {
Eli Friedman23ef1052009-06-06 03:57:58 +00005735 if (Op.getValueType().isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005736 if (Op.getValueType() == MVT::v2i32 &&
5737 Op.getOperand(0).getValueType() == MVT::v2f64) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005738 return Op;
5739 }
5740 return SDValue();
5741 }
5742
Eli Friedman948e95a2009-05-23 09:59:16 +00005743 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00005744 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00005745 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
5746 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00005747
Chris Lattner27a6c732007-11-24 07:07:01 +00005748 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005749 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
David Greene67c9d422010-02-15 16:53:33 +00005750 FIST, StackSlot, NULL, 0, false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00005751}
5752
Dan Gohmand858e902010-04-17 15:26:15 +00005753SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
5754 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00005755 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
5756 SDValue FIST = Vals.first, StackSlot = Vals.second;
5757 assert(FIST.getNode() && "Unexpected failure");
5758
5759 // Load the result.
5760 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
David Greene67c9d422010-02-15 16:53:33 +00005761 FIST, StackSlot, NULL, 0, false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00005762}
5763
Dan Gohmand858e902010-04-17 15:26:15 +00005764SDValue X86TargetLowering::LowerFABS(SDValue Op,
5765 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005766 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005767 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005768 EVT VT = Op.getValueType();
5769 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005770 if (VT.isVector())
5771 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005772 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005773 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005774 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00005775 CV.push_back(C);
5776 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005777 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005778 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00005779 CV.push_back(C);
5780 CV.push_back(C);
5781 CV.push_back(C);
5782 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005783 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005784 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005785 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005786 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005787 PseudoSourceValue::getConstantPool(), 0,
5788 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005789 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005790}
5791
Dan Gohmand858e902010-04-17 15:26:15 +00005792SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005793 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005794 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005795 EVT VT = Op.getValueType();
5796 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00005797 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00005798 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005799 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005800 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005801 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00005802 CV.push_back(C);
5803 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005804 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005805 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00005806 CV.push_back(C);
5807 CV.push_back(C);
5808 CV.push_back(C);
5809 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005810 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005811 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005812 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005813 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005814 PseudoSourceValue::getConstantPool(), 0,
5815 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005816 if (VT.isVector()) {
Dale Johannesenace16102009-02-03 19:33:06 +00005817 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00005818 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
5819 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005820 Op.getOperand(0)),
Owen Anderson825b72b2009-08-11 20:47:22 +00005821 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00005822 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005823 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00005824 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005825}
5826
Dan Gohmand858e902010-04-17 15:26:15 +00005827SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005828 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00005829 SDValue Op0 = Op.getOperand(0);
5830 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005831 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005832 EVT VT = Op.getValueType();
5833 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00005834
5835 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005836 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005837 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005838 SrcVT = VT;
5839 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005840 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005841 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005842 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005843 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005844 }
5845
5846 // At this point the operands and the result should have the same
5847 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00005848
Evan Cheng68c47cb2007-01-05 07:55:56 +00005849 // First get the sign bit of second operand.
5850 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005851 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005852 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
5853 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005854 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005855 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
5856 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5857 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5858 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005859 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005860 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005861 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005862 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005863 PseudoSourceValue::getConstantPool(), 0,
5864 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005865 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005866
5867 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005868 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005869 // Op0 is MVT::f32, Op1 is MVT::f64.
5870 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
5871 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
5872 DAG.getConstant(32, MVT::i32));
5873 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, SignBit);
5874 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00005875 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005876 }
5877
Evan Cheng73d6cf12007-01-05 21:37:56 +00005878 // Clear first operand sign bit.
5879 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00005880 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005881 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
5882 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005883 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005884 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
5885 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5886 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5887 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005888 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005889 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005890 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005891 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005892 PseudoSourceValue::getConstantPool(), 0,
5893 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005894 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005895
5896 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00005897 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005898}
5899
Dan Gohman076aee32009-03-04 19:44:21 +00005900/// Emit nodes that will be selected as "test Op0,Op0", or something
5901/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005902SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00005903 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00005904 DebugLoc dl = Op.getDebugLoc();
5905
Dan Gohman31125812009-03-07 01:58:32 +00005906 // CF and OF aren't always set the way we want. Determine which
5907 // of these we need.
5908 bool NeedCF = false;
5909 bool NeedOF = false;
5910 switch (X86CC) {
5911 case X86::COND_A: case X86::COND_AE:
5912 case X86::COND_B: case X86::COND_BE:
5913 NeedCF = true;
5914 break;
5915 case X86::COND_G: case X86::COND_GE:
5916 case X86::COND_L: case X86::COND_LE:
5917 case X86::COND_O: case X86::COND_NO:
5918 NeedOF = true;
5919 break;
5920 default: break;
5921 }
5922
Dan Gohman076aee32009-03-04 19:44:21 +00005923 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00005924 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
5925 // we prove that the arithmetic won't overflow, we can't use OF or CF.
5926 if (Op.getResNo() == 0 && !NeedOF && !NeedCF) {
Dan Gohman076aee32009-03-04 19:44:21 +00005927 unsigned Opcode = 0;
Dan Gohman51bb4742009-03-05 21:29:28 +00005928 unsigned NumOperands = 0;
Dan Gohman076aee32009-03-04 19:44:21 +00005929 switch (Op.getNode()->getOpcode()) {
5930 case ISD::ADD:
Stuart Hastings5a6a65b2010-04-28 00:35:10 +00005931 // Due to an isel shortcoming, be conservative if this add is
5932 // likely to be selected as part of a load-modify-store
5933 // instruction. When the root node in a match is a store, isel
5934 // doesn't know how to remap non-chain non-flag uses of other
5935 // nodes in the match, such as the ADD in this case. This leads
5936 // to the ADD being left around and reselected, with the result
5937 // being two adds in the output. Alas, even if none our users
5938 // are stores, that doesn't prove we're O.K. Ergo, if we have
5939 // any parents that aren't CopyToReg or SETCC, eschew INC/DEC.
5940 // A better fix seems to require climbing the DAG back to the
5941 // root, and it doesn't seem to be worth the effort.
Dan Gohman076aee32009-03-04 19:44:21 +00005942 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Stuart Hastings5a6a65b2010-04-28 00:35:10 +00005943 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5944 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
Dan Gohman076aee32009-03-04 19:44:21 +00005945 goto default_case;
Dan Gohman076aee32009-03-04 19:44:21 +00005946 if (ConstantSDNode *C =
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005947 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
5948 // An add of one will be selected as an INC.
Dan Gohman076aee32009-03-04 19:44:21 +00005949 if (C->getAPIntValue() == 1) {
5950 Opcode = X86ISD::INC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005951 NumOperands = 1;
Dan Gohman076aee32009-03-04 19:44:21 +00005952 break;
5953 }
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005954 // An add of negative one (subtract of one) will be selected as a DEC.
5955 if (C->getAPIntValue().isAllOnesValue()) {
5956 Opcode = X86ISD::DEC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005957 NumOperands = 1;
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005958 break;
5959 }
5960 }
Dan Gohman076aee32009-03-04 19:44:21 +00005961 // Otherwise use a regular EFLAGS-setting add.
5962 Opcode = X86ISD::ADD;
Dan Gohman51bb4742009-03-05 21:29:28 +00005963 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005964 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00005965 case ISD::AND: {
5966 // If the primary and result isn't used, don't bother using X86ISD::AND,
5967 // because a TEST instruction will be better.
5968 bool NonFlagUse = false;
5969 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Evan Cheng17751da2010-01-07 00:54:06 +00005970 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
5971 SDNode *User = *UI;
5972 unsigned UOpNo = UI.getOperandNo();
5973 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
5974 // Look pass truncate.
5975 UOpNo = User->use_begin().getOperandNo();
5976 User = *User->use_begin();
5977 }
5978 if (User->getOpcode() != ISD::BRCOND &&
5979 User->getOpcode() != ISD::SETCC &&
5980 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
Dan Gohmane220c4b2009-09-18 19:59:53 +00005981 NonFlagUse = true;
5982 break;
5983 }
Evan Cheng17751da2010-01-07 00:54:06 +00005984 }
Dan Gohmane220c4b2009-09-18 19:59:53 +00005985 if (!NonFlagUse)
5986 break;
5987 }
5988 // FALL THROUGH
Dan Gohman076aee32009-03-04 19:44:21 +00005989 case ISD::SUB:
Dan Gohmane220c4b2009-09-18 19:59:53 +00005990 case ISD::OR:
5991 case ISD::XOR:
5992 // Due to the ISEL shortcoming noted above, be conservative if this op is
Dan Gohman076aee32009-03-04 19:44:21 +00005993 // likely to be selected as part of a load-modify-store instruction.
5994 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5995 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5996 if (UI->getOpcode() == ISD::STORE)
5997 goto default_case;
Dan Gohmane220c4b2009-09-18 19:59:53 +00005998 // Otherwise use a regular EFLAGS-setting instruction.
5999 switch (Op.getNode()->getOpcode()) {
6000 case ISD::SUB: Opcode = X86ISD::SUB; break;
6001 case ISD::OR: Opcode = X86ISD::OR; break;
6002 case ISD::XOR: Opcode = X86ISD::XOR; break;
6003 case ISD::AND: Opcode = X86ISD::AND; break;
6004 default: llvm_unreachable("unexpected operator!");
6005 }
Dan Gohman51bb4742009-03-05 21:29:28 +00006006 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00006007 break;
6008 case X86ISD::ADD:
6009 case X86ISD::SUB:
6010 case X86ISD::INC:
6011 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +00006012 case X86ISD::OR:
6013 case X86ISD::XOR:
6014 case X86ISD::AND:
Dan Gohman076aee32009-03-04 19:44:21 +00006015 return SDValue(Op.getNode(), 1);
6016 default:
6017 default_case:
6018 break;
6019 }
6020 if (Opcode != 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006021 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
Dan Gohman076aee32009-03-04 19:44:21 +00006022 SmallVector<SDValue, 4> Ops;
Dan Gohman31125812009-03-07 01:58:32 +00006023 for (unsigned i = 0; i != NumOperands; ++i)
Dan Gohman076aee32009-03-04 19:44:21 +00006024 Ops.push_back(Op.getOperand(i));
Dan Gohmanfc166572009-04-09 23:54:40 +00006025 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
Dan Gohman076aee32009-03-04 19:44:21 +00006026 DAG.ReplaceAllUsesWith(Op, New);
6027 return SDValue(New.getNode(), 1);
6028 }
6029 }
6030
6031 // Otherwise just emit a CMP with 0, which is the TEST pattern.
Owen Anderson825b72b2009-08-11 20:47:22 +00006032 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
Dan Gohman076aee32009-03-04 19:44:21 +00006033 DAG.getConstant(0, Op.getValueType()));
6034}
6035
6036/// Emit nodes that will be selected as "cmp Op0,Op1", or something
6037/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00006038SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00006039 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00006040 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
6041 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00006042 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00006043
6044 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00006045 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00006046}
6047
Evan Chengd40d03e2010-01-06 19:38:29 +00006048/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
6049/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00006050SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
6051 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00006052 SDValue Op0 = And.getOperand(0);
6053 SDValue Op1 = And.getOperand(1);
6054 if (Op0.getOpcode() == ISD::TRUNCATE)
6055 Op0 = Op0.getOperand(0);
6056 if (Op1.getOpcode() == ISD::TRUNCATE)
6057 Op1 = Op1.getOperand(0);
6058
Evan Chengd40d03e2010-01-06 19:38:29 +00006059 SDValue LHS, RHS;
Evan Cheng2c755ba2010-02-27 07:36:59 +00006060 if (Op1.getOpcode() == ISD::SHL) {
6061 if (ConstantSDNode *And10C = dyn_cast<ConstantSDNode>(Op1.getOperand(0)))
6062 if (And10C->getZExtValue() == 1) {
6063 LHS = Op0;
6064 RHS = Op1.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00006065 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00006066 } else if (Op0.getOpcode() == ISD::SHL) {
6067 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
6068 if (And00C->getZExtValue() == 1) {
6069 LHS = Op1;
6070 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00006071 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00006072 } else if (Op1.getOpcode() == ISD::Constant) {
6073 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
6074 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00006075 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
6076 LHS = AndLHS.getOperand(0);
6077 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00006078 }
Evan Chengd40d03e2010-01-06 19:38:29 +00006079 }
Evan Cheng0488db92007-09-25 01:57:46 +00006080
Evan Chengd40d03e2010-01-06 19:38:29 +00006081 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00006082 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00006083 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00006084 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00006085 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00006086 // Also promote i16 to i32 for performance / code size reason.
6087 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00006088 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00006089 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00006090
Evan Chengd40d03e2010-01-06 19:38:29 +00006091 // If the operand types disagree, extend the shift amount to match. Since
6092 // BT ignores high bits (like shifts) we can use anyextend.
6093 if (LHS.getValueType() != RHS.getValueType())
6094 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00006095
Evan Chengd40d03e2010-01-06 19:38:29 +00006096 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
6097 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
6098 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6099 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00006100 }
6101
Evan Cheng54de3ea2010-01-05 06:52:31 +00006102 return SDValue();
6103}
6104
Dan Gohmand858e902010-04-17 15:26:15 +00006105SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng54de3ea2010-01-05 06:52:31 +00006106 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
6107 SDValue Op0 = Op.getOperand(0);
6108 SDValue Op1 = Op.getOperand(1);
6109 DebugLoc dl = Op.getDebugLoc();
6110 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
6111
6112 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00006113 // Lower (X & (1 << N)) == 0 to BT(X, N).
6114 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
6115 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
6116 if (Op0.getOpcode() == ISD::AND &&
6117 Op0.hasOneUse() &&
6118 Op1.getOpcode() == ISD::Constant &&
6119 cast<ConstantSDNode>(Op1)->getZExtValue() == 0 &&
6120 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
6121 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
6122 if (NewSetCC.getNode())
6123 return NewSetCC;
6124 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00006125
Evan Cheng2c755ba2010-02-27 07:36:59 +00006126 // Look for "(setcc) == / != 1" to avoid unncessary setcc.
6127 if (Op0.getOpcode() == X86ISD::SETCC &&
6128 Op1.getOpcode() == ISD::Constant &&
6129 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
6130 cast<ConstantSDNode>(Op1)->isNullValue()) &&
6131 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
6132 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
6133 bool Invert = (CC == ISD::SETNE) ^
6134 cast<ConstantSDNode>(Op1)->isNullValue();
6135 if (Invert)
6136 CCode = X86::GetOppositeBranchCondition(CCode);
6137 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6138 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
6139 }
6140
Evan Chenge5b51ac2010-04-17 06:13:15 +00006141 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00006142 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00006143 if (X86CC == X86::COND_INVALID)
6144 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00006145
Evan Cheng552f09a2010-04-26 19:06:11 +00006146 SDValue Cond = EmitCmp(Op0, Op1, X86CC, DAG);
Evan Chengad9c0a32009-12-15 00:53:42 +00006147
6148 // Use sbb x, x to materialize carry bit into a GPR.
Evan Cheng2e489c42009-12-16 00:53:11 +00006149 if (X86CC == X86::COND_B)
Evan Chengad9c0a32009-12-15 00:53:42 +00006150 return DAG.getNode(ISD::AND, dl, MVT::i8,
6151 DAG.getNode(X86ISD::SETCC_CARRY, dl, MVT::i8,
6152 DAG.getConstant(X86CC, MVT::i8), Cond),
6153 DAG.getConstant(1, MVT::i8));
Evan Chengad9c0a32009-12-15 00:53:42 +00006154
Owen Anderson825b72b2009-08-11 20:47:22 +00006155 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6156 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00006157}
6158
Dan Gohmand858e902010-04-17 15:26:15 +00006159SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00006160 SDValue Cond;
6161 SDValue Op0 = Op.getOperand(0);
6162 SDValue Op1 = Op.getOperand(1);
6163 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00006164 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00006165 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
6166 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006167 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00006168
6169 if (isFP) {
6170 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00006171 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00006172 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
6173 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00006174 bool Swap = false;
6175
6176 switch (SetCCOpcode) {
6177 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00006178 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00006179 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00006180 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00006181 case ISD::SETGT: Swap = true; // Fallthrough
6182 case ISD::SETLT:
6183 case ISD::SETOLT: SSECC = 1; break;
6184 case ISD::SETOGE:
6185 case ISD::SETGE: Swap = true; // Fallthrough
6186 case ISD::SETLE:
6187 case ISD::SETOLE: SSECC = 2; break;
6188 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00006189 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00006190 case ISD::SETNE: SSECC = 4; break;
6191 case ISD::SETULE: Swap = true;
6192 case ISD::SETUGE: SSECC = 5; break;
6193 case ISD::SETULT: Swap = true;
6194 case ISD::SETUGT: SSECC = 6; break;
6195 case ISD::SETO: SSECC = 7; break;
6196 }
6197 if (Swap)
6198 std::swap(Op0, Op1);
6199
Nate Begemanfb8ead02008-07-25 19:05:58 +00006200 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00006201 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00006202 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00006203 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00006204 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
6205 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00006206 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00006207 }
6208 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00006209 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00006210 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
6211 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00006212 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00006213 }
Torok Edwinc23197a2009-07-14 16:55:14 +00006214 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00006215 }
6216 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00006217 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00006218 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006219
Nate Begeman30a0de92008-07-17 16:51:19 +00006220 // We are handling one of the integer comparisons here. Since SSE only has
6221 // GT and EQ comparisons for integer, swapping operands and multiple
6222 // operations may be required for some comparisons.
6223 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
6224 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00006225
Owen Anderson825b72b2009-08-11 20:47:22 +00006226 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00006227 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00006228 case MVT::v8i8:
6229 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
6230 case MVT::v4i16:
6231 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
6232 case MVT::v2i32:
6233 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
6234 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00006235 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006236
Nate Begeman30a0de92008-07-17 16:51:19 +00006237 switch (SetCCOpcode) {
6238 default: break;
6239 case ISD::SETNE: Invert = true;
6240 case ISD::SETEQ: Opc = EQOpc; break;
6241 case ISD::SETLT: Swap = true;
6242 case ISD::SETGT: Opc = GTOpc; break;
6243 case ISD::SETGE: Swap = true;
6244 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
6245 case ISD::SETULT: Swap = true;
6246 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
6247 case ISD::SETUGE: Swap = true;
6248 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
6249 }
6250 if (Swap)
6251 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006252
Nate Begeman30a0de92008-07-17 16:51:19 +00006253 // Since SSE has no unsigned integer comparisons, we need to flip the sign
6254 // bits of the inputs before performing those operations.
6255 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00006256 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00006257 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
6258 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00006259 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00006260 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
6261 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00006262 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
6263 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00006264 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006265
Dale Johannesenace16102009-02-03 19:33:06 +00006266 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00006267
6268 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00006269 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00006270 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00006271
Nate Begeman30a0de92008-07-17 16:51:19 +00006272 return Result;
6273}
Evan Cheng0488db92007-09-25 01:57:46 +00006274
Evan Cheng370e5342008-12-03 08:38:43 +00006275// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00006276static bool isX86LogicalCmp(SDValue Op) {
6277 unsigned Opc = Op.getNode()->getOpcode();
6278 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
6279 return true;
6280 if (Op.getResNo() == 1 &&
6281 (Opc == X86ISD::ADD ||
6282 Opc == X86ISD::SUB ||
6283 Opc == X86ISD::SMUL ||
6284 Opc == X86ISD::UMUL ||
6285 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00006286 Opc == X86ISD::DEC ||
6287 Opc == X86ISD::OR ||
6288 Opc == X86ISD::XOR ||
6289 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00006290 return true;
6291
6292 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00006293}
6294
Dan Gohmand858e902010-04-17 15:26:15 +00006295SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00006296 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00006297 SDValue Cond = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006298 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00006299 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00006300
Dan Gohman1a492952009-10-20 16:22:37 +00006301 if (Cond.getOpcode() == ISD::SETCC) {
6302 SDValue NewCond = LowerSETCC(Cond, DAG);
6303 if (NewCond.getNode())
6304 Cond = NewCond;
6305 }
Evan Cheng734503b2006-09-11 02:19:56 +00006306
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006307 // (select (x == 0), -1, 0) -> (sign_bit (x - 1))
6308 SDValue Op1 = Op.getOperand(1);
6309 SDValue Op2 = Op.getOperand(2);
6310 if (Cond.getOpcode() == X86ISD::SETCC &&
6311 cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue() == X86::COND_E) {
6312 SDValue Cmp = Cond.getOperand(1);
6313 if (Cmp.getOpcode() == X86ISD::CMP) {
6314 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op1);
6315 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
6316 ConstantSDNode *RHSC =
6317 dyn_cast<ConstantSDNode>(Cmp.getOperand(1).getNode());
6318 if (N1C && N1C->isAllOnesValue() &&
6319 N2C && N2C->isNullValue() &&
6320 RHSC && RHSC->isNullValue()) {
6321 SDValue CmpOp0 = Cmp.getOperand(0);
Chris Lattnerda0688e2010-03-14 18:44:35 +00006322 Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006323 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
6324 return DAG.getNode(X86ISD::SETCC_CARRY, dl, Op.getValueType(),
6325 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
6326 }
6327 }
6328 }
6329
Evan Chengad9c0a32009-12-15 00:53:42 +00006330 // Look pass (and (setcc_carry (cmp ...)), 1).
6331 if (Cond.getOpcode() == ISD::AND &&
6332 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
6333 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
6334 if (C && C->getAPIntValue() == 1)
6335 Cond = Cond.getOperand(0);
6336 }
6337
Evan Cheng3f41d662007-10-08 22:16:29 +00006338 // If condition flag is set by a X86ISD::CMP, then use it as the condition
6339 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00006340 if (Cond.getOpcode() == X86ISD::SETCC ||
6341 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00006342 CC = Cond.getOperand(0);
6343
Dan Gohman475871a2008-07-27 21:46:04 +00006344 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00006345 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00006346 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00006347
Evan Cheng3f41d662007-10-08 22:16:29 +00006348 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006349 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00006350 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00006351 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00006352
Chris Lattnerd1980a52009-03-12 06:52:53 +00006353 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
6354 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00006355 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00006356 addTest = false;
6357 }
6358 }
6359
6360 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00006361 // Look pass the truncate.
6362 if (Cond.getOpcode() == ISD::TRUNCATE)
6363 Cond = Cond.getOperand(0);
6364
6365 // We know the result of AND is compared against zero. Try to match
6366 // it to BT.
6367 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
6368 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
6369 if (NewSetCC.getNode()) {
6370 CC = NewSetCC.getOperand(0);
6371 Cond = NewSetCC.getOperand(1);
6372 addTest = false;
6373 }
6374 }
6375 }
6376
6377 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006378 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00006379 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00006380 }
6381
Evan Cheng0488db92007-09-25 01:57:46 +00006382 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
6383 // condition is true.
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006384 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
6385 SDValue Ops[] = { Op2, Op1, CC, Cond };
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006386 return DAG.getNode(X86ISD::CMOV, dl, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00006387}
6388
Evan Cheng370e5342008-12-03 08:38:43 +00006389// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
6390// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
6391// from the AND / OR.
6392static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
6393 Opc = Op.getOpcode();
6394 if (Opc != ISD::OR && Opc != ISD::AND)
6395 return false;
6396 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
6397 Op.getOperand(0).hasOneUse() &&
6398 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
6399 Op.getOperand(1).hasOneUse());
6400}
6401
Evan Cheng961d6d42009-02-02 08:19:07 +00006402// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
6403// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00006404static bool isXor1OfSetCC(SDValue Op) {
6405 if (Op.getOpcode() != ISD::XOR)
6406 return false;
6407 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6408 if (N1C && N1C->getAPIntValue() == 1) {
6409 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
6410 Op.getOperand(0).hasOneUse();
6411 }
6412 return false;
6413}
6414
Dan Gohmand858e902010-04-17 15:26:15 +00006415SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00006416 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00006417 SDValue Chain = Op.getOperand(0);
6418 SDValue Cond = Op.getOperand(1);
6419 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006420 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00006421 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00006422
Dan Gohman1a492952009-10-20 16:22:37 +00006423 if (Cond.getOpcode() == ISD::SETCC) {
6424 SDValue NewCond = LowerSETCC(Cond, DAG);
6425 if (NewCond.getNode())
6426 Cond = NewCond;
6427 }
Chris Lattnere55484e2008-12-25 05:34:37 +00006428#if 0
6429 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00006430 else if (Cond.getOpcode() == X86ISD::ADD ||
6431 Cond.getOpcode() == X86ISD::SUB ||
6432 Cond.getOpcode() == X86ISD::SMUL ||
6433 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00006434 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00006435#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00006436
Evan Chengad9c0a32009-12-15 00:53:42 +00006437 // Look pass (and (setcc_carry (cmp ...)), 1).
6438 if (Cond.getOpcode() == ISD::AND &&
6439 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
6440 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
6441 if (C && C->getAPIntValue() == 1)
6442 Cond = Cond.getOperand(0);
6443 }
6444
Evan Cheng3f41d662007-10-08 22:16:29 +00006445 // If condition flag is set by a X86ISD::CMP, then use it as the condition
6446 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00006447 if (Cond.getOpcode() == X86ISD::SETCC ||
6448 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00006449 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006450
Dan Gohman475871a2008-07-27 21:46:04 +00006451 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00006452 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00006453 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00006454 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00006455 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00006456 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00006457 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00006458 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00006459 default: break;
6460 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00006461 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00006462 // These can only come from an arithmetic instruction with overflow,
6463 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00006464 Cond = Cond.getNode()->getOperand(1);
6465 addTest = false;
6466 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00006467 }
Evan Cheng0488db92007-09-25 01:57:46 +00006468 }
Evan Cheng370e5342008-12-03 08:38:43 +00006469 } else {
6470 unsigned CondOpc;
6471 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
6472 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00006473 if (CondOpc == ISD::OR) {
6474 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
6475 // two branches instead of an explicit OR instruction with a
6476 // separate test.
6477 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00006478 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00006479 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006480 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00006481 Chain, Dest, CC, Cmp);
6482 CC = Cond.getOperand(1).getOperand(0);
6483 Cond = Cmp;
6484 addTest = false;
6485 }
6486 } else { // ISD::AND
6487 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
6488 // two branches instead of an explicit AND instruction with a
6489 // separate test. However, we only do this if this block doesn't
6490 // have a fall-through edge, because this requires an explicit
6491 // jmp when the condition is false.
6492 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00006493 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00006494 Op.getNode()->hasOneUse()) {
6495 X86::CondCode CCode =
6496 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
6497 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006498 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00006499 SDValue User = SDValue(*Op.getNode()->use_begin(), 0);
6500 // Look for an unconditional branch following this conditional branch.
6501 // We need this because we need to reverse the successors in order
6502 // to implement FCMP_OEQ.
6503 if (User.getOpcode() == ISD::BR) {
6504 SDValue FalseBB = User.getOperand(1);
6505 SDValue NewBR =
6506 DAG.UpdateNodeOperands(User, User.getOperand(0), Dest);
6507 assert(NewBR == User);
6508 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00006509
Dale Johannesene4d209d2009-02-03 20:21:25 +00006510 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00006511 Chain, Dest, CC, Cmp);
6512 X86::CondCode CCode =
6513 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
6514 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006515 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00006516 Cond = Cmp;
6517 addTest = false;
6518 }
6519 }
Dan Gohman279c22e2008-10-21 03:29:32 +00006520 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00006521 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
6522 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
6523 // It should be transformed during dag combiner except when the condition
6524 // is set by a arithmetics with overflow node.
6525 X86::CondCode CCode =
6526 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
6527 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006528 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00006529 Cond = Cond.getOperand(0).getOperand(1);
6530 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00006531 }
Evan Cheng0488db92007-09-25 01:57:46 +00006532 }
6533
6534 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00006535 // Look pass the truncate.
6536 if (Cond.getOpcode() == ISD::TRUNCATE)
6537 Cond = Cond.getOperand(0);
6538
6539 // We know the result of AND is compared against zero. Try to match
6540 // it to BT.
6541 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
6542 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
6543 if (NewSetCC.getNode()) {
6544 CC = NewSetCC.getOperand(0);
6545 Cond = NewSetCC.getOperand(1);
6546 addTest = false;
6547 }
6548 }
6549 }
6550
6551 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006552 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00006553 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00006554 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00006555 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00006556 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00006557}
6558
Anton Korobeynikove060b532007-04-17 19:34:00 +00006559
6560// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
6561// Calls to _alloca is needed to probe the stack when allocating more than 4k
6562// bytes in one go. Touching the stack at 4K increments is necessary to ensure
6563// that the guard pages used by the OS virtual memory manager are allocated in
6564// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00006565SDValue
6566X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006567 SelectionDAG &DAG) const {
Anton Korobeynikove060b532007-04-17 19:34:00 +00006568 assert(Subtarget->isTargetCygMing() &&
6569 "This should be used only on Cygwin/Mingw targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006570 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006571
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006572 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00006573 SDValue Chain = Op.getOperand(0);
6574 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006575 // FIXME: Ensure alignment here
6576
Dan Gohman475871a2008-07-27 21:46:04 +00006577 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006578
Owen Andersone50ed302009-08-10 22:56:29 +00006579 EVT IntPtr = getPointerTy();
Owen Anderson825b72b2009-08-11 20:47:22 +00006580 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006581
Dale Johannesendd64c412009-02-04 00:33:20 +00006582 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006583 Flag = Chain.getValue(1);
6584
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00006585 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006586
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00006587 Chain = DAG.getNode(X86ISD::MINGW_ALLOCA, dl, NodeTys, Chain, Flag);
6588 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006589
Dale Johannesendd64c412009-02-04 00:33:20 +00006590 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006591
Dan Gohman475871a2008-07-27 21:46:04 +00006592 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006593 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006594}
6595
Dan Gohman475871a2008-07-27 21:46:04 +00006596SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00006597X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG, DebugLoc dl,
Bill Wendling6f287b22008-09-30 21:22:07 +00006598 SDValue Chain,
6599 SDValue Dst, SDValue Src,
6600 SDValue Size, unsigned Align,
Mon P Wang20adc9d2010-04-04 03:10:48 +00006601 bool isVolatile,
Bill Wendling6f287b22008-09-30 21:22:07 +00006602 const Value *DstSV,
Dan Gohmand858e902010-04-17 15:26:15 +00006603 uint64_t DstSVOff) const {
Dan Gohman707e0182008-04-12 04:36:06 +00006604 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006605
Bill Wendling6f287b22008-09-30 21:22:07 +00006606 // If not DWORD aligned or size is more than the threshold, call the library.
6607 // The libc version is likely to be faster for these cases. It can use the
6608 // address value and run time information about the CPU.
Evan Cheng1887c1c2008-08-21 21:00:15 +00006609 if ((Align & 3) != 0 ||
Dan Gohman707e0182008-04-12 04:36:06 +00006610 !ConstantSize ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006611 ConstantSize->getZExtValue() >
6612 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006613 SDValue InFlag(0, 0);
Dan Gohman68d599d2008-04-01 20:38:36 +00006614
6615 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohman707e0182008-04-12 04:36:06 +00006616 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
Bill Wendling6f287b22008-09-30 21:22:07 +00006617
Bill Wendling6158d842008-10-01 00:59:58 +00006618 if (const char *bzeroEntry = V &&
6619 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00006620 EVT IntPtr = getPointerTy();
Owen Anderson1d0be152009-08-13 21:58:54 +00006621 const Type *IntPtrTy = TD->getIntPtrType(*DAG.getContext());
Scott Michelfdc40a02009-02-17 22:15:04 +00006622 TargetLowering::ArgListTy Args;
Bill Wendling6158d842008-10-01 00:59:58 +00006623 TargetLowering::ArgListEntry Entry;
6624 Entry.Node = Dst;
6625 Entry.Ty = IntPtrTy;
6626 Args.push_back(Entry);
6627 Entry.Node = Size;
6628 Args.push_back(Entry);
6629 std::pair<SDValue,SDValue> CallResult =
Owen Anderson1d0be152009-08-13 21:58:54 +00006630 LowerCallTo(Chain, Type::getVoidTy(*DAG.getContext()),
6631 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00006632 0, CallingConv::C, false, /*isReturnValueUsed=*/false,
Bill Wendling46ada192010-03-02 01:55:18 +00006633 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG, dl);
Bill Wendling6158d842008-10-01 00:59:58 +00006634 return CallResult.second;
Dan Gohman68d599d2008-04-01 20:38:36 +00006635 }
6636
Dan Gohman707e0182008-04-12 04:36:06 +00006637 // Otherwise have the target-independent code call memset.
Dan Gohman475871a2008-07-27 21:46:04 +00006638 return SDValue();
Evan Cheng48090aa2006-03-21 23:01:21 +00006639 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00006640
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006641 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00006642 SDValue InFlag(0, 0);
Owen Andersone50ed302009-08-10 22:56:29 +00006643 EVT AVT;
Dan Gohman475871a2008-07-27 21:46:04 +00006644 SDValue Count;
Dan Gohman707e0182008-04-12 04:36:06 +00006645 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006646 unsigned BytesLeft = 0;
6647 bool TwoRepStos = false;
6648 if (ValC) {
6649 unsigned ValReg;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006650 uint64_t Val = ValC->getZExtValue() & 255;
Evan Cheng5ced1d82006-04-06 23:23:56 +00006651
Evan Cheng0db9fe62006-04-25 20:13:52 +00006652 // If the value is a constant, then we can potentially use larger sets.
6653 switch (Align & 3) {
Evan Cheng1887c1c2008-08-21 21:00:15 +00006654 case 2: // WORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006655 AVT = MVT::i16;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006656 ValReg = X86::AX;
6657 Val = (Val << 8) | Val;
6658 break;
6659 case 0: // DWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006660 AVT = MVT::i32;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006661 ValReg = X86::EAX;
6662 Val = (Val << 8) | Val;
6663 Val = (Val << 16) | Val;
6664 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006665 AVT = MVT::i64;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006666 ValReg = X86::RAX;
6667 Val = (Val << 32) | Val;
6668 }
6669 break;
6670 default: // Byte aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006671 AVT = MVT::i8;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006672 ValReg = X86::AL;
6673 Count = DAG.getIntPtrConstant(SizeVal);
6674 break;
Evan Cheng80d428c2006-04-19 22:48:17 +00006675 }
6676
Owen Anderson825b72b2009-08-11 20:47:22 +00006677 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006678 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00006679 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
6680 BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00006681 }
6682
Dale Johannesen0f502f62009-02-03 22:26:09 +00006683 Chain = DAG.getCopyToReg(Chain, dl, ValReg, DAG.getConstant(Val, AVT),
Evan Cheng0db9fe62006-04-25 20:13:52 +00006684 InFlag);
6685 InFlag = Chain.getValue(1);
6686 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00006687 AVT = MVT::i8;
Dan Gohmanbcda2852008-04-16 01:32:32 +00006688 Count = DAG.getIntPtrConstant(SizeVal);
Dale Johannesen0f502f62009-02-03 22:26:09 +00006689 Chain = DAG.getCopyToReg(Chain, dl, X86::AL, Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006690 InFlag = Chain.getValue(1);
Evan Chengb9df0ca2006-03-22 02:53:00 +00006691 }
Evan Chengc78d3b42006-04-24 18:01:45 +00006692
Scott Michelfdc40a02009-02-17 22:15:04 +00006693 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006694 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00006695 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006696 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006697 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006698 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00006699 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006700 InFlag = Chain.getValue(1);
Evan Chenga0b3afb2006-03-27 07:00:16 +00006701
Owen Anderson825b72b2009-08-11 20:47:22 +00006702 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006703 SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
6704 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops, array_lengthof(Ops));
Evan Chengc78d3b42006-04-24 18:01:45 +00006705
Evan Cheng0db9fe62006-04-25 20:13:52 +00006706 if (TwoRepStos) {
6707 InFlag = Chain.getValue(1);
Dan Gohman707e0182008-04-12 04:36:06 +00006708 Count = Size;
Owen Andersone50ed302009-08-10 22:56:29 +00006709 EVT CVT = Count.getValueType();
Dale Johannesen0f502f62009-02-03 22:26:09 +00006710 SDValue Left = DAG.getNode(ISD::AND, dl, CVT, Count,
Owen Anderson825b72b2009-08-11 20:47:22 +00006711 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
6712 Chain = DAG.getCopyToReg(Chain, dl, (CVT == MVT::i64) ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006713 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00006714 Left, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006715 InFlag = Chain.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00006716 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006717 SDValue Ops[] = { Chain, DAG.getValueType(MVT::i8), InFlag };
6718 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops, array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006719 } else if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00006720 // Handle the last 1 - 7 bytes.
6721 unsigned Offset = SizeVal - BytesLeft;
Owen Andersone50ed302009-08-10 22:56:29 +00006722 EVT AddrVT = Dst.getValueType();
6723 EVT SizeVT = Size.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00006724
Dale Johannesen0f502f62009-02-03 22:26:09 +00006725 Chain = DAG.getMemset(Chain, dl,
6726 DAG.getNode(ISD::ADD, dl, AddrVT, Dst,
Dan Gohman707e0182008-04-12 04:36:06 +00006727 DAG.getConstant(Offset, AddrVT)),
6728 Src,
6729 DAG.getConstant(BytesLeft, SizeVT),
Mon P Wang20adc9d2010-04-04 03:10:48 +00006730 Align, isVolatile, DstSV, DstSVOff + Offset);
Evan Cheng386031a2006-03-24 07:29:27 +00006731 }
Evan Cheng11e15b32006-04-03 20:53:28 +00006732
Dan Gohman707e0182008-04-12 04:36:06 +00006733 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Evan Cheng0db9fe62006-04-25 20:13:52 +00006734 return Chain;
6735}
Evan Cheng11e15b32006-04-03 20:53:28 +00006736
Dan Gohman475871a2008-07-27 21:46:04 +00006737SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00006738X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng1887c1c2008-08-21 21:00:15 +00006739 SDValue Chain, SDValue Dst, SDValue Src,
6740 SDValue Size, unsigned Align,
Mon P Wang20adc9d2010-04-04 03:10:48 +00006741 bool isVolatile, bool AlwaysInline,
Dan Gohmand858e902010-04-17 15:26:15 +00006742 const Value *DstSV,
6743 uint64_t DstSVOff,
6744 const Value *SrcSV,
6745 uint64_t SrcSVOff) const {
Dan Gohman707e0182008-04-12 04:36:06 +00006746 // This requires the copy size to be a constant, preferrably
6747 // within a subtarget-specific limit.
6748 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
6749 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00006750 return SDValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006751 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman707e0182008-04-12 04:36:06 +00006752 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00006753 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00006754
Evan Cheng1887c1c2008-08-21 21:00:15 +00006755 /// If not DWORD aligned, call the library.
6756 if ((Align & 3) != 0)
6757 return SDValue();
6758
6759 // DWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006760 EVT AVT = MVT::i32;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006761 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006762 AVT = MVT::i64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006763
Duncan Sands83ec4b62008-06-06 12:08:01 +00006764 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00006765 unsigned CountVal = SizeVal / UBytes;
Dan Gohman475871a2008-07-27 21:46:04 +00006766 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng1887c1c2008-08-21 21:00:15 +00006767 unsigned BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00006768
Dan Gohman475871a2008-07-27 21:46:04 +00006769 SDValue InFlag(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006770 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006771 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00006772 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006773 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006774 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Evan Chengc3b0c342010-04-08 07:37:57 +00006775 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00006776 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006777 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006778 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RSI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006779 X86::ESI,
Dan Gohman707e0182008-04-12 04:36:06 +00006780 Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006781 InFlag = Chain.getValue(1);
6782
Owen Anderson825b72b2009-08-11 20:47:22 +00006783 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006784 SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
6785 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, dl, Tys, Ops,
6786 array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006787
Dan Gohman475871a2008-07-27 21:46:04 +00006788 SmallVector<SDValue, 4> Results;
Evan Cheng2749c722008-04-25 00:26:43 +00006789 Results.push_back(RepMovs);
Rafael Espindola068317b2007-09-28 12:53:01 +00006790 if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00006791 // Handle the last 1 - 7 bytes.
6792 unsigned Offset = SizeVal - BytesLeft;
Owen Andersone50ed302009-08-10 22:56:29 +00006793 EVT DstVT = Dst.getValueType();
6794 EVT SrcVT = Src.getValueType();
6795 EVT SizeVT = Size.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00006796 Results.push_back(DAG.getMemcpy(Chain, dl,
Dale Johannesen0f502f62009-02-03 22:26:09 +00006797 DAG.getNode(ISD::ADD, dl, DstVT, Dst,
Evan Cheng2749c722008-04-25 00:26:43 +00006798 DAG.getConstant(Offset, DstVT)),
Dale Johannesen0f502f62009-02-03 22:26:09 +00006799 DAG.getNode(ISD::ADD, dl, SrcVT, Src,
Evan Cheng2749c722008-04-25 00:26:43 +00006800 DAG.getConstant(Offset, SrcVT)),
Dan Gohman707e0182008-04-12 04:36:06 +00006801 DAG.getConstant(BytesLeft, SizeVT),
Mon P Wang20adc9d2010-04-04 03:10:48 +00006802 Align, isVolatile, AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00006803 DstSV, DstSVOff + Offset,
6804 SrcSV, SrcSVOff + Offset));
Evan Chengb067a1e2006-03-31 19:22:53 +00006805 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006806
Owen Anderson825b72b2009-08-11 20:47:22 +00006807 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesen0f502f62009-02-03 22:26:09 +00006808 &Results[0], Results.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00006809}
6810
Dan Gohmand858e902010-04-17 15:26:15 +00006811SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00006812 MachineFunction &MF = DAG.getMachineFunction();
6813 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
6814
Dan Gohman69de1932008-02-06 22:27:42 +00006815 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006816 DebugLoc dl = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00006817
Evan Cheng25ab6902006-09-08 06:48:29 +00006818 if (!Subtarget->is64Bit()) {
6819 // vastart just stores the address of the VarArgsFrameIndex slot into the
6820 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00006821 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
6822 getPointerTy());
David Greene67c9d422010-02-15 16:53:33 +00006823 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0,
6824 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006825 }
6826
6827 // __va_list_tag:
6828 // gp_offset (0 - 6 * 8)
6829 // fp_offset (48 - 48 + 8 * 16)
6830 // overflow_arg_area (point to parameters coming in memory).
6831 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00006832 SmallVector<SDValue, 8> MemOps;
6833 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00006834 // Store gp_offset
Dale Johannesene4d209d2009-02-03 20:21:25 +00006835 SDValue Store = DAG.getStore(Op.getOperand(0), dl,
Dan Gohman1e93df62010-04-17 14:41:14 +00006836 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
6837 MVT::i32),
David Greene67c9d422010-02-15 16:53:33 +00006838 FIN, SV, 0, false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006839 MemOps.push_back(Store);
6840
6841 // Store fp_offset
Scott Michelfdc40a02009-02-17 22:15:04 +00006842 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006843 FIN, DAG.getIntPtrConstant(4));
6844 Store = DAG.getStore(Op.getOperand(0), dl,
Dan Gohman1e93df62010-04-17 14:41:14 +00006845 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
6846 MVT::i32),
David Greene67c9d422010-02-15 16:53:33 +00006847 FIN, SV, 0, false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006848 MemOps.push_back(Store);
6849
6850 // Store ptr to overflow_arg_area
Scott Michelfdc40a02009-02-17 22:15:04 +00006851 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006852 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00006853 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
6854 getPointerTy());
David Greene67c9d422010-02-15 16:53:33 +00006855 Store = DAG.getStore(Op.getOperand(0), dl, OVFIN, FIN, SV, 0,
6856 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006857 MemOps.push_back(Store);
6858
6859 // Store ptr to reg_save_area.
Scott Michelfdc40a02009-02-17 22:15:04 +00006860 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006861 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00006862 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
6863 getPointerTy());
David Greene67c9d422010-02-15 16:53:33 +00006864 Store = DAG.getStore(Op.getOperand(0), dl, RSFIN, FIN, SV, 0,
6865 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006866 MemOps.push_back(Store);
Owen Anderson825b72b2009-08-11 20:47:22 +00006867 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006868 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00006869}
6870
Dan Gohmand858e902010-04-17 15:26:15 +00006871SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman9018e832008-05-10 01:26:14 +00006872 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
6873 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman475871a2008-07-27 21:46:04 +00006874 SDValue Chain = Op.getOperand(0);
6875 SDValue SrcPtr = Op.getOperand(1);
6876 SDValue SrcSV = Op.getOperand(2);
Dan Gohman9018e832008-05-10 01:26:14 +00006877
Chris Lattner75361b62010-04-07 22:58:41 +00006878 report_fatal_error("VAArgInst is not yet implemented for x86-64!");
Dan Gohman475871a2008-07-27 21:46:04 +00006879 return SDValue();
Dan Gohman9018e832008-05-10 01:26:14 +00006880}
6881
Dan Gohmand858e902010-04-17 15:26:15 +00006882SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00006883 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00006884 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00006885 SDValue Chain = Op.getOperand(0);
6886 SDValue DstPtr = Op.getOperand(1);
6887 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00006888 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
6889 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006890 DebugLoc dl = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00006891
Dale Johannesendd64c412009-02-04 00:33:20 +00006892 return DAG.getMemcpy(Chain, dl, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00006893 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
6894 false, DstSV, 0, SrcSV, 0);
Evan Chengae642192007-03-02 23:16:35 +00006895}
6896
Dan Gohman475871a2008-07-27 21:46:04 +00006897SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006898X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006899 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006900 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006901 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00006902 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00006903 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00006904 case Intrinsic::x86_sse_comieq_ss:
6905 case Intrinsic::x86_sse_comilt_ss:
6906 case Intrinsic::x86_sse_comile_ss:
6907 case Intrinsic::x86_sse_comigt_ss:
6908 case Intrinsic::x86_sse_comige_ss:
6909 case Intrinsic::x86_sse_comineq_ss:
6910 case Intrinsic::x86_sse_ucomieq_ss:
6911 case Intrinsic::x86_sse_ucomilt_ss:
6912 case Intrinsic::x86_sse_ucomile_ss:
6913 case Intrinsic::x86_sse_ucomigt_ss:
6914 case Intrinsic::x86_sse_ucomige_ss:
6915 case Intrinsic::x86_sse_ucomineq_ss:
6916 case Intrinsic::x86_sse2_comieq_sd:
6917 case Intrinsic::x86_sse2_comilt_sd:
6918 case Intrinsic::x86_sse2_comile_sd:
6919 case Intrinsic::x86_sse2_comigt_sd:
6920 case Intrinsic::x86_sse2_comige_sd:
6921 case Intrinsic::x86_sse2_comineq_sd:
6922 case Intrinsic::x86_sse2_ucomieq_sd:
6923 case Intrinsic::x86_sse2_ucomilt_sd:
6924 case Intrinsic::x86_sse2_ucomile_sd:
6925 case Intrinsic::x86_sse2_ucomigt_sd:
6926 case Intrinsic::x86_sse2_ucomige_sd:
6927 case Intrinsic::x86_sse2_ucomineq_sd: {
6928 unsigned Opc = 0;
6929 ISD::CondCode CC = ISD::SETCC_INVALID;
6930 switch (IntNo) {
6931 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006932 case Intrinsic::x86_sse_comieq_ss:
6933 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006934 Opc = X86ISD::COMI;
6935 CC = ISD::SETEQ;
6936 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006937 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006938 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006939 Opc = X86ISD::COMI;
6940 CC = ISD::SETLT;
6941 break;
6942 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006943 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006944 Opc = X86ISD::COMI;
6945 CC = ISD::SETLE;
6946 break;
6947 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006948 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006949 Opc = X86ISD::COMI;
6950 CC = ISD::SETGT;
6951 break;
6952 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006953 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006954 Opc = X86ISD::COMI;
6955 CC = ISD::SETGE;
6956 break;
6957 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006958 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006959 Opc = X86ISD::COMI;
6960 CC = ISD::SETNE;
6961 break;
6962 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006963 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006964 Opc = X86ISD::UCOMI;
6965 CC = ISD::SETEQ;
6966 break;
6967 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006968 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006969 Opc = X86ISD::UCOMI;
6970 CC = ISD::SETLT;
6971 break;
6972 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006973 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006974 Opc = X86ISD::UCOMI;
6975 CC = ISD::SETLE;
6976 break;
6977 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006978 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006979 Opc = X86ISD::UCOMI;
6980 CC = ISD::SETGT;
6981 break;
6982 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006983 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006984 Opc = X86ISD::UCOMI;
6985 CC = ISD::SETGE;
6986 break;
6987 case Intrinsic::x86_sse_ucomineq_ss:
6988 case Intrinsic::x86_sse2_ucomineq_sd:
6989 Opc = X86ISD::UCOMI;
6990 CC = ISD::SETNE;
6991 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006992 }
Evan Cheng734503b2006-09-11 02:19:56 +00006993
Dan Gohman475871a2008-07-27 21:46:04 +00006994 SDValue LHS = Op.getOperand(1);
6995 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00006996 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00006997 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00006998 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
6999 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7000 DAG.getConstant(X86CC, MVT::i8), Cond);
7001 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00007002 }
Eric Christopher71c67532009-07-29 00:28:05 +00007003 // ptest intrinsics. The intrinsic these come from are designed to return
Eric Christopher794bfed2009-07-29 01:01:19 +00007004 // an integer value, not just an instruction so lower it to the ptest
7005 // pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00007006 case Intrinsic::x86_sse41_ptestz:
7007 case Intrinsic::x86_sse41_ptestc:
7008 case Intrinsic::x86_sse41_ptestnzc:{
7009 unsigned X86CC = 0;
7010 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00007011 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Eric Christopher71c67532009-07-29 00:28:05 +00007012 case Intrinsic::x86_sse41_ptestz:
7013 // ZF = 1
7014 X86CC = X86::COND_E;
7015 break;
7016 case Intrinsic::x86_sse41_ptestc:
7017 // CF = 1
7018 X86CC = X86::COND_B;
7019 break;
Eric Christopherfd179292009-08-27 18:07:15 +00007020 case Intrinsic::x86_sse41_ptestnzc:
Eric Christopher71c67532009-07-29 00:28:05 +00007021 // ZF and CF = 0
7022 X86CC = X86::COND_A;
7023 break;
7024 }
Eric Christopherfd179292009-08-27 18:07:15 +00007025
Eric Christopher71c67532009-07-29 00:28:05 +00007026 SDValue LHS = Op.getOperand(1);
7027 SDValue RHS = Op.getOperand(2);
Owen Anderson825b72b2009-08-11 20:47:22 +00007028 SDValue Test = DAG.getNode(X86ISD::PTEST, dl, MVT::i32, LHS, RHS);
7029 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
7030 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
7031 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00007032 }
Evan Cheng5759f972008-05-04 09:15:50 +00007033
7034 // Fix vector shift instructions where the last operand is a non-immediate
7035 // i32 value.
7036 case Intrinsic::x86_sse2_pslli_w:
7037 case Intrinsic::x86_sse2_pslli_d:
7038 case Intrinsic::x86_sse2_pslli_q:
7039 case Intrinsic::x86_sse2_psrli_w:
7040 case Intrinsic::x86_sse2_psrli_d:
7041 case Intrinsic::x86_sse2_psrli_q:
7042 case Intrinsic::x86_sse2_psrai_w:
7043 case Intrinsic::x86_sse2_psrai_d:
7044 case Intrinsic::x86_mmx_pslli_w:
7045 case Intrinsic::x86_mmx_pslli_d:
7046 case Intrinsic::x86_mmx_pslli_q:
7047 case Intrinsic::x86_mmx_psrli_w:
7048 case Intrinsic::x86_mmx_psrli_d:
7049 case Intrinsic::x86_mmx_psrli_q:
7050 case Intrinsic::x86_mmx_psrai_w:
7051 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00007052 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00007053 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00007054 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00007055
7056 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00007057 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00007058 switch (IntNo) {
7059 case Intrinsic::x86_sse2_pslli_w:
7060 NewIntNo = Intrinsic::x86_sse2_psll_w;
7061 break;
7062 case Intrinsic::x86_sse2_pslli_d:
7063 NewIntNo = Intrinsic::x86_sse2_psll_d;
7064 break;
7065 case Intrinsic::x86_sse2_pslli_q:
7066 NewIntNo = Intrinsic::x86_sse2_psll_q;
7067 break;
7068 case Intrinsic::x86_sse2_psrli_w:
7069 NewIntNo = Intrinsic::x86_sse2_psrl_w;
7070 break;
7071 case Intrinsic::x86_sse2_psrli_d:
7072 NewIntNo = Intrinsic::x86_sse2_psrl_d;
7073 break;
7074 case Intrinsic::x86_sse2_psrli_q:
7075 NewIntNo = Intrinsic::x86_sse2_psrl_q;
7076 break;
7077 case Intrinsic::x86_sse2_psrai_w:
7078 NewIntNo = Intrinsic::x86_sse2_psra_w;
7079 break;
7080 case Intrinsic::x86_sse2_psrai_d:
7081 NewIntNo = Intrinsic::x86_sse2_psra_d;
7082 break;
7083 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00007084 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00007085 switch (IntNo) {
7086 case Intrinsic::x86_mmx_pslli_w:
7087 NewIntNo = Intrinsic::x86_mmx_psll_w;
7088 break;
7089 case Intrinsic::x86_mmx_pslli_d:
7090 NewIntNo = Intrinsic::x86_mmx_psll_d;
7091 break;
7092 case Intrinsic::x86_mmx_pslli_q:
7093 NewIntNo = Intrinsic::x86_mmx_psll_q;
7094 break;
7095 case Intrinsic::x86_mmx_psrli_w:
7096 NewIntNo = Intrinsic::x86_mmx_psrl_w;
7097 break;
7098 case Intrinsic::x86_mmx_psrli_d:
7099 NewIntNo = Intrinsic::x86_mmx_psrl_d;
7100 break;
7101 case Intrinsic::x86_mmx_psrli_q:
7102 NewIntNo = Intrinsic::x86_mmx_psrl_q;
7103 break;
7104 case Intrinsic::x86_mmx_psrai_w:
7105 NewIntNo = Intrinsic::x86_mmx_psra_w;
7106 break;
7107 case Intrinsic::x86_mmx_psrai_d:
7108 NewIntNo = Intrinsic::x86_mmx_psra_d;
7109 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00007110 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00007111 }
7112 break;
7113 }
7114 }
Mon P Wangefa42202009-09-03 19:56:25 +00007115
7116 // The vector shift intrinsics with scalars uses 32b shift amounts but
7117 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
7118 // to be zero.
7119 SDValue ShOps[4];
7120 ShOps[0] = ShAmt;
7121 ShOps[1] = DAG.getConstant(0, MVT::i32);
7122 if (ShAmtVT == MVT::v4i32) {
7123 ShOps[2] = DAG.getUNDEF(MVT::i32);
7124 ShOps[3] = DAG.getUNDEF(MVT::i32);
7125 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
7126 } else {
7127 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
7128 }
7129
Owen Andersone50ed302009-08-10 22:56:29 +00007130 EVT VT = Op.getValueType();
Mon P Wangefa42202009-09-03 19:56:25 +00007131 ShAmt = DAG.getNode(ISD::BIT_CONVERT, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007132 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007133 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00007134 Op.getOperand(1), ShAmt);
7135 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00007136 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007137}
Evan Cheng72261582005-12-20 06:22:03 +00007138
Dan Gohmand858e902010-04-17 15:26:15 +00007139SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
7140 SelectionDAG &DAG) const {
Bill Wendling64e87322009-01-16 19:25:27 +00007141 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007142 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00007143
7144 if (Depth > 0) {
7145 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
7146 SDValue Offset =
7147 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00007148 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007149 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00007150 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007151 FrameAddr, Offset),
David Greene67c9d422010-02-15 16:53:33 +00007152 NULL, 0, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00007153 }
7154
7155 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00007156 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00007157 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
David Greene67c9d422010-02-15 16:53:33 +00007158 RetAddrFI, NULL, 0, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00007159}
7160
Dan Gohmand858e902010-04-17 15:26:15 +00007161SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00007162 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7163 MFI->setFrameAddressIsTaken(true);
Owen Andersone50ed302009-08-10 22:56:29 +00007164 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007165 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00007166 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
7167 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00007168 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00007169 while (Depth--)
David Greene67c9d422010-02-15 16:53:33 +00007170 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0,
7171 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00007172 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00007173}
7174
Dan Gohman475871a2008-07-27 21:46:04 +00007175SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007176 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00007177 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007178}
7179
Dan Gohmand858e902010-04-17 15:26:15 +00007180SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007181 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00007182 SDValue Chain = Op.getOperand(0);
7183 SDValue Offset = Op.getOperand(1);
7184 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007185 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007186
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007187 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
7188 getPointerTy());
7189 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007190
Dale Johannesene4d209d2009-02-03 20:21:25 +00007191 SDValue StoreAddr = DAG.getNode(ISD::SUB, dl, getPointerTy(), Frame,
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00007192 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007193 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
David Greene67c9d422010-02-15 16:53:33 +00007194 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, NULL, 0, false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00007195 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007196 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007197
Dale Johannesene4d209d2009-02-03 20:21:25 +00007198 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007199 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007200 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007201}
7202
Dan Gohman475871a2008-07-27 21:46:04 +00007203SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007204 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007205 SDValue Root = Op.getOperand(0);
7206 SDValue Trmp = Op.getOperand(1); // trampoline
7207 SDValue FPtr = Op.getOperand(2); // nested function
7208 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007209 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00007210
Dan Gohman69de1932008-02-06 22:27:42 +00007211 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00007212
7213 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00007214 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00007215
7216 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00007217 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
7218 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00007219
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00007220 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
7221 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00007222
7223 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
7224
7225 // Load the pointer to the nested function into R11.
7226 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00007227 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00007228 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
David Greene67c9d422010-02-15 16:53:33 +00007229 Addr, TrmpAddr, 0, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007230
Owen Anderson825b72b2009-08-11 20:47:22 +00007231 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7232 DAG.getConstant(2, MVT::i64));
David Greene67c9d422010-02-15 16:53:33 +00007233 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr, TrmpAddr, 2,
7234 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00007235
7236 // Load the 'nest' parameter value into R10.
7237 // R10 is specified in X86CallingConv.td
7238 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00007239 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7240 DAG.getConstant(10, MVT::i64));
7241 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
David Greene67c9d422010-02-15 16:53:33 +00007242 Addr, TrmpAddr, 10, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007243
Owen Anderson825b72b2009-08-11 20:47:22 +00007244 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7245 DAG.getConstant(12, MVT::i64));
David Greene67c9d422010-02-15 16:53:33 +00007246 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 12,
7247 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00007248
7249 // Jump to the nested function.
7250 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00007251 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7252 DAG.getConstant(20, MVT::i64));
7253 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
David Greene67c9d422010-02-15 16:53:33 +00007254 Addr, TrmpAddr, 20, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007255
7256 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00007257 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7258 DAG.getConstant(22, MVT::i64));
7259 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
David Greene67c9d422010-02-15 16:53:33 +00007260 TrmpAddr, 22, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007261
Dan Gohman475871a2008-07-27 21:46:04 +00007262 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00007263 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007264 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007265 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00007266 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00007267 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00007268 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00007269 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007270
7271 switch (CC) {
7272 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00007273 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00007274 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00007275 case CallingConv::X86_StdCall: {
7276 // Pass 'nest' parameter in ECX.
7277 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00007278 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007279
7280 // Check that ECX wasn't needed by an 'inreg' parameter.
7281 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00007282 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00007283
Chris Lattner58d74912008-03-12 17:45:29 +00007284 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00007285 unsigned InRegCount = 0;
7286 unsigned Idx = 1;
7287
7288 for (FunctionType::param_iterator I = FTy->param_begin(),
7289 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00007290 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00007291 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00007292 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007293
7294 if (InRegCount > 2) {
Chris Lattner75361b62010-04-07 22:58:41 +00007295 report_fatal_error("Nest register in use - reduce number of inreg parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00007296 }
7297 }
7298 break;
7299 }
7300 case CallingConv::X86_FastCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00007301 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00007302 // Pass 'nest' parameter in EAX.
7303 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00007304 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007305 break;
7306 }
7307
Dan Gohman475871a2008-07-27 21:46:04 +00007308 SDValue OutChains[4];
7309 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007310
Owen Anderson825b72b2009-08-11 20:47:22 +00007311 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7312 DAG.getConstant(10, MVT::i32));
7313 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007314
Chris Lattnera62fe662010-02-05 19:20:30 +00007315 // This is storing the opcode for MOV32ri.
7316 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00007317 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00007318 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007319 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
David Greene67c9d422010-02-15 16:53:33 +00007320 Trmp, TrmpAddr, 0, false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007321
Owen Anderson825b72b2009-08-11 20:47:22 +00007322 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7323 DAG.getConstant(1, MVT::i32));
David Greene67c9d422010-02-15 16:53:33 +00007324 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 1,
7325 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007326
Chris Lattnera62fe662010-02-05 19:20:30 +00007327 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00007328 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7329 DAG.getConstant(5, MVT::i32));
7330 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
David Greene67c9d422010-02-15 16:53:33 +00007331 TrmpAddr, 5, false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007332
Owen Anderson825b72b2009-08-11 20:47:22 +00007333 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7334 DAG.getConstant(6, MVT::i32));
David Greene67c9d422010-02-15 16:53:33 +00007335 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr, TrmpAddr, 6,
7336 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007337
Dan Gohman475871a2008-07-27 21:46:04 +00007338 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00007339 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007340 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007341 }
7342}
7343
Dan Gohmand858e902010-04-17 15:26:15 +00007344SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
7345 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007346 /*
7347 The rounding mode is in bits 11:10 of FPSR, and has the following
7348 settings:
7349 00 Round to nearest
7350 01 Round to -inf
7351 10 Round to +inf
7352 11 Round to 0
7353
7354 FLT_ROUNDS, on the other hand, expects the following:
7355 -1 Undefined
7356 0 Round to 0
7357 1 Round to nearest
7358 2 Round to +inf
7359 3 Round to -inf
7360
7361 To perform the conversion, we do:
7362 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
7363 */
7364
7365 MachineFunction &MF = DAG.getMachineFunction();
7366 const TargetMachine &TM = MF.getTarget();
7367 const TargetFrameInfo &TFI = *TM.getFrameInfo();
7368 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00007369 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007370 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007371
7372 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00007373 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007374 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007375
Owen Anderson825b72b2009-08-11 20:47:22 +00007376 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, dl, MVT::Other,
Evan Cheng8a186ae2008-09-24 23:26:36 +00007377 DAG.getEntryNode(), StackSlot);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007378
7379 // Load FP Control Word from stack slot
David Greene67c9d422010-02-15 16:53:33 +00007380 SDValue CWD = DAG.getLoad(MVT::i16, dl, Chain, StackSlot, NULL, 0,
7381 false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007382
7383 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00007384 SDValue CWD1 =
Owen Anderson825b72b2009-08-11 20:47:22 +00007385 DAG.getNode(ISD::SRL, dl, MVT::i16,
7386 DAG.getNode(ISD::AND, dl, MVT::i16,
7387 CWD, DAG.getConstant(0x800, MVT::i16)),
7388 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00007389 SDValue CWD2 =
Owen Anderson825b72b2009-08-11 20:47:22 +00007390 DAG.getNode(ISD::SRL, dl, MVT::i16,
7391 DAG.getNode(ISD::AND, dl, MVT::i16,
7392 CWD, DAG.getConstant(0x400, MVT::i16)),
7393 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007394
Dan Gohman475871a2008-07-27 21:46:04 +00007395 SDValue RetVal =
Owen Anderson825b72b2009-08-11 20:47:22 +00007396 DAG.getNode(ISD::AND, dl, MVT::i16,
7397 DAG.getNode(ISD::ADD, dl, MVT::i16,
7398 DAG.getNode(ISD::OR, dl, MVT::i16, CWD1, CWD2),
7399 DAG.getConstant(1, MVT::i16)),
7400 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007401
7402
Duncan Sands83ec4b62008-06-06 12:08:01 +00007403 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007404 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007405}
7406
Dan Gohmand858e902010-04-17 15:26:15 +00007407SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007408 EVT VT = Op.getValueType();
7409 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007410 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007411 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00007412
7413 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007414 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00007415 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00007416 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007417 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007418 }
Evan Cheng18efe262007-12-14 02:13:44 +00007419
Evan Cheng152804e2007-12-14 08:30:15 +00007420 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007421 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007422 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00007423
7424 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007425 SDValue Ops[] = {
7426 Op,
7427 DAG.getConstant(NumBits+NumBits-1, OpVT),
7428 DAG.getConstant(X86::COND_E, MVT::i8),
7429 Op.getValue(1)
7430 };
7431 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00007432
7433 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00007434 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00007435
Owen Anderson825b72b2009-08-11 20:47:22 +00007436 if (VT == MVT::i8)
7437 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007438 return Op;
7439}
7440
Dan Gohmand858e902010-04-17 15:26:15 +00007441SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007442 EVT VT = Op.getValueType();
7443 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007444 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007445 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00007446
7447 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007448 if (VT == MVT::i8) {
7449 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007450 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007451 }
Evan Cheng152804e2007-12-14 08:30:15 +00007452
7453 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007454 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007455 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00007456
7457 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007458 SDValue Ops[] = {
7459 Op,
7460 DAG.getConstant(NumBits, OpVT),
7461 DAG.getConstant(X86::COND_E, MVT::i8),
7462 Op.getValue(1)
7463 };
7464 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00007465
Owen Anderson825b72b2009-08-11 20:47:22 +00007466 if (VT == MVT::i8)
7467 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007468 return Op;
7469}
7470
Dan Gohmand858e902010-04-17 15:26:15 +00007471SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007472 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007473 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007474 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00007475
Mon P Wangaf9b9522008-12-18 21:42:19 +00007476 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
7477 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
7478 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
7479 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
7480 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
7481 //
7482 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
7483 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
7484 // return AloBlo + AloBhi + AhiBlo;
7485
7486 SDValue A = Op.getOperand(0);
7487 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007488
Dale Johannesene4d209d2009-02-03 20:21:25 +00007489 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007490 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7491 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007492 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007493 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7494 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007495 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007496 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007497 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007498 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007499 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007500 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007501 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007502 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007503 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007504 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007505 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7506 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007507 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007508 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7509 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007510 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
7511 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00007512 return Res;
7513}
7514
7515
Dan Gohmand858e902010-04-17 15:26:15 +00007516SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +00007517 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
7518 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00007519 // looks for this combo and may remove the "setcc" instruction if the "setcc"
7520 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00007521 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00007522 SDValue LHS = N->getOperand(0);
7523 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00007524 unsigned BaseOp = 0;
7525 unsigned Cond = 0;
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007526 DebugLoc dl = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00007527
7528 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007529 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00007530 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00007531 // A subtract of one will be selected as a INC. Note that INC doesn't
7532 // set CF, so we can't do this for UADDO.
7533 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
7534 if (C->getAPIntValue() == 1) {
7535 BaseOp = X86ISD::INC;
7536 Cond = X86::COND_O;
7537 break;
7538 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007539 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00007540 Cond = X86::COND_O;
7541 break;
7542 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007543 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00007544 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007545 break;
7546 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00007547 // A subtract of one will be selected as a DEC. Note that DEC doesn't
7548 // set CF, so we can't do this for USUBO.
7549 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
7550 if (C->getAPIntValue() == 1) {
7551 BaseOp = X86ISD::DEC;
7552 Cond = X86::COND_O;
7553 break;
7554 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007555 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00007556 Cond = X86::COND_O;
7557 break;
7558 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007559 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00007560 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007561 break;
7562 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00007563 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00007564 Cond = X86::COND_O;
7565 break;
7566 case ISD::UMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00007567 BaseOp = X86ISD::UMUL;
Dan Gohman653456c2009-01-07 00:15:08 +00007568 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007569 break;
7570 }
Bill Wendling3fafd932008-11-26 22:37:40 +00007571
Bill Wendling61edeb52008-12-02 01:06:39 +00007572 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007573 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007574 SDValue Sum = DAG.getNode(BaseOp, dl, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00007575
Bill Wendling61edeb52008-12-02 01:06:39 +00007576 SDValue SetCC =
Dale Johannesene4d209d2009-02-03 20:21:25 +00007577 DAG.getNode(X86ISD::SETCC, dl, N->getValueType(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00007578 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00007579
Bill Wendling61edeb52008-12-02 01:06:39 +00007580 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
7581 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00007582}
7583
Dan Gohmand858e902010-04-17 15:26:15 +00007584SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007585 EVT T = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007586 DebugLoc dl = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00007587 unsigned Reg = 0;
7588 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00007589 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007590 default:
7591 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007592 case MVT::i8: Reg = X86::AL; size = 1; break;
7593 case MVT::i16: Reg = X86::AX; size = 2; break;
7594 case MVT::i32: Reg = X86::EAX; size = 4; break;
7595 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00007596 assert(Subtarget->is64Bit() && "Node not type legal!");
7597 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00007598 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007599 }
Dale Johannesendd64c412009-02-04 00:33:20 +00007600 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), dl, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00007601 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00007602 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00007603 Op.getOperand(1),
7604 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00007605 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00007606 cpIn.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00007607 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007608 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, dl, Tys, Ops, 5);
Scott Michelfdc40a02009-02-17 22:15:04 +00007609 SDValue cpOut =
Dale Johannesendd64c412009-02-04 00:33:20 +00007610 DAG.getCopyFromReg(Result.getValue(0), dl, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00007611 return cpOut;
7612}
7613
Duncan Sands1607f052008-12-01 11:39:25 +00007614SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007615 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +00007616 assert(Subtarget->is64Bit() && "Result not type legalized?");
Owen Anderson825b72b2009-08-11 20:47:22 +00007617 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00007618 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007619 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007620 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007621 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
7622 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00007623 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00007624 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
7625 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00007626 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00007627 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00007628 rdx.getValue(1)
7629 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007630 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007631}
7632
Dan Gohmand858e902010-04-17 15:26:15 +00007633SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +00007634 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007635 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007636 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007637 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00007638 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007639 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007640 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00007641 Node->getOperand(0),
7642 Node->getOperand(1), negOp,
7643 cast<AtomicSDNode>(Node)->getSrcValue(),
7644 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00007645}
7646
Evan Cheng0db9fe62006-04-25 20:13:52 +00007647/// LowerOperation - Provide custom lowering hooks for some operations.
7648///
Dan Gohmand858e902010-04-17 15:26:15 +00007649SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007650 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007651 default: llvm_unreachable("Should not custom lower this!");
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007652 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
7653 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007654 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00007655 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007656 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
7657 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
7658 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
7659 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
7660 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
7661 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007662 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00007663 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007664 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007665 case ISD::SHL_PARTS:
7666 case ISD::SRA_PARTS:
7667 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
7668 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007669 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007670 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00007671 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007672 case ISD::FABS: return LowerFABS(Op, DAG);
7673 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007674 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00007675 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00007676 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00007677 case ISD::SELECT: return LowerSELECT(Op, DAG);
7678 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007679 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007680 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00007681 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00007682 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007683 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00007684 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
7685 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007686 case ISD::FRAME_TO_ARGS_OFFSET:
7687 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007688 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007689 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007690 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00007691 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00007692 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
7693 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00007694 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00007695 case ISD::SADDO:
7696 case ISD::UADDO:
7697 case ISD::SSUBO:
7698 case ISD::USUBO:
7699 case ISD::SMULO:
7700 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00007701 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007702 }
Chris Lattner27a6c732007-11-24 07:07:01 +00007703}
7704
Duncan Sands1607f052008-12-01 11:39:25 +00007705void X86TargetLowering::
7706ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00007707 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007708 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007709 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00007710 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00007711
7712 SDValue Chain = Node->getOperand(0);
7713 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007714 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007715 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007716 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007717 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00007718 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00007719 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00007720 SDValue Result =
7721 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
7722 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00007723 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00007724 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007725 Results.push_back(Result.getValue(2));
7726}
7727
Duncan Sands126d9072008-07-04 11:47:58 +00007728/// ReplaceNodeResults - Replace a node with an illegal result type
7729/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00007730void X86TargetLowering::ReplaceNodeResults(SDNode *N,
7731 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00007732 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007733 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00007734 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00007735 default:
Duncan Sands1607f052008-12-01 11:39:25 +00007736 assert(false && "Do not know how to custom type legalize this operation!");
7737 return;
7738 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00007739 std::pair<SDValue,SDValue> Vals =
7740 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00007741 SDValue FIST = Vals.first, StackSlot = Vals.second;
7742 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00007743 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00007744 // Return a load from the stack slot.
David Greene67c9d422010-02-15 16:53:33 +00007745 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot, NULL, 0,
7746 false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00007747 }
7748 return;
7749 }
7750 case ISD::READCYCLECOUNTER: {
Owen Anderson825b72b2009-08-11 20:47:22 +00007751 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00007752 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007753 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007754 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00007755 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007756 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007757 eax.getValue(2));
7758 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
7759 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00007760 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007761 Results.push_back(edx.getValue(1));
7762 return;
7763 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007764 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00007765 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007766 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00007767 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00007768 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
7769 DAG.getConstant(0, MVT::i32));
7770 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
7771 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00007772 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
7773 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00007774 cpInL.getValue(1));
7775 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00007776 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
7777 DAG.getConstant(0, MVT::i32));
7778 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
7779 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00007780 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00007781 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00007782 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00007783 swapInL.getValue(1));
7784 SDValue Ops[] = { swapInH.getValue(0),
7785 N->getOperand(1),
7786 swapInH.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00007787 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007788 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, dl, Tys, Ops, 3);
Dale Johannesendd64c412009-02-04 00:33:20 +00007789 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00007790 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00007791 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00007792 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00007793 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00007794 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007795 Results.push_back(cpOutH.getValue(1));
7796 return;
7797 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007798 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00007799 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
7800 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007801 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00007802 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
7803 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007804 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00007805 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
7806 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007807 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00007808 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
7809 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007810 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00007811 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
7812 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007813 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00007814 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
7815 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007816 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00007817 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
7818 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00007819 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007820}
7821
Evan Cheng72261582005-12-20 06:22:03 +00007822const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
7823 switch (Opcode) {
7824 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00007825 case X86ISD::BSF: return "X86ISD::BSF";
7826 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00007827 case X86ISD::SHLD: return "X86ISD::SHLD";
7828 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00007829 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00007830 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00007831 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00007832 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00007833 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00007834 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00007835 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
7836 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
7837 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00007838 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00007839 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00007840 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00007841 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00007842 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00007843 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00007844 case X86ISD::COMI: return "X86ISD::COMI";
7845 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00007846 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00007847 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Evan Cheng72261582005-12-20 06:22:03 +00007848 case X86ISD::CMOV: return "X86ISD::CMOV";
7849 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00007850 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00007851 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
7852 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00007853 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00007854 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00007855 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00007856 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00007857 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00007858 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
7859 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00007860 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007861 case X86ISD::MMX_PINSRW: return "X86ISD::MMX_PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00007862 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Evan Cheng8ca29322006-11-10 21:43:37 +00007863 case X86ISD::FMAX: return "X86ISD::FMAX";
7864 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00007865 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
7866 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007867 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Rafael Espindola094fad32009-04-08 21:14:34 +00007868 case X86ISD::SegmentBaseAddress: return "X86ISD::SegmentBaseAddress";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007869 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00007870 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007871 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00007872 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
7873 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007874 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
7875 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
7876 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
7877 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
7878 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
7879 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00007880 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
7881 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00007882 case X86ISD::VSHL: return "X86ISD::VSHL";
7883 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00007884 case X86ISD::CMPPD: return "X86ISD::CMPPD";
7885 case X86ISD::CMPPS: return "X86ISD::CMPPS";
7886 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
7887 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
7888 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
7889 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
7890 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
7891 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
7892 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
7893 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007894 case X86ISD::ADD: return "X86ISD::ADD";
7895 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingd350e022008-12-12 21:15:41 +00007896 case X86ISD::SMUL: return "X86ISD::SMUL";
7897 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00007898 case X86ISD::INC: return "X86ISD::INC";
7899 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00007900 case X86ISD::OR: return "X86ISD::OR";
7901 case X86ISD::XOR: return "X86ISD::XOR";
7902 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00007903 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00007904 case X86ISD::PTEST: return "X86ISD::PTEST";
Dan Gohmand6708ea2009-08-15 01:38:56 +00007905 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00007906 case X86ISD::MINGW_ALLOCA: return "X86ISD::MINGW_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00007907 }
7908}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007909
Chris Lattnerc9addb72007-03-30 23:15:24 +00007910// isLegalAddressingMode - Return true if the addressing mode represented
7911// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00007912bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00007913 const Type *Ty) const {
7914 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007915 CodeModel::Model M = getTargetMachine().getCodeModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00007916
Chris Lattnerc9addb72007-03-30 23:15:24 +00007917 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007918 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00007919 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007920
Chris Lattnerc9addb72007-03-30 23:15:24 +00007921 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00007922 unsigned GVFlags =
7923 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007924
Chris Lattnerdfed4132009-07-10 07:38:24 +00007925 // If a reference to this global requires an extra load, we can't fold it.
7926 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00007927 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007928
Chris Lattnerdfed4132009-07-10 07:38:24 +00007929 // If BaseGV requires a register for the PIC base, we cannot also have a
7930 // BaseReg specified.
7931 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00007932 return false;
Evan Cheng52787842007-08-01 23:46:47 +00007933
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007934 // If lower 4G is not available, then we must use rip-relative addressing.
7935 if (Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
7936 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00007937 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007938
Chris Lattnerc9addb72007-03-30 23:15:24 +00007939 switch (AM.Scale) {
7940 case 0:
7941 case 1:
7942 case 2:
7943 case 4:
7944 case 8:
7945 // These scales always work.
7946 break;
7947 case 3:
7948 case 5:
7949 case 9:
7950 // These scales are formed with basereg+scalereg. Only accept if there is
7951 // no basereg yet.
7952 if (AM.HasBaseReg)
7953 return false;
7954 break;
7955 default: // Other stuff never works.
7956 return false;
7957 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007958
Chris Lattnerc9addb72007-03-30 23:15:24 +00007959 return true;
7960}
7961
7962
Evan Cheng2bd122c2007-10-26 01:56:11 +00007963bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00007964 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00007965 return false;
Evan Chenge127a732007-10-29 07:57:50 +00007966 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
7967 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00007968 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00007969 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00007970 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00007971}
7972
Owen Andersone50ed302009-08-10 22:56:29 +00007973bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007974 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007975 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007976 unsigned NumBits1 = VT1.getSizeInBits();
7977 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00007978 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007979 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00007980 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007981}
Evan Cheng2bd122c2007-10-26 01:56:11 +00007982
Dan Gohman97121ba2009-04-08 00:15:30 +00007983bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00007984 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00007985 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00007986}
7987
Owen Andersone50ed302009-08-10 22:56:29 +00007988bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00007989 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00007990 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00007991}
7992
Owen Andersone50ed302009-08-10 22:56:29 +00007993bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00007994 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00007995 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00007996}
7997
Evan Cheng60c07e12006-07-05 22:17:51 +00007998/// isShuffleMaskLegal - Targets can use this to indicate that they only
7999/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
8000/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
8001/// are assumed to be legal.
8002bool
Eric Christopherfd179292009-08-27 18:07:15 +00008003X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00008004 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +00008005 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +00008006 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +00008007 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +00008008
Nate Begemana09008b2009-10-19 02:17:23 +00008009 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00008010 return (VT.getVectorNumElements() == 2 ||
8011 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
8012 isMOVLMask(M, VT) ||
8013 isSHUFPMask(M, VT) ||
8014 isPSHUFDMask(M, VT) ||
8015 isPSHUFHWMask(M, VT) ||
8016 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00008017 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00008018 isUNPCKLMask(M, VT) ||
8019 isUNPCKHMask(M, VT) ||
8020 isUNPCKL_v_undef_Mask(M, VT) ||
8021 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00008022}
8023
Dan Gohman7d8143f2008-04-09 20:09:42 +00008024bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00008025X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00008026 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00008027 unsigned NumElts = VT.getVectorNumElements();
8028 // FIXME: This collection of masks seems suspect.
8029 if (NumElts == 2)
8030 return true;
8031 if (NumElts == 4 && VT.getSizeInBits() == 128) {
8032 return (isMOVLMask(Mask, VT) ||
8033 isCommutedMOVLMask(Mask, VT, true) ||
8034 isSHUFPMask(Mask, VT) ||
8035 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00008036 }
8037 return false;
8038}
8039
8040//===----------------------------------------------------------------------===//
8041// X86 Scheduler Hooks
8042//===----------------------------------------------------------------------===//
8043
Mon P Wang63307c32008-05-05 19:05:59 +00008044// private utility function
8045MachineBasicBlock *
8046X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
8047 MachineBasicBlock *MBB,
8048 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008049 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008050 unsigned LoadOpc,
8051 unsigned CXchgOpc,
8052 unsigned copyOpc,
8053 unsigned notOpc,
8054 unsigned EAXreg,
8055 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00008056 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00008057 // For the atomic bitwise operator, we generate
8058 // thisMBB:
8059 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00008060 // ld t1 = [bitinstr.addr]
8061 // op t2 = t1, [bitinstr.val]
8062 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00008063 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
8064 // bz newMBB
8065 // fallthrough -->nextMBB
8066 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8067 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008068 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00008069 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00008070
Mon P Wang63307c32008-05-05 19:05:59 +00008071 /// First build the CFG
8072 MachineFunction *F = MBB->getParent();
8073 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008074 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
8075 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
8076 F->insert(MBBIter, newMBB);
8077 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008078
Mon P Wang63307c32008-05-05 19:05:59 +00008079 // Move all successors to thisMBB to nextMBB
8080 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008081
Mon P Wang63307c32008-05-05 19:05:59 +00008082 // Update thisMBB to fall through to newMBB
8083 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008084
Mon P Wang63307c32008-05-05 19:05:59 +00008085 // newMBB jumps to itself and fall through to nextMBB
8086 newMBB->addSuccessor(nextMBB);
8087 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008088
Mon P Wang63307c32008-05-05 19:05:59 +00008089 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008090 assert(bInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00008091 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00008092 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00008093 MachineOperand& destOper = bInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008094 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00008095 int numArgs = bInstr->getNumOperands() - 1;
8096 for (int i=0; i < numArgs; ++i)
8097 argOpers[i] = &bInstr->getOperand(i+1);
8098
8099 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008100 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
8101 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00008102
Dale Johannesen140be2d2008-08-19 18:47:28 +00008103 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008104 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00008105 for (int i=0; i <= lastAddrIndx; ++i)
8106 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008107
Dale Johannesen140be2d2008-08-19 18:47:28 +00008108 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008109 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00008110 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008111 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008112 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008113 tt = t1;
8114
Dale Johannesen140be2d2008-08-19 18:47:28 +00008115 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00008116 assert((argOpers[valArgIndx]->isReg() ||
8117 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00008118 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00008119 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008120 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00008121 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008122 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008123 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00008124 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008125
Dale Johannesene4d209d2009-02-03 20:21:25 +00008126 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00008127 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008128
Dale Johannesene4d209d2009-02-03 20:21:25 +00008129 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00008130 for (int i=0; i <= lastAddrIndx; ++i)
8131 (*MIB).addOperand(*argOpers[i]);
8132 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00008133 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00008134 (*MIB).setMemRefs(bInstr->memoperands_begin(),
8135 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00008136
Dale Johannesene4d209d2009-02-03 20:21:25 +00008137 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00008138 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00008139
Mon P Wang63307c32008-05-05 19:05:59 +00008140 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008141 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00008142
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008143 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00008144 return nextMBB;
8145}
8146
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00008147// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00008148MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008149X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
8150 MachineBasicBlock *MBB,
8151 unsigned regOpcL,
8152 unsigned regOpcH,
8153 unsigned immOpcL,
8154 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00008155 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008156 // For the atomic bitwise operator, we generate
8157 // thisMBB (instructions are in pairs, except cmpxchg8b)
8158 // ld t1,t2 = [bitinstr.addr]
8159 // newMBB:
8160 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
8161 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00008162 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008163 // mov ECX, EBX <- t5, t6
8164 // mov EAX, EDX <- t1, t2
8165 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
8166 // mov t3, t4 <- EAX, EDX
8167 // bz newMBB
8168 // result in out1, out2
8169 // fallthrough -->nextMBB
8170
8171 const TargetRegisterClass *RC = X86::GR32RegisterClass;
8172 const unsigned LoadOpc = X86::MOV32rm;
8173 const unsigned copyOpc = X86::MOV32rr;
8174 const unsigned NotOpc = X86::NOT32r;
8175 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8176 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
8177 MachineFunction::iterator MBBIter = MBB;
8178 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00008179
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008180 /// First build the CFG
8181 MachineFunction *F = MBB->getParent();
8182 MachineBasicBlock *thisMBB = MBB;
8183 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
8184 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
8185 F->insert(MBBIter, newMBB);
8186 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008187
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008188 // Move all successors to thisMBB to nextMBB
8189 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008190
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008191 // Update thisMBB to fall through to newMBB
8192 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008193
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008194 // newMBB jumps to itself and fall through to nextMBB
8195 newMBB->addSuccessor(nextMBB);
8196 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008197
Dale Johannesene4d209d2009-02-03 20:21:25 +00008198 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008199 // Insert instructions into newMBB based on incoming instruction
8200 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008201 assert(bInstr->getNumOperands() < X86AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00008202 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008203 MachineOperand& dest1Oper = bInstr->getOperand(0);
8204 MachineOperand& dest2Oper = bInstr->getOperand(1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008205 MachineOperand* argOpers[2 + X86AddrNumOperands];
8206 for (int i=0; i < 2 + X86AddrNumOperands; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008207 argOpers[i] = &bInstr->getOperand(i+2);
8208
Evan Chengad5b52f2010-01-08 19:14:57 +00008209 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008210 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00008211
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008212 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008213 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008214 for (int i=0; i <= lastAddrIndx; ++i)
8215 (*MIB).addOperand(*argOpers[i]);
8216 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008217 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00008218 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00008219 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008220 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00008221 MachineOperand newOp3 = *(argOpers[3]);
8222 if (newOp3.isImm())
8223 newOp3.setImm(newOp3.getImm()+4);
8224 else
8225 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008226 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00008227 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008228
8229 // t3/4 are defined later, at the bottom of the loop
8230 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
8231 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008232 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008233 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008234 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008235 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
8236
Evan Cheng306b4ca2010-01-08 23:41:50 +00008237 // The subsequent operations should be using the destination registers of
8238 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00008239 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00008240 t1 = F->getRegInfo().createVirtualRegister(RC);
8241 t2 = F->getRegInfo().createVirtualRegister(RC);
8242 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
8243 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008244 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00008245 t1 = dest1Oper.getReg();
8246 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008247 }
8248
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008249 int valArgIndx = lastAddrIndx + 1;
8250 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00008251 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008252 "invalid operand");
8253 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
8254 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008255 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008256 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008257 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008258 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00008259 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00008260 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008261 (*MIB).addOperand(*argOpers[valArgIndx]);
8262 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00008263 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008264 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00008265 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008266 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008267 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008268 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008269 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00008270 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00008271 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008272 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008273
Dale Johannesene4d209d2009-02-03 20:21:25 +00008274 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008275 MIB.addReg(t1);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008276 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008277 MIB.addReg(t2);
8278
Dale Johannesene4d209d2009-02-03 20:21:25 +00008279 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008280 MIB.addReg(t5);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008281 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008282 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00008283
Dale Johannesene4d209d2009-02-03 20:21:25 +00008284 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008285 for (int i=0; i <= lastAddrIndx; ++i)
8286 (*MIB).addOperand(*argOpers[i]);
8287
8288 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00008289 (*MIB).setMemRefs(bInstr->memoperands_begin(),
8290 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008291
Dale Johannesene4d209d2009-02-03 20:21:25 +00008292 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008293 MIB.addReg(X86::EAX);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008294 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008295 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00008296
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008297 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008298 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008299
8300 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
8301 return nextMBB;
8302}
8303
8304// private utility function
8305MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00008306X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
8307 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00008308 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00008309 // For the atomic min/max operator, we generate
8310 // thisMBB:
8311 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00008312 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00008313 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00008314 // cmp t1, t2
8315 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00008316 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00008317 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
8318 // bz newMBB
8319 // fallthrough -->nextMBB
8320 //
8321 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8322 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008323 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00008324 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00008325
Mon P Wang63307c32008-05-05 19:05:59 +00008326 /// First build the CFG
8327 MachineFunction *F = MBB->getParent();
8328 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008329 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
8330 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
8331 F->insert(MBBIter, newMBB);
8332 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008333
Dan Gohmand6708ea2009-08-15 01:38:56 +00008334 // Move all successors of thisMBB to nextMBB
Mon P Wang63307c32008-05-05 19:05:59 +00008335 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008336
Mon P Wang63307c32008-05-05 19:05:59 +00008337 // Update thisMBB to fall through to newMBB
8338 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008339
Mon P Wang63307c32008-05-05 19:05:59 +00008340 // newMBB jumps to newMBB and fall through to nextMBB
8341 newMBB->addSuccessor(nextMBB);
8342 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008343
Dale Johannesene4d209d2009-02-03 20:21:25 +00008344 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00008345 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008346 assert(mInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00008347 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00008348 MachineOperand& destOper = mInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008349 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00008350 int numArgs = mInstr->getNumOperands() - 1;
8351 for (int i=0; i < numArgs; ++i)
8352 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008353
Mon P Wang63307c32008-05-05 19:05:59 +00008354 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008355 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
8356 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00008357
Mon P Wangab3e7472008-05-05 22:56:23 +00008358 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008359 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00008360 for (int i=0; i <= lastAddrIndx; ++i)
8361 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00008362
Mon P Wang63307c32008-05-05 19:05:59 +00008363 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00008364 assert((argOpers[valArgIndx]->isReg() ||
8365 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00008366 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00008367
8368 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00008369 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008370 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00008371 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008372 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00008373 (*MIB).addOperand(*argOpers[valArgIndx]);
8374
Dale Johannesene4d209d2009-02-03 20:21:25 +00008375 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00008376 MIB.addReg(t1);
8377
Dale Johannesene4d209d2009-02-03 20:21:25 +00008378 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00008379 MIB.addReg(t1);
8380 MIB.addReg(t2);
8381
8382 // Generate movc
8383 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008384 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00008385 MIB.addReg(t2);
8386 MIB.addReg(t1);
8387
8388 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00008389 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00008390 for (int i=0; i <= lastAddrIndx; ++i)
8391 (*MIB).addOperand(*argOpers[i]);
8392 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00008393 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00008394 (*MIB).setMemRefs(mInstr->memoperands_begin(),
8395 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +00008396
Dale Johannesene4d209d2009-02-03 20:21:25 +00008397 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00008398 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00008399
Mon P Wang63307c32008-05-05 19:05:59 +00008400 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008401 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00008402
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008403 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00008404 return nextMBB;
8405}
8406
Eric Christopherf83a5de2009-08-27 18:08:16 +00008407// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
8408// all of this code can be replaced with that in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +00008409MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +00008410X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +00008411 unsigned numArgs, bool memArg) const {
Eric Christopherb120ab42009-08-18 22:50:32 +00008412
8413 MachineFunction *F = BB->getParent();
8414 DebugLoc dl = MI->getDebugLoc();
8415 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8416
8417 unsigned Opc;
Evan Chengce319102009-09-19 09:51:03 +00008418 if (memArg)
8419 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
8420 else
8421 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
Eric Christopherb120ab42009-08-18 22:50:32 +00008422
8423 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(Opc));
8424
8425 for (unsigned i = 0; i < numArgs; ++i) {
8426 MachineOperand &Op = MI->getOperand(i+1);
8427
8428 if (!(Op.isReg() && Op.isImplicit()))
8429 MIB.addOperand(Op);
8430 }
8431
8432 BuildMI(BB, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
8433 .addReg(X86::XMM0);
8434
8435 F->DeleteMachineInstr(MI);
8436
8437 return BB;
8438}
8439
8440MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +00008441X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
8442 MachineInstr *MI,
8443 MachineBasicBlock *MBB) const {
8444 // Emit code to save XMM registers to the stack. The ABI says that the
8445 // number of registers to save is given in %al, so it's theoretically
8446 // possible to do an indirect jump trick to avoid saving all of them,
8447 // however this code takes a simpler approach and just executes all
8448 // of the stores if %al is non-zero. It's less code, and it's probably
8449 // easier on the hardware branch predictor, and stores aren't all that
8450 // expensive anyway.
8451
8452 // Create the new basic blocks. One block contains all the XMM stores,
8453 // and one block is the final destination regardless of whether any
8454 // stores were performed.
8455 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
8456 MachineFunction *F = MBB->getParent();
8457 MachineFunction::iterator MBBIter = MBB;
8458 ++MBBIter;
8459 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
8460 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
8461 F->insert(MBBIter, XMMSaveMBB);
8462 F->insert(MBBIter, EndMBB);
8463
8464 // Set up the CFG.
8465 // Move any original successors of MBB to the end block.
8466 EndMBB->transferSuccessors(MBB);
8467 // The original block will now fall through to the XMM save block.
8468 MBB->addSuccessor(XMMSaveMBB);
8469 // The XMMSaveMBB will fall through to the end block.
8470 XMMSaveMBB->addSuccessor(EndMBB);
8471
8472 // Now add the instructions.
8473 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8474 DebugLoc DL = MI->getDebugLoc();
8475
8476 unsigned CountReg = MI->getOperand(0).getReg();
8477 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
8478 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
8479
8480 if (!Subtarget->isTargetWin64()) {
8481 // If %al is 0, branch around the XMM save block.
8482 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008483 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008484 MBB->addSuccessor(EndMBB);
8485 }
8486
8487 // In the XMM save block, save all the XMM argument registers.
8488 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
8489 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +00008490 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +00008491 F->getMachineMemOperand(
8492 PseudoSourceValue::getFixedStack(RegSaveFrameIndex),
8493 MachineMemOperand::MOStore, Offset,
8494 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008495 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
8496 .addFrameIndex(RegSaveFrameIndex)
8497 .addImm(/*Scale=*/1)
8498 .addReg(/*IndexReg=*/0)
8499 .addImm(/*Disp=*/Offset)
8500 .addReg(/*Segment=*/0)
8501 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +00008502 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008503 }
8504
8505 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8506
8507 return EndMBB;
8508}
Mon P Wang63307c32008-05-05 19:05:59 +00008509
Evan Cheng60c07e12006-07-05 22:17:51 +00008510MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +00008511X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Evan Chengce319102009-09-19 09:51:03 +00008512 MachineBasicBlock *BB,
8513 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Chris Lattner52600972009-09-02 05:57:00 +00008514 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8515 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +00008516
Chris Lattner52600972009-09-02 05:57:00 +00008517 // To "insert" a SELECT_CC instruction, we actually have to insert the
8518 // diamond control-flow pattern. The incoming instruction knows the
8519 // destination vreg to set, the condition code register to branch on, the
8520 // true/false values to select between, and a branch opcode to use.
8521 const BasicBlock *LLVM_BB = BB->getBasicBlock();
8522 MachineFunction::iterator It = BB;
8523 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +00008524
Chris Lattner52600972009-09-02 05:57:00 +00008525 // thisMBB:
8526 // ...
8527 // TrueVal = ...
8528 // cmpTY ccX, r1, r2
8529 // bCC copy1MBB
8530 // fallthrough --> copy0MBB
8531 MachineBasicBlock *thisMBB = BB;
8532 MachineFunction *F = BB->getParent();
8533 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
8534 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
8535 unsigned Opc =
8536 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
8537 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
8538 F->insert(It, copy0MBB);
8539 F->insert(It, sinkMBB);
Evan Chengce319102009-09-19 09:51:03 +00008540 // Update machine-CFG edges by first adding all successors of the current
Chris Lattner52600972009-09-02 05:57:00 +00008541 // block to the new block which will contain the Phi node for the select.
Evan Chengce319102009-09-19 09:51:03 +00008542 // Also inform sdisel of the edge changes.
Daniel Dunbara279bc32009-09-20 02:20:51 +00008543 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
Evan Chengce319102009-09-19 09:51:03 +00008544 E = BB->succ_end(); I != E; ++I) {
8545 EM->insert(std::make_pair(*I, sinkMBB));
8546 sinkMBB->addSuccessor(*I);
8547 }
8548 // Next, remove all successors of the current block, and add the true
8549 // and fallthrough blocks as its successors.
8550 while (!BB->succ_empty())
8551 BB->removeSuccessor(BB->succ_begin());
Chris Lattner52600972009-09-02 05:57:00 +00008552 // Add the true and fallthrough blocks as its successors.
8553 BB->addSuccessor(copy0MBB);
8554 BB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00008555
Chris Lattner52600972009-09-02 05:57:00 +00008556 // copy0MBB:
8557 // %FalseValue = ...
8558 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +00008559 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00008560
Chris Lattner52600972009-09-02 05:57:00 +00008561 // sinkMBB:
8562 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
8563 // ...
Dan Gohman3335a222010-04-30 20:14:26 +00008564 BuildMI(sinkMBB, DL, TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +00008565 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
8566 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
8567
8568 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +00008569 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +00008570}
8571
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008572MachineBasicBlock *
8573X86TargetLowering::EmitLoweredMingwAlloca(MachineInstr *MI,
8574 MachineBasicBlock *BB,
8575 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
8576 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8577 DebugLoc DL = MI->getDebugLoc();
8578 MachineFunction *F = BB->getParent();
8579
8580 // The lowering is pretty easy: we're just emitting the call to _alloca. The
8581 // non-trivial part is impdef of ESP.
8582 // FIXME: The code should be tweaked as soon as we'll try to do codegen for
8583 // mingw-w64.
8584
8585 BuildMI(BB, DL, TII->get(X86::CALLpcrel32))
8586 .addExternalSymbol("_alloca")
8587 .addReg(X86::EAX, RegState::Implicit)
8588 .addReg(X86::ESP, RegState::Implicit)
8589 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
8590 .addReg(X86::ESP, RegState::Define | RegState::Implicit);
8591
8592 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8593 return BB;
8594}
Chris Lattner52600972009-09-02 05:57:00 +00008595
8596MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00008597X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Evan Chengfb2e7522009-09-18 21:02:19 +00008598 MachineBasicBlock *BB,
8599 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00008600 switch (MI->getOpcode()) {
8601 default: assert(false && "Unexpected instr type to insert");
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008602 case X86::MINGW_ALLOCA:
8603 return EmitLoweredMingwAlloca(MI, BB, EM);
Dan Gohmancbbea0f2009-08-27 00:14:12 +00008604 case X86::CMOV_GR8:
Mon P Wang9e5ecb82008-12-12 01:25:51 +00008605 case X86::CMOV_V1I64:
Evan Cheng60c07e12006-07-05 22:17:51 +00008606 case X86::CMOV_FR32:
8607 case X86::CMOV_FR64:
8608 case X86::CMOV_V4F32:
8609 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +00008610 case X86::CMOV_V2I64:
Chris Lattner314a1132010-03-14 18:31:44 +00008611 case X86::CMOV_GR16:
8612 case X86::CMOV_GR32:
8613 case X86::CMOV_RFP32:
8614 case X86::CMOV_RFP64:
8615 case X86::CMOV_RFP80:
Evan Chengce319102009-09-19 09:51:03 +00008616 return EmitLoweredSelect(MI, BB, EM);
Evan Cheng60c07e12006-07-05 22:17:51 +00008617
Dale Johannesen849f2142007-07-03 00:53:03 +00008618 case X86::FP32_TO_INT16_IN_MEM:
8619 case X86::FP32_TO_INT32_IN_MEM:
8620 case X86::FP32_TO_INT64_IN_MEM:
8621 case X86::FP64_TO_INT16_IN_MEM:
8622 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00008623 case X86::FP64_TO_INT64_IN_MEM:
8624 case X86::FP80_TO_INT16_IN_MEM:
8625 case X86::FP80_TO_INT32_IN_MEM:
8626 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +00008627 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8628 DebugLoc DL = MI->getDebugLoc();
8629
Evan Cheng60c07e12006-07-05 22:17:51 +00008630 // Change the floating point control register to use "round towards zero"
8631 // mode when truncating to an integer value.
8632 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +00008633 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Chris Lattner52600972009-09-02 05:57:00 +00008634 addFrameReference(BuildMI(BB, DL, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008635
8636 // Load the old value of the high byte of the control word...
8637 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +00008638 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Chris Lattner52600972009-09-02 05:57:00 +00008639 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008640 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008641
8642 // Set the high part to be round to zero...
Chris Lattner52600972009-09-02 05:57:00 +00008643 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00008644 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00008645
8646 // Reload the modified control word now...
Chris Lattner52600972009-09-02 05:57:00 +00008647 addFrameReference(BuildMI(BB, DL, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008648
8649 // Restore the memory image of control word to original value
Chris Lattner52600972009-09-02 05:57:00 +00008650 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00008651 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00008652
8653 // Get the X86 opcode to use.
8654 unsigned Opc;
8655 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008656 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00008657 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
8658 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
8659 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
8660 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
8661 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
8662 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00008663 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
8664 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
8665 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00008666 }
8667
8668 X86AddressMode AM;
8669 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +00008670 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00008671 AM.BaseType = X86AddressMode::RegBase;
8672 AM.Base.Reg = Op.getReg();
8673 } else {
8674 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +00008675 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +00008676 }
8677 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +00008678 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00008679 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008680 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +00008681 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00008682 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008683 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +00008684 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00008685 AM.GV = Op.getGlobal();
8686 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00008687 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008688 }
Chris Lattner52600972009-09-02 05:57:00 +00008689 addFullAddress(BuildMI(BB, DL, TII->get(Opc)), AM)
Rafael Espindola8ef2b892009-04-08 08:09:33 +00008690 .addReg(MI->getOperand(X86AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00008691
8692 // Reload the original control word now.
Chris Lattner52600972009-09-02 05:57:00 +00008693 addFrameReference(BuildMI(BB, DL, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008694
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008695 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00008696 return BB;
8697 }
Eric Christopherb120ab42009-08-18 22:50:32 +00008698 // String/text processing lowering.
8699 case X86::PCMPISTRM128REG:
8700 return EmitPCMP(MI, BB, 3, false /* in-mem */);
8701 case X86::PCMPISTRM128MEM:
8702 return EmitPCMP(MI, BB, 3, true /* in-mem */);
8703 case X86::PCMPESTRM128REG:
8704 return EmitPCMP(MI, BB, 5, false /* in mem */);
8705 case X86::PCMPESTRM128MEM:
8706 return EmitPCMP(MI, BB, 5, true /* in mem */);
8707
8708 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +00008709 case X86::ATOMAND32:
8710 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008711 X86::AND32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008712 X86::LCMPXCHG32, X86::MOV32rr,
8713 X86::NOT32r, X86::EAX,
8714 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00008715 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +00008716 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
8717 X86::OR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008718 X86::LCMPXCHG32, X86::MOV32rr,
8719 X86::NOT32r, X86::EAX,
8720 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00008721 case X86::ATOMXOR32:
8722 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008723 X86::XOR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008724 X86::LCMPXCHG32, X86::MOV32rr,
8725 X86::NOT32r, X86::EAX,
8726 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008727 case X86::ATOMNAND32:
8728 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008729 X86::AND32ri, X86::MOV32rm,
8730 X86::LCMPXCHG32, X86::MOV32rr,
8731 X86::NOT32r, X86::EAX,
8732 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +00008733 case X86::ATOMMIN32:
8734 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
8735 case X86::ATOMMAX32:
8736 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
8737 case X86::ATOMUMIN32:
8738 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
8739 case X86::ATOMUMAX32:
8740 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +00008741
8742 case X86::ATOMAND16:
8743 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
8744 X86::AND16ri, X86::MOV16rm,
8745 X86::LCMPXCHG16, X86::MOV16rr,
8746 X86::NOT16r, X86::AX,
8747 X86::GR16RegisterClass);
8748 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +00008749 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008750 X86::OR16ri, X86::MOV16rm,
8751 X86::LCMPXCHG16, X86::MOV16rr,
8752 X86::NOT16r, X86::AX,
8753 X86::GR16RegisterClass);
8754 case X86::ATOMXOR16:
8755 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
8756 X86::XOR16ri, X86::MOV16rm,
8757 X86::LCMPXCHG16, X86::MOV16rr,
8758 X86::NOT16r, X86::AX,
8759 X86::GR16RegisterClass);
8760 case X86::ATOMNAND16:
8761 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
8762 X86::AND16ri, X86::MOV16rm,
8763 X86::LCMPXCHG16, X86::MOV16rr,
8764 X86::NOT16r, X86::AX,
8765 X86::GR16RegisterClass, true);
8766 case X86::ATOMMIN16:
8767 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
8768 case X86::ATOMMAX16:
8769 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
8770 case X86::ATOMUMIN16:
8771 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
8772 case X86::ATOMUMAX16:
8773 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
8774
8775 case X86::ATOMAND8:
8776 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
8777 X86::AND8ri, X86::MOV8rm,
8778 X86::LCMPXCHG8, X86::MOV8rr,
8779 X86::NOT8r, X86::AL,
8780 X86::GR8RegisterClass);
8781 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +00008782 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008783 X86::OR8ri, X86::MOV8rm,
8784 X86::LCMPXCHG8, X86::MOV8rr,
8785 X86::NOT8r, X86::AL,
8786 X86::GR8RegisterClass);
8787 case X86::ATOMXOR8:
8788 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
8789 X86::XOR8ri, X86::MOV8rm,
8790 X86::LCMPXCHG8, X86::MOV8rr,
8791 X86::NOT8r, X86::AL,
8792 X86::GR8RegisterClass);
8793 case X86::ATOMNAND8:
8794 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
8795 X86::AND8ri, X86::MOV8rm,
8796 X86::LCMPXCHG8, X86::MOV8rr,
8797 X86::NOT8r, X86::AL,
8798 X86::GR8RegisterClass, true);
8799 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008800 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +00008801 case X86::ATOMAND64:
8802 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008803 X86::AND64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008804 X86::LCMPXCHG64, X86::MOV64rr,
8805 X86::NOT64r, X86::RAX,
8806 X86::GR64RegisterClass);
8807 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +00008808 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
8809 X86::OR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008810 X86::LCMPXCHG64, X86::MOV64rr,
8811 X86::NOT64r, X86::RAX,
8812 X86::GR64RegisterClass);
8813 case X86::ATOMXOR64:
8814 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008815 X86::XOR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008816 X86::LCMPXCHG64, X86::MOV64rr,
8817 X86::NOT64r, X86::RAX,
8818 X86::GR64RegisterClass);
8819 case X86::ATOMNAND64:
8820 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
8821 X86::AND64ri32, X86::MOV64rm,
8822 X86::LCMPXCHG64, X86::MOV64rr,
8823 X86::NOT64r, X86::RAX,
8824 X86::GR64RegisterClass, true);
8825 case X86::ATOMMIN64:
8826 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
8827 case X86::ATOMMAX64:
8828 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
8829 case X86::ATOMUMIN64:
8830 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
8831 case X86::ATOMUMAX64:
8832 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008833
8834 // This group does 64-bit operations on a 32-bit host.
8835 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008836 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008837 X86::AND32rr, X86::AND32rr,
8838 X86::AND32ri, X86::AND32ri,
8839 false);
8840 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008841 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008842 X86::OR32rr, X86::OR32rr,
8843 X86::OR32ri, X86::OR32ri,
8844 false);
8845 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008846 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008847 X86::XOR32rr, X86::XOR32rr,
8848 X86::XOR32ri, X86::XOR32ri,
8849 false);
8850 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008851 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008852 X86::AND32rr, X86::AND32rr,
8853 X86::AND32ri, X86::AND32ri,
8854 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008855 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008856 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008857 X86::ADD32rr, X86::ADC32rr,
8858 X86::ADD32ri, X86::ADC32ri,
8859 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008860 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008861 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008862 X86::SUB32rr, X86::SBB32rr,
8863 X86::SUB32ri, X86::SBB32ri,
8864 false);
Dale Johannesen880ae362008-10-03 22:25:52 +00008865 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008866 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +00008867 X86::MOV32rr, X86::MOV32rr,
8868 X86::MOV32ri, X86::MOV32ri,
8869 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008870 case X86::VASTART_SAVE_XMM_REGS:
8871 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +00008872 }
8873}
8874
8875//===----------------------------------------------------------------------===//
8876// X86 Optimization Hooks
8877//===----------------------------------------------------------------------===//
8878
Dan Gohman475871a2008-07-27 21:46:04 +00008879void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00008880 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008881 APInt &KnownZero,
8882 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00008883 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00008884 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008885 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00008886 assert((Opc >= ISD::BUILTIN_OP_END ||
8887 Opc == ISD::INTRINSIC_WO_CHAIN ||
8888 Opc == ISD::INTRINSIC_W_CHAIN ||
8889 Opc == ISD::INTRINSIC_VOID) &&
8890 "Should use MaskedValueIsZero if you don't know whether Op"
8891 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008892
Dan Gohmanf4f92f52008-02-13 23:07:24 +00008893 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008894 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00008895 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +00008896 case X86ISD::ADD:
8897 case X86ISD::SUB:
8898 case X86ISD::SMUL:
8899 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +00008900 case X86ISD::INC:
8901 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +00008902 case X86ISD::OR:
8903 case X86ISD::XOR:
8904 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +00008905 // These nodes' second result is a boolean.
8906 if (Op.getResNo() == 0)
8907 break;
8908 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008909 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008910 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
8911 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +00008912 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008913 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008914}
Chris Lattner259e97c2006-01-31 19:43:35 +00008915
Evan Cheng206ee9d2006-07-07 08:33:52 +00008916/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +00008917/// node is a GlobalAddress + offset.
8918bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +00008919 const GlobalValue* &GA,
8920 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +00008921 if (N->getOpcode() == X86ISD::Wrapper) {
8922 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00008923 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00008924 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +00008925 return true;
8926 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00008927 }
Evan Chengad4196b2008-05-12 19:56:52 +00008928 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +00008929}
8930
Evan Cheng206ee9d2006-07-07 08:33:52 +00008931/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
8932/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
8933/// if the load addresses are consecutive, non-overlapping, and in the right
Nate Begemanfdea31a2010-03-24 20:49:50 +00008934/// order.
Dan Gohman475871a2008-07-27 21:46:04 +00008935static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00008936 const TargetLowering &TLI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00008937 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008938 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00008939 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
Mon P Wang1e955802009-04-03 02:43:30 +00008940
Eli Friedman7a5e5552009-06-07 06:52:44 +00008941 if (VT.getSizeInBits() != 128)
8942 return SDValue();
8943
Nate Begemanfdea31a2010-03-24 20:49:50 +00008944 SmallVector<SDValue, 16> Elts;
8945 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
8946 Elts.push_back(DAG.getShuffleScalarElt(SVN, i));
8947
8948 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00008949}
Evan Chengd880b972008-05-09 21:53:03 +00008950
Dan Gohman1bbf72b2010-03-15 23:23:03 +00008951/// PerformShuffleCombine - Detect vector gather/scatter index generation
8952/// and convert it from being a bunch of shuffles and extracts to a simple
8953/// store and scalar loads to extract the elements.
8954static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
8955 const TargetLowering &TLI) {
8956 SDValue InputVector = N->getOperand(0);
8957
8958 // Only operate on vectors of 4 elements, where the alternative shuffling
8959 // gets to be more expensive.
8960 if (InputVector.getValueType() != MVT::v4i32)
8961 return SDValue();
8962
8963 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
8964 // single use which is a sign-extend or zero-extend, and all elements are
8965 // used.
8966 SmallVector<SDNode *, 4> Uses;
8967 unsigned ExtractedElements = 0;
8968 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
8969 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
8970 if (UI.getUse().getResNo() != InputVector.getResNo())
8971 return SDValue();
8972
8973 SDNode *Extract = *UI;
8974 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8975 return SDValue();
8976
8977 if (Extract->getValueType(0) != MVT::i32)
8978 return SDValue();
8979 if (!Extract->hasOneUse())
8980 return SDValue();
8981 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
8982 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
8983 return SDValue();
8984 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
8985 return SDValue();
8986
8987 // Record which element was extracted.
8988 ExtractedElements |=
8989 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
8990
8991 Uses.push_back(Extract);
8992 }
8993
8994 // If not all the elements were used, this may not be worthwhile.
8995 if (ExtractedElements != 15)
8996 return SDValue();
8997
8998 // Ok, we've now decided to do the transformation.
8999 DebugLoc dl = InputVector.getDebugLoc();
9000
9001 // Store the value to a temporary stack slot.
9002 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
9003 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr, NULL, 0,
9004 false, false, 0);
9005
9006 // Replace each use (extract) with a load of the appropriate element.
9007 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
9008 UE = Uses.end(); UI != UE; ++UI) {
9009 SDNode *Extract = *UI;
9010
9011 // Compute the element's address.
9012 SDValue Idx = Extract->getOperand(1);
9013 unsigned EltSize =
9014 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
9015 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
9016 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
9017
9018 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), OffsetVal, StackPtr);
9019
9020 // Load the scalar.
9021 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch, ScalarAddr,
9022 NULL, 0, false, false, 0);
9023
9024 // Replace the exact with the load.
9025 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
9026 }
9027
9028 // The replacement was made in place; don't return anything.
9029 return SDValue();
9030}
9031
Chris Lattner83e6c992006-10-04 06:57:07 +00009032/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009033static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +00009034 const X86Subtarget *Subtarget) {
9035 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009036 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +00009037 // Get the LHS/RHS of the select.
9038 SDValue LHS = N->getOperand(1);
9039 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +00009040
Dan Gohman670e5392009-09-21 18:03:22 +00009041 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +00009042 // instructions match the semantics of the common C idiom x<y?x:y but not
9043 // x<=y?x:y, because of how they handle negative zero (which can be
9044 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +00009045 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00009046 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +00009047 Cond.getOpcode() == ISD::SETCC) {
9048 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009049
Chris Lattner47b4ce82009-03-11 05:48:52 +00009050 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +00009051 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +00009052 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
9053 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +00009054 switch (CC) {
9055 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +00009056 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +00009057 // Converting this to a min would handle NaNs incorrectly, and swapping
9058 // the operands would cause it to handle comparisons between positive
9059 // and negative zero incorrectly.
9060 if (!FiniteOnlyFPMath() &&
9061 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))) {
9062 if (!UnsafeFPMath &&
9063 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9064 break;
9065 std::swap(LHS, RHS);
9066 }
Dan Gohman670e5392009-09-21 18:03:22 +00009067 Opcode = X86ISD::FMIN;
9068 break;
9069 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +00009070 // Converting this to a min would handle comparisons between positive
9071 // and negative zero incorrectly.
9072 if (!UnsafeFPMath &&
9073 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
9074 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009075 Opcode = X86ISD::FMIN;
9076 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +00009077 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +00009078 // Converting this to a min would handle both negative zeros and NaNs
9079 // incorrectly, but we can swap the operands to fix both.
9080 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00009081 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009082 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +00009083 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009084 Opcode = X86ISD::FMIN;
9085 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009086
Dan Gohman670e5392009-09-21 18:03:22 +00009087 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +00009088 // Converting this to a max would handle comparisons between positive
9089 // and negative zero incorrectly.
9090 if (!UnsafeFPMath &&
9091 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
9092 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009093 Opcode = X86ISD::FMAX;
9094 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +00009095 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +00009096 // Converting this to a max would handle NaNs incorrectly, and swapping
9097 // the operands would cause it to handle comparisons between positive
9098 // and negative zero incorrectly.
9099 if (!FiniteOnlyFPMath() &&
9100 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))) {
9101 if (!UnsafeFPMath &&
9102 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9103 break;
9104 std::swap(LHS, RHS);
9105 }
Dan Gohman670e5392009-09-21 18:03:22 +00009106 Opcode = X86ISD::FMAX;
9107 break;
9108 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +00009109 // Converting this to a max would handle both negative zeros and NaNs
9110 // incorrectly, but we can swap the operands to fix both.
9111 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00009112 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009113 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009114 case ISD::SETGE:
9115 Opcode = X86ISD::FMAX;
9116 break;
Chris Lattner83e6c992006-10-04 06:57:07 +00009117 }
Dan Gohman670e5392009-09-21 18:03:22 +00009118 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +00009119 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
9120 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +00009121 switch (CC) {
9122 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +00009123 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +00009124 // Converting this to a min would handle comparisons between positive
9125 // and negative zero incorrectly, and swapping the operands would
9126 // cause it to handle NaNs incorrectly.
9127 if (!UnsafeFPMath &&
9128 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
9129 if (!FiniteOnlyFPMath() &&
9130 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
9131 break;
9132 std::swap(LHS, RHS);
9133 }
Dan Gohman670e5392009-09-21 18:03:22 +00009134 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +00009135 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009136 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +00009137 // Converting this to a min would handle NaNs incorrectly.
9138 if (!UnsafeFPMath &&
9139 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
9140 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009141 Opcode = X86ISD::FMIN;
9142 break;
9143 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +00009144 // Converting this to a min would handle both negative zeros and NaNs
9145 // incorrectly, but we can swap the operands to fix both.
9146 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00009147 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009148 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009149 case ISD::SETGE:
9150 Opcode = X86ISD::FMIN;
9151 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009152
Dan Gohman670e5392009-09-21 18:03:22 +00009153 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +00009154 // Converting this to a max would handle NaNs incorrectly.
9155 if (!FiniteOnlyFPMath() &&
9156 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
9157 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009158 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +00009159 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009160 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +00009161 // Converting this to a max would handle comparisons between positive
9162 // and negative zero incorrectly, and swapping the operands would
9163 // cause it to handle NaNs incorrectly.
9164 if (!UnsafeFPMath &&
9165 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
9166 if (!FiniteOnlyFPMath() &&
9167 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
9168 break;
9169 std::swap(LHS, RHS);
9170 }
Dan Gohman670e5392009-09-21 18:03:22 +00009171 Opcode = X86ISD::FMAX;
9172 break;
9173 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +00009174 // Converting this to a max would handle both negative zeros and NaNs
9175 // incorrectly, but we can swap the operands to fix both.
9176 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00009177 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009178 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +00009179 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009180 Opcode = X86ISD::FMAX;
9181 break;
9182 }
Chris Lattner83e6c992006-10-04 06:57:07 +00009183 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009184
Chris Lattner47b4ce82009-03-11 05:48:52 +00009185 if (Opcode)
9186 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +00009187 }
Eric Christopherfd179292009-08-27 18:07:15 +00009188
Chris Lattnerd1980a52009-03-12 06:52:53 +00009189 // If this is a select between two integer constants, try to do some
9190 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +00009191 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
9192 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +00009193 // Don't do this for crazy integer types.
9194 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
9195 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +00009196 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +00009197 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +00009198
Chris Lattnercee56e72009-03-13 05:53:31 +00009199 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +00009200 // Efficiently invertible.
9201 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
9202 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
9203 isa<ConstantSDNode>(Cond.getOperand(1))))) {
9204 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +00009205 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +00009206 }
Eric Christopherfd179292009-08-27 18:07:15 +00009207
Chris Lattnerd1980a52009-03-12 06:52:53 +00009208 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00009209 if (FalseC->getAPIntValue() == 0 &&
9210 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00009211 if (NeedsCondInvert) // Invert the condition if needed.
9212 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
9213 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009214
Chris Lattnerd1980a52009-03-12 06:52:53 +00009215 // Zero extend the condition if needed.
9216 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009217
Chris Lattnercee56e72009-03-13 05:53:31 +00009218 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +00009219 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +00009220 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +00009221 }
Eric Christopherfd179292009-08-27 18:07:15 +00009222
Chris Lattner97a29a52009-03-13 05:22:11 +00009223 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +00009224 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +00009225 if (NeedsCondInvert) // Invert the condition if needed.
9226 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
9227 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009228
Chris Lattner97a29a52009-03-13 05:22:11 +00009229 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00009230 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
9231 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00009232 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +00009233 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +00009234 }
Eric Christopherfd179292009-08-27 18:07:15 +00009235
Chris Lattnercee56e72009-03-13 05:53:31 +00009236 // Optimize cases that will turn into an LEA instruction. This requires
9237 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +00009238 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +00009239 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00009240 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +00009241
Chris Lattnercee56e72009-03-13 05:53:31 +00009242 bool isFastMultiplier = false;
9243 if (Diff < 10) {
9244 switch ((unsigned char)Diff) {
9245 default: break;
9246 case 1: // result = add base, cond
9247 case 2: // result = lea base( , cond*2)
9248 case 3: // result = lea base(cond, cond*2)
9249 case 4: // result = lea base( , cond*4)
9250 case 5: // result = lea base(cond, cond*4)
9251 case 8: // result = lea base( , cond*8)
9252 case 9: // result = lea base(cond, cond*8)
9253 isFastMultiplier = true;
9254 break;
9255 }
9256 }
Eric Christopherfd179292009-08-27 18:07:15 +00009257
Chris Lattnercee56e72009-03-13 05:53:31 +00009258 if (isFastMultiplier) {
9259 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
9260 if (NeedsCondInvert) // Invert the condition if needed.
9261 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
9262 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009263
Chris Lattnercee56e72009-03-13 05:53:31 +00009264 // Zero extend the condition if needed.
9265 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
9266 Cond);
9267 // Scale the condition by the difference.
9268 if (Diff != 1)
9269 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
9270 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009271
Chris Lattnercee56e72009-03-13 05:53:31 +00009272 // Add the base if non-zero.
9273 if (FalseC->getAPIntValue() != 0)
9274 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9275 SDValue(FalseC, 0));
9276 return Cond;
9277 }
Eric Christopherfd179292009-08-27 18:07:15 +00009278 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00009279 }
9280 }
Eric Christopherfd179292009-08-27 18:07:15 +00009281
Dan Gohman475871a2008-07-27 21:46:04 +00009282 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +00009283}
9284
Chris Lattnerd1980a52009-03-12 06:52:53 +00009285/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
9286static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
9287 TargetLowering::DAGCombinerInfo &DCI) {
9288 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +00009289
Chris Lattnerd1980a52009-03-12 06:52:53 +00009290 // If the flag operand isn't dead, don't touch this CMOV.
9291 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
9292 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +00009293
Chris Lattnerd1980a52009-03-12 06:52:53 +00009294 // If this is a select between two integer constants, try to do some
9295 // optimizations. Note that the operands are ordered the opposite of SELECT
9296 // operands.
9297 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
9298 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
9299 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
9300 // larger than FalseC (the false value).
9301 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
Eric Christopherfd179292009-08-27 18:07:15 +00009302
Chris Lattnerd1980a52009-03-12 06:52:53 +00009303 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
9304 CC = X86::GetOppositeBranchCondition(CC);
9305 std::swap(TrueC, FalseC);
9306 }
Eric Christopherfd179292009-08-27 18:07:15 +00009307
Chris Lattnerd1980a52009-03-12 06:52:53 +00009308 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00009309 // This is efficient for any integer data type (including i8/i16) and
9310 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +00009311 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
9312 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00009313 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9314 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009315
Chris Lattnerd1980a52009-03-12 06:52:53 +00009316 // Zero extend the condition if needed.
9317 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009318
Chris Lattnerd1980a52009-03-12 06:52:53 +00009319 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
9320 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +00009321 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +00009322 if (N->getNumValues() == 2) // Dead flag value?
9323 return DCI.CombineTo(N, Cond, SDValue());
9324 return Cond;
9325 }
Eric Christopherfd179292009-08-27 18:07:15 +00009326
Chris Lattnercee56e72009-03-13 05:53:31 +00009327 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
9328 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +00009329 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
9330 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00009331 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9332 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009333
Chris Lattner97a29a52009-03-13 05:22:11 +00009334 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00009335 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
9336 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00009337 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9338 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +00009339
Chris Lattner97a29a52009-03-13 05:22:11 +00009340 if (N->getNumValues() == 2) // Dead flag value?
9341 return DCI.CombineTo(N, Cond, SDValue());
9342 return Cond;
9343 }
Eric Christopherfd179292009-08-27 18:07:15 +00009344
Chris Lattnercee56e72009-03-13 05:53:31 +00009345 // Optimize cases that will turn into an LEA instruction. This requires
9346 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +00009347 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +00009348 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00009349 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +00009350
Chris Lattnercee56e72009-03-13 05:53:31 +00009351 bool isFastMultiplier = false;
9352 if (Diff < 10) {
9353 switch ((unsigned char)Diff) {
9354 default: break;
9355 case 1: // result = add base, cond
9356 case 2: // result = lea base( , cond*2)
9357 case 3: // result = lea base(cond, cond*2)
9358 case 4: // result = lea base( , cond*4)
9359 case 5: // result = lea base(cond, cond*4)
9360 case 8: // result = lea base( , cond*8)
9361 case 9: // result = lea base(cond, cond*8)
9362 isFastMultiplier = true;
9363 break;
9364 }
9365 }
Eric Christopherfd179292009-08-27 18:07:15 +00009366
Chris Lattnercee56e72009-03-13 05:53:31 +00009367 if (isFastMultiplier) {
9368 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
9369 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00009370 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9371 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +00009372 // Zero extend the condition if needed.
9373 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
9374 Cond);
9375 // Scale the condition by the difference.
9376 if (Diff != 1)
9377 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
9378 DAG.getConstant(Diff, Cond.getValueType()));
9379
9380 // Add the base if non-zero.
9381 if (FalseC->getAPIntValue() != 0)
9382 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9383 SDValue(FalseC, 0));
9384 if (N->getNumValues() == 2) // Dead flag value?
9385 return DCI.CombineTo(N, Cond, SDValue());
9386 return Cond;
9387 }
Eric Christopherfd179292009-08-27 18:07:15 +00009388 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00009389 }
9390 }
9391 return SDValue();
9392}
9393
9394
Evan Cheng0b0cd912009-03-28 05:57:29 +00009395/// PerformMulCombine - Optimize a single multiply with constant into two
9396/// in order to implement it with two cheaper instructions, e.g.
9397/// LEA + SHL, LEA + LEA.
9398static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
9399 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +00009400 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
9401 return SDValue();
9402
Owen Andersone50ed302009-08-10 22:56:29 +00009403 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009404 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +00009405 return SDValue();
9406
9407 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
9408 if (!C)
9409 return SDValue();
9410 uint64_t MulAmt = C->getZExtValue();
9411 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
9412 return SDValue();
9413
9414 uint64_t MulAmt1 = 0;
9415 uint64_t MulAmt2 = 0;
9416 if ((MulAmt % 9) == 0) {
9417 MulAmt1 = 9;
9418 MulAmt2 = MulAmt / 9;
9419 } else if ((MulAmt % 5) == 0) {
9420 MulAmt1 = 5;
9421 MulAmt2 = MulAmt / 5;
9422 } else if ((MulAmt % 3) == 0) {
9423 MulAmt1 = 3;
9424 MulAmt2 = MulAmt / 3;
9425 }
9426 if (MulAmt2 &&
9427 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
9428 DebugLoc DL = N->getDebugLoc();
9429
9430 if (isPowerOf2_64(MulAmt2) &&
9431 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
9432 // If second multiplifer is pow2, issue it first. We want the multiply by
9433 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
9434 // is an add.
9435 std::swap(MulAmt1, MulAmt2);
9436
9437 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +00009438 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +00009439 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00009440 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +00009441 else
Evan Cheng73f24c92009-03-30 21:36:47 +00009442 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +00009443 DAG.getConstant(MulAmt1, VT));
9444
Eric Christopherfd179292009-08-27 18:07:15 +00009445 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +00009446 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +00009447 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +00009448 else
Evan Cheng73f24c92009-03-30 21:36:47 +00009449 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +00009450 DAG.getConstant(MulAmt2, VT));
9451
9452 // Do not add new nodes to DAG combiner worklist.
9453 DCI.CombineTo(N, NewMul, false);
9454 }
9455 return SDValue();
9456}
9457
Evan Chengad9c0a32009-12-15 00:53:42 +00009458static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
9459 SDValue N0 = N->getOperand(0);
9460 SDValue N1 = N->getOperand(1);
9461 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
9462 EVT VT = N0.getValueType();
9463
9464 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
9465 // since the result of setcc_c is all zero's or all ones.
9466 if (N1C && N0.getOpcode() == ISD::AND &&
9467 N0.getOperand(1).getOpcode() == ISD::Constant) {
9468 SDValue N00 = N0.getOperand(0);
9469 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
9470 ((N00.getOpcode() == ISD::ANY_EXTEND ||
9471 N00.getOpcode() == ISD::ZERO_EXTEND) &&
9472 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
9473 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
9474 APInt ShAmt = N1C->getAPIntValue();
9475 Mask = Mask.shl(ShAmt);
9476 if (Mask != 0)
9477 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
9478 N00, DAG.getConstant(Mask, VT));
9479 }
9480 }
9481
9482 return SDValue();
9483}
Evan Cheng0b0cd912009-03-28 05:57:29 +00009484
Nate Begeman740ab032009-01-26 00:52:55 +00009485/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
9486/// when possible.
9487static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
9488 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +00009489 EVT VT = N->getValueType(0);
9490 if (!VT.isVector() && VT.isInteger() &&
9491 N->getOpcode() == ISD::SHL)
9492 return PerformSHLCombine(N, DAG);
9493
Nate Begeman740ab032009-01-26 00:52:55 +00009494 // On X86 with SSE2 support, we can transform this to a vector shift if
9495 // all elements are shifted by the same amount. We can't do this in legalize
9496 // because the a constant vector is typically transformed to a constant pool
9497 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009498 if (!Subtarget->hasSSE2())
9499 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009500
Owen Anderson825b72b2009-08-11 20:47:22 +00009501 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009502 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009503
Mon P Wang3becd092009-01-28 08:12:05 +00009504 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00009505 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +00009506 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +00009507 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +00009508 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
9509 unsigned NumElts = VT.getVectorNumElements();
9510 unsigned i = 0;
9511 for (; i != NumElts; ++i) {
9512 SDValue Arg = ShAmtOp.getOperand(i);
9513 if (Arg.getOpcode() == ISD::UNDEF) continue;
9514 BaseShAmt = Arg;
9515 break;
9516 }
9517 for (; i != NumElts; ++i) {
9518 SDValue Arg = ShAmtOp.getOperand(i);
9519 if (Arg.getOpcode() == ISD::UNDEF) continue;
9520 if (Arg != BaseShAmt) {
9521 return SDValue();
9522 }
9523 }
9524 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +00009525 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +00009526 SDValue InVec = ShAmtOp.getOperand(0);
9527 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
9528 unsigned NumElts = InVec.getValueType().getVectorNumElements();
9529 unsigned i = 0;
9530 for (; i != NumElts; ++i) {
9531 SDValue Arg = InVec.getOperand(i);
9532 if (Arg.getOpcode() == ISD::UNDEF) continue;
9533 BaseShAmt = Arg;
9534 break;
9535 }
9536 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
9537 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +00009538 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +00009539 if (C->getZExtValue() == SplatIdx)
9540 BaseShAmt = InVec.getOperand(1);
9541 }
9542 }
9543 if (BaseShAmt.getNode() == 0)
9544 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
9545 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +00009546 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009547 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +00009548
Mon P Wangefa42202009-09-03 19:56:25 +00009549 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +00009550 if (EltVT.bitsGT(MVT::i32))
9551 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
9552 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +00009553 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +00009554
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009555 // The shift amount is identical so we can do a vector shift.
9556 SDValue ValOp = N->getOperand(0);
9557 switch (N->getOpcode()) {
9558 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009559 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009560 break;
9561 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +00009562 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009563 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009564 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009565 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009566 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009567 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009568 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009569 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009570 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009571 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009572 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009573 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009574 break;
9575 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +00009576 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009577 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009578 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009579 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009580 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009581 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009582 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009583 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009584 break;
9585 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +00009586 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009587 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009588 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009589 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009590 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009591 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009592 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009593 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009594 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009595 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009596 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009597 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009598 break;
Nate Begeman740ab032009-01-26 00:52:55 +00009599 }
9600 return SDValue();
9601}
9602
Evan Cheng760d1942010-01-04 21:22:48 +00009603static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +00009604 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +00009605 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +00009606 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +00009607 return SDValue();
9608
Evan Cheng760d1942010-01-04 21:22:48 +00009609 EVT VT = N->getValueType(0);
Evan Cheng8b1190a2010-04-28 01:18:01 +00009610 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
Evan Cheng760d1942010-01-04 21:22:48 +00009611 return SDValue();
9612
9613 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
9614 SDValue N0 = N->getOperand(0);
9615 SDValue N1 = N->getOperand(1);
9616 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
9617 std::swap(N0, N1);
9618 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
9619 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +00009620 if (!N0.hasOneUse() || !N1.hasOneUse())
9621 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +00009622
9623 SDValue ShAmt0 = N0.getOperand(1);
9624 if (ShAmt0.getValueType() != MVT::i8)
9625 return SDValue();
9626 SDValue ShAmt1 = N1.getOperand(1);
9627 if (ShAmt1.getValueType() != MVT::i8)
9628 return SDValue();
9629 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
9630 ShAmt0 = ShAmt0.getOperand(0);
9631 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
9632 ShAmt1 = ShAmt1.getOperand(0);
9633
9634 DebugLoc DL = N->getDebugLoc();
9635 unsigned Opc = X86ISD::SHLD;
9636 SDValue Op0 = N0.getOperand(0);
9637 SDValue Op1 = N1.getOperand(0);
9638 if (ShAmt0.getOpcode() == ISD::SUB) {
9639 Opc = X86ISD::SHRD;
9640 std::swap(Op0, Op1);
9641 std::swap(ShAmt0, ShAmt1);
9642 }
9643
Evan Cheng8b1190a2010-04-28 01:18:01 +00009644 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +00009645 if (ShAmt1.getOpcode() == ISD::SUB) {
9646 SDValue Sum = ShAmt1.getOperand(0);
9647 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Evan Cheng8b1190a2010-04-28 01:18:01 +00009648 if (SumC->getSExtValue() == Bits &&
Evan Cheng760d1942010-01-04 21:22:48 +00009649 ShAmt1.getOperand(1) == ShAmt0)
9650 return DAG.getNode(Opc, DL, VT,
9651 Op0, Op1,
9652 DAG.getNode(ISD::TRUNCATE, DL,
9653 MVT::i8, ShAmt0));
9654 }
9655 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
9656 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
9657 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +00009658 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +00009659 return DAG.getNode(Opc, DL, VT,
9660 N0.getOperand(0), N1.getOperand(0),
9661 DAG.getNode(ISD::TRUNCATE, DL,
9662 MVT::i8, ShAmt0));
9663 }
9664
9665 return SDValue();
9666}
9667
Chris Lattner149a4e52008-02-22 02:09:43 +00009668/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009669static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +00009670 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +00009671 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
9672 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +00009673 // A preferable solution to the general problem is to figure out the right
9674 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +00009675
9676 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +00009677 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +00009678 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +00009679 if (VT.getSizeInBits() != 64)
9680 return SDValue();
9681
Devang Patel578efa92009-06-05 21:57:13 +00009682 const Function *F = DAG.getMachineFunction().getFunction();
9683 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +00009684 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +00009685 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +00009686 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +00009687 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +00009688 isa<LoadSDNode>(St->getValue()) &&
9689 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
9690 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00009691 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009692 LoadSDNode *Ld = 0;
9693 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +00009694 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +00009695 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009696 // Must be a store of a load. We currently handle two cases: the load
9697 // is a direct child, and it's under an intervening TokenFactor. It is
9698 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +00009699 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +00009700 Ld = cast<LoadSDNode>(St->getChain());
9701 else if (St->getValue().hasOneUse() &&
9702 ChainVal->getOpcode() == ISD::TokenFactor) {
9703 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +00009704 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +00009705 TokenFactorIndex = i;
9706 Ld = cast<LoadSDNode>(St->getValue());
9707 } else
9708 Ops.push_back(ChainVal->getOperand(i));
9709 }
9710 }
Dale Johannesen079f2a62008-02-25 19:20:14 +00009711
Evan Cheng536e6672009-03-12 05:59:15 +00009712 if (!Ld || !ISD::isNormalLoad(Ld))
9713 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009714
Evan Cheng536e6672009-03-12 05:59:15 +00009715 // If this is not the MMX case, i.e. we are just turning i64 load/store
9716 // into f64 load/store, avoid the transformation if there are multiple
9717 // uses of the loaded value.
9718 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
9719 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009720
Evan Cheng536e6672009-03-12 05:59:15 +00009721 DebugLoc LdDL = Ld->getDebugLoc();
9722 DebugLoc StDL = N->getDebugLoc();
9723 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
9724 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
9725 // pair instead.
9726 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009727 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Evan Cheng536e6672009-03-12 05:59:15 +00009728 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(),
9729 Ld->getBasePtr(), Ld->getSrcValue(),
9730 Ld->getSrcValueOffset(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +00009731 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +00009732 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +00009733 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +00009734 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +00009735 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +00009736 Ops.size());
9737 }
Evan Cheng536e6672009-03-12 05:59:15 +00009738 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner149a4e52008-02-22 02:09:43 +00009739 St->getSrcValue(), St->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00009740 St->isVolatile(), St->isNonTemporal(),
9741 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +00009742 }
Evan Cheng536e6672009-03-12 05:59:15 +00009743
9744 // Otherwise, lower to two pairs of 32-bit loads / stores.
9745 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +00009746 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
9747 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +00009748
Owen Anderson825b72b2009-08-11 20:47:22 +00009749 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Evan Cheng536e6672009-03-12 05:59:15 +00009750 Ld->getSrcValue(), Ld->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00009751 Ld->isVolatile(), Ld->isNonTemporal(),
9752 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00009753 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Evan Cheng536e6672009-03-12 05:59:15 +00009754 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
David Greene67c9d422010-02-15 16:53:33 +00009755 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +00009756 MinAlign(Ld->getAlignment(), 4));
9757
9758 SDValue NewChain = LoLd.getValue(1);
9759 if (TokenFactorIndex != -1) {
9760 Ops.push_back(LoLd);
9761 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +00009762 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +00009763 Ops.size());
9764 }
9765
9766 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +00009767 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
9768 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +00009769
9770 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
9771 St->getSrcValue(), St->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00009772 St->isVolatile(), St->isNonTemporal(),
9773 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +00009774 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
9775 St->getSrcValue(),
9776 St->getSrcValueOffset() + 4,
9777 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +00009778 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +00009779 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +00009780 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +00009781 }
Dan Gohman475871a2008-07-27 21:46:04 +00009782 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +00009783}
9784
Chris Lattner6cf73262008-01-25 06:14:17 +00009785/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
9786/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009787static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +00009788 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
9789 // F[X]OR(0.0, x) -> x
9790 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +00009791 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
9792 if (C->getValueAPF().isPosZero())
9793 return N->getOperand(1);
9794 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
9795 if (C->getValueAPF().isPosZero())
9796 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +00009797 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00009798}
9799
9800/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009801static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +00009802 // FAND(0.0, x) -> 0.0
9803 // FAND(x, 0.0) -> 0.0
9804 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
9805 if (C->getValueAPF().isPosZero())
9806 return N->getOperand(0);
9807 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
9808 if (C->getValueAPF().isPosZero())
9809 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +00009810 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00009811}
9812
Dan Gohmane5af2d32009-01-29 01:59:02 +00009813static SDValue PerformBTCombine(SDNode *N,
9814 SelectionDAG &DAG,
9815 TargetLowering::DAGCombinerInfo &DCI) {
9816 // BT ignores high bits in the bit index operand.
9817 SDValue Op1 = N->getOperand(1);
9818 if (Op1.hasOneUse()) {
9819 unsigned BitWidth = Op1.getValueSizeInBits();
9820 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
9821 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +00009822 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
9823 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +00009824 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +00009825 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
9826 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
9827 DCI.CommitTargetLoweringOpt(TLO);
9828 }
9829 return SDValue();
9830}
Chris Lattner83e6c992006-10-04 06:57:07 +00009831
Eli Friedman7a5e5552009-06-07 06:52:44 +00009832static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
9833 SDValue Op = N->getOperand(0);
9834 if (Op.getOpcode() == ISD::BIT_CONVERT)
9835 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00009836 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +00009837 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +00009838 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +00009839 OpVT.getVectorElementType().getSizeInBits()) {
9840 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT, Op);
9841 }
9842 return SDValue();
9843}
9844
Owen Anderson99177002009-06-29 18:04:45 +00009845// On X86 and X86-64, atomic operations are lowered to locked instructions.
9846// Locked instructions, in turn, have implicit fence semantics (all memory
9847// operations are flushed before issuing the locked instruction, and the
Eric Christopherfd179292009-08-27 18:07:15 +00009848// are not buffered), so we can fold away the common pattern of
Owen Anderson99177002009-06-29 18:04:45 +00009849// fence-atomic-fence.
9850static SDValue PerformMEMBARRIERCombine(SDNode* N, SelectionDAG &DAG) {
9851 SDValue atomic = N->getOperand(0);
9852 switch (atomic.getOpcode()) {
9853 case ISD::ATOMIC_CMP_SWAP:
9854 case ISD::ATOMIC_SWAP:
9855 case ISD::ATOMIC_LOAD_ADD:
9856 case ISD::ATOMIC_LOAD_SUB:
9857 case ISD::ATOMIC_LOAD_AND:
9858 case ISD::ATOMIC_LOAD_OR:
9859 case ISD::ATOMIC_LOAD_XOR:
9860 case ISD::ATOMIC_LOAD_NAND:
9861 case ISD::ATOMIC_LOAD_MIN:
9862 case ISD::ATOMIC_LOAD_MAX:
9863 case ISD::ATOMIC_LOAD_UMIN:
9864 case ISD::ATOMIC_LOAD_UMAX:
9865 break;
9866 default:
9867 return SDValue();
9868 }
Eric Christopherfd179292009-08-27 18:07:15 +00009869
Owen Anderson99177002009-06-29 18:04:45 +00009870 SDValue fence = atomic.getOperand(0);
9871 if (fence.getOpcode() != ISD::MEMBARRIER)
9872 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +00009873
Owen Anderson99177002009-06-29 18:04:45 +00009874 switch (atomic.getOpcode()) {
9875 case ISD::ATOMIC_CMP_SWAP:
9876 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
9877 atomic.getOperand(1), atomic.getOperand(2),
9878 atomic.getOperand(3));
9879 case ISD::ATOMIC_SWAP:
9880 case ISD::ATOMIC_LOAD_ADD:
9881 case ISD::ATOMIC_LOAD_SUB:
9882 case ISD::ATOMIC_LOAD_AND:
9883 case ISD::ATOMIC_LOAD_OR:
9884 case ISD::ATOMIC_LOAD_XOR:
9885 case ISD::ATOMIC_LOAD_NAND:
9886 case ISD::ATOMIC_LOAD_MIN:
9887 case ISD::ATOMIC_LOAD_MAX:
9888 case ISD::ATOMIC_LOAD_UMIN:
9889 case ISD::ATOMIC_LOAD_UMAX:
9890 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
9891 atomic.getOperand(1), atomic.getOperand(2));
9892 default:
9893 return SDValue();
9894 }
9895}
9896
Evan Cheng2e489c42009-12-16 00:53:11 +00009897static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
9898 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
9899 // (and (i32 x86isd::setcc_carry), 1)
9900 // This eliminates the zext. This transformation is necessary because
9901 // ISD::SETCC is always legalized to i8.
9902 DebugLoc dl = N->getDebugLoc();
9903 SDValue N0 = N->getOperand(0);
9904 EVT VT = N->getValueType(0);
9905 if (N0.getOpcode() == ISD::AND &&
9906 N0.hasOneUse() &&
9907 N0.getOperand(0).hasOneUse()) {
9908 SDValue N00 = N0.getOperand(0);
9909 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
9910 return SDValue();
9911 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
9912 if (!C || C->getZExtValue() != 1)
9913 return SDValue();
9914 return DAG.getNode(ISD::AND, dl, VT,
9915 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
9916 N00.getOperand(0), N00.getOperand(1)),
9917 DAG.getConstant(1, VT));
9918 }
9919
9920 return SDValue();
9921}
9922
Dan Gohman475871a2008-07-27 21:46:04 +00009923SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +00009924 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +00009925 SelectionDAG &DAG = DCI.DAG;
9926 switch (N->getOpcode()) {
9927 default: break;
Evan Chengad4196b2008-05-12 19:56:52 +00009928 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00009929 case ISD::EXTRACT_VECTOR_ELT:
9930 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +00009931 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +00009932 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +00009933 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +00009934 case ISD::SHL:
9935 case ISD::SRA:
9936 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +00009937 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +00009938 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +00009939 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +00009940 case X86ISD::FOR: return PerformFORCombine(N, DAG);
9941 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +00009942 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +00009943 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Owen Anderson99177002009-06-29 18:04:45 +00009944 case ISD::MEMBARRIER: return PerformMEMBARRIERCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +00009945 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Evan Cheng206ee9d2006-07-07 08:33:52 +00009946 }
9947
Dan Gohman475871a2008-07-27 21:46:04 +00009948 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00009949}
9950
Evan Chenge5b51ac2010-04-17 06:13:15 +00009951/// isTypeDesirableForOp - Return true if the target has native support for
9952/// the specified value type and it is 'desirable' to use the type for the
9953/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
9954/// instruction encodings are longer and some i16 instructions are slow.
9955bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
9956 if (!isTypeLegal(VT))
9957 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00009958 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +00009959 return true;
9960
9961 switch (Opc) {
9962 default:
9963 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +00009964 case ISD::LOAD:
9965 case ISD::SIGN_EXTEND:
9966 case ISD::ZERO_EXTEND:
9967 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +00009968 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +00009969 case ISD::SRL:
9970 case ISD::SUB:
9971 case ISD::ADD:
9972 case ISD::MUL:
9973 case ISD::AND:
9974 case ISD::OR:
9975 case ISD::XOR:
9976 return false;
9977 }
9978}
9979
Evan Chengc82c20b2010-04-24 04:44:57 +00009980static bool MayFoldLoad(SDValue Op) {
9981 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
9982}
9983
9984static bool MayFoldIntoStore(SDValue Op) {
9985 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
9986}
9987
Evan Chenge5b51ac2010-04-17 06:13:15 +00009988/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +00009989/// beneficial for dag combiner to promote the specified node. If true, it
9990/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +00009991bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +00009992 EVT VT = Op.getValueType();
9993 if (VT != MVT::i16)
9994 return false;
9995
Evan Cheng4c26e932010-04-19 19:29:22 +00009996 bool Promote = false;
9997 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +00009998 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +00009999 default: break;
10000 case ISD::LOAD: {
10001 LoadSDNode *LD = cast<LoadSDNode>(Op);
10002 // If the non-extending load has a single use and it's not live out, then it
10003 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000010004 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
10005 Op.hasOneUse()*/) {
10006 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
10007 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
10008 // The only case where we'd want to promote LOAD (rather then it being
10009 // promoted as an operand is when it's only use is liveout.
10010 if (UI->getOpcode() != ISD::CopyToReg)
10011 return false;
10012 }
10013 }
Evan Cheng4c26e932010-04-19 19:29:22 +000010014 Promote = true;
10015 break;
10016 }
10017 case ISD::SIGN_EXTEND:
10018 case ISD::ZERO_EXTEND:
10019 case ISD::ANY_EXTEND:
10020 Promote = true;
10021 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000010022 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000010023 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000010024 SDValue N0 = Op.getOperand(0);
10025 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000010026 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000010027 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000010028 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000010029 break;
10030 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000010031 case ISD::ADD:
10032 case ISD::MUL:
10033 case ISD::AND:
10034 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000010035 case ISD::XOR:
10036 Commute = true;
10037 // fallthrough
10038 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000010039 SDValue N0 = Op.getOperand(0);
10040 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000010041 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000010042 return false;
10043 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000010044 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000010045 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000010046 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000010047 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000010048 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000010049 }
10050 }
10051
10052 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000010053 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000010054}
10055
Evan Cheng60c07e12006-07-05 22:17:51 +000010056//===----------------------------------------------------------------------===//
10057// X86 Inline Assembly Support
10058//===----------------------------------------------------------------------===//
10059
Chris Lattnerb8105652009-07-20 17:51:36 +000010060static bool LowerToBSwap(CallInst *CI) {
10061 // FIXME: this should verify that we are targetting a 486 or better. If not,
10062 // we will turn this bswap into something that will be lowered to logical ops
10063 // instead of emitting the bswap asm. For now, we don't support 486 or lower
10064 // so don't worry about this.
Eric Christopherfd179292009-08-27 18:07:15 +000010065
Chris Lattnerb8105652009-07-20 17:51:36 +000010066 // Verify this is a simple bswap.
10067 if (CI->getNumOperands() != 2 ||
Eric Christopher551754c2010-04-16 23:37:20 +000010068 CI->getType() != CI->getOperand(1)->getType() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000010069 !CI->getType()->isIntegerTy())
Chris Lattnerb8105652009-07-20 17:51:36 +000010070 return false;
Eric Christopherfd179292009-08-27 18:07:15 +000010071
Chris Lattnerb8105652009-07-20 17:51:36 +000010072 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
10073 if (!Ty || Ty->getBitWidth() % 16 != 0)
10074 return false;
Eric Christopherfd179292009-08-27 18:07:15 +000010075
Chris Lattnerb8105652009-07-20 17:51:36 +000010076 // Okay, we can do this xform, do so now.
10077 const Type *Tys[] = { Ty };
10078 Module *M = CI->getParent()->getParent()->getParent();
10079 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
Eric Christopherfd179292009-08-27 18:07:15 +000010080
Eric Christopher551754c2010-04-16 23:37:20 +000010081 Value *Op = CI->getOperand(1);
Chris Lattnerb8105652009-07-20 17:51:36 +000010082 Op = CallInst::Create(Int, Op, CI->getName(), CI);
Eric Christopherfd179292009-08-27 18:07:15 +000010083
Chris Lattnerb8105652009-07-20 17:51:36 +000010084 CI->replaceAllUsesWith(Op);
10085 CI->eraseFromParent();
10086 return true;
10087}
10088
10089bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
10090 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
10091 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
10092
10093 std::string AsmStr = IA->getAsmString();
10094
10095 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000010096 SmallVector<StringRef, 4> AsmPieces;
Chris Lattnerb8105652009-07-20 17:51:36 +000010097 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
10098
10099 switch (AsmPieces.size()) {
10100 default: return false;
10101 case 1:
10102 AsmStr = AsmPieces[0];
10103 AsmPieces.clear();
10104 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
10105
10106 // bswap $0
10107 if (AsmPieces.size() == 2 &&
10108 (AsmPieces[0] == "bswap" ||
10109 AsmPieces[0] == "bswapq" ||
10110 AsmPieces[0] == "bswapl") &&
10111 (AsmPieces[1] == "$0" ||
10112 AsmPieces[1] == "${0:q}")) {
10113 // No need to check constraints, nothing other than the equivalent of
10114 // "=r,0" would be valid here.
10115 return LowerToBSwap(CI);
10116 }
10117 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000010118 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000010119 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000010120 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000010121 AsmPieces[1] == "$$8," &&
10122 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000010123 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
10124 AsmPieces.clear();
Benjamin Kramer018cbd52010-03-12 13:54:59 +000010125 const std::string &Constraints = IA->getConstraintString();
10126 SplitString(StringRef(Constraints).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000010127 std::sort(AsmPieces.begin(), AsmPieces.end());
10128 if (AsmPieces.size() == 4 &&
10129 AsmPieces[0] == "~{cc}" &&
10130 AsmPieces[1] == "~{dirflag}" &&
10131 AsmPieces[2] == "~{flags}" &&
10132 AsmPieces[3] == "~{fpsr}") {
10133 return LowerToBSwap(CI);
10134 }
Chris Lattnerb8105652009-07-20 17:51:36 +000010135 }
10136 break;
10137 case 3:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000010138 if (CI->getType()->isIntegerTy(64) &&
Owen Anderson1d0be152009-08-13 21:58:54 +000010139 Constraints.size() >= 2 &&
Chris Lattnerb8105652009-07-20 17:51:36 +000010140 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
10141 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
10142 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramerd4f19592010-01-11 18:03:24 +000010143 SmallVector<StringRef, 4> Words;
Chris Lattnerb8105652009-07-20 17:51:36 +000010144 SplitString(AsmPieces[0], Words, " \t");
10145 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
10146 Words.clear();
10147 SplitString(AsmPieces[1], Words, " \t");
10148 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
10149 Words.clear();
10150 SplitString(AsmPieces[2], Words, " \t,");
10151 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
10152 Words[2] == "%edx") {
10153 return LowerToBSwap(CI);
10154 }
10155 }
10156 }
10157 }
10158 break;
10159 }
10160 return false;
10161}
10162
10163
10164
Chris Lattnerf4dff842006-07-11 02:54:03 +000010165/// getConstraintType - Given a constraint letter, return the type of
10166/// constraint it is for this target.
10167X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000010168X86TargetLowering::getConstraintType(const std::string &Constraint) const {
10169 if (Constraint.size() == 1) {
10170 switch (Constraint[0]) {
10171 case 'A':
Dale Johannesen330169f2008-11-13 21:52:36 +000010172 return C_Register;
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010173 case 'f':
Chris Lattner4234f572007-03-25 02:14:49 +000010174 case 'r':
10175 case 'R':
10176 case 'l':
10177 case 'q':
10178 case 'Q':
10179 case 'x':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000010180 case 'y':
Chris Lattner4234f572007-03-25 02:14:49 +000010181 case 'Y':
10182 return C_RegisterClass;
Dale Johannesen78e3e522009-02-12 20:58:09 +000010183 case 'e':
10184 case 'Z':
10185 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000010186 default:
10187 break;
10188 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000010189 }
Chris Lattner4234f572007-03-25 02:14:49 +000010190 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000010191}
10192
Dale Johannesenba2a0b92008-01-29 02:21:21 +000010193/// LowerXConstraint - try to replace an X constraint, which matches anything,
10194/// with another that has more specific requirements based on the type of the
10195/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000010196const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000010197LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000010198 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
10199 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000010200 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesenba2a0b92008-01-29 02:21:21 +000010201 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +000010202 return "Y";
10203 if (Subtarget->hasSSE1())
10204 return "x";
10205 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010206
Chris Lattner5e764232008-04-26 23:02:14 +000010207 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000010208}
10209
Chris Lattner48884cd2007-08-25 00:47:38 +000010210/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
10211/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000010212void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +000010213 char Constraint,
Evan Chengda43bcf2008-09-24 00:05:32 +000010214 bool hasMemory,
Dan Gohman475871a2008-07-27 21:46:04 +000010215 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000010216 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010217 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000010218
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010219 switch (Constraint) {
10220 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000010221 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000010222 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000010223 if (C->getZExtValue() <= 31) {
10224 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000010225 break;
10226 }
Devang Patel84f7fd22007-03-17 00:13:28 +000010227 }
Chris Lattner48884cd2007-08-25 00:47:38 +000010228 return;
Evan Cheng364091e2008-09-22 23:57:37 +000010229 case 'J':
10230 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000010231 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000010232 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
10233 break;
10234 }
10235 }
10236 return;
10237 case 'K':
10238 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000010239 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000010240 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
10241 break;
10242 }
10243 }
10244 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000010245 case 'N':
10246 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000010247 if (C->getZExtValue() <= 255) {
10248 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000010249 break;
10250 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000010251 }
Chris Lattner48884cd2007-08-25 00:47:38 +000010252 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000010253 case 'e': {
10254 // 32-bit signed value
10255 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
10256 const ConstantInt *CI = C->getConstantIntValue();
Owen Anderson1d0be152009-08-13 21:58:54 +000010257 if (CI->isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
10258 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000010259 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000010260 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000010261 break;
10262 }
10263 // FIXME gcc accepts some relocatable values here too, but only in certain
10264 // memory models; it's complicated.
10265 }
10266 return;
10267 }
10268 case 'Z': {
10269 // 32-bit unsigned value
10270 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
10271 const ConstantInt *CI = C->getConstantIntValue();
Owen Anderson1d0be152009-08-13 21:58:54 +000010272 if (CI->isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
10273 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000010274 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
10275 break;
10276 }
10277 }
10278 // FIXME gcc accepts some relocatable values here too, but only in certain
10279 // memory models; it's complicated.
10280 return;
10281 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000010282 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010283 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000010284 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000010285 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000010286 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000010287 break;
10288 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010289
Chris Lattnerdc43a882007-05-03 16:52:29 +000010290 // If we are in non-pic codegen mode, we allow the address of a global (with
10291 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000010292 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000010293 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000010294
Chris Lattner49921962009-05-08 18:23:14 +000010295 // Match either (GA), (GA+C), (GA+C1+C2), etc.
10296 while (1) {
10297 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
10298 Offset += GA->getOffset();
10299 break;
10300 } else if (Op.getOpcode() == ISD::ADD) {
10301 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
10302 Offset += C->getZExtValue();
10303 Op = Op.getOperand(0);
10304 continue;
10305 }
10306 } else if (Op.getOpcode() == ISD::SUB) {
10307 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
10308 Offset += -C->getZExtValue();
10309 Op = Op.getOperand(0);
10310 continue;
10311 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000010312 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000010313
Chris Lattner49921962009-05-08 18:23:14 +000010314 // Otherwise, this isn't something we can handle, reject it.
10315 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000010316 }
Eric Christopherfd179292009-08-27 18:07:15 +000010317
Dan Gohman46510a72010-04-15 01:51:59 +000010318 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000010319 // If we require an extra load to get this address, as in PIC mode, we
10320 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000010321 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
10322 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000010323 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000010324
Dale Johannesen60b3ba02009-07-21 00:12:29 +000010325 if (hasMemory)
10326 Op = LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
10327 else
10328 Op = DAG.getTargetGlobalAddress(GV, GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000010329 Result = Op;
10330 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010331 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000010332 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010333
Gabor Greifba36cb52008-08-28 21:40:38 +000010334 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000010335 Ops.push_back(Result);
10336 return;
10337 }
Evan Chengda43bcf2008-09-24 00:05:32 +000010338 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
10339 Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010340}
10341
Chris Lattner259e97c2006-01-31 19:43:35 +000010342std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +000010343getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000010344 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +000010345 if (Constraint.size() == 1) {
10346 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +000010347 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +000010348 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +000010349 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
10350 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000010351 if (VT == MVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010352 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
10353 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
10354 X86::R10D,X86::R11D,X86::R12D,
10355 X86::R13D,X86::R14D,X86::R15D,
10356 X86::EBP, X86::ESP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010357 else if (VT == MVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010358 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
10359 X86::SI, X86::DI, X86::R8W,X86::R9W,
10360 X86::R10W,X86::R11W,X86::R12W,
10361 X86::R13W,X86::R14W,X86::R15W,
10362 X86::BP, X86::SP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010363 else if (VT == MVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010364 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
10365 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
10366 X86::R10B,X86::R11B,X86::R12B,
10367 X86::R13B,X86::R14B,X86::R15B,
10368 X86::BPL, X86::SPL, 0);
10369
Owen Anderson825b72b2009-08-11 20:47:22 +000010370 else if (VT == MVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010371 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
10372 X86::RSI, X86::RDI, X86::R8, X86::R9,
10373 X86::R10, X86::R11, X86::R12,
10374 X86::R13, X86::R14, X86::R15,
10375 X86::RBP, X86::RSP, 0);
10376
10377 break;
10378 }
Eric Christopherfd179292009-08-27 18:07:15 +000010379 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +000010380 case 'Q': // Q_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000010381 if (VT == MVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000010382 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010383 else if (VT == MVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000010384 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010385 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +000010386 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010387 else if (VT == MVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +000010388 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
10389 break;
Chris Lattner259e97c2006-01-31 19:43:35 +000010390 }
10391 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010392
Chris Lattner1efa40f2006-02-22 00:56:39 +000010393 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +000010394}
Chris Lattnerf76d1802006-07-31 23:26:50 +000010395
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010396std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000010397X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000010398 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000010399 // First, see if this is a constraint that directly corresponds to an LLVM
10400 // register class.
10401 if (Constraint.size() == 1) {
10402 // GCC Constraint Letters
10403 switch (Constraint[0]) {
10404 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000010405 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000010406 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000010407 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +000010408 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010409 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000010410 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010411 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000010412 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000010413 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000010414 case 'R': // LEGACY_REGS
10415 if (VT == MVT::i8)
10416 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
10417 if (VT == MVT::i16)
10418 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
10419 if (VT == MVT::i32 || !Subtarget->is64Bit())
10420 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
10421 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010422 case 'f': // FP Stack registers.
10423 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
10424 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000010425 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010426 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010427 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010428 return std::make_pair(0U, X86::RFP64RegisterClass);
10429 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000010430 case 'y': // MMX_REGS if MMX allowed.
10431 if (!Subtarget->hasMMX()) break;
10432 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000010433 case 'Y': // SSE_REGS if SSE2 allowed
10434 if (!Subtarget->hasSSE2()) break;
10435 // FALL THROUGH.
10436 case 'x': // SSE_REGS if SSE1 allowed
10437 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010438
Owen Anderson825b72b2009-08-11 20:47:22 +000010439 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000010440 default: break;
10441 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000010442 case MVT::f32:
10443 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000010444 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010445 case MVT::f64:
10446 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000010447 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000010448 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000010449 case MVT::v16i8:
10450 case MVT::v8i16:
10451 case MVT::v4i32:
10452 case MVT::v2i64:
10453 case MVT::v4f32:
10454 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000010455 return std::make_pair(0U, X86::VR128RegisterClass);
10456 }
Chris Lattnerad043e82007-04-09 05:11:28 +000010457 break;
10458 }
10459 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010460
Chris Lattnerf76d1802006-07-31 23:26:50 +000010461 // Use the default implementation in TargetLowering to convert the register
10462 // constraint into a member of a register class.
10463 std::pair<unsigned, const TargetRegisterClass*> Res;
10464 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000010465
10466 // Not found as a standard register?
10467 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000010468 // Map st(0) -> st(7) -> ST0
10469 if (Constraint.size() == 7 && Constraint[0] == '{' &&
10470 tolower(Constraint[1]) == 's' &&
10471 tolower(Constraint[2]) == 't' &&
10472 Constraint[3] == '(' &&
10473 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
10474 Constraint[5] == ')' &&
10475 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000010476
Chris Lattner56d77c72009-09-13 22:41:48 +000010477 Res.first = X86::ST0+Constraint[4]-'0';
10478 Res.second = X86::RFP80RegisterClass;
10479 return Res;
10480 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000010481
Chris Lattner56d77c72009-09-13 22:41:48 +000010482 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000010483 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000010484 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000010485 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000010486 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000010487 }
Chris Lattner56d77c72009-09-13 22:41:48 +000010488
10489 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000010490 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000010491 Res.first = X86::EFLAGS;
10492 Res.second = X86::CCRRegisterClass;
10493 return Res;
10494 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000010495
Dale Johannesen330169f2008-11-13 21:52:36 +000010496 // 'A' means EAX + EDX.
10497 if (Constraint == "A") {
10498 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000010499 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000010500 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000010501 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000010502 return Res;
10503 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010504
Chris Lattnerf76d1802006-07-31 23:26:50 +000010505 // Otherwise, check to see if this is a register class of the wrong value
10506 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
10507 // turn into {ax},{dx}.
10508 if (Res.second->hasType(VT))
10509 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010510
Chris Lattnerf76d1802006-07-31 23:26:50 +000010511 // All of the single-register GCC register classes map their values onto
10512 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
10513 // really want an 8-bit or 32-bit register, map to the appropriate register
10514 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000010515 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000010516 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010517 unsigned DestReg = 0;
10518 switch (Res.first) {
10519 default: break;
10520 case X86::AX: DestReg = X86::AL; break;
10521 case X86::DX: DestReg = X86::DL; break;
10522 case X86::CX: DestReg = X86::CL; break;
10523 case X86::BX: DestReg = X86::BL; break;
10524 }
10525 if (DestReg) {
10526 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010527 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010528 }
Owen Anderson825b72b2009-08-11 20:47:22 +000010529 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010530 unsigned DestReg = 0;
10531 switch (Res.first) {
10532 default: break;
10533 case X86::AX: DestReg = X86::EAX; break;
10534 case X86::DX: DestReg = X86::EDX; break;
10535 case X86::CX: DestReg = X86::ECX; break;
10536 case X86::BX: DestReg = X86::EBX; break;
10537 case X86::SI: DestReg = X86::ESI; break;
10538 case X86::DI: DestReg = X86::EDI; break;
10539 case X86::BP: DestReg = X86::EBP; break;
10540 case X86::SP: DestReg = X86::ESP; break;
10541 }
10542 if (DestReg) {
10543 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010544 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010545 }
Owen Anderson825b72b2009-08-11 20:47:22 +000010546 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010547 unsigned DestReg = 0;
10548 switch (Res.first) {
10549 default: break;
10550 case X86::AX: DestReg = X86::RAX; break;
10551 case X86::DX: DestReg = X86::RDX; break;
10552 case X86::CX: DestReg = X86::RCX; break;
10553 case X86::BX: DestReg = X86::RBX; break;
10554 case X86::SI: DestReg = X86::RSI; break;
10555 case X86::DI: DestReg = X86::RDI; break;
10556 case X86::BP: DestReg = X86::RBP; break;
10557 case X86::SP: DestReg = X86::RSP; break;
10558 }
10559 if (DestReg) {
10560 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010561 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010562 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000010563 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000010564 } else if (Res.second == X86::FR32RegisterClass ||
10565 Res.second == X86::FR64RegisterClass ||
10566 Res.second == X86::VR128RegisterClass) {
10567 // Handle references to XMM physical registers that got mapped into the
10568 // wrong class. This can happen with constraints like {xmm0} where the
10569 // target independent register mapper will just pick the first match it can
10570 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000010571 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000010572 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000010573 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000010574 Res.second = X86::FR64RegisterClass;
10575 else if (X86::VR128RegisterClass->hasType(VT))
10576 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000010577 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010578
Chris Lattnerf76d1802006-07-31 23:26:50 +000010579 return Res;
10580}