blob: 88ee52f9b7b11a5a6d11fc8c1988f7003fd7030c [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) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000148 // We have an algorithm for SSE2->double, and we turn this into a
149 // 64-bit FILD followed by conditional FADD for other targets.
150 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000151 // We have an algorithm for SSE2, and we turn this into a 64-bit
152 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000153 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000154 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000155
156 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
157 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000158 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
159 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000160
Devang Patel6a784892009-06-05 18:48:29 +0000161 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000162 // SSE has no i16 to fp conversion, only i32
163 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000164 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000165 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000166 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000167 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000168 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
169 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000170 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000171 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000172 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
173 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000174 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000175
Dale Johannesen73328d12007-09-19 23:55:34 +0000176 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
177 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000178 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
179 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000180
Evan Cheng02568ff2006-01-30 22:13:22 +0000181 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
182 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000183 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
184 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000185
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000186 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000187 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000188 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000189 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000190 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000191 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
192 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000193 }
194
195 // Handle FP_TO_UINT by promoting the destination to a larger signed
196 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000197 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
198 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
199 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000200
Evan Cheng25ab6902006-09-08 06:48:29 +0000201 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000202 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
203 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000204 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000205 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000206 // Expand FP_TO_UINT into a select.
207 // FIXME: We would like to use a Custom expander here eventually to do
208 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000209 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000210 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000211 // With SSE3 we can use fisttpll to convert to a signed i64; without
212 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000213 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000214 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000215
Chris Lattner399610a2006-12-05 18:22:22 +0000216 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000217 if (!X86ScalarSSEf64) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000218 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
219 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
Chris Lattnerf3597a12006-12-05 18:45:06 +0000220 }
Chris Lattner21f66852005-12-23 05:15:23 +0000221
Dan Gohmanb00ee212008-02-18 19:34:53 +0000222 // Scalar integer divide and remainder are lowered to use operations that
223 // produce two results, to match the available instructions. This exposes
224 // the two-result form to trivial CSE, which is able to combine x/y and x%y
225 // into a single instruction.
226 //
227 // Scalar integer multiply-high is also lowered to use two-result
228 // operations, to match the available instructions. However, plain multiply
229 // (low) operations are left as Legal, as there are single-result
230 // instructions for this in x86. Using the two-result multiply instructions
231 // when both high and low results are needed must be arranged by dagcombine.
Owen Anderson825b72b2009-08-11 20:47:22 +0000232 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
233 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
234 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
235 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
236 setOperationAction(ISD::SREM , MVT::i8 , Expand);
237 setOperationAction(ISD::UREM , MVT::i8 , Expand);
238 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
239 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
240 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
241 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
242 setOperationAction(ISD::SREM , MVT::i16 , Expand);
243 setOperationAction(ISD::UREM , MVT::i16 , Expand);
244 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
245 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
246 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
247 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
248 setOperationAction(ISD::SREM , MVT::i32 , Expand);
249 setOperationAction(ISD::UREM , MVT::i32 , Expand);
250 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
251 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
252 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
253 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
254 setOperationAction(ISD::SREM , MVT::i64 , Expand);
255 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohmana37c9f72007-09-25 18:23:27 +0000256
Owen Anderson825b72b2009-08-11 20:47:22 +0000257 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
258 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
259 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
260 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000261 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000262 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
263 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
264 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
265 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
266 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
267 setOperationAction(ISD::FREM , MVT::f32 , Expand);
268 setOperationAction(ISD::FREM , MVT::f64 , Expand);
269 setOperationAction(ISD::FREM , MVT::f80 , Expand);
270 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000271
Owen Anderson825b72b2009-08-11 20:47:22 +0000272 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
273 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
274 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
275 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000276 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
277 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000278 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
279 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
280 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000281 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000282 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
283 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
284 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000285 }
286
Owen Anderson825b72b2009-08-11 20:47:22 +0000287 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
288 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000289
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000290 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000291 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000292 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000293 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000294 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000295 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
296 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
297 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
298 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
299 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000300 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000301 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
302 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
303 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
304 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000305 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000306 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
307 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000308 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000309 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000310
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000311 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000312 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
313 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
314 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
315 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000316 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000317 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
318 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000319 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000320 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000321 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
322 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
323 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
324 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000325 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000326 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000327 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000328 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
329 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
330 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000331 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000332 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
333 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
334 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000335 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000336
Evan Chengd2cde682008-03-10 19:38:10 +0000337 if (Subtarget->hasSSE1())
Owen Anderson825b72b2009-08-11 20:47:22 +0000338 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000339
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000340 if (!Subtarget->hasSSE2())
Owen Anderson825b72b2009-08-11 20:47:22 +0000341 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000342
Mon P Wang63307c32008-05-05 19:05:59 +0000343 // Expand certain atomics
Owen Anderson825b72b2009-08-11 20:47:22 +0000344 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Custom);
345 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Custom);
346 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
347 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Bill Wendling5bf1b4e2008-08-20 00:28:16 +0000348
Owen Anderson825b72b2009-08-11 20:47:22 +0000349 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Custom);
350 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Custom);
351 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
352 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000353
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000354 if (!Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000355 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
356 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
357 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
358 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
359 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
360 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
361 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000362 }
363
Evan Cheng3c992d22006-03-07 02:02:57 +0000364 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000365 if (!Subtarget->isTargetDarwin() &&
366 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000367 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000368 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000369 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000370
Owen Anderson825b72b2009-08-11 20:47:22 +0000371 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
372 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
373 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
374 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000375 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000376 setExceptionPointerRegister(X86::RAX);
377 setExceptionSelectorRegister(X86::RDX);
378 } else {
379 setExceptionPointerRegister(X86::EAX);
380 setExceptionSelectorRegister(X86::EDX);
381 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000382 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
383 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000384
Owen Anderson825b72b2009-08-11 20:47:22 +0000385 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000386
Owen Anderson825b72b2009-08-11 20:47:22 +0000387 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000388
Nate Begemanacc398c2006-01-25 18:21:52 +0000389 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000390 setOperationAction(ISD::VASTART , MVT::Other, Custom);
391 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000392 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000393 setOperationAction(ISD::VAARG , MVT::Other, Custom);
394 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000395 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000396 setOperationAction(ISD::VAARG , MVT::Other, Expand);
397 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000398 }
Evan Chengae642192007-03-02 23:16:35 +0000399
Owen Anderson825b72b2009-08-11 20:47:22 +0000400 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
401 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000402 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000403 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000404 if (Subtarget->isTargetCygMing())
Owen Anderson825b72b2009-08-11 20:47:22 +0000405 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000406 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000407 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000408
Evan Chengc7ce29b2009-02-13 22:36:38 +0000409 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000410 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000411 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000412 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
413 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000414
Evan Cheng223547a2006-01-31 22:28:30 +0000415 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000416 setOperationAction(ISD::FABS , MVT::f64, Custom);
417 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000418
419 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000420 setOperationAction(ISD::FNEG , MVT::f64, Custom);
421 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000422
Evan Cheng68c47cb2007-01-05 07:55:56 +0000423 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000424 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
425 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000426
Evan Chengd25e9e82006-02-02 00:28:23 +0000427 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000428 setOperationAction(ISD::FSIN , MVT::f64, Expand);
429 setOperationAction(ISD::FCOS , MVT::f64, Expand);
430 setOperationAction(ISD::FSIN , MVT::f32, Expand);
431 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000432
Chris Lattnera54aa942006-01-29 06:26:08 +0000433 // Expand FP immediates into loads from the stack, except for the special
434 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000435 addLegalFPImmediate(APFloat(+0.0)); // xorpd
436 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000437 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000438 // Use SSE for f32, x87 for f64.
439 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000440 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
441 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000442
443 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000444 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000445
446 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000447 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000448
Owen Anderson825b72b2009-08-11 20:47:22 +0000449 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000450
451 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000452 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
453 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000454
455 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000456 setOperationAction(ISD::FSIN , MVT::f32, Expand);
457 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000458
Nate Begemane1795842008-02-14 08:57:00 +0000459 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000460 addLegalFPImmediate(APFloat(+0.0f)); // xorps
461 addLegalFPImmediate(APFloat(+0.0)); // FLD0
462 addLegalFPImmediate(APFloat(+1.0)); // FLD1
463 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
464 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
465
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000466 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000467 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
468 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000469 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000470 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000471 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000472 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000473 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
474 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000475
Owen Anderson825b72b2009-08-11 20:47:22 +0000476 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
477 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
478 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
479 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000480
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000481 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000482 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
483 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000484 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000485 addLegalFPImmediate(APFloat(+0.0)); // FLD0
486 addLegalFPImmediate(APFloat(+1.0)); // FLD1
487 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
488 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000489 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
490 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
491 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
492 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000493 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000494
Dale Johannesen59a58732007-08-05 18:49:15 +0000495 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000496 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000497 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
498 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
499 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000500 {
501 bool ignored;
502 APFloat TmpFlt(+0.0);
503 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
504 &ignored);
505 addLegalFPImmediate(TmpFlt); // FLD0
506 TmpFlt.changeSign();
507 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
508 APFloat TmpFlt2(+1.0);
509 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
510 &ignored);
511 addLegalFPImmediate(TmpFlt2); // FLD1
512 TmpFlt2.changeSign();
513 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
514 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000515
Evan Chengc7ce29b2009-02-13 22:36:38 +0000516 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000517 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
518 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000519 }
Dale Johannesen2f429012007-09-26 21:10:55 +0000520 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000521
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000522 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000523 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
524 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
525 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000526
Owen Anderson825b72b2009-08-11 20:47:22 +0000527 setOperationAction(ISD::FLOG, MVT::f80, Expand);
528 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
529 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
530 setOperationAction(ISD::FEXP, MVT::f80, Expand);
531 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000532
Mon P Wangf007a8b2008-11-06 05:31:54 +0000533 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000534 // (for widening) or expand (for scalarization). Then we will selectively
535 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000536 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
537 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
538 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
539 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
540 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
541 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
542 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
543 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
544 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
545 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
546 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
547 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
548 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
549 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
550 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
551 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
552 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
553 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
554 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
555 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
556 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
557 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
558 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
559 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
560 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
561 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
562 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
563 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
564 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
565 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
566 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
567 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
568 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
569 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
570 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
571 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
572 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
573 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
574 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
575 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
576 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
577 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
578 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
579 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
580 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
581 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
582 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
583 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
584 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
585 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000586 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000587 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
588 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
589 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
590 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
591 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
592 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
593 setTruncStoreAction((MVT::SimpleValueType)VT,
594 (MVT::SimpleValueType)InnerVT, Expand);
595 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
596 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
597 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000598 }
599
Evan Chengc7ce29b2009-02-13 22:36:38 +0000600 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
601 // with -msoft-float, disable use of MMX as well.
Evan Cheng92722532009-03-26 23:06:32 +0000602 if (!UseSoftFloat && !DisableMMX && Subtarget->hasMMX()) {
Dale Johannesen76090172010-04-20 22:34:09 +0000603 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass, false);
604 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass, false);
605 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass, false);
606 addRegisterClass(MVT::v2f32, X86::VR64RegisterClass, false);
607 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass, false);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000608
Owen Anderson825b72b2009-08-11 20:47:22 +0000609 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
610 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
611 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
612 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000613
Owen Anderson825b72b2009-08-11 20:47:22 +0000614 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
615 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
616 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
617 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000618
Owen Anderson825b72b2009-08-11 20:47:22 +0000619 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
620 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
Bill Wendling74027e92007-03-15 21:24:36 +0000621
Owen Anderson825b72b2009-08-11 20:47:22 +0000622 setOperationAction(ISD::AND, MVT::v8i8, Promote);
623 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
624 setOperationAction(ISD::AND, MVT::v4i16, Promote);
625 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
626 setOperationAction(ISD::AND, MVT::v2i32, Promote);
627 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
628 setOperationAction(ISD::AND, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000629
Owen Anderson825b72b2009-08-11 20:47:22 +0000630 setOperationAction(ISD::OR, MVT::v8i8, Promote);
631 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
632 setOperationAction(ISD::OR, MVT::v4i16, Promote);
633 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
634 setOperationAction(ISD::OR, MVT::v2i32, Promote);
635 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
636 setOperationAction(ISD::OR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000637
Owen Anderson825b72b2009-08-11 20:47:22 +0000638 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
639 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
640 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
641 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
642 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
643 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
644 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000645
Owen Anderson825b72b2009-08-11 20:47:22 +0000646 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
647 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
648 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
649 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
650 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
651 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
652 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
653 AddPromotedToType (ISD::LOAD, MVT::v2f32, MVT::v1i64);
654 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000655
Owen Anderson825b72b2009-08-11 20:47:22 +0000656 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
657 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
658 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
659 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom);
660 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
Bill Wendlinga348c562007-03-22 18:42:45 +0000661
Owen Anderson825b72b2009-08-11 20:47:22 +0000662 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
663 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
664 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
665 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000666
Owen Anderson825b72b2009-08-11 20:47:22 +0000667 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f32, Custom);
668 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
669 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
670 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Bill Wendling3180e202008-07-20 02:32:23 +0000671
Owen Anderson825b72b2009-08-11 20:47:22 +0000672 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
Mon P Wang9e5ecb82008-12-12 01:25:51 +0000673
Owen Anderson825b72b2009-08-11 20:47:22 +0000674 setOperationAction(ISD::SELECT, MVT::v8i8, Promote);
675 setOperationAction(ISD::SELECT, MVT::v4i16, Promote);
676 setOperationAction(ISD::SELECT, MVT::v2i32, Promote);
677 setOperationAction(ISD::SELECT, MVT::v1i64, Custom);
678 setOperationAction(ISD::VSETCC, MVT::v8i8, Custom);
679 setOperationAction(ISD::VSETCC, MVT::v4i16, Custom);
680 setOperationAction(ISD::VSETCC, MVT::v2i32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000681 }
682
Evan Cheng92722532009-03-26 23:06:32 +0000683 if (!UseSoftFloat && Subtarget->hasSSE1()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000684 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000685
Owen Anderson825b72b2009-08-11 20:47:22 +0000686 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
687 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
688 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
689 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
690 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
691 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
692 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
693 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
694 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
695 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
696 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
697 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000698 }
699
Evan Cheng92722532009-03-26 23:06:32 +0000700 if (!UseSoftFloat && Subtarget->hasSSE2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000701 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000702
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000703 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
704 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000705 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
706 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
707 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
708 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000709
Owen Anderson825b72b2009-08-11 20:47:22 +0000710 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
711 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
712 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
713 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
714 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
715 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
716 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
717 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
718 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
719 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
720 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
721 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
722 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
723 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
724 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
725 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000726
Owen Anderson825b72b2009-08-11 20:47:22 +0000727 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
728 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
729 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
730 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000731
Owen Anderson825b72b2009-08-11 20:47:22 +0000732 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
733 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
734 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
735 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
736 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000737
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000738 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
739 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
740 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
741 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
742 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
743
Evan Cheng2c3ae372006-04-12 21:21:57 +0000744 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000745 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
746 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000747 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000748 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000749 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000750 // Do not attempt to custom lower non-128-bit vectors
751 if (!VT.is128BitVector())
752 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000753 setOperationAction(ISD::BUILD_VECTOR,
754 VT.getSimpleVT().SimpleTy, Custom);
755 setOperationAction(ISD::VECTOR_SHUFFLE,
756 VT.getSimpleVT().SimpleTy, Custom);
757 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
758 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000759 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000760
Owen Anderson825b72b2009-08-11 20:47:22 +0000761 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
762 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
763 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
764 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
765 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
766 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000767
Nate Begemancdd1eec2008-02-12 22:51:28 +0000768 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000769 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
770 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000771 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000772
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000773 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000774 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
775 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000776 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000777
778 // Do not attempt to promote non-128-bit vectors
779 if (!VT.is128BitVector()) {
780 continue;
781 }
Eric Christopher4bd24c22010-03-30 01:04:59 +0000782
Owen Andersond6662ad2009-08-10 20:46:15 +0000783 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000784 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000785 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000786 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000787 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000788 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000789 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000790 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000791 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000792 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000793 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000794
Owen Anderson825b72b2009-08-11 20:47:22 +0000795 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000796
Evan Cheng2c3ae372006-04-12 21:21:57 +0000797 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000798 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
799 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
800 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
801 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000802
Owen Anderson825b72b2009-08-11 20:47:22 +0000803 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
804 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Eli Friedman23ef1052009-06-06 03:57:58 +0000805 if (!DisableMMX && Subtarget->hasMMX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000806 setOperationAction(ISD::FP_TO_SINT, MVT::v2i32, Custom);
807 setOperationAction(ISD::SINT_TO_FP, MVT::v2i32, Custom);
Eli Friedman23ef1052009-06-06 03:57:58 +0000808 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000809 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000810
Nate Begeman14d12ca2008-02-11 04:19:36 +0000811 if (Subtarget->hasSSE41()) {
812 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000813 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000814
815 // i8 and i16 vectors are custom , because the source register and source
816 // source memory operand types are not the same width. f32 vectors are
817 // custom since the immediate controlling the insert encodes additional
818 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000819 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
820 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
821 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
822 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000823
Owen Anderson825b72b2009-08-11 20:47:22 +0000824 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
825 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
826 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
827 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000828
829 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000830 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
831 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000832 }
833 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000834
Nate Begeman30a0de92008-07-17 16:51:19 +0000835 if (Subtarget->hasSSE42()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000836 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
Nate Begeman30a0de92008-07-17 16:51:19 +0000837 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000838
David Greene9b9838d2009-06-29 16:47:10 +0000839 if (!UseSoftFloat && Subtarget->hasAVX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000840 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
841 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
842 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
843 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000844
Owen Anderson825b72b2009-08-11 20:47:22 +0000845 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
846 setOperationAction(ISD::LOAD, MVT::v8i32, Legal);
847 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
848 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
849 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
850 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
851 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
852 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
853 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
854 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
855 //setOperationAction(ISD::BUILD_VECTOR, MVT::v8f32, Custom);
856 //setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Custom);
857 //setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8f32, Custom);
858 //setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
859 //setOperationAction(ISD::VSETCC, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000860
861 // Operations to consider commented out -v16i16 v32i8
Owen Anderson825b72b2009-08-11 20:47:22 +0000862 //setOperationAction(ISD::ADD, MVT::v16i16, Legal);
863 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
864 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
865 //setOperationAction(ISD::SUB, MVT::v32i8, Legal);
866 //setOperationAction(ISD::SUB, MVT::v16i16, Legal);
867 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
868 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
869 //setOperationAction(ISD::MUL, MVT::v16i16, Legal);
870 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
871 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
872 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
873 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
874 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
875 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000876
Owen Anderson825b72b2009-08-11 20:47:22 +0000877 setOperationAction(ISD::VSETCC, MVT::v4f64, Custom);
878 // setOperationAction(ISD::VSETCC, MVT::v32i8, Custom);
879 // setOperationAction(ISD::VSETCC, MVT::v16i16, Custom);
880 setOperationAction(ISD::VSETCC, MVT::v8i32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000881
Owen Anderson825b72b2009-08-11 20:47:22 +0000882 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v32i8, Custom);
883 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i16, Custom);
884 // setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i16, Custom);
885 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i32, Custom);
886 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000887
Owen Anderson825b72b2009-08-11 20:47:22 +0000888 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f64, Custom);
889 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i64, Custom);
890 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f64, Custom);
891 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i64, Custom);
892 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f64, Custom);
893 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000894
895#if 0
896 // Not sure we want to do this since there are no 256-bit integer
897 // operations in AVX
898
899 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
900 // This includes 256-bit vectors
Owen Anderson825b72b2009-08-11 20:47:22 +0000901 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; ++i) {
902 EVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000903
904 // Do not attempt to custom lower non-power-of-2 vectors
905 if (!isPowerOf2_32(VT.getVectorNumElements()))
906 continue;
907
908 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
909 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
910 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
911 }
912
913 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000914 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i64, Custom);
915 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i64, Custom);
Eric Christopherfd179292009-08-27 18:07:15 +0000916 }
David Greene9b9838d2009-06-29 16:47:10 +0000917#endif
918
919#if 0
920 // Not sure we want to do this since there are no 256-bit integer
921 // operations in AVX
922
923 // Promote v32i8, v16i16, v8i32 load, select, and, or, xor to v4i64.
924 // Including 256-bit vectors
Owen Anderson825b72b2009-08-11 20:47:22 +0000925 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; i++) {
926 EVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000927
928 if (!VT.is256BitVector()) {
929 continue;
930 }
931 setOperationAction(ISD::AND, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000932 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000933 setOperationAction(ISD::OR, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000934 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000935 setOperationAction(ISD::XOR, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000936 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000937 setOperationAction(ISD::LOAD, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000938 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000939 setOperationAction(ISD::SELECT, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000940 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000941 }
942
Owen Anderson825b72b2009-08-11 20:47:22 +0000943 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
David Greene9b9838d2009-06-29 16:47:10 +0000944#endif
945 }
946
Evan Cheng6be2c582006-04-05 23:38:46 +0000947 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000948 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +0000949
Bill Wendling74c37652008-12-09 22:08:41 +0000950 // Add/Sub/Mul with overflow operations are custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000951 setOperationAction(ISD::SADDO, MVT::i32, Custom);
952 setOperationAction(ISD::SADDO, MVT::i64, Custom);
953 setOperationAction(ISD::UADDO, MVT::i32, Custom);
954 setOperationAction(ISD::UADDO, MVT::i64, Custom);
955 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
956 setOperationAction(ISD::SSUBO, MVT::i64, Custom);
957 setOperationAction(ISD::USUBO, MVT::i32, Custom);
958 setOperationAction(ISD::USUBO, MVT::i64, Custom);
959 setOperationAction(ISD::SMULO, MVT::i32, Custom);
960 setOperationAction(ISD::SMULO, MVT::i64, Custom);
Bill Wendling41ea7e72008-11-24 19:21:46 +0000961
Evan Chengd54f2d52009-03-31 19:38:51 +0000962 if (!Subtarget->is64Bit()) {
963 // These libcalls are not available in 32-bit.
964 setLibcallName(RTLIB::SHL_I128, 0);
965 setLibcallName(RTLIB::SRL_I128, 0);
966 setLibcallName(RTLIB::SRA_I128, 0);
967 }
968
Evan Cheng206ee9d2006-07-07 08:33:52 +0000969 // We have target-specific dag combine patterns for the following nodes:
970 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +0000971 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +0000972 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +0000973 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +0000974 setTargetDAGCombine(ISD::SHL);
975 setTargetDAGCombine(ISD::SRA);
976 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +0000977 setTargetDAGCombine(ISD::OR);
Chris Lattner149a4e52008-02-22 02:09:43 +0000978 setTargetDAGCombine(ISD::STORE);
Owen Anderson99177002009-06-29 18:04:45 +0000979 setTargetDAGCombine(ISD::MEMBARRIER);
Evan Cheng2e489c42009-12-16 00:53:11 +0000980 setTargetDAGCombine(ISD::ZERO_EXTEND);
Evan Cheng0b0cd912009-03-28 05:57:29 +0000981 if (Subtarget->is64Bit())
982 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +0000983
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000984 computeRegisterProperties();
985
Evan Cheng87ed7162006-02-14 08:25:08 +0000986 // FIXME: These should be based on subtarget info. Plus, the values should
987 // be smaller when we are in optimizing for size mode.
Dan Gohman87060f52008-06-30 21:00:56 +0000988 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng255f20f2010-04-01 06:04:33 +0000989 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Dan Gohman87060f52008-06-30 21:00:56 +0000990 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Evan Chengfb8075d2008-02-28 00:43:03 +0000991 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +0000992 benefitFromCodePlacementOpt = true;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000993}
994
Scott Michel5b8f82e2008-03-10 15:42:14 +0000995
Owen Anderson825b72b2009-08-11 20:47:22 +0000996MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
997 return MVT::i8;
Scott Michel5b8f82e2008-03-10 15:42:14 +0000998}
999
1000
Evan Cheng29286502008-01-23 23:17:41 +00001001/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1002/// the desired ByVal argument alignment.
1003static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
1004 if (MaxAlign == 16)
1005 return;
1006 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
1007 if (VTy->getBitWidth() == 128)
1008 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +00001009 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1010 unsigned EltAlign = 0;
1011 getMaxByValAlign(ATy->getElementType(), EltAlign);
1012 if (EltAlign > MaxAlign)
1013 MaxAlign = EltAlign;
1014 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1015 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1016 unsigned EltAlign = 0;
1017 getMaxByValAlign(STy->getElementType(i), EltAlign);
1018 if (EltAlign > MaxAlign)
1019 MaxAlign = EltAlign;
1020 if (MaxAlign == 16)
1021 break;
1022 }
1023 }
1024 return;
1025}
1026
1027/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1028/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001029/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1030/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +00001031unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001032 if (Subtarget->is64Bit()) {
1033 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001034 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001035 if (TyAlign > 8)
1036 return TyAlign;
1037 return 8;
1038 }
1039
Evan Cheng29286502008-01-23 23:17:41 +00001040 unsigned Align = 4;
Dale Johannesen0c191872008-02-08 19:48:20 +00001041 if (Subtarget->hasSSE1())
1042 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001043 return Align;
1044}
Chris Lattner2b02a442007-02-25 08:29:00 +00001045
Evan Chengf0df0312008-05-15 08:39:06 +00001046/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001047/// and store operations as a result of memset, memcpy, and memmove
1048/// lowering. If DstAlign is zero that means it's safe to destination
1049/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1050/// means there isn't a need to check it against alignment requirement,
1051/// probably because the source does not need to be loaded. If
1052/// 'NonScalarIntSafe' is true, that means it's safe to return a
1053/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1054/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1055/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001056/// It returns EVT::Other if the type should be determined using generic
1057/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001058EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001059X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1060 unsigned DstAlign, unsigned SrcAlign,
Evan Chengf28f8bc2010-04-02 19:36:14 +00001061 bool NonScalarIntSafe,
Evan Chengc3b0c342010-04-08 07:37:57 +00001062 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001063 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001064 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1065 // linux. This is because the stack realignment code can't handle certain
1066 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001067 const Function *F = MF.getFunction();
Evan Chengf28f8bc2010-04-02 19:36:14 +00001068 if (NonScalarIntSafe &&
1069 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001070 if (Size >= 16 &&
1071 (Subtarget->isUnalignedMemAccessFast() ||
Chandler Carruthae1d41c2010-04-02 01:31:24 +00001072 ((DstAlign == 0 || DstAlign >= 16) &&
1073 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001074 Subtarget->getStackAlignment() >= 16) {
1075 if (Subtarget->hasSSE2())
1076 return MVT::v4i32;
Evan Chengf28f8bc2010-04-02 19:36:14 +00001077 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001078 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001079 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001080 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001081 Subtarget->getStackAlignment() >= 8 &&
Evan Chengc3b0c342010-04-08 07:37:57 +00001082 Subtarget->hasSSE2()) {
1083 // Do not use f64 to lower memcpy if source is string constant. It's
1084 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001085 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001086 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001087 }
Evan Chengf0df0312008-05-15 08:39:06 +00001088 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001089 return MVT::i64;
1090 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001091}
1092
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001093/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1094/// current function. The returned value is a member of the
1095/// MachineJumpTableInfo::JTEntryKind enum.
1096unsigned X86TargetLowering::getJumpTableEncoding() const {
1097 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1098 // symbol.
1099 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1100 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001101 return MachineJumpTableInfo::EK_Custom32;
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001102
1103 // Otherwise, use the normal jump table encoding heuristics.
1104 return TargetLowering::getJumpTableEncoding();
1105}
1106
Chris Lattner589c6f62010-01-26 06:28:43 +00001107/// getPICBaseSymbol - Return the X86-32 PIC base.
1108MCSymbol *
1109X86TargetLowering::getPICBaseSymbol(const MachineFunction *MF,
1110 MCContext &Ctx) const {
1111 const MCAsmInfo &MAI = *getTargetMachine().getMCAsmInfo();
Chris Lattner9b97a732010-03-30 18:10:53 +00001112 return Ctx.GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix())+
1113 Twine(MF->getFunctionNumber())+"$pb");
Chris Lattner589c6f62010-01-26 06:28:43 +00001114}
1115
1116
Chris Lattnerc64daab2010-01-26 05:02:42 +00001117const MCExpr *
1118X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1119 const MachineBasicBlock *MBB,
1120 unsigned uid,MCContext &Ctx) const{
1121 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1122 Subtarget->isPICStyleGOT());
1123 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1124 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001125 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1126 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001127}
1128
Evan Chengcc415862007-11-09 01:32:10 +00001129/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1130/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001131SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001132 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001133 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001134 // This doesn't have DebugLoc associated with it, but is not really the
1135 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001136 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001137 return Table;
1138}
1139
Chris Lattner589c6f62010-01-26 06:28:43 +00001140/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1141/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1142/// MCExpr.
1143const MCExpr *X86TargetLowering::
1144getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1145 MCContext &Ctx) const {
1146 // X86-64 uses RIP relative addressing based on the jump table label.
1147 if (Subtarget->isPICStyleRIPRel())
1148 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1149
1150 // Otherwise, the reference is relative to the PIC base.
1151 return MCSymbolRefExpr::Create(getPICBaseSymbol(MF, Ctx), Ctx);
1152}
1153
Bill Wendlingb4202b82009-07-01 18:50:55 +00001154/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +00001155unsigned X86TargetLowering::getFunctionAlignment(const Function *F) const {
Dan Gohman25103a22009-08-18 00:20:06 +00001156 return F->hasFnAttr(Attribute::OptimizeForSize) ? 0 : 4;
Bill Wendling20c568f2009-06-30 22:38:32 +00001157}
1158
Chris Lattner2b02a442007-02-25 08:29:00 +00001159//===----------------------------------------------------------------------===//
1160// Return Value Calling Convention Implementation
1161//===----------------------------------------------------------------------===//
1162
Chris Lattner59ed56b2007-02-28 04:55:35 +00001163#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001164
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001165bool
1166X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv, bool isVarArg,
1167 const SmallVectorImpl<EVT> &OutTys,
1168 const SmallVectorImpl<ISD::ArgFlagsTy> &ArgsFlags,
Dan Gohmand858e902010-04-17 15:26:15 +00001169 SelectionDAG &DAG) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001170 SmallVector<CCValAssign, 16> RVLocs;
1171 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1172 RVLocs, *DAG.getContext());
1173 return CCInfo.CheckReturn(OutTys, ArgsFlags, RetCC_X86);
1174}
1175
Dan Gohman98ca4f22009-08-05 01:29:28 +00001176SDValue
1177X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001178 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001179 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmand858e902010-04-17 15:26:15 +00001180 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001181 MachineFunction &MF = DAG.getMachineFunction();
1182 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001183
Chris Lattner9774c912007-02-27 05:28:59 +00001184 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001185 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1186 RVLocs, *DAG.getContext());
1187 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001188
Evan Chengdcea1632010-02-04 02:40:39 +00001189 // Add the regs to the liveout set for the function.
1190 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1191 for (unsigned i = 0; i != RVLocs.size(); ++i)
1192 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1193 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001194
Dan Gohman475871a2008-07-27 21:46:04 +00001195 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001196
Dan Gohman475871a2008-07-27 21:46:04 +00001197 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001198 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1199 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001200 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1201 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001202
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001203 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001204 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1205 CCValAssign &VA = RVLocs[i];
1206 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman98ca4f22009-08-05 01:29:28 +00001207 SDValue ValToCopy = Outs[i].Val;
Scott Michelfdc40a02009-02-17 22:15:04 +00001208
Chris Lattner447ff682008-03-11 03:23:40 +00001209 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1210 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001211 if (VA.getLocReg() == X86::ST0 ||
1212 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001213 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1214 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001215 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001216 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001217 RetOps.push_back(ValToCopy);
1218 // Don't emit a copytoreg.
1219 continue;
1220 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001221
Evan Cheng242b38b2009-02-23 09:03:22 +00001222 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1223 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001224 if (Subtarget->is64Bit()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001225 EVT ValVT = ValToCopy.getValueType();
Evan Cheng242b38b2009-02-23 09:03:22 +00001226 if (ValVT.isVector() && ValVT.getSizeInBits() == 64) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001227 ValToCopy = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, ValToCopy);
Evan Cheng242b38b2009-02-23 09:03:22 +00001228 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1)
Owen Anderson825b72b2009-08-11 20:47:22 +00001229 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, ValToCopy);
Evan Cheng242b38b2009-02-23 09:03:22 +00001230 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001231 }
1232
Dale Johannesendd64c412009-02-04 00:33:20 +00001233 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001234 Flag = Chain.getValue(1);
1235 }
Dan Gohman61a92132008-04-21 23:59:07 +00001236
1237 // The x86-64 ABI for returning structs by value requires that we copy
1238 // the sret argument into %rax for the return. We saved the argument into
1239 // a virtual register in the entry block, so now we copy the value out
1240 // and into %rax.
1241 if (Subtarget->is64Bit() &&
1242 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1243 MachineFunction &MF = DAG.getMachineFunction();
1244 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1245 unsigned Reg = FuncInfo->getSRetReturnReg();
1246 if (!Reg) {
Evan Chengdcea1632010-02-04 02:40:39 +00001247 Reg = MRI.createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001248 FuncInfo->setSRetReturnReg(Reg);
1249 }
Dale Johannesendd64c412009-02-04 00:33:20 +00001250 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001251
Dale Johannesendd64c412009-02-04 00:33:20 +00001252 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001253 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001254
1255 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001256 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001257 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001258
Chris Lattner447ff682008-03-11 03:23:40 +00001259 RetOps[0] = Chain; // Update chain.
1260
1261 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001262 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001263 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001264
1265 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001266 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001267}
1268
Dan Gohman98ca4f22009-08-05 01:29:28 +00001269/// LowerCallResult - Lower the result values of a call into the
1270/// appropriate copies out of appropriate physical registers.
1271///
1272SDValue
1273X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001274 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001275 const SmallVectorImpl<ISD::InputArg> &Ins,
1276 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001277 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001278
Chris Lattnere32bbf62007-02-28 07:09:55 +00001279 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001280 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001281 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001282 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001283 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001284 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001285
Chris Lattner3085e152007-02-25 08:59:22 +00001286 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001287 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001288 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001289 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001290
Torok Edwin3f142c32009-02-01 18:15:56 +00001291 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001292 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Dan Gohman98ca4f22009-08-05 01:29:28 +00001293 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001294 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001295 }
1296
Chris Lattner8e6da152008-03-10 21:08:41 +00001297 // If this is a call to a function that returns an fp value on the floating
1298 // point stack, but where we prefer to use the value in xmm registers, copy
1299 // 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 +00001300 if ((VA.getLocReg() == X86::ST0 ||
1301 VA.getLocReg() == X86::ST1) &&
1302 isScalarFPTypeInSSEReg(VA.getValVT())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001303 CopyVT = MVT::f80;
Chris Lattner3085e152007-02-25 08:59:22 +00001304 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001305
Evan Cheng79fb3b42009-02-20 20:43:02 +00001306 SDValue Val;
1307 if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001308 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1309 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1310 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001311 MVT::v2i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001312 Val = Chain.getValue(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001313 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1314 Val, DAG.getConstant(0, MVT::i64));
Evan Cheng242b38b2009-02-23 09:03:22 +00001315 } else {
1316 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001317 MVT::i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001318 Val = Chain.getValue(0);
1319 }
Evan Cheng79fb3b42009-02-20 20:43:02 +00001320 Val = DAG.getNode(ISD::BIT_CONVERT, dl, CopyVT, Val);
1321 } else {
1322 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1323 CopyVT, InFlag).getValue(1);
1324 Val = Chain.getValue(0);
1325 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001326 InFlag = Chain.getValue(2);
Chris Lattner112dedc2007-12-29 06:41:28 +00001327
Dan Gohman37eed792009-02-04 17:28:58 +00001328 if (CopyVT != VA.getValVT()) {
Chris Lattner8e6da152008-03-10 21:08:41 +00001329 // Round the F80 the right size, which also moves to the appropriate xmm
1330 // register.
Dan Gohman37eed792009-02-04 17:28:58 +00001331 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
Chris Lattner8e6da152008-03-10 21:08:41 +00001332 // This truncation won't change the value.
1333 DAG.getIntPtrConstant(1));
1334 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001335
Dan Gohman98ca4f22009-08-05 01:29:28 +00001336 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001337 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001338
Dan Gohman98ca4f22009-08-05 01:29:28 +00001339 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001340}
1341
1342
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001343//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001344// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001345//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001346// StdCall calling convention seems to be standard for many Windows' API
1347// routines and around. It differs from C calling convention just a little:
1348// callee should clean up the stack, not caller. Symbols should be also
1349// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001350// For info on fast calling convention see Fast Calling Convention (tail call)
1351// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001352
Dan Gohman98ca4f22009-08-05 01:29:28 +00001353/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001354/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001355static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1356 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001357 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001358
Dan Gohman98ca4f22009-08-05 01:29:28 +00001359 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001360}
1361
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001362/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001363/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001364static bool
1365ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1366 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001367 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001368
Dan Gohman98ca4f22009-08-05 01:29:28 +00001369 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001370}
1371
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001372/// IsCalleePop - Determines whether the callee is required to pop its
1373/// own arguments. Callee pop is necessary to support tail calls.
Dan Gohmand858e902010-04-17 15:26:15 +00001374bool X86TargetLowering::IsCalleePop(bool IsVarArg,
1375 CallingConv::ID CallingConv) const {
Gordon Henriksen86737662008-01-05 16:56:59 +00001376 if (IsVarArg)
1377 return false;
1378
Dan Gohman095cc292008-09-13 01:54:27 +00001379 switch (CallingConv) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001380 default:
1381 return false;
1382 case CallingConv::X86_StdCall:
1383 return !Subtarget->is64Bit();
1384 case CallingConv::X86_FastCall:
1385 return !Subtarget->is64Bit();
1386 case CallingConv::Fast:
Dan Gohman1797ed52010-02-08 20:27:50 +00001387 return GuaranteedTailCallOpt;
Chris Lattner29689432010-03-11 00:22:57 +00001388 case CallingConv::GHC:
1389 return GuaranteedTailCallOpt;
Gordon Henriksen86737662008-01-05 16:56:59 +00001390 }
1391}
1392
Dan Gohman095cc292008-09-13 01:54:27 +00001393/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1394/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001395CCAssignFn *X86TargetLowering::CCAssignFnForNode(CallingConv::ID CC) const {
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001396 if (Subtarget->is64Bit()) {
Chris Lattner29689432010-03-11 00:22:57 +00001397 if (CC == CallingConv::GHC)
1398 return CC_X86_64_GHC;
1399 else if (Subtarget->isTargetWin64())
Anton Korobeynikov8f88cb02008-03-22 20:37:30 +00001400 return CC_X86_Win64_C;
Evan Chenge9ac9e62008-09-07 09:07:23 +00001401 else
1402 return CC_X86_64_C;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001403 }
1404
Gordon Henriksen86737662008-01-05 16:56:59 +00001405 if (CC == CallingConv::X86_FastCall)
1406 return CC_X86_32_FastCall;
Evan Chengb188dd92008-09-10 18:25:29 +00001407 else if (CC == CallingConv::Fast)
1408 return CC_X86_32_FastCC;
Chris Lattner29689432010-03-11 00:22:57 +00001409 else if (CC == CallingConv::GHC)
1410 return CC_X86_32_GHC;
Gordon Henriksen86737662008-01-05 16:56:59 +00001411 else
1412 return CC_X86_32_C;
1413}
1414
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001415/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1416/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001417/// the specific parameter attribute. The copy will be passed as a byval
1418/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001419static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001420CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001421 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1422 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001423 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesendd64c412009-02-04 00:33:20 +00001424 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001425 /*isVolatile*/false, /*AlwaysInline=*/true,
1426 NULL, 0, NULL, 0);
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001427}
1428
Chris Lattner29689432010-03-11 00:22:57 +00001429/// IsTailCallConvention - Return true if the calling convention is one that
1430/// supports tail call optimization.
1431static bool IsTailCallConvention(CallingConv::ID CC) {
1432 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1433}
1434
Evan Cheng0c439eb2010-01-27 00:07:07 +00001435/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1436/// a tailcall target by changing its ABI.
1437static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001438 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001439}
1440
Dan Gohman98ca4f22009-08-05 01:29:28 +00001441SDValue
1442X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001443 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001444 const SmallVectorImpl<ISD::InputArg> &Ins,
1445 DebugLoc dl, SelectionDAG &DAG,
1446 const CCValAssign &VA,
1447 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001448 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001449 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001450 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001451 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001452 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001453 EVT ValVT;
1454
1455 // If value is passed by pointer we have address passed instead of the value
1456 // itself.
1457 if (VA.getLocInfo() == CCValAssign::Indirect)
1458 ValVT = VA.getLocVT();
1459 else
1460 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001461
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001462 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001463 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001464 // In case of tail call optimization mark all arguments mutable. Since they
1465 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001466 if (Flags.isByVal()) {
1467 int FI = MFI->CreateFixedObject(Flags.getByValSize(),
1468 VA.getLocMemOffset(), isImmutable, false);
1469 return DAG.getFrameIndex(FI, getPointerTy());
1470 } else {
1471 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
1472 VA.getLocMemOffset(), isImmutable, false);
1473 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1474 return DAG.getLoad(ValVT, dl, Chain, FIN,
David Greene67c9d422010-02-15 16:53:33 +00001475 PseudoSourceValue::getFixedStack(FI), 0,
1476 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001477 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001478}
1479
Dan Gohman475871a2008-07-27 21:46:04 +00001480SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001481X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001482 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001483 bool isVarArg,
1484 const SmallVectorImpl<ISD::InputArg> &Ins,
1485 DebugLoc dl,
1486 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001487 SmallVectorImpl<SDValue> &InVals)
1488 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001489 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001490 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001491
Gordon Henriksen86737662008-01-05 16:56:59 +00001492 const Function* Fn = MF.getFunction();
1493 if (Fn->hasExternalLinkage() &&
1494 Subtarget->isTargetCygMing() &&
1495 Fn->getName() == "main")
1496 FuncInfo->setForceFramePointer(true);
1497
Evan Cheng1bc78042006-04-26 01:20:17 +00001498 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001499 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001500 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001501
Chris Lattner29689432010-03-11 00:22:57 +00001502 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1503 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001504
Chris Lattner638402b2007-02-28 07:00:42 +00001505 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001506 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001507 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1508 ArgLocs, *DAG.getContext());
1509 CCInfo.AnalyzeFormalArguments(Ins, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001510
Chris Lattnerf39f7712007-02-28 05:46:49 +00001511 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001512 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001513 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1514 CCValAssign &VA = ArgLocs[i];
1515 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1516 // places.
1517 assert(VA.getValNo() != LastVal &&
1518 "Don't support value assigned to multiple locs yet");
1519 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001520
Chris Lattnerf39f7712007-02-28 05:46:49 +00001521 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001522 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001523 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001524 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001525 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001526 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001527 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001528 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001529 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001530 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001531 RC = X86::FR64RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001532 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001533 RC = X86::VR128RegisterClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001534 else if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
1535 RC = X86::VR64RegisterClass;
1536 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001537 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001538
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001539 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001540 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001541
Chris Lattnerf39f7712007-02-28 05:46:49 +00001542 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1543 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1544 // right size.
1545 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001546 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001547 DAG.getValueType(VA.getValVT()));
1548 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001549 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001550 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001551 else if (VA.getLocInfo() == CCValAssign::BCvt)
Anton Korobeynikov6dde14b2009-08-03 08:14:14 +00001552 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001553
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001554 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001555 // Handle MMX values passed in XMM regs.
1556 if (RegVT.isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001557 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1558 ArgValue, DAG.getConstant(0, MVT::i64));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001559 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1560 } else
1561 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001562 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001563 } else {
1564 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001565 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001566 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001567
1568 // If value is passed via pointer - do a load.
1569 if (VA.getLocInfo() == CCValAssign::Indirect)
David Greene67c9d422010-02-15 16:53:33 +00001570 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue, NULL, 0,
1571 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001572
Dan Gohman98ca4f22009-08-05 01:29:28 +00001573 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001574 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001575
Dan Gohman61a92132008-04-21 23:59:07 +00001576 // The x86-64 ABI for returning structs by value requires that we copy
1577 // the sret argument into %rax for the return. Save the argument into
1578 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001579 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001580 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1581 unsigned Reg = FuncInfo->getSRetReturnReg();
1582 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001583 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001584 FuncInfo->setSRetReturnReg(Reg);
1585 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001586 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001587 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001588 }
1589
Chris Lattnerf39f7712007-02-28 05:46:49 +00001590 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001591 // Align stack specially for tail calls.
1592 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001593 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001594
Evan Cheng1bc78042006-04-26 01:20:17 +00001595 // If the function takes variable number of arguments, make a frame index for
1596 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001597 if (isVarArg) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001598 if (Is64Bit || CallConv != CallingConv::X86_FastCall) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001599 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,
1600 true, false));
Gordon Henriksen86737662008-01-05 16:56:59 +00001601 }
1602 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001603 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1604
1605 // FIXME: We should really autogenerate these arrays
1606 static const unsigned GPR64ArgRegsWin64[] = {
1607 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001608 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001609 static const unsigned XMMArgRegsWin64[] = {
1610 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1611 };
1612 static const unsigned GPR64ArgRegs64Bit[] = {
1613 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1614 };
1615 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001616 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1617 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1618 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001619 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1620
1621 if (IsWin64) {
1622 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1623 GPR64ArgRegs = GPR64ArgRegsWin64;
1624 XMMArgRegs = XMMArgRegsWin64;
1625 } else {
1626 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1627 GPR64ArgRegs = GPR64ArgRegs64Bit;
1628 XMMArgRegs = XMMArgRegs64Bit;
1629 }
1630 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1631 TotalNumIntRegs);
1632 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1633 TotalNumXMMRegs);
1634
Devang Patel578efa92009-06-05 21:57:13 +00001635 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Evan Chengc7ce29b2009-02-13 22:36:38 +00001636 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001637 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001638 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001639 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001640 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00001641 // Kernel mode asks for SSE to be disabled, so don't push them
1642 // on the stack.
1643 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001644
Gordon Henriksen86737662008-01-05 16:56:59 +00001645 // For X86-64, if there are vararg parameters that are passed via
1646 // registers, then we must store them to their spots on the stack so they
1647 // may be loaded by deferencing the result of va_next.
Dan Gohman1e93df62010-04-17 14:41:14 +00001648 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1649 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1650 FuncInfo->setRegSaveFrameIndex(
1651 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
1652 false));
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001653
Gordon Henriksen86737662008-01-05 16:56:59 +00001654 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001655 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001656 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1657 getPointerTy());
1658 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001659 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001660 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1661 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001662 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
1663 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001664 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001665 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001666 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Dan Gohman1e93df62010-04-17 14:41:14 +00001667 PseudoSourceValue::getFixedStack(
1668 FuncInfo->getRegSaveFrameIndex()),
David Greene67c9d422010-02-15 16:53:33 +00001669 Offset, false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001670 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001671 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001672 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001673
Dan Gohmanface41a2009-08-16 21:24:25 +00001674 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1675 // Now store the XMM (fp + vector) parameter registers.
1676 SmallVector<SDValue, 11> SaveXMMOps;
1677 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001678
Dan Gohmanface41a2009-08-16 21:24:25 +00001679 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
1680 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1681 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001682
Dan Gohman1e93df62010-04-17 14:41:14 +00001683 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1684 FuncInfo->getRegSaveFrameIndex()));
1685 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1686 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001687
Dan Gohmanface41a2009-08-16 21:24:25 +00001688 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
1689 unsigned VReg = MF.addLiveIn(XMMArgRegs[NumXMMRegs],
1690 X86::VR128RegisterClass);
1691 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1692 SaveXMMOps.push_back(Val);
1693 }
1694 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1695 MVT::Other,
1696 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001697 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001698
1699 if (!MemOps.empty())
1700 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1701 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001702 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001703 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001704
Gordon Henriksen86737662008-01-05 16:56:59 +00001705 // Some CCs need callee pop.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001706 if (IsCalleePop(isVarArg, CallConv)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001707 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001708 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001709 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001710 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001711 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001712 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001713 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001714
Gordon Henriksen86737662008-01-05 16:56:59 +00001715 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001716 // RegSaveFrameIndex is X86-64 only.
1717 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001718 if (CallConv == CallingConv::X86_FastCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001719 // fastcc functions can't have varargs.
1720 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001721 }
Evan Cheng25caf632006-05-23 21:06:34 +00001722
Dan Gohman98ca4f22009-08-05 01:29:28 +00001723 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001724}
1725
Dan Gohman475871a2008-07-27 21:46:04 +00001726SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001727X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1728 SDValue StackPtr, SDValue Arg,
1729 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001730 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001731 ISD::ArgFlagsTy Flags) const {
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +00001732 const unsigned FirstStackArgOffset = (Subtarget->isTargetWin64() ? 32 : 0);
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +00001733 unsigned LocMemOffset = FirstStackArgOffset + VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001734 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001735 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001736 if (Flags.isByVal()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001737 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Evan Chengdffbd832008-01-10 00:09:10 +00001738 }
Dale Johannesenace16102009-02-03 19:33:06 +00001739 return DAG.getStore(Chain, dl, Arg, PtrOff,
David Greene67c9d422010-02-15 16:53:33 +00001740 PseudoSourceValue::getStack(), LocMemOffset,
1741 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001742}
1743
Bill Wendling64e87322009-01-16 19:25:27 +00001744/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001745/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001746SDValue
1747X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001748 SDValue &OutRetAddr, SDValue Chain,
1749 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001750 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001751 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001752 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001753 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001754
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001755 // Load the "old" Return address.
David Greene67c9d422010-02-15 16:53:33 +00001756 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, NULL, 0, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001757 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001758}
1759
1760/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1761/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001762static SDValue
1763EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001764 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001765 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001766 // Store the return address to the appropriate stack slot.
1767 if (!FPDiff) return Chain;
1768 // Calculate the new stack slot for the return address.
1769 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001770 int NewReturnAddrFI =
Arnold Schwaighofer92652752010-02-22 16:18:09 +00001771 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001772 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001773 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001774 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
David Greene67c9d422010-02-15 16:53:33 +00001775 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0,
1776 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001777 return Chain;
1778}
1779
Dan Gohman98ca4f22009-08-05 01:29:28 +00001780SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001781X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001782 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001783 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001784 const SmallVectorImpl<ISD::OutputArg> &Outs,
1785 const SmallVectorImpl<ISD::InputArg> &Ins,
1786 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001787 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001788 MachineFunction &MF = DAG.getMachineFunction();
1789 bool Is64Bit = Subtarget->is64Bit();
1790 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00001791 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001792
Evan Cheng5f941932010-02-05 02:21:12 +00001793 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001794 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00001795 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1796 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Evan Cheng022d9e12010-02-02 23:55:14 +00001797 Outs, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00001798
1799 // Sibcalls are automatically detected tailcalls which do not require
1800 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00001801 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00001802 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00001803
1804 if (isTailCall)
1805 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00001806 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00001807
Chris Lattner29689432010-03-11 00:22:57 +00001808 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1809 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001810
Chris Lattner638402b2007-02-28 07:00:42 +00001811 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001812 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001813 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1814 ArgLocs, *DAG.getContext());
1815 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001816
Chris Lattner423c5f42007-02-28 05:31:48 +00001817 // Get a count of how many bytes are to be pushed on the stack.
1818 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00001819 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00001820 // This is a sibcall. The memory operands are available in caller's
1821 // own caller's stack.
1822 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00001823 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00001824 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001825
Gordon Henriksen86737662008-01-05 16:56:59 +00001826 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00001827 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001828 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00001829 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00001830 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1831 FPDiff = NumBytesCallerPushed - NumBytes;
1832
1833 // Set the delta of movement of the returnaddr stackslot.
1834 // But only set if delta is greater than previous delta.
1835 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1836 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1837 }
1838
Evan Chengf22f9b32010-02-06 03:28:46 +00001839 if (!IsSibcall)
1840 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001841
Dan Gohman475871a2008-07-27 21:46:04 +00001842 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001843 // Load return adress for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00001844 if (isTailCall && FPDiff)
1845 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
1846 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001847
Dan Gohman475871a2008-07-27 21:46:04 +00001848 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1849 SmallVector<SDValue, 8> MemOpChains;
1850 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001851
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001852 // Walk the register/memloc assignments, inserting copies/loads. In the case
1853 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00001854 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1855 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001856 EVT RegVT = VA.getLocVT();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001857 SDValue Arg = Outs[i].Val;
1858 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00001859 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00001860
Chris Lattner423c5f42007-02-28 05:31:48 +00001861 // Promote the value if needed.
1862 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001863 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00001864 case CCValAssign::Full: break;
1865 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001866 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001867 break;
1868 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001869 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001870 break;
1871 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001872 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
1873 // Special case: passing MMX values in XMM registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001874 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
1875 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
1876 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001877 } else
1878 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
1879 break;
1880 case CCValAssign::BCvt:
1881 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001882 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001883 case CCValAssign::Indirect: {
1884 // Store the argument.
1885 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00001886 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001887 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
David Greene67c9d422010-02-15 16:53:33 +00001888 PseudoSourceValue::getFixedStack(FI), 0,
1889 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001890 Arg = SpillSlot;
1891 break;
1892 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00001893 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001894
Chris Lattner423c5f42007-02-28 05:31:48 +00001895 if (VA.isRegLoc()) {
1896 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Evan Chengf22f9b32010-02-06 03:28:46 +00001897 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00001898 assert(VA.isMemLoc());
1899 if (StackPtr.getNode() == 0)
1900 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
1901 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1902 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001903 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001904 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001905
Evan Cheng32fe1032006-05-25 00:59:30 +00001906 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001907 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001908 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001909
Evan Cheng347d5f72006-04-28 21:29:37 +00001910 // Build a sequence of copy-to-reg nodes chained together with token chain
1911 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001912 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001913 // Tail call byval lowering might overwrite argument registers so in case of
1914 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001915 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001916 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001917 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001918 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001919 InFlag = Chain.getValue(1);
1920 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001921
Chris Lattner88e1fd52009-07-09 04:24:46 +00001922 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001923 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1924 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001925 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001926 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
1927 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001928 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001929 InFlag);
1930 InFlag = Chain.getValue(1);
1931 } else {
1932 // If we are tail calling and generating PIC/GOT style code load the
1933 // address of the callee into ECX. The value in ecx is used as target of
1934 // the tail jump. This is done to circumvent the ebx/callee-saved problem
1935 // for tail calls on PIC/GOT architectures. Normally we would just put the
1936 // address of GOT into ebx and then call target@PLT. But for tail calls
1937 // ebx would be restored (since ebx is callee saved) before jumping to the
1938 // target@PLT.
1939
1940 // Note: The actual moving to ECX is done further down.
1941 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1942 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1943 !G->getGlobal()->hasProtectedVisibility())
1944 Callee = LowerGlobalAddress(Callee, DAG);
1945 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00001946 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001947 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001948 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001949
Gordon Henriksen86737662008-01-05 16:56:59 +00001950 if (Is64Bit && isVarArg) {
1951 // From AMD64 ABI document:
1952 // For calls that may call functions that use varargs or stdargs
1953 // (prototype-less calls or calls to functions containing ellipsis (...) in
1954 // the declaration) %al is used as hidden argument to specify the number
1955 // of SSE registers used. The contents of %al do not need to match exactly
1956 // the number of registers, but must be an ubound on the number of SSE
1957 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001958
1959 // FIXME: Verify this on Win64
Gordon Henriksen86737662008-01-05 16:56:59 +00001960 // Count the number of XMM registers allocated.
1961 static const unsigned XMMArgRegs[] = {
1962 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1963 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1964 };
1965 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Scott Michelfdc40a02009-02-17 22:15:04 +00001966 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00001967 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001968
Dale Johannesendd64c412009-02-04 00:33:20 +00001969 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00001970 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00001971 InFlag = Chain.getValue(1);
1972 }
1973
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001974
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001975 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001976 if (isTailCall) {
1977 // Force all the incoming stack arguments to be loaded from the stack
1978 // before any new outgoing arguments are stored to the stack, because the
1979 // outgoing stack slots may alias the incoming argument stack slots, and
1980 // the alias isn't otherwise explicit. This is slightly more conservative
1981 // than necessary, because it means that each store effectively depends
1982 // on every argument instead of just those arguments it would clobber.
1983 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
1984
Dan Gohman475871a2008-07-27 21:46:04 +00001985 SmallVector<SDValue, 8> MemOpChains2;
1986 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00001987 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001988 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00001989 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00001990 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00001991 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1992 CCValAssign &VA = ArgLocs[i];
1993 if (VA.isRegLoc())
1994 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001995 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001996 SDValue Arg = Outs[i].Val;
1997 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00001998 // Create frame index.
1999 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002000 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
David Greene3f2bf852009-11-12 20:49:22 +00002001 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true, false);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002002 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002003
Duncan Sands276dcbd2008-03-21 09:14:45 +00002004 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002005 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002006 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002007 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002008 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002009 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002010 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002011
Dan Gohman98ca4f22009-08-05 01:29:28 +00002012 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2013 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002014 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002015 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002016 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002017 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002018 DAG.getStore(ArgChain, dl, Arg, FIN,
David Greene67c9d422010-02-15 16:53:33 +00002019 PseudoSourceValue::getFixedStack(FI), 0,
2020 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002021 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002022 }
2023 }
2024
2025 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002026 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002027 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002028
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002029 // Copy arguments to their registers.
2030 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002031 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002032 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002033 InFlag = Chain.getValue(1);
2034 }
Dan Gohman475871a2008-07-27 21:46:04 +00002035 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002036
Gordon Henriksen86737662008-01-05 16:56:59 +00002037 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002038 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002039 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002040 }
2041
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002042 bool WasGlobalOrExternal = false;
2043 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2044 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2045 // In the 64-bit large code model, we have to make all calls
2046 // through a register, since the call instruction's 32-bit
2047 // pc-relative offset may not be large enough to hold the whole
2048 // address.
2049 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
2050 WasGlobalOrExternal = true;
2051 // If the callee is a GlobalAddress node (quite common, every direct call
2052 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2053 // it.
2054
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002055 // We should use extra load for direct calls to dllimported functions in
2056 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002057 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002058 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002059 unsigned char OpFlags = 0;
Eric Christopherfd179292009-08-27 18:07:15 +00002060
Chris Lattner48a7d022009-07-09 05:02:21 +00002061 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2062 // external symbols most go through the PLT in PIC mode. If the symbol
2063 // has hidden or protected visibility, or if it is static or local, then
2064 // we don't need to use the PLT - we can directly call it.
2065 if (Subtarget->isTargetELF() &&
2066 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002067 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002068 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002069 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002070 (GV->isDeclaration() || GV->isWeakForLinker()) &&
2071 Subtarget->getDarwinVers() < 9) {
2072 // PC-relative references to external symbols should go through $stub,
2073 // unless we're building with the leopard linker or later, which
2074 // automatically synthesizes these stubs.
2075 OpFlags = X86II::MO_DARWIN_STUB;
2076 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002077
Chris Lattner74e726e2009-07-09 05:27:35 +00002078 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002079 G->getOffset(), OpFlags);
2080 }
Bill Wendling056292f2008-09-16 21:48:12 +00002081 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002082 WasGlobalOrExternal = true;
Chris Lattner48a7d022009-07-09 05:02:21 +00002083 unsigned char OpFlags = 0;
2084
2085 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to external
2086 // symbols should go through the PLT.
2087 if (Subtarget->isTargetELF() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002088 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002089 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002090 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002091 Subtarget->getDarwinVers() < 9) {
2092 // PC-relative references to external symbols should go through $stub,
2093 // unless we're building with the leopard linker or later, which
2094 // automatically synthesizes these stubs.
2095 OpFlags = X86II::MO_DARWIN_STUB;
2096 }
Eric Christopherfd179292009-08-27 18:07:15 +00002097
Chris Lattner48a7d022009-07-09 05:02:21 +00002098 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2099 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002100 }
2101
Chris Lattnerd96d0722007-02-25 06:40:16 +00002102 // Returns a chain & a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +00002103 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00002104 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002105
Evan Chengf22f9b32010-02-06 03:28:46 +00002106 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002107 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2108 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002109 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002110 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002111
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002112 Ops.push_back(Chain);
2113 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002114
Dan Gohman98ca4f22009-08-05 01:29:28 +00002115 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002116 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002117
Gordon Henriksen86737662008-01-05 16:56:59 +00002118 // Add argument registers to the end of the list so that they are known live
2119 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002120 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2121 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2122 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002123
Evan Cheng586ccac2008-03-18 23:36:35 +00002124 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002125 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002126 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2127
2128 // Add an implicit use of AL for x86 vararg functions.
2129 if (Is64Bit && isVarArg)
Owen Anderson825b72b2009-08-11 20:47:22 +00002130 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002131
Gabor Greifba36cb52008-08-28 21:40:38 +00002132 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002133 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002134
Dan Gohman98ca4f22009-08-05 01:29:28 +00002135 if (isTailCall) {
2136 // If this is the first return lowered for this function, add the regs
2137 // to the liveout set for the function.
2138 if (MF.getRegInfo().liveout_empty()) {
2139 SmallVector<CCValAssign, 16> RVLocs;
2140 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
2141 *DAG.getContext());
2142 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
2143 for (unsigned i = 0; i != RVLocs.size(); ++i)
2144 if (RVLocs[i].isRegLoc())
2145 MF.getRegInfo().addLiveOut(RVLocs[i].getLocReg());
2146 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002147 return DAG.getNode(X86ISD::TC_RETURN, dl,
2148 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002149 }
2150
Dale Johannesenace16102009-02-03 19:33:06 +00002151 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002152 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002153
Chris Lattner2d297092006-05-23 18:50:38 +00002154 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002155 unsigned NumBytesForCalleeToPush;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002156 if (IsCalleePop(isVarArg, CallConv))
Gordon Henriksen86737662008-01-05 16:56:59 +00002157 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002158 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002159 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002160 // pops the hidden struct pointer, so we have to push it back.
2161 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002162 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002163 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002164 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002165
Gordon Henriksenae636f82008-01-03 16:47:34 +00002166 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002167 if (!IsSibcall) {
2168 Chain = DAG.getCALLSEQ_END(Chain,
2169 DAG.getIntPtrConstant(NumBytes, true),
2170 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2171 true),
2172 InFlag);
2173 InFlag = Chain.getValue(1);
2174 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002175
Chris Lattner3085e152007-02-25 08:59:22 +00002176 // Handle result values, copying them out of physregs into vregs that we
2177 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002178 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2179 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002180}
2181
Evan Cheng25ab6902006-09-08 06:48:29 +00002182
2183//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002184// Fast Calling Convention (tail call) implementation
2185//===----------------------------------------------------------------------===//
2186
2187// Like std call, callee cleans arguments, convention except that ECX is
2188// reserved for storing the tail called function address. Only 2 registers are
2189// free for argument passing (inreg). Tail call optimization is performed
2190// provided:
2191// * tailcallopt is enabled
2192// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002193// On X86_64 architecture with GOT-style position independent code only local
2194// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002195// To keep the stack aligned according to platform abi the function
2196// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2197// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002198// If a tail called function callee has more arguments than the caller the
2199// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002200// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002201// original REtADDR, but before the saved framepointer or the spilled registers
2202// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2203// stack layout:
2204// arg1
2205// arg2
2206// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002207// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002208// move area ]
2209// (possible EBP)
2210// ESI
2211// EDI
2212// local1 ..
2213
2214/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2215/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002216unsigned
2217X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2218 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002219 MachineFunction &MF = DAG.getMachineFunction();
2220 const TargetMachine &TM = MF.getTarget();
2221 const TargetFrameInfo &TFI = *TM.getFrameInfo();
2222 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002223 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002224 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002225 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002226 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2227 // Number smaller than 12 so just add the difference.
2228 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2229 } else {
2230 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002231 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002232 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002233 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002234 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002235}
2236
Evan Cheng5f941932010-02-05 02:21:12 +00002237/// MatchingStackOffset - Return true if the given stack call argument is
2238/// already available in the same position (relatively) of the caller's
2239/// incoming argument stack.
2240static
2241bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2242 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2243 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002244 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2245 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002246 if (Arg.getOpcode() == ISD::CopyFromReg) {
2247 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
2248 if (!VR || TargetRegisterInfo::isPhysicalRegister(VR))
2249 return false;
2250 MachineInstr *Def = MRI->getVRegDef(VR);
2251 if (!Def)
2252 return false;
2253 if (!Flags.isByVal()) {
2254 if (!TII->isLoadFromStackSlot(Def, FI))
2255 return false;
2256 } else {
2257 unsigned Opcode = Def->getOpcode();
2258 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2259 Def->getOperand(1).isFI()) {
2260 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002261 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002262 } else
2263 return false;
2264 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002265 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2266 if (Flags.isByVal())
2267 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002268 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002269 // define @foo(%struct.X* %A) {
2270 // tail call @bar(%struct.X* byval %A)
2271 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002272 return false;
2273 SDValue Ptr = Ld->getBasePtr();
2274 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2275 if (!FINode)
2276 return false;
2277 FI = FINode->getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002278 } else
2279 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002280
Evan Cheng4cae1332010-03-05 08:38:04 +00002281 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002282 if (!MFI->isFixedObjectIndex(FI))
2283 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002284 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002285}
2286
Dan Gohman98ca4f22009-08-05 01:29:28 +00002287/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2288/// for tail call optimization. Targets which want to do tail call
2289/// optimization should implement this function.
2290bool
2291X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002292 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002293 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002294 bool isCalleeStructRet,
2295 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002296 const SmallVectorImpl<ISD::OutputArg> &Outs,
2297 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002298 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002299 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002300 CalleeCC != CallingConv::C)
2301 return false;
2302
Evan Cheng7096ae42010-01-29 06:45:59 +00002303 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002304 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002305 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002306 CallingConv::ID CallerCC = CallerF->getCallingConv();
2307 bool CCMatch = CallerCC == CalleeCC;
2308
Dan Gohman1797ed52010-02-08 20:27:50 +00002309 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002310 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002311 return true;
2312 return false;
2313 }
2314
Evan Chengb2c92902010-02-02 02:22:50 +00002315 // Look for obvious safe cases to perform tail call optimization that does not
2316 // requite ABI changes. This is what gcc calls sibcall.
2317
Evan Cheng2c12cb42010-03-26 16:26:03 +00002318 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2319 // emit a special epilogue.
2320 if (RegInfo->needsStackRealignment(MF))
2321 return false;
2322
Evan Cheng3c262ee2010-03-26 02:13:13 +00002323 // Do not sibcall optimize vararg calls unless the call site is not passing any
2324 // arguments.
2325 if (isVarArg && !Outs.empty())
Evan Cheng843bd692010-01-31 06:44:49 +00002326 return false;
2327
Evan Chenga375d472010-03-15 18:54:48 +00002328 // Also avoid sibcall optimization if either caller or callee uses struct
2329 // return semantics.
2330 if (isCalleeStructRet || isCallerStructRet)
2331 return false;
2332
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002333 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2334 // Therefore if it's not used by the call it is not safe to optimize this into
2335 // a sibcall.
2336 bool Unused = false;
2337 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2338 if (!Ins[i].Used) {
2339 Unused = true;
2340 break;
2341 }
2342 }
2343 if (Unused) {
2344 SmallVector<CCValAssign, 16> RVLocs;
2345 CCState CCInfo(CalleeCC, false, getTargetMachine(),
2346 RVLocs, *DAG.getContext());
2347 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002348 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002349 CCValAssign &VA = RVLocs[i];
2350 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2351 return false;
2352 }
2353 }
2354
Evan Cheng13617962010-04-30 01:12:32 +00002355 // If the calling conventions do not match, then we'd better make sure the
2356 // results are returned in the same way as what the caller expects.
2357 if (!CCMatch) {
2358 SmallVector<CCValAssign, 16> RVLocs1;
2359 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
2360 RVLocs1, *DAG.getContext());
2361 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2362
2363 SmallVector<CCValAssign, 16> RVLocs2;
2364 CCState CCInfo2(CallerCC, false, getTargetMachine(),
2365 RVLocs2, *DAG.getContext());
2366 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2367
2368 if (RVLocs1.size() != RVLocs2.size())
2369 return false;
2370 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2371 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2372 return false;
2373 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2374 return false;
2375 if (RVLocs1[i].isRegLoc()) {
2376 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2377 return false;
2378 } else {
2379 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2380 return false;
2381 }
2382 }
2383 }
2384
Evan Chenga6bff982010-01-30 01:22:00 +00002385 // If the callee takes no arguments then go on to check the results of the
2386 // call.
2387 if (!Outs.empty()) {
2388 // Check if stack adjustment is needed. For now, do not do this if any
2389 // argument is passed on the stack.
2390 SmallVector<CCValAssign, 16> ArgLocs;
2391 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
2392 ArgLocs, *DAG.getContext());
2393 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CalleeCC));
Evan Chengb2c92902010-02-02 02:22:50 +00002394 if (CCInfo.getNextStackOffset()) {
2395 MachineFunction &MF = DAG.getMachineFunction();
2396 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2397 return false;
2398 if (Subtarget->isTargetWin64())
2399 // Win64 ABI has additional complications.
2400 return false;
2401
2402 // Check if the arguments are already laid out in the right way as
2403 // the caller's fixed stack objects.
2404 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002405 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2406 const X86InstrInfo *TII =
2407 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002408 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2409 CCValAssign &VA = ArgLocs[i];
2410 EVT RegVT = VA.getLocVT();
2411 SDValue Arg = Outs[i].Val;
2412 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002413 if (VA.getLocInfo() == CCValAssign::Indirect)
2414 return false;
2415 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002416 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2417 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002418 return false;
2419 }
2420 }
2421 }
Evan Chenga6bff982010-01-30 01:22:00 +00002422 }
Evan Chengb1712452010-01-27 06:25:16 +00002423
Evan Cheng86809cc2010-02-03 03:28:02 +00002424 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002425}
2426
Dan Gohman3df24e62008-09-03 23:12:08 +00002427FastISel *
Chris Lattnered3a8062010-04-05 06:05:26 +00002428X86TargetLowering::createFastISel(MachineFunction &mf,
Evan Chengddc419c2010-01-26 19:04:47 +00002429 DenseMap<const Value *, unsigned> &vm,
2430 DenseMap<const BasicBlock*, MachineBasicBlock*> &bm,
Dan Gohmanf81eca02010-04-22 20:46:50 +00002431 DenseMap<const AllocaInst *, int> &am,
2432 std::vector<std::pair<MachineInstr*, unsigned> > &pn
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002433#ifndef NDEBUG
Dan Gohman25208642010-04-14 19:53:31 +00002434 , SmallSet<const Instruction *, 8> &cil
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002435#endif
Dan Gohmand858e902010-04-17 15:26:15 +00002436 ) const {
Dan Gohmanf81eca02010-04-22 20:46:50 +00002437 return X86::createFastISel(mf, vm, bm, am, pn
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002438#ifndef NDEBUG
2439 , cil
2440#endif
2441 );
Dan Gohmand9f3c482008-08-19 21:32:53 +00002442}
2443
2444
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002445//===----------------------------------------------------------------------===//
2446// Other Lowering Hooks
2447//===----------------------------------------------------------------------===//
2448
2449
Dan Gohmand858e902010-04-17 15:26:15 +00002450SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002451 MachineFunction &MF = DAG.getMachineFunction();
2452 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2453 int ReturnAddrIndex = FuncInfo->getRAIndex();
2454
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002455 if (ReturnAddrIndex == 0) {
2456 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002457 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002458 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Arnold Schwaighofer92652752010-02-22 16:18:09 +00002459 false, false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002460 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002461 }
2462
Evan Cheng25ab6902006-09-08 06:48:29 +00002463 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002464}
2465
2466
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002467bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2468 bool hasSymbolicDisplacement) {
2469 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002470 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002471 return false;
2472
2473 // If we don't have a symbolic displacement - we don't have any extra
2474 // restrictions.
2475 if (!hasSymbolicDisplacement)
2476 return true;
2477
2478 // FIXME: Some tweaks might be needed for medium code model.
2479 if (M != CodeModel::Small && M != CodeModel::Kernel)
2480 return false;
2481
2482 // For small code model we assume that latest object is 16MB before end of 31
2483 // bits boundary. We may also accept pretty large negative constants knowing
2484 // that all objects are in the positive half of address space.
2485 if (M == CodeModel::Small && Offset < 16*1024*1024)
2486 return true;
2487
2488 // For kernel code model we know that all object resist in the negative half
2489 // of 32bits address space. We may not accept negative offsets, since they may
2490 // be just off and we may accept pretty large positive ones.
2491 if (M == CodeModel::Kernel && Offset > 0)
2492 return true;
2493
2494 return false;
2495}
2496
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002497/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2498/// specific condition code, returning the condition code and the LHS/RHS of the
2499/// comparison to make.
2500static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2501 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002502 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002503 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2504 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2505 // X > -1 -> X == 0, jump !sign.
2506 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002507 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002508 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2509 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002510 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002511 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002512 // X < 1 -> X <= 0
2513 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002514 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002515 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002516 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002517
Evan Chengd9558e02006-01-06 00:43:03 +00002518 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002519 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002520 case ISD::SETEQ: return X86::COND_E;
2521 case ISD::SETGT: return X86::COND_G;
2522 case ISD::SETGE: return X86::COND_GE;
2523 case ISD::SETLT: return X86::COND_L;
2524 case ISD::SETLE: return X86::COND_LE;
2525 case ISD::SETNE: return X86::COND_NE;
2526 case ISD::SETULT: return X86::COND_B;
2527 case ISD::SETUGT: return X86::COND_A;
2528 case ISD::SETULE: return X86::COND_BE;
2529 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002530 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002531 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002532
Chris Lattner4c78e022008-12-23 23:42:27 +00002533 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002534
Chris Lattner4c78e022008-12-23 23:42:27 +00002535 // If LHS is a foldable load, but RHS is not, flip the condition.
2536 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2537 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2538 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2539 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002540 }
2541
Chris Lattner4c78e022008-12-23 23:42:27 +00002542 switch (SetCCOpcode) {
2543 default: break;
2544 case ISD::SETOLT:
2545 case ISD::SETOLE:
2546 case ISD::SETUGT:
2547 case ISD::SETUGE:
2548 std::swap(LHS, RHS);
2549 break;
2550 }
2551
2552 // On a floating point condition, the flags are set as follows:
2553 // ZF PF CF op
2554 // 0 | 0 | 0 | X > Y
2555 // 0 | 0 | 1 | X < Y
2556 // 1 | 0 | 0 | X == Y
2557 // 1 | 1 | 1 | unordered
2558 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002559 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002560 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002561 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002562 case ISD::SETOLT: // flipped
2563 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002564 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002565 case ISD::SETOLE: // flipped
2566 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002567 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002568 case ISD::SETUGT: // flipped
2569 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002570 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002571 case ISD::SETUGE: // flipped
2572 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002573 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002574 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002575 case ISD::SETNE: return X86::COND_NE;
2576 case ISD::SETUO: return X86::COND_P;
2577 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002578 case ISD::SETOEQ:
2579 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002580 }
Evan Chengd9558e02006-01-06 00:43:03 +00002581}
2582
Evan Cheng4a460802006-01-11 00:33:36 +00002583/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2584/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002585/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002586static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002587 switch (X86CC) {
2588 default:
2589 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002590 case X86::COND_B:
2591 case X86::COND_BE:
2592 case X86::COND_E:
2593 case X86::COND_P:
2594 case X86::COND_A:
2595 case X86::COND_AE:
2596 case X86::COND_NE:
2597 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002598 return true;
2599 }
2600}
2601
Evan Chengeb2f9692009-10-27 19:56:55 +00002602/// isFPImmLegal - Returns true if the target can instruction select the
2603/// specified FP immediate natively. If false, the legalizer will
2604/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002605bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00002606 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
2607 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
2608 return true;
2609 }
2610 return false;
2611}
2612
Nate Begeman9008ca62009-04-27 18:41:29 +00002613/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2614/// the specified range (L, H].
2615static bool isUndefOrInRange(int Val, int Low, int Hi) {
2616 return (Val < 0) || (Val >= Low && Val < Hi);
2617}
2618
2619/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2620/// specified value.
2621static bool isUndefOrEqual(int Val, int CmpVal) {
2622 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002623 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002624 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002625}
2626
Nate Begeman9008ca62009-04-27 18:41:29 +00002627/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2628/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2629/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00002630static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002631 if (VT == MVT::v4f32 || VT == MVT::v4i32 || VT == MVT::v4i16)
Nate Begeman9008ca62009-04-27 18:41:29 +00002632 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002633 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00002634 return (Mask[0] < 2 && Mask[1] < 2);
2635 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002636}
2637
Nate Begeman9008ca62009-04-27 18:41:29 +00002638bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002639 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002640 N->getMask(M);
2641 return ::isPSHUFDMask(M, N->getValueType(0));
2642}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002643
Nate Begeman9008ca62009-04-27 18:41:29 +00002644/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2645/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00002646static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002647 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002648 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002649
Nate Begeman9008ca62009-04-27 18:41:29 +00002650 // Lower quadword copied in order or undef.
2651 for (int i = 0; i != 4; ++i)
2652 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002653 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002654
Evan Cheng506d3df2006-03-29 23:07:14 +00002655 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002656 for (int i = 4; i != 8; ++i)
2657 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002658 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002659
Evan Cheng506d3df2006-03-29 23:07:14 +00002660 return true;
2661}
2662
Nate Begeman9008ca62009-04-27 18:41:29 +00002663bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002664 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002665 N->getMask(M);
2666 return ::isPSHUFHWMask(M, N->getValueType(0));
2667}
Evan Cheng506d3df2006-03-29 23:07:14 +00002668
Nate Begeman9008ca62009-04-27 18:41:29 +00002669/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2670/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00002671static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002672 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002673 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002674
Rafael Espindola15684b22009-04-24 12:40:33 +00002675 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00002676 for (int i = 4; i != 8; ++i)
2677 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00002678 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002679
Rafael Espindola15684b22009-04-24 12:40:33 +00002680 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002681 for (int i = 0; i != 4; ++i)
2682 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00002683 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002684
Rafael Espindola15684b22009-04-24 12:40:33 +00002685 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002686}
2687
Nate Begeman9008ca62009-04-27 18:41:29 +00002688bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002689 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002690 N->getMask(M);
2691 return ::isPSHUFLWMask(M, N->getValueType(0));
2692}
2693
Nate Begemana09008b2009-10-19 02:17:23 +00002694/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
2695/// is suitable for input to PALIGNR.
2696static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
2697 bool hasSSSE3) {
2698 int i, e = VT.getVectorNumElements();
2699
2700 // Do not handle v2i64 / v2f64 shuffles with palignr.
2701 if (e < 4 || !hasSSSE3)
2702 return false;
2703
2704 for (i = 0; i != e; ++i)
2705 if (Mask[i] >= 0)
2706 break;
2707
2708 // All undef, not a palignr.
2709 if (i == e)
2710 return false;
2711
2712 // Determine if it's ok to perform a palignr with only the LHS, since we
2713 // don't have access to the actual shuffle elements to see if RHS is undef.
2714 bool Unary = Mask[i] < (int)e;
2715 bool NeedsUnary = false;
2716
2717 int s = Mask[i] - i;
2718
2719 // Check the rest of the elements to see if they are consecutive.
2720 for (++i; i != e; ++i) {
2721 int m = Mask[i];
2722 if (m < 0)
2723 continue;
2724
2725 Unary = Unary && (m < (int)e);
2726 NeedsUnary = NeedsUnary || (m < s);
2727
2728 if (NeedsUnary && !Unary)
2729 return false;
2730 if (Unary && m != ((s+i) & (e-1)))
2731 return false;
2732 if (!Unary && m != (s+i))
2733 return false;
2734 }
2735 return true;
2736}
2737
2738bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
2739 SmallVector<int, 8> M;
2740 N->getMask(M);
2741 return ::isPALIGNRMask(M, N->getValueType(0), true);
2742}
2743
Evan Cheng14aed5e2006-03-24 01:18:28 +00002744/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2745/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00002746static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002747 int NumElems = VT.getVectorNumElements();
2748 if (NumElems != 2 && NumElems != 4)
2749 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002750
Nate Begeman9008ca62009-04-27 18:41:29 +00002751 int Half = NumElems / 2;
2752 for (int i = 0; i < Half; ++i)
2753 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002754 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002755 for (int i = Half; i < NumElems; ++i)
2756 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002757 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002758
Evan Cheng14aed5e2006-03-24 01:18:28 +00002759 return true;
2760}
2761
Nate Begeman9008ca62009-04-27 18:41:29 +00002762bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
2763 SmallVector<int, 8> M;
2764 N->getMask(M);
2765 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002766}
2767
Evan Cheng213d2cf2007-05-17 18:45:50 +00002768/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002769/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2770/// half elements to come from vector 1 (which would equal the dest.) and
2771/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00002772static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002773 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002774
2775 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00002776 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002777
Nate Begeman9008ca62009-04-27 18:41:29 +00002778 int Half = NumElems / 2;
2779 for (int i = 0; i < Half; ++i)
2780 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002781 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002782 for (int i = Half; i < NumElems; ++i)
2783 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002784 return false;
2785 return true;
2786}
2787
Nate Begeman9008ca62009-04-27 18:41:29 +00002788static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
2789 SmallVector<int, 8> M;
2790 N->getMask(M);
2791 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002792}
2793
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002794/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2795/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002796bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
2797 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002798 return false;
2799
Evan Cheng2064a2b2006-03-28 06:50:32 +00002800 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00002801 return isUndefOrEqual(N->getMaskElt(0), 6) &&
2802 isUndefOrEqual(N->getMaskElt(1), 7) &&
2803 isUndefOrEqual(N->getMaskElt(2), 2) &&
2804 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002805}
2806
Nate Begeman0b10b912009-11-07 23:17:15 +00002807/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2808/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2809/// <2, 3, 2, 3>
2810bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
2811 unsigned NumElems = N->getValueType(0).getVectorNumElements();
2812
2813 if (NumElems != 4)
2814 return false;
2815
2816 return isUndefOrEqual(N->getMaskElt(0), 2) &&
2817 isUndefOrEqual(N->getMaskElt(1), 3) &&
2818 isUndefOrEqual(N->getMaskElt(2), 2) &&
2819 isUndefOrEqual(N->getMaskElt(3), 3);
2820}
2821
Evan Cheng5ced1d82006-04-06 23:23:56 +00002822/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2823/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00002824bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
2825 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002826
Evan Cheng5ced1d82006-04-06 23:23:56 +00002827 if (NumElems != 2 && NumElems != 4)
2828 return false;
2829
Evan Chengc5cdff22006-04-07 21:53:05 +00002830 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002831 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002832 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002833
Evan Chengc5cdff22006-04-07 21:53:05 +00002834 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002835 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002836 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002837
2838 return true;
2839}
2840
Nate Begeman0b10b912009-11-07 23:17:15 +00002841/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
2842/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
2843bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002844 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002845
Evan Cheng5ced1d82006-04-06 23:23:56 +00002846 if (NumElems != 2 && NumElems != 4)
2847 return false;
2848
Evan Chengc5cdff22006-04-07 21:53:05 +00002849 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002850 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002851 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002852
Nate Begeman9008ca62009-04-27 18:41:29 +00002853 for (unsigned i = 0; i < NumElems/2; ++i)
2854 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002855 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002856
2857 return true;
2858}
2859
Evan Cheng0038e592006-03-28 00:39:58 +00002860/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2861/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00002862static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002863 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002864 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002865 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00002866 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002867
Nate Begeman9008ca62009-04-27 18:41:29 +00002868 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2869 int BitI = Mask[i];
2870 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002871 if (!isUndefOrEqual(BitI, j))
2872 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002873 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002874 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002875 return false;
2876 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002877 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002878 return false;
2879 }
Evan Cheng0038e592006-03-28 00:39:58 +00002880 }
Evan Cheng0038e592006-03-28 00:39:58 +00002881 return true;
2882}
2883
Nate Begeman9008ca62009-04-27 18:41:29 +00002884bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2885 SmallVector<int, 8> M;
2886 N->getMask(M);
2887 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002888}
2889
Evan Cheng4fcb9222006-03-28 02:43:26 +00002890/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2891/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00002892static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002893 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002894 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002895 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00002896 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002897
Nate Begeman9008ca62009-04-27 18:41:29 +00002898 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2899 int BitI = Mask[i];
2900 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00002901 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00002902 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002903 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002904 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002905 return false;
2906 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002907 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002908 return false;
2909 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002910 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002911 return true;
2912}
2913
Nate Begeman9008ca62009-04-27 18:41:29 +00002914bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2915 SmallVector<int, 8> M;
2916 N->getMask(M);
2917 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002918}
2919
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002920/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2921/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2922/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00002923static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002924 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002925 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002926 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002927
Nate Begeman9008ca62009-04-27 18:41:29 +00002928 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
2929 int BitI = Mask[i];
2930 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002931 if (!isUndefOrEqual(BitI, j))
2932 return false;
2933 if (!isUndefOrEqual(BitI1, j))
2934 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002935 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002936 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002937}
2938
Nate Begeman9008ca62009-04-27 18:41:29 +00002939bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
2940 SmallVector<int, 8> M;
2941 N->getMask(M);
2942 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
2943}
2944
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002945/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2946/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2947/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00002948static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002949 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002950 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2951 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002952
Nate Begeman9008ca62009-04-27 18:41:29 +00002953 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2954 int BitI = Mask[i];
2955 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002956 if (!isUndefOrEqual(BitI, j))
2957 return false;
2958 if (!isUndefOrEqual(BitI1, j))
2959 return false;
2960 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002961 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002962}
2963
Nate Begeman9008ca62009-04-27 18:41:29 +00002964bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
2965 SmallVector<int, 8> M;
2966 N->getMask(M);
2967 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
2968}
2969
Evan Cheng017dcc62006-04-21 01:05:10 +00002970/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2971/// specifies a shuffle of elements that is suitable for input to MOVSS,
2972/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00002973static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00002974 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002975 return false;
Eli Friedman10415532009-06-06 06:05:10 +00002976
2977 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002978
Nate Begeman9008ca62009-04-27 18:41:29 +00002979 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002980 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002981
Nate Begeman9008ca62009-04-27 18:41:29 +00002982 for (int i = 1; i < NumElts; ++i)
2983 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002984 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002985
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002986 return true;
2987}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002988
Nate Begeman9008ca62009-04-27 18:41:29 +00002989bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
2990 SmallVector<int, 8> M;
2991 N->getMask(M);
2992 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002993}
2994
Evan Cheng017dcc62006-04-21 01:05:10 +00002995/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2996/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00002997/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00002998static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002999 bool V2IsSplat = false, bool V2IsUndef = false) {
3000 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003001 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003002 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003003
Nate Begeman9008ca62009-04-27 18:41:29 +00003004 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003005 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003006
Nate Begeman9008ca62009-04-27 18:41:29 +00003007 for (int i = 1; i < NumOps; ++i)
3008 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3009 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3010 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003011 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003012
Evan Cheng39623da2006-04-20 08:58:49 +00003013 return true;
3014}
3015
Nate Begeman9008ca62009-04-27 18:41:29 +00003016static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003017 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003018 SmallVector<int, 8> M;
3019 N->getMask(M);
3020 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003021}
3022
Evan Chengd9539472006-04-14 21:59:03 +00003023/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3024/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003025bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
3026 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003027 return false;
3028
3029 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00003030 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003031 int Elt = N->getMaskElt(i);
3032 if (Elt >= 0 && Elt != 1)
3033 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00003034 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003035
3036 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003037 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003038 int Elt = N->getMaskElt(i);
3039 if (Elt >= 0 && Elt != 3)
3040 return false;
3041 if (Elt == 3)
3042 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003043 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003044 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00003045 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003046 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003047}
3048
3049/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3050/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003051bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
3052 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003053 return false;
3054
3055 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00003056 for (unsigned i = 0; i < 2; ++i)
3057 if (N->getMaskElt(i) > 0)
3058 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003059
3060 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003061 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003062 int Elt = N->getMaskElt(i);
3063 if (Elt >= 0 && Elt != 2)
3064 return false;
3065 if (Elt == 2)
3066 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003067 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003068 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003069 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003070}
3071
Evan Cheng0b457f02008-09-25 20:50:48 +00003072/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3073/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003074bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3075 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003076
Nate Begeman9008ca62009-04-27 18:41:29 +00003077 for (int i = 0; i < e; ++i)
3078 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003079 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003080 for (int i = 0; i < e; ++i)
3081 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003082 return false;
3083 return true;
3084}
3085
Evan Cheng63d33002006-03-22 08:01:21 +00003086/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003087/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003088unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003089 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3090 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3091
Evan Chengb9df0ca2006-03-22 02:53:00 +00003092 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3093 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003094 for (int i = 0; i < NumOperands; ++i) {
3095 int Val = SVOp->getMaskElt(NumOperands-i-1);
3096 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003097 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003098 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003099 if (i != NumOperands - 1)
3100 Mask <<= Shift;
3101 }
Evan Cheng63d33002006-03-22 08:01:21 +00003102 return Mask;
3103}
3104
Evan Cheng506d3df2006-03-29 23:07:14 +00003105/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003106/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003107unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003108 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003109 unsigned Mask = 0;
3110 // 8 nodes, but we only care about the last 4.
3111 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003112 int Val = SVOp->getMaskElt(i);
3113 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003114 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003115 if (i != 4)
3116 Mask <<= 2;
3117 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003118 return Mask;
3119}
3120
3121/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003122/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003123unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003124 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003125 unsigned Mask = 0;
3126 // 8 nodes, but we only care about the first 4.
3127 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003128 int Val = SVOp->getMaskElt(i);
3129 if (Val >= 0)
3130 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003131 if (i != 0)
3132 Mask <<= 2;
3133 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003134 return Mask;
3135}
3136
Nate Begemana09008b2009-10-19 02:17:23 +00003137/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3138/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3139unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3140 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3141 EVT VVT = N->getValueType(0);
3142 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3143 int Val = 0;
3144
3145 unsigned i, e;
3146 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3147 Val = SVOp->getMaskElt(i);
3148 if (Val >= 0)
3149 break;
3150 }
3151 return (Val - i) * EltSize;
3152}
3153
Evan Cheng37b73872009-07-30 08:33:02 +00003154/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3155/// constant +0.0.
3156bool X86::isZeroNode(SDValue Elt) {
3157 return ((isa<ConstantSDNode>(Elt) &&
3158 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
3159 (isa<ConstantFPSDNode>(Elt) &&
3160 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3161}
3162
Nate Begeman9008ca62009-04-27 18:41:29 +00003163/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3164/// their permute mask.
3165static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3166 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003167 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003168 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003169 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003170
Nate Begeman5a5ca152009-04-29 05:20:52 +00003171 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003172 int idx = SVOp->getMaskElt(i);
3173 if (idx < 0)
3174 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003175 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003176 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003177 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003178 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003179 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003180 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3181 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003182}
3183
Evan Cheng779ccea2007-12-07 21:30:01 +00003184/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3185/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003186static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003187 unsigned NumElems = VT.getVectorNumElements();
3188 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003189 int idx = Mask[i];
3190 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003191 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003192 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003193 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003194 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003195 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003196 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003197}
3198
Evan Cheng533a0aa2006-04-19 20:35:22 +00003199/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3200/// match movhlps. The lower half elements should come from upper half of
3201/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003202/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003203static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3204 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003205 return false;
3206 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003207 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003208 return false;
3209 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003210 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003211 return false;
3212 return true;
3213}
3214
Evan Cheng5ced1d82006-04-06 23:23:56 +00003215/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003216/// is promoted to a vector. It also returns the LoadSDNode by reference if
3217/// required.
3218static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003219 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3220 return false;
3221 N = N->getOperand(0).getNode();
3222 if (!ISD::isNON_EXTLoad(N))
3223 return false;
3224 if (LD)
3225 *LD = cast<LoadSDNode>(N);
3226 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003227}
3228
Evan Cheng533a0aa2006-04-19 20:35:22 +00003229/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3230/// match movlp{s|d}. The lower half elements should come from lower half of
3231/// V1 (and in order), and the upper half elements should come from the upper
3232/// half of V2 (and in order). And since V1 will become the source of the
3233/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003234static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3235 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003236 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003237 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003238 // Is V2 is a vector load, don't do this transformation. We will try to use
3239 // load folding shufps op.
3240 if (ISD::isNON_EXTLoad(V2))
3241 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003242
Nate Begeman5a5ca152009-04-29 05:20:52 +00003243 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003244
Evan Cheng533a0aa2006-04-19 20:35:22 +00003245 if (NumElems != 2 && NumElems != 4)
3246 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003247 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003248 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003249 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003250 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003251 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003252 return false;
3253 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003254}
3255
Evan Cheng39623da2006-04-20 08:58:49 +00003256/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3257/// all the same.
3258static bool isSplatVector(SDNode *N) {
3259 if (N->getOpcode() != ISD::BUILD_VECTOR)
3260 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003261
Dan Gohman475871a2008-07-27 21:46:04 +00003262 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003263 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3264 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003265 return false;
3266 return true;
3267}
3268
Evan Cheng213d2cf2007-05-17 18:45:50 +00003269/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003270/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003271/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003272static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003273 SDValue V1 = N->getOperand(0);
3274 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003275 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3276 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003277 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003278 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003279 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003280 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3281 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003282 if (Opc != ISD::BUILD_VECTOR ||
3283 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003284 return false;
3285 } else if (Idx >= 0) {
3286 unsigned Opc = V1.getOpcode();
3287 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3288 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003289 if (Opc != ISD::BUILD_VECTOR ||
3290 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003291 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003292 }
3293 }
3294 return true;
3295}
3296
3297/// getZeroVector - Returns a vector of specified type with all zero elements.
3298///
Owen Andersone50ed302009-08-10 22:56:29 +00003299static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003300 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003301 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003302
Chris Lattner8a594482007-11-25 00:24:49 +00003303 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3304 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003305 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003306 if (VT.getSizeInBits() == 64) { // MMX
Owen Anderson825b72b2009-08-11 20:47:22 +00003307 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3308 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003309 } else if (HasSSE2) { // SSE2
Owen Anderson825b72b2009-08-11 20:47:22 +00003310 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3311 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003312 } else { // SSE1
Owen Anderson825b72b2009-08-11 20:47:22 +00003313 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3314 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003315 }
Dale Johannesenace16102009-02-03 19:33:06 +00003316 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003317}
3318
Chris Lattner8a594482007-11-25 00:24:49 +00003319/// getOnesVector - Returns a vector of specified type with all bits set.
3320///
Owen Andersone50ed302009-08-10 22:56:29 +00003321static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003322 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003323
Chris Lattner8a594482007-11-25 00:24:49 +00003324 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3325 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003326 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003327 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003328 if (VT.getSizeInBits() == 64) // MMX
Owen Anderson825b72b2009-08-11 20:47:22 +00003329 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Chris Lattner8a594482007-11-25 00:24:49 +00003330 else // SSE
Owen Anderson825b72b2009-08-11 20:47:22 +00003331 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Dale Johannesenace16102009-02-03 19:33:06 +00003332 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003333}
3334
3335
Evan Cheng39623da2006-04-20 08:58:49 +00003336/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3337/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003338static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003339 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003340 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003341
Evan Cheng39623da2006-04-20 08:58:49 +00003342 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003343 SmallVector<int, 8> MaskVec;
3344 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003345
Nate Begeman5a5ca152009-04-29 05:20:52 +00003346 for (unsigned i = 0; i != NumElems; ++i) {
3347 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003348 MaskVec[i] = NumElems;
3349 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003350 }
Evan Cheng39623da2006-04-20 08:58:49 +00003351 }
Evan Cheng39623da2006-04-20 08:58:49 +00003352 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003353 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3354 SVOp->getOperand(1), &MaskVec[0]);
3355 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003356}
3357
Evan Cheng017dcc62006-04-21 01:05:10 +00003358/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3359/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003360static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003361 SDValue V2) {
3362 unsigned NumElems = VT.getVectorNumElements();
3363 SmallVector<int, 8> Mask;
3364 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003365 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003366 Mask.push_back(i);
3367 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003368}
3369
Nate Begeman9008ca62009-04-27 18:41:29 +00003370/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003371static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003372 SDValue V2) {
3373 unsigned NumElems = VT.getVectorNumElements();
3374 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003375 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003376 Mask.push_back(i);
3377 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003378 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003379 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003380}
3381
Nate Begeman9008ca62009-04-27 18:41:29 +00003382/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003383static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003384 SDValue V2) {
3385 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003386 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003387 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003388 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003389 Mask.push_back(i + Half);
3390 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003391 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003392 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003393}
3394
Evan Cheng0c0f83f2008-04-05 00:30:36 +00003395/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Eric Christopherfd179292009-08-27 18:07:15 +00003396static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00003397 bool HasSSE2) {
3398 if (SV->getValueType(0).getVectorNumElements() <= 4)
3399 return SDValue(SV, 0);
Eric Christopherfd179292009-08-27 18:07:15 +00003400
Owen Anderson825b72b2009-08-11 20:47:22 +00003401 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003402 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003403 DebugLoc dl = SV->getDebugLoc();
3404 SDValue V1 = SV->getOperand(0);
3405 int NumElems = VT.getVectorNumElements();
3406 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003407
Nate Begeman9008ca62009-04-27 18:41:29 +00003408 // unpack elements to the correct location
3409 while (NumElems > 4) {
3410 if (EltNo < NumElems/2) {
3411 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3412 } else {
3413 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3414 EltNo -= NumElems/2;
3415 }
3416 NumElems >>= 1;
3417 }
Eric Christopherfd179292009-08-27 18:07:15 +00003418
Nate Begeman9008ca62009-04-27 18:41:29 +00003419 // Perform the splat.
3420 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Dale Johannesenace16102009-02-03 19:33:06 +00003421 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003422 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
3423 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003424}
3425
Evan Chengba05f722006-04-21 23:03:30 +00003426/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003427/// vector of zero or undef vector. This produces a shuffle where the low
3428/// element of V2 is swizzled into the zero/undef vector, landing at element
3429/// 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 +00003430static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003431 bool isZero, bool HasSSE2,
3432 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003433 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003434 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003435 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3436 unsigned NumElems = VT.getVectorNumElements();
3437 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003438 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003439 // If this is the insertion idx, put the low elt of V2 here.
3440 MaskVec.push_back(i == Idx ? NumElems : i);
3441 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003442}
3443
Evan Chengf26ffe92008-05-29 08:22:04 +00003444/// getNumOfConsecutiveZeros - Return the number of elements in a result of
3445/// a shuffle that is zero.
3446static
Nate Begeman9008ca62009-04-27 18:41:29 +00003447unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, int NumElems,
3448 bool Low, SelectionDAG &DAG) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003449 unsigned NumZeros = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003450 for (int i = 0; i < NumElems; ++i) {
Evan Chengab262272008-06-25 20:52:59 +00003451 unsigned Index = Low ? i : NumElems-i-1;
Nate Begeman9008ca62009-04-27 18:41:29 +00003452 int Idx = SVOp->getMaskElt(Index);
3453 if (Idx < 0) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003454 ++NumZeros;
3455 continue;
3456 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003457 SDValue Elt = DAG.getShuffleScalarElt(SVOp, Index);
Evan Cheng37b73872009-07-30 08:33:02 +00003458 if (Elt.getNode() && X86::isZeroNode(Elt))
Evan Chengf26ffe92008-05-29 08:22:04 +00003459 ++NumZeros;
3460 else
3461 break;
3462 }
3463 return NumZeros;
3464}
3465
3466/// isVectorShift - Returns true if the shuffle can be implemented as a
3467/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003468/// FIXME: split into pslldqi, psrldqi, palignr variants.
3469static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00003470 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
John McCallb1fb4492010-04-07 01:49:15 +00003471 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
Evan Chengf26ffe92008-05-29 08:22:04 +00003472
3473 isLeft = true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003474 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, true, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003475 if (!NumZeros) {
3476 isLeft = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003477 NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, false, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003478 if (!NumZeros)
3479 return false;
3480 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003481 bool SeenV1 = false;
3482 bool SeenV2 = false;
John McCallb1fb4492010-04-07 01:49:15 +00003483 for (unsigned i = NumZeros; i < NumElems; ++i) {
3484 unsigned Val = isLeft ? (i - NumZeros) : i;
3485 int Idx_ = SVOp->getMaskElt(isLeft ? i : (i - NumZeros));
3486 if (Idx_ < 0)
Evan Chengf26ffe92008-05-29 08:22:04 +00003487 continue;
John McCallb1fb4492010-04-07 01:49:15 +00003488 unsigned Idx = (unsigned) Idx_;
Nate Begeman9008ca62009-04-27 18:41:29 +00003489 if (Idx < NumElems)
Evan Chengf26ffe92008-05-29 08:22:04 +00003490 SeenV1 = true;
3491 else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003492 Idx -= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00003493 SeenV2 = true;
3494 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003495 if (Idx != Val)
Evan Chengf26ffe92008-05-29 08:22:04 +00003496 return false;
3497 }
3498 if (SeenV1 && SeenV2)
3499 return false;
3500
Nate Begeman9008ca62009-04-27 18:41:29 +00003501 ShVal = SeenV1 ? SVOp->getOperand(0) : SVOp->getOperand(1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003502 ShAmt = NumZeros;
3503 return true;
3504}
3505
3506
Evan Chengc78d3b42006-04-24 18:01:45 +00003507/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3508///
Dan Gohman475871a2008-07-27 21:46:04 +00003509static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003510 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00003511 SelectionDAG &DAG,
3512 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003513 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00003514 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003515
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003516 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003517 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003518 bool First = true;
3519 for (unsigned i = 0; i < 16; ++i) {
3520 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3521 if (ThisIsNonZero && First) {
3522 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003523 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003524 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003525 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003526 First = false;
3527 }
3528
3529 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00003530 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003531 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3532 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003533 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003534 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00003535 }
3536 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003537 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
3538 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
3539 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00003540 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003541 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00003542 } else
3543 ThisElt = LastElt;
3544
Gabor Greifba36cb52008-08-28 21:40:38 +00003545 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00003546 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00003547 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00003548 }
3549 }
3550
Owen Anderson825b72b2009-08-11 20:47:22 +00003551 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00003552}
3553
Bill Wendlinga348c562007-03-22 18:42:45 +00003554/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003555///
Dan Gohman475871a2008-07-27 21:46:04 +00003556static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00003557 unsigned NumNonZero, unsigned NumZero,
3558 SelectionDAG &DAG,
3559 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003560 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00003561 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003562
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003563 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003564 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003565 bool First = true;
3566 for (unsigned i = 0; i < 8; ++i) {
3567 bool isNonZero = (NonZeros & (1 << i)) != 0;
3568 if (isNonZero) {
3569 if (First) {
3570 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003571 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003572 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003573 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003574 First = false;
3575 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003576 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003577 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00003578 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00003579 }
3580 }
3581
3582 return V;
3583}
3584
Evan Chengf26ffe92008-05-29 08:22:04 +00003585/// getVShift - Return a vector logical shift node.
3586///
Owen Andersone50ed302009-08-10 22:56:29 +00003587static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00003588 unsigned NumBits, SelectionDAG &DAG,
3589 const TargetLowering &TLI, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003590 bool isMMX = VT.getSizeInBits() == 64;
Owen Anderson825b72b2009-08-11 20:47:22 +00003591 EVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00003592 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Dale Johannesenace16102009-02-03 19:33:06 +00003593 SrcOp = DAG.getNode(ISD::BIT_CONVERT, dl, ShVT, SrcOp);
3594 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3595 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00003596 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00003597}
3598
Dan Gohman475871a2008-07-27 21:46:04 +00003599SDValue
Evan Chengc3630942009-12-09 21:00:30 +00003600X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00003601 SelectionDAG &DAG) const {
Evan Chengc3630942009-12-09 21:00:30 +00003602
3603 // Check if the scalar load can be widened into a vector load. And if
3604 // the address is "base + cst" see if the cst can be "absorbed" into
3605 // the shuffle mask.
3606 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
3607 SDValue Ptr = LD->getBasePtr();
3608 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
3609 return SDValue();
3610 EVT PVT = LD->getValueType(0);
3611 if (PVT != MVT::i32 && PVT != MVT::f32)
3612 return SDValue();
3613
3614 int FI = -1;
3615 int64_t Offset = 0;
3616 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
3617 FI = FINode->getIndex();
3618 Offset = 0;
3619 } else if (Ptr.getOpcode() == ISD::ADD &&
3620 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
3621 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
3622 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
3623 Offset = Ptr.getConstantOperandVal(1);
3624 Ptr = Ptr.getOperand(0);
3625 } else {
3626 return SDValue();
3627 }
3628
3629 SDValue Chain = LD->getChain();
3630 // Make sure the stack object alignment is at least 16.
3631 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3632 if (DAG.InferPtrAlignment(Ptr) < 16) {
3633 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00003634 // Can't change the alignment. FIXME: It's possible to compute
3635 // the exact stack offset and reference FI + adjust offset instead.
3636 // If someone *really* cares about this. That's the way to implement it.
3637 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00003638 } else {
3639 MFI->setObjectAlignment(FI, 16);
3640 }
3641 }
3642
3643 // (Offset % 16) must be multiple of 4. Then address is then
3644 // Ptr + (Offset & ~15).
3645 if (Offset < 0)
3646 return SDValue();
3647 if ((Offset % 16) & 3)
3648 return SDValue();
3649 int64_t StartOffset = Offset & ~15;
3650 if (StartOffset)
3651 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
3652 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
3653
3654 int EltNo = (Offset - StartOffset) >> 2;
3655 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
3656 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
David Greene67c9d422010-02-15 16:53:33 +00003657 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,LD->getSrcValue(),0,
3658 false, false, 0);
Evan Chengc3630942009-12-09 21:00:30 +00003659 // Canonicalize it to a v4i32 shuffle.
3660 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32, V1);
3661 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3662 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
3663 DAG.getUNDEF(MVT::v4i32), &Mask[0]));
3664 }
3665
3666 return SDValue();
3667}
3668
Nate Begeman1449f292010-03-24 22:19:06 +00003669/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
3670/// vector of type 'VT', see if the elements can be replaced by a single large
3671/// load which has the same value as a build_vector whose operands are 'elts'.
3672///
3673/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
3674///
3675/// FIXME: we'd also like to handle the case where the last elements are zero
3676/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
3677/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00003678static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
3679 DebugLoc &dl, SelectionDAG &DAG) {
3680 EVT EltVT = VT.getVectorElementType();
3681 unsigned NumElems = Elts.size();
3682
Nate Begemanfdea31a2010-03-24 20:49:50 +00003683 LoadSDNode *LDBase = NULL;
3684 unsigned LastLoadedElt = -1U;
Nate Begeman1449f292010-03-24 22:19:06 +00003685
3686 // For each element in the initializer, see if we've found a load or an undef.
3687 // If we don't find an initial load element, or later load elements are
3688 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00003689 for (unsigned i = 0; i < NumElems; ++i) {
3690 SDValue Elt = Elts[i];
3691
3692 if (!Elt.getNode() ||
3693 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
3694 return SDValue();
3695 if (!LDBase) {
3696 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
3697 return SDValue();
3698 LDBase = cast<LoadSDNode>(Elt.getNode());
3699 LastLoadedElt = i;
3700 continue;
3701 }
3702 if (Elt.getOpcode() == ISD::UNDEF)
3703 continue;
3704
3705 LoadSDNode *LD = cast<LoadSDNode>(Elt);
3706 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
3707 return SDValue();
3708 LastLoadedElt = i;
3709 }
Nate Begeman1449f292010-03-24 22:19:06 +00003710
3711 // If we have found an entire vector of loads and undefs, then return a large
3712 // load of the entire vector width starting at the base pointer. If we found
3713 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00003714 if (LastLoadedElt == NumElems - 1) {
3715 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
3716 return DAG.getLoad(VT, dl, LDBase->getChain(), LDBase->getBasePtr(),
3717 LDBase->getSrcValue(), LDBase->getSrcValueOffset(),
3718 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
3719 return DAG.getLoad(VT, dl, LDBase->getChain(), LDBase->getBasePtr(),
3720 LDBase->getSrcValue(), LDBase->getSrcValueOffset(),
3721 LDBase->isVolatile(), LDBase->isNonTemporal(),
3722 LDBase->getAlignment());
3723 } else if (NumElems == 4 && LastLoadedElt == 1) {
3724 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
3725 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
3726 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2);
3727 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, ResNode);
3728 }
3729 return SDValue();
3730}
3731
Evan Chengc3630942009-12-09 21:00:30 +00003732SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00003733X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003734 DebugLoc dl = Op.getDebugLoc();
Chris Lattner8a594482007-11-25 00:24:49 +00003735 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif327ef032008-08-28 23:19:51 +00003736 if (ISD::isBuildVectorAllZeros(Op.getNode())
3737 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattner8a594482007-11-25 00:24:49 +00003738 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3739 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3740 // eliminated on x86-32 hosts.
Owen Anderson825b72b2009-08-11 20:47:22 +00003741 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
Chris Lattner8a594482007-11-25 00:24:49 +00003742 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003743
Gabor Greifba36cb52008-08-28 21:40:38 +00003744 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00003745 return getOnesVector(Op.getValueType(), DAG, dl);
3746 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00003747 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003748
Owen Andersone50ed302009-08-10 22:56:29 +00003749 EVT VT = Op.getValueType();
3750 EVT ExtVT = VT.getVectorElementType();
3751 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003752
3753 unsigned NumElems = Op.getNumOperands();
3754 unsigned NumZero = 0;
3755 unsigned NumNonZero = 0;
3756 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003757 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00003758 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003759 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003760 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00003761 if (Elt.getOpcode() == ISD::UNDEF)
3762 continue;
3763 Values.insert(Elt);
3764 if (Elt.getOpcode() != ISD::Constant &&
3765 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003766 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00003767 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00003768 NumZero++;
3769 else {
3770 NonZeros |= (1 << i);
3771 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003772 }
3773 }
3774
Dan Gohman7f321562007-06-25 16:23:39 +00003775 if (NumNonZero == 0) {
Chris Lattner8a594482007-11-25 00:24:49 +00003776 // All undef vector. Return an UNDEF. All zero vectors were handled above.
Dale Johannesene8d72302009-02-06 23:05:02 +00003777 return DAG.getUNDEF(VT);
Dan Gohman7f321562007-06-25 16:23:39 +00003778 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003779
Chris Lattner67f453a2008-03-09 05:42:06 +00003780 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00003781 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003782 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00003783 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00003784
Chris Lattner62098042008-03-09 01:05:04 +00003785 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3786 // the value are obviously zero, truncate the value to i32 and do the
3787 // insertion that way. Only do this if the value is non-constant or if the
3788 // value is a constant being inserted into element 0. It is cheaper to do
3789 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00003790 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00003791 (!IsAllConstants || Idx == 0)) {
3792 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3793 // Handle MMX and SSE both.
Owen Anderson825b72b2009-08-11 20:47:22 +00003794 EVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3795 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Scott Michelfdc40a02009-02-17 22:15:04 +00003796
Chris Lattner62098042008-03-09 01:05:04 +00003797 // Truncate the value (which may itself be a constant) to i32, and
3798 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00003799 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00003800 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00003801 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3802 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00003803
Chris Lattner62098042008-03-09 01:05:04 +00003804 // Now we have our 32-bit value zero extended in the low element of
3805 // a vector. If Idx != 0, swizzle it into place.
3806 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003807 SmallVector<int, 4> Mask;
3808 Mask.push_back(Idx);
3809 for (unsigned i = 1; i != VecElts; ++i)
3810 Mask.push_back(i);
3811 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00003812 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00003813 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003814 }
Dale Johannesenace16102009-02-03 19:33:06 +00003815 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00003816 }
3817 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003818
Chris Lattner19f79692008-03-08 22:59:52 +00003819 // If we have a constant or non-constant insertion into the low element of
3820 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3821 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00003822 // depending on what the source datatype is.
3823 if (Idx == 0) {
3824 if (NumZero == 0) {
3825 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00003826 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
3827 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00003828 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
3829 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
3830 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
3831 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00003832 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
3833 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
3834 EVT MiddleVT = VT.getSizeInBits() == 64 ? MVT::v2i32 : MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00003835 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
3836 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3837 Subtarget->hasSSE2(), DAG);
3838 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Item);
3839 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003840 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003841
3842 // Is it a vector logical left shift?
3843 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00003844 X86::isZeroNode(Op.getOperand(0)) &&
3845 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003846 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00003847 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00003848 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00003849 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00003850 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00003851 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003852
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003853 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00003854 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003855
Chris Lattner19f79692008-03-08 22:59:52 +00003856 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3857 // is a non-constant being inserted into an element other than the low one,
3858 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3859 // movd/movss) to move this into the low element, then shuffle it into
3860 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003861 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00003862 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00003863
Evan Cheng0db9fe62006-04-25 20:13:52 +00003864 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003865 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3866 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00003867 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003868 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00003869 MaskVec.push_back(i == Idx ? 0 : 1);
3870 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003871 }
3872 }
3873
Chris Lattner67f453a2008-03-09 05:42:06 +00003874 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00003875 if (Values.size() == 1) {
3876 if (EVTBits == 32) {
3877 // Instead of a shuffle like this:
3878 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
3879 // Check if it's possible to issue this instead.
3880 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
3881 unsigned Idx = CountTrailingZeros_32(NonZeros);
3882 SDValue Item = Op.getOperand(Idx);
3883 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
3884 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
3885 }
Dan Gohman475871a2008-07-27 21:46:04 +00003886 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00003887 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003888
Dan Gohmana3941172007-07-24 22:55:08 +00003889 // A vector full of immediates; various special cases are already
3890 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003891 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00003892 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00003893
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003894 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003895 if (EVTBits == 64) {
3896 if (NumNonZero == 1) {
3897 // One half is zero or undef.
3898 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00003899 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003900 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00003901 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3902 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00003903 }
Dan Gohman475871a2008-07-27 21:46:04 +00003904 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00003905 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003906
3907 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00003908 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00003909 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003910 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003911 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003912 }
3913
Bill Wendling826f36f2007-03-28 00:57:11 +00003914 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00003915 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003916 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003917 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003918 }
3919
3920 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003921 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00003922 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003923 if (NumElems == 4 && NumZero > 0) {
3924 for (unsigned i = 0; i < 4; ++i) {
3925 bool isZero = !(NonZeros & (1 << i));
3926 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003927 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003928 else
Dale Johannesenace16102009-02-03 19:33:06 +00003929 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003930 }
3931
3932 for (unsigned i = 0; i < 2; ++i) {
3933 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3934 default: break;
3935 case 0:
3936 V[i] = V[i*2]; // Must be a zero vector.
3937 break;
3938 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00003939 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003940 break;
3941 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00003942 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003943 break;
3944 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00003945 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003946 break;
3947 }
3948 }
3949
Nate Begeman9008ca62009-04-27 18:41:29 +00003950 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003951 bool Reverse = (NonZeros & 0x3) == 2;
3952 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003953 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003954 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3955 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003956 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
3957 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003958 }
3959
Nate Begemanfdea31a2010-03-24 20:49:50 +00003960 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
3961 // Check for a build vector of consecutive loads.
3962 for (unsigned i = 0; i < NumElems; ++i)
3963 V[i] = Op.getOperand(i);
3964
3965 // Check for elements which are consecutive loads.
3966 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
3967 if (LD.getNode())
3968 return LD;
3969
3970 // For SSE 4.1, use inserts into undef.
3971 if (getSubtarget()->hasSSE41()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003972 V[0] = DAG.getUNDEF(VT);
3973 for (unsigned i = 0; i < NumElems; ++i)
3974 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
3975 V[0] = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, V[0],
3976 Op.getOperand(i), DAG.getIntPtrConstant(i));
3977 return V[0];
3978 }
Nate Begemanfdea31a2010-03-24 20:49:50 +00003979
3980 // Otherwise, expand into a number of unpckl*
Evan Cheng0db9fe62006-04-25 20:13:52 +00003981 // e.g. for v4f32
3982 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3983 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3984 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Evan Cheng0db9fe62006-04-25 20:13:52 +00003985 for (unsigned i = 0; i < NumElems; ++i)
Dale Johannesenace16102009-02-03 19:33:06 +00003986 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003987 NumElems >>= 1;
3988 while (NumElems != 0) {
3989 for (unsigned i = 0; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003990 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + NumElems]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003991 NumElems >>= 1;
3992 }
3993 return V[0];
3994 }
Dan Gohman475871a2008-07-27 21:46:04 +00003995 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003996}
3997
Mon P Wangeb38ebf2010-01-24 00:05:03 +00003998SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00003999X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004000 // We support concatenate two MMX registers and place them in a MMX
4001 // register. This is better than doing a stack convert.
4002 DebugLoc dl = Op.getDebugLoc();
4003 EVT ResVT = Op.getValueType();
4004 assert(Op.getNumOperands() == 2);
4005 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
4006 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
4007 int Mask[2];
4008 SDValue InVec = DAG.getNode(ISD::BIT_CONVERT,dl, MVT::v1i64, Op.getOperand(0));
4009 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4010 InVec = Op.getOperand(1);
4011 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4012 unsigned NumElts = ResVT.getVectorNumElements();
4013 VecOp = DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
4014 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
4015 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
4016 } else {
4017 InVec = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v1i64, InVec);
4018 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4019 Mask[0] = 0; Mask[1] = 2;
4020 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
4021 }
4022 return DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
4023}
4024
Nate Begemanb9a47b82009-02-23 08:49:38 +00004025// v8i16 shuffles - Prefer shuffles in the following order:
4026// 1. [all] pshuflw, pshufhw, optional move
4027// 2. [ssse3] 1 x pshufb
4028// 3. [ssse3] 2 x pshufb + 1 x por
4029// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004030static
Nate Begeman9008ca62009-04-27 18:41:29 +00004031SDValue LowerVECTOR_SHUFFLEv8i16(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00004032 SelectionDAG &DAG,
4033 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004034 SDValue V1 = SVOp->getOperand(0);
4035 SDValue V2 = SVOp->getOperand(1);
4036 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004037 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00004038
Nate Begemanb9a47b82009-02-23 08:49:38 +00004039 // Determine if more than 1 of the words in each of the low and high quadwords
4040 // of the result come from the same quadword of one of the two inputs. Undef
4041 // mask values count as coming from any quadword, for better codegen.
4042 SmallVector<unsigned, 4> LoQuad(4);
4043 SmallVector<unsigned, 4> HiQuad(4);
4044 BitVector InputQuads(4);
4045 for (unsigned i = 0; i < 8; ++i) {
4046 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00004047 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004048 MaskVals.push_back(EltIdx);
4049 if (EltIdx < 0) {
4050 ++Quad[0];
4051 ++Quad[1];
4052 ++Quad[2];
4053 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00004054 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004055 }
4056 ++Quad[EltIdx / 4];
4057 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00004058 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004059
Nate Begemanb9a47b82009-02-23 08:49:38 +00004060 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004061 unsigned MaxQuad = 1;
4062 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004063 if (LoQuad[i] > MaxQuad) {
4064 BestLoQuad = i;
4065 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004066 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004067 }
4068
Nate Begemanb9a47b82009-02-23 08:49:38 +00004069 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004070 MaxQuad = 1;
4071 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004072 if (HiQuad[i] > MaxQuad) {
4073 BestHiQuad = i;
4074 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004075 }
4076 }
4077
Nate Begemanb9a47b82009-02-23 08:49:38 +00004078 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00004079 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00004080 // single pshufb instruction is necessary. If There are more than 2 input
4081 // quads, disable the next transformation since it does not help SSSE3.
4082 bool V1Used = InputQuads[0] || InputQuads[1];
4083 bool V2Used = InputQuads[2] || InputQuads[3];
4084 if (TLI.getSubtarget()->hasSSSE3()) {
4085 if (InputQuads.count() == 2 && V1Used && V2Used) {
4086 BestLoQuad = InputQuads.find_first();
4087 BestHiQuad = InputQuads.find_next(BestLoQuad);
4088 }
4089 if (InputQuads.count() > 2) {
4090 BestLoQuad = -1;
4091 BestHiQuad = -1;
4092 }
4093 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004094
Nate Begemanb9a47b82009-02-23 08:49:38 +00004095 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
4096 // the shuffle mask. If a quad is scored as -1, that means that it contains
4097 // words from all 4 input quadwords.
4098 SDValue NewV;
4099 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004100 SmallVector<int, 8> MaskV;
4101 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
4102 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00004103 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004104 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V1),
4105 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V2), &MaskV[0]);
4106 NewV = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004107
Nate Begemanb9a47b82009-02-23 08:49:38 +00004108 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
4109 // source words for the shuffle, to aid later transformations.
4110 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00004111 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00004112 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004113 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00004114 if (idx != (int)i)
4115 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004116 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00004117 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004118 AllWordsInNewV = false;
4119 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00004120 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004121
Nate Begemanb9a47b82009-02-23 08:49:38 +00004122 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
4123 if (AllWordsInNewV) {
4124 for (int i = 0; i != 8; ++i) {
4125 int idx = MaskVals[i];
4126 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004127 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004128 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004129 if ((idx != i) && idx < 4)
4130 pshufhw = false;
4131 if ((idx != i) && idx > 3)
4132 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00004133 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00004134 V1 = NewV;
4135 V2Used = false;
4136 BestLoQuad = 0;
4137 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004138 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004139
Nate Begemanb9a47b82009-02-23 08:49:38 +00004140 // If we've eliminated the use of V2, and the new mask is a pshuflw or
4141 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00004142 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Eric Christopherfd179292009-08-27 18:07:15 +00004143 return DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00004144 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Evan Cheng14b32e12007-12-11 01:46:18 +00004145 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004146 }
Eric Christopherfd179292009-08-27 18:07:15 +00004147
Nate Begemanb9a47b82009-02-23 08:49:38 +00004148 // If we have SSSE3, and all words of the result are from 1 input vector,
4149 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
4150 // is present, fall back to case 4.
4151 if (TLI.getSubtarget()->hasSSSE3()) {
4152 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004153
Nate Begemanb9a47b82009-02-23 08:49:38 +00004154 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00004155 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00004156 // mask, and elements that come from V1 in the V2 mask, so that the two
4157 // results can be OR'd together.
4158 bool TwoInputs = V1Used && V2Used;
4159 for (unsigned i = 0; i != 8; ++i) {
4160 int EltIdx = MaskVals[i] * 2;
4161 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004162 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4163 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004164 continue;
4165 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004166 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
4167 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004168 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004169 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004170 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004171 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004172 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004173 if (!TwoInputs)
Owen Anderson825b72b2009-08-11 20:47:22 +00004174 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004175
Nate Begemanb9a47b82009-02-23 08:49:38 +00004176 // Calculate the shuffle mask for the second input, shuffle it, and
4177 // OR it with the first shuffled input.
4178 pshufbMask.clear();
4179 for (unsigned i = 0; i != 8; ++i) {
4180 int EltIdx = MaskVals[i] * 2;
4181 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004182 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4183 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004184 continue;
4185 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004186 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
4187 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004188 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004189 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00004190 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004191 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004192 MVT::v16i8, &pshufbMask[0], 16));
4193 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
4194 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004195 }
4196
4197 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
4198 // and update MaskVals with new element order.
4199 BitVector InOrder(8);
4200 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004201 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004202 for (int i = 0; i != 4; ++i) {
4203 int idx = MaskVals[i];
4204 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004205 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004206 InOrder.set(i);
4207 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004208 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004209 InOrder.set(i);
4210 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004211 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004212 }
4213 }
4214 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004215 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00004216 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004217 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004218 }
Eric Christopherfd179292009-08-27 18:07:15 +00004219
Nate Begemanb9a47b82009-02-23 08:49:38 +00004220 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
4221 // and update MaskVals with the new element order.
4222 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004223 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004224 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004225 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004226 for (unsigned i = 4; i != 8; ++i) {
4227 int idx = MaskVals[i];
4228 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004229 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004230 InOrder.set(i);
4231 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004232 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004233 InOrder.set(i);
4234 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004235 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004236 }
4237 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004238 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004239 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004240 }
Eric Christopherfd179292009-08-27 18:07:15 +00004241
Nate Begemanb9a47b82009-02-23 08:49:38 +00004242 // In case BestHi & BestLo were both -1, which means each quadword has a word
4243 // from each of the four input quadwords, calculate the InOrder bitvector now
4244 // before falling through to the insert/extract cleanup.
4245 if (BestLoQuad == -1 && BestHiQuad == -1) {
4246 NewV = V1;
4247 for (int i = 0; i != 8; ++i)
4248 if (MaskVals[i] < 0 || MaskVals[i] == i)
4249 InOrder.set(i);
4250 }
Eric Christopherfd179292009-08-27 18:07:15 +00004251
Nate Begemanb9a47b82009-02-23 08:49:38 +00004252 // The other elements are put in the right place using pextrw and pinsrw.
4253 for (unsigned i = 0; i != 8; ++i) {
4254 if (InOrder[i])
4255 continue;
4256 int EltIdx = MaskVals[i];
4257 if (EltIdx < 0)
4258 continue;
4259 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004260 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004261 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00004262 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004263 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004264 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004265 DAG.getIntPtrConstant(i));
4266 }
4267 return NewV;
4268}
4269
4270// v16i8 shuffles - Prefer shuffles in the following order:
4271// 1. [ssse3] 1 x pshufb
4272// 2. [ssse3] 2 x pshufb + 1 x por
4273// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
4274static
Nate Begeman9008ca62009-04-27 18:41:29 +00004275SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00004276 SelectionDAG &DAG,
4277 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004278 SDValue V1 = SVOp->getOperand(0);
4279 SDValue V2 = SVOp->getOperand(1);
4280 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004281 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00004282 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00004283
Nate Begemanb9a47b82009-02-23 08:49:38 +00004284 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00004285 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00004286 // present, fall back to case 3.
4287 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
4288 bool V1Only = true;
4289 bool V2Only = true;
4290 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004291 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00004292 if (EltIdx < 0)
4293 continue;
4294 if (EltIdx < 16)
4295 V2Only = false;
4296 else
4297 V1Only = false;
4298 }
Eric Christopherfd179292009-08-27 18:07:15 +00004299
Nate Begemanb9a47b82009-02-23 08:49:38 +00004300 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
4301 if (TLI.getSubtarget()->hasSSSE3()) {
4302 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004303
Nate Begemanb9a47b82009-02-23 08:49:38 +00004304 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00004305 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004306 //
4307 // Otherwise, we have elements from both input vectors, and must zero out
4308 // elements that come from V2 in the first mask, and V1 in the second mask
4309 // so that we can OR them together.
4310 bool TwoInputs = !(V1Only || V2Only);
4311 for (unsigned i = 0; i != 16; ++i) {
4312 int EltIdx = MaskVals[i];
4313 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004314 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004315 continue;
4316 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004317 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004318 }
4319 // If all the elements are from V2, assign it to V1 and return after
4320 // building the first pshufb.
4321 if (V2Only)
4322 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00004323 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004324 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004325 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004326 if (!TwoInputs)
4327 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00004328
Nate Begemanb9a47b82009-02-23 08:49:38 +00004329 // Calculate the shuffle mask for the second input, shuffle it, and
4330 // OR it with the first shuffled input.
4331 pshufbMask.clear();
4332 for (unsigned i = 0; i != 16; ++i) {
4333 int EltIdx = MaskVals[i];
4334 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004335 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004336 continue;
4337 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004338 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004339 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004340 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004341 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004342 MVT::v16i8, &pshufbMask[0], 16));
4343 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004344 }
Eric Christopherfd179292009-08-27 18:07:15 +00004345
Nate Begemanb9a47b82009-02-23 08:49:38 +00004346 // No SSSE3 - Calculate in place words and then fix all out of place words
4347 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
4348 // the 16 different words that comprise the two doublequadword input vectors.
Owen Anderson825b72b2009-08-11 20:47:22 +00004349 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
4350 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004351 SDValue NewV = V2Only ? V2 : V1;
4352 for (int i = 0; i != 8; ++i) {
4353 int Elt0 = MaskVals[i*2];
4354 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00004355
Nate Begemanb9a47b82009-02-23 08:49:38 +00004356 // This word of the result is all undef, skip it.
4357 if (Elt0 < 0 && Elt1 < 0)
4358 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004359
Nate Begemanb9a47b82009-02-23 08:49:38 +00004360 // This word of the result is already in the correct place, skip it.
4361 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
4362 continue;
4363 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
4364 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004365
Nate Begemanb9a47b82009-02-23 08:49:38 +00004366 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
4367 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
4368 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00004369
4370 // If Elt0 and Elt1 are defined, are consecutive, and can be load
4371 // using a single extract together, load it and store it.
4372 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004373 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004374 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00004375 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004376 DAG.getIntPtrConstant(i));
4377 continue;
4378 }
4379
Nate Begemanb9a47b82009-02-23 08:49:38 +00004380 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00004381 // source byte is not also odd, shift the extracted word left 8 bits
4382 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004383 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004384 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004385 DAG.getIntPtrConstant(Elt1 / 2));
4386 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004387 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004388 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004389 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004390 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
4391 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004392 }
4393 // If Elt0 is defined, extract it from the appropriate source. If the
4394 // source byte is not also even, shift the extracted word right 8 bits. If
4395 // Elt1 was also defined, OR the extracted values together before
4396 // inserting them in the result.
4397 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004398 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004399 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
4400 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004401 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004402 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004403 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004404 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
4405 DAG.getConstant(0x00FF, MVT::i16));
4406 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00004407 : InsElt0;
4408 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004409 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004410 DAG.getIntPtrConstant(i));
4411 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004412 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004413}
4414
Evan Cheng7a831ce2007-12-15 03:00:47 +00004415/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
4416/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
4417/// done when every pair / quad of shuffle mask elements point to elements in
4418/// the right sequence. e.g.
Evan Cheng14b32e12007-12-11 01:46:18 +00004419/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
4420static
Nate Begeman9008ca62009-04-27 18:41:29 +00004421SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
4422 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00004423 const TargetLowering &TLI, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00004424 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00004425 SDValue V1 = SVOp->getOperand(0);
4426 SDValue V2 = SVOp->getOperand(1);
4427 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00004428 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Owen Anderson825b72b2009-08-11 20:47:22 +00004429 EVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Owen Andersone50ed302009-08-10 22:56:29 +00004430 EVT MaskEltVT = MaskVT.getVectorElementType();
4431 EVT NewVT = MaskVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00004432 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004433 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004434 case MVT::v4f32: NewVT = MVT::v2f64; break;
4435 case MVT::v4i32: NewVT = MVT::v2i64; break;
4436 case MVT::v8i16: NewVT = MVT::v4i32; break;
4437 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004438 }
4439
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004440 if (NewWidth == 2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004441 if (VT.isInteger())
Owen Anderson825b72b2009-08-11 20:47:22 +00004442 NewVT = MVT::v2i64;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004443 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004444 NewVT = MVT::v2f64;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004445 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004446 int Scale = NumElems / NewWidth;
4447 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00004448 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004449 int StartIdx = -1;
4450 for (int j = 0; j < Scale; ++j) {
4451 int EltIdx = SVOp->getMaskElt(i+j);
4452 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004453 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00004454 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00004455 StartIdx = EltIdx - (EltIdx % Scale);
4456 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00004457 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004458 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004459 if (StartIdx == -1)
4460 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00004461 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004462 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004463 }
4464
Dale Johannesenace16102009-02-03 19:33:06 +00004465 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V1);
4466 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00004467 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004468}
4469
Evan Chengd880b972008-05-09 21:53:03 +00004470/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004471///
Owen Andersone50ed302009-08-10 22:56:29 +00004472static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00004473 SDValue SrcOp, SelectionDAG &DAG,
4474 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004475 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004476 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00004477 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00004478 LD = dyn_cast<LoadSDNode>(SrcOp);
4479 if (!LD) {
4480 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
4481 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00004482 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
4483 if ((ExtVT.SimpleTy != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00004484 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4485 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00004486 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004487 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00004488 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Dale Johannesenace16102009-02-03 19:33:06 +00004489 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4490 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
4491 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
4492 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00004493 SrcOp.getOperand(0)
4494 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004495 }
4496 }
4497 }
4498
Dale Johannesenace16102009-02-03 19:33:06 +00004499 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4500 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004501 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00004502 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004503}
4504
Evan Chengace3c172008-07-22 21:13:36 +00004505/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
4506/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004507static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00004508LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
4509 SDValue V1 = SVOp->getOperand(0);
4510 SDValue V2 = SVOp->getOperand(1);
4511 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004512 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00004513
Evan Chengace3c172008-07-22 21:13:36 +00004514 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00004515 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00004516 SmallVector<int, 8> Mask1(4U, -1);
4517 SmallVector<int, 8> PermMask;
4518 SVOp->getMask(PermMask);
4519
Evan Chengace3c172008-07-22 21:13:36 +00004520 unsigned NumHi = 0;
4521 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00004522 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004523 int Idx = PermMask[i];
4524 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004525 Locs[i] = std::make_pair(-1, -1);
4526 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004527 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
4528 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004529 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00004530 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004531 NumLo++;
4532 } else {
4533 Locs[i] = std::make_pair(1, NumHi);
4534 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004535 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004536 NumHi++;
4537 }
4538 }
4539 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004540
Evan Chengace3c172008-07-22 21:13:36 +00004541 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004542 // If no more than two elements come from either vector. This can be
4543 // implemented with two shuffles. First shuffle gather the elements.
4544 // The second shuffle, which takes the first shuffle as both of its
4545 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00004546 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004547
Nate Begeman9008ca62009-04-27 18:41:29 +00004548 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00004549
Evan Chengace3c172008-07-22 21:13:36 +00004550 for (unsigned i = 0; i != 4; ++i) {
4551 if (Locs[i].first == -1)
4552 continue;
4553 else {
4554 unsigned Idx = (i < 2) ? 0 : 4;
4555 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004556 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004557 }
4558 }
4559
Nate Begeman9008ca62009-04-27 18:41:29 +00004560 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004561 } else if (NumLo == 3 || NumHi == 3) {
4562 // Otherwise, we must have three elements from one vector, call it X, and
4563 // one element from the other, call it Y. First, use a shufps to build an
4564 // intermediate vector with the one element from Y and the element from X
4565 // that will be in the same half in the final destination (the indexes don't
4566 // matter). Then, use a shufps to build the final vector, taking the half
4567 // containing the element from Y from the intermediate, and the other half
4568 // from X.
4569 if (NumHi == 3) {
4570 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00004571 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004572 std::swap(V1, V2);
4573 }
4574
4575 // Find the element from V2.
4576 unsigned HiIndex;
4577 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004578 int Val = PermMask[HiIndex];
4579 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004580 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004581 if (Val >= 4)
4582 break;
4583 }
4584
Nate Begeman9008ca62009-04-27 18:41:29 +00004585 Mask1[0] = PermMask[HiIndex];
4586 Mask1[1] = -1;
4587 Mask1[2] = PermMask[HiIndex^1];
4588 Mask1[3] = -1;
4589 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004590
4591 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004592 Mask1[0] = PermMask[0];
4593 Mask1[1] = PermMask[1];
4594 Mask1[2] = HiIndex & 1 ? 6 : 4;
4595 Mask1[3] = HiIndex & 1 ? 4 : 6;
4596 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004597 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004598 Mask1[0] = HiIndex & 1 ? 2 : 0;
4599 Mask1[1] = HiIndex & 1 ? 0 : 2;
4600 Mask1[2] = PermMask[2];
4601 Mask1[3] = PermMask[3];
4602 if (Mask1[2] >= 0)
4603 Mask1[2] += 4;
4604 if (Mask1[3] >= 0)
4605 Mask1[3] += 4;
4606 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004607 }
Evan Chengace3c172008-07-22 21:13:36 +00004608 }
4609
4610 // Break it into (shuffle shuffle_hi, shuffle_lo).
4611 Locs.clear();
Nate Begeman9008ca62009-04-27 18:41:29 +00004612 SmallVector<int,8> LoMask(4U, -1);
4613 SmallVector<int,8> HiMask(4U, -1);
4614
4615 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00004616 unsigned MaskIdx = 0;
4617 unsigned LoIdx = 0;
4618 unsigned HiIdx = 2;
4619 for (unsigned i = 0; i != 4; ++i) {
4620 if (i == 2) {
4621 MaskPtr = &HiMask;
4622 MaskIdx = 1;
4623 LoIdx = 0;
4624 HiIdx = 2;
4625 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004626 int Idx = PermMask[i];
4627 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004628 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00004629 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004630 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004631 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004632 LoIdx++;
4633 } else {
4634 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004635 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004636 HiIdx++;
4637 }
4638 }
4639
Nate Begeman9008ca62009-04-27 18:41:29 +00004640 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
4641 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
4642 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00004643 for (unsigned i = 0; i != 4; ++i) {
4644 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004645 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00004646 } else {
4647 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004648 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00004649 }
4650 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004651 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00004652}
4653
Dan Gohman475871a2008-07-27 21:46:04 +00004654SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004655X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00004656 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00004657 SDValue V1 = Op.getOperand(0);
4658 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00004659 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004660 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00004661 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004662 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004663 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
4664 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00004665 bool V1IsSplat = false;
4666 bool V2IsSplat = false;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004667
Nate Begeman9008ca62009-04-27 18:41:29 +00004668 if (isZeroShuffle(SVOp))
Dale Johannesenace16102009-02-03 19:33:06 +00004669 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004670
Nate Begeman9008ca62009-04-27 18:41:29 +00004671 // Promote splats to v4f32.
4672 if (SVOp->isSplat()) {
Eric Christopherfd179292009-08-27 18:07:15 +00004673 if (isMMX || NumElems < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004674 return Op;
4675 return PromoteSplat(SVOp, DAG, Subtarget->hasSSE2());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004676 }
4677
Evan Cheng7a831ce2007-12-15 03:00:47 +00004678 // If the shuffle can be profitably rewritten as a narrower shuffle, then
4679 // do it!
Owen Anderson825b72b2009-08-11 20:47:22 +00004680 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004681 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004682 if (NewOp.getNode())
Scott Michelfdc40a02009-02-17 22:15:04 +00004683 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004684 LowerVECTOR_SHUFFLE(NewOp, DAG));
Owen Anderson825b72b2009-08-11 20:47:22 +00004685 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Evan Cheng7a831ce2007-12-15 03:00:47 +00004686 // FIXME: Figure out a cleaner way to do this.
4687 // Try to make use of movq to zero out the top part.
Gabor Greifba36cb52008-08-28 21:40:38 +00004688 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004689 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004690 if (NewOp.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004691 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
4692 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
4693 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004694 }
Gabor Greifba36cb52008-08-28 21:40:38 +00004695 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004696 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
4697 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
Evan Chengd880b972008-05-09 21:53:03 +00004698 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Nate Begeman9008ca62009-04-27 18:41:29 +00004699 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004700 }
4701 }
Eric Christopherfd179292009-08-27 18:07:15 +00004702
Nate Begeman9008ca62009-04-27 18:41:29 +00004703 if (X86::isPSHUFDMask(SVOp))
4704 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004705
Evan Chengf26ffe92008-05-29 08:22:04 +00004706 // Check if this can be converted into a logical shift.
4707 bool isLeft = false;
4708 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00004709 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00004710 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00004711 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00004712 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004713 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00004714 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00004715 EVT EltVT = VT.getVectorElementType();
4716 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004717 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004718 }
Eric Christopherfd179292009-08-27 18:07:15 +00004719
Nate Begeman9008ca62009-04-27 18:41:29 +00004720 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004721 if (V1IsUndef)
4722 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00004723 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004724 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Nate Begemanfb8ead02008-07-25 19:05:58 +00004725 if (!isMMX)
4726 return Op;
Evan Cheng7e2ff772008-05-08 00:57:18 +00004727 }
Eric Christopherfd179292009-08-27 18:07:15 +00004728
Nate Begeman9008ca62009-04-27 18:41:29 +00004729 // FIXME: fold these into legal mask.
4730 if (!isMMX && (X86::isMOVSHDUPMask(SVOp) ||
4731 X86::isMOVSLDUPMask(SVOp) ||
4732 X86::isMOVHLPSMask(SVOp) ||
Nate Begeman0b10b912009-11-07 23:17:15 +00004733 X86::isMOVLHPSMask(SVOp) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00004734 X86::isMOVLPMask(SVOp)))
Evan Cheng9bbbb982006-10-25 20:48:19 +00004735 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004736
Nate Begeman9008ca62009-04-27 18:41:29 +00004737 if (ShouldXformToMOVHLPS(SVOp) ||
4738 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
4739 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004740
Evan Chengf26ffe92008-05-29 08:22:04 +00004741 if (isShift) {
4742 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00004743 EVT EltVT = VT.getVectorElementType();
4744 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004745 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004746 }
Eric Christopherfd179292009-08-27 18:07:15 +00004747
Evan Cheng9eca5e82006-10-25 21:49:50 +00004748 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00004749 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4750 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00004751 V1IsSplat = isSplatVector(V1.getNode());
4752 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00004753
Chris Lattner8a594482007-11-25 00:24:49 +00004754 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00004755 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004756 Op = CommuteVectorShuffle(SVOp, DAG);
4757 SVOp = cast<ShuffleVectorSDNode>(Op);
4758 V1 = SVOp->getOperand(0);
4759 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00004760 std::swap(V1IsSplat, V2IsSplat);
4761 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00004762 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00004763 }
4764
Nate Begeman9008ca62009-04-27 18:41:29 +00004765 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
4766 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00004767 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00004768 return V1;
4769 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
4770 // the instruction selector will not match, so get a canonical MOVL with
4771 // swapped operands to undo the commute.
4772 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00004773 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004774
Nate Begeman9008ca62009-04-27 18:41:29 +00004775 if (X86::isUNPCKL_v_undef_Mask(SVOp) ||
4776 X86::isUNPCKH_v_undef_Mask(SVOp) ||
4777 X86::isUNPCKLMask(SVOp) ||
4778 X86::isUNPCKHMask(SVOp))
Evan Chengd9b8e402006-10-16 06:36:00 +00004779 return Op;
Evan Chenge1113032006-10-04 18:33:38 +00004780
Evan Cheng9bbbb982006-10-25 20:48:19 +00004781 if (V2IsSplat) {
4782 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004783 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00004784 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00004785 SDValue NewMask = NormalizeMask(SVOp, DAG);
4786 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
4787 if (NSVOp != SVOp) {
4788 if (X86::isUNPCKLMask(NSVOp, true)) {
4789 return NewMask;
4790 } else if (X86::isUNPCKHMask(NSVOp, true)) {
4791 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004792 }
4793 }
4794 }
4795
Evan Cheng9eca5e82006-10-25 21:49:50 +00004796 if (Commuted) {
4797 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00004798 // FIXME: this seems wrong.
4799 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
4800 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
4801 if (X86::isUNPCKL_v_undef_Mask(NewSVOp) ||
4802 X86::isUNPCKH_v_undef_Mask(NewSVOp) ||
4803 X86::isUNPCKLMask(NewSVOp) ||
4804 X86::isUNPCKHMask(NewSVOp))
4805 return NewOp;
Evan Cheng9eca5e82006-10-25 21:49:50 +00004806 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004807
Nate Begemanb9a47b82009-02-23 08:49:38 +00004808 // FIXME: for mmx, bitcast v2i32 to v4i16 for shuffle.
Nate Begeman9008ca62009-04-27 18:41:29 +00004809
4810 // Normalize the node to match x86 shuffle ops if needed
4811 if (!isMMX && V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
4812 return CommuteVectorShuffle(SVOp, DAG);
4813
4814 // Check for legal shuffle and return?
4815 SmallVector<int, 16> PermMask;
4816 SVOp->getMask(PermMask);
4817 if (isShuffleMaskLegal(PermMask, VT))
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004818 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004819
Evan Cheng14b32e12007-12-11 01:46:18 +00004820 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00004821 if (VT == MVT::v8i16) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004822 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(SVOp, DAG, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004823 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00004824 return NewOp;
4825 }
4826
Owen Anderson825b72b2009-08-11 20:47:22 +00004827 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004828 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004829 if (NewOp.getNode())
4830 return NewOp;
4831 }
Eric Christopherfd179292009-08-27 18:07:15 +00004832
Evan Chengace3c172008-07-22 21:13:36 +00004833 // Handle all 4 wide cases with a number of shuffles except for MMX.
4834 if (NumElems == 4 && !isMMX)
Nate Begeman9008ca62009-04-27 18:41:29 +00004835 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004836
Dan Gohman475871a2008-07-27 21:46:04 +00004837 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004838}
4839
Dan Gohman475871a2008-07-27 21:46:04 +00004840SDValue
4841X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00004842 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00004843 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004844 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004845 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004846 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004847 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004848 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004849 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004850 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004851 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00004852 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
4853 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
4854 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004855 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4856 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004857 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004858 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00004859 Op.getOperand(0)),
4860 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00004861 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004862 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004863 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004864 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004865 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00004866 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00004867 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4868 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004869 // result has a single use which is a store or a bitcast to i32. And in
4870 // the case of a store, it's not worth it if the index is a constant 0,
4871 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00004872 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00004873 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004874 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004875 if ((User->getOpcode() != ISD::STORE ||
4876 (isa<ConstantSDNode>(Op.getOperand(1)) &&
4877 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman171c11e2008-04-16 02:32:24 +00004878 (User->getOpcode() != ISD::BIT_CONVERT ||
Owen Anderson825b72b2009-08-11 20:47:22 +00004879 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00004880 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00004881 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
4882 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004883 Op.getOperand(0)),
4884 Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004885 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Extract);
4886 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00004887 // ExtractPS works with constant index.
4888 if (isa<ConstantSDNode>(Op.getOperand(1)))
4889 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004890 }
Dan Gohman475871a2008-07-27 21:46:04 +00004891 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004892}
4893
4894
Dan Gohman475871a2008-07-27 21:46:04 +00004895SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004896X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
4897 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004898 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00004899 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004900
Evan Cheng62a3f152008-03-24 21:52:23 +00004901 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004902 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004903 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00004904 return Res;
4905 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00004906
Owen Andersone50ed302009-08-10 22:56:29 +00004907 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004908 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004909 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004910 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004911 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004912 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004913 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004914 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4915 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Scott Michelfdc40a02009-02-17 22:15:04 +00004916 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004917 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00004918 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004919 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00004920 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00004921 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004922 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00004923 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004924 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004925 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004926 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004927 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004928 if (Idx == 0)
4929 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004930
Evan Cheng0db9fe62006-04-25 20:13:52 +00004931 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00004932 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00004933 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00004934 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00004935 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004936 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004937 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004938 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004939 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4940 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4941 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004942 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004943 if (Idx == 0)
4944 return Op;
4945
4946 // UNPCKHPD the element to the lowest double word, then movsd.
4947 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4948 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00004949 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00004950 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00004951 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00004952 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004953 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004954 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004955 }
4956
Dan Gohman475871a2008-07-27 21:46:04 +00004957 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004958}
4959
Dan Gohman475871a2008-07-27 21:46:04 +00004960SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004961X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
4962 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00004963 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00004964 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004965 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004966
Dan Gohman475871a2008-07-27 21:46:04 +00004967 SDValue N0 = Op.getOperand(0);
4968 SDValue N1 = Op.getOperand(1);
4969 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004970
Dan Gohman8a55ce42009-09-23 21:02:20 +00004971 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00004972 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00004973 unsigned Opc;
4974 if (VT == MVT::v8i16)
4975 Opc = X86ISD::PINSRW;
4976 else if (VT == MVT::v4i16)
4977 Opc = X86ISD::MMX_PINSRW;
4978 else if (VT == MVT::v16i8)
4979 Opc = X86ISD::PINSRB;
4980 else
4981 Opc = X86ISD::PINSRB;
4982
Nate Begeman14d12ca2008-02-11 04:19:36 +00004983 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4984 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00004985 if (N1.getValueType() != MVT::i32)
4986 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
4987 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004988 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00004989 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004990 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004991 // Bits [7:6] of the constant are the source select. This will always be
4992 // zero here. The DAG Combiner may combine an extract_elt index into these
4993 // bits. For example (insert (extract, 3), 2) could be matched by putting
4994 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00004995 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00004996 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00004997 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00004998 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004999 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00005000 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00005001 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00005002 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00005003 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00005004 // PINSR* works with constant index.
5005 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00005006 }
Dan Gohman475871a2008-07-27 21:46:04 +00005007 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005008}
5009
Dan Gohman475871a2008-07-27 21:46:04 +00005010SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005011X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005012 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00005013 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005014
5015 if (Subtarget->hasSSE41())
5016 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
5017
Dan Gohman8a55ce42009-09-23 21:02:20 +00005018 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00005019 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00005020
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005021 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005022 SDValue N0 = Op.getOperand(0);
5023 SDValue N1 = Op.getOperand(1);
5024 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00005025
Dan Gohman8a55ce42009-09-23 21:02:20 +00005026 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00005027 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
5028 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00005029 if (N1.getValueType() != MVT::i32)
5030 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
5031 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005032 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00005033 return DAG.getNode(VT == MVT::v8i16 ? X86ISD::PINSRW : X86ISD::MMX_PINSRW,
5034 dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005035 }
Dan Gohman475871a2008-07-27 21:46:04 +00005036 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005037}
5038
Dan Gohman475871a2008-07-27 21:46:04 +00005039SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005040X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005041 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00005042 if (Op.getValueType() == MVT::v2f32)
5043 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f32,
5044 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i32,
5045 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32,
Evan Cheng52672b82008-07-22 18:39:19 +00005046 Op.getOperand(0))));
5047
Owen Anderson825b72b2009-08-11 20:47:22 +00005048 if (Op.getValueType() == MVT::v1i64 && Op.getOperand(0).getValueType() == MVT::i64)
5049 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00005050
Owen Anderson825b72b2009-08-11 20:47:22 +00005051 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
5052 EVT VT = MVT::v2i32;
5053 switch (Op.getValueType().getSimpleVT().SimpleTy) {
Evan Chengefec7512008-02-18 23:04:32 +00005054 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00005055 case MVT::v16i8:
5056 case MVT::v8i16:
5057 VT = MVT::v4i32;
Evan Chengefec7512008-02-18 23:04:32 +00005058 break;
5059 }
Dale Johannesenace16102009-02-03 19:33:06 +00005060 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(),
5061 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005062}
5063
Bill Wendling056292f2008-09-16 21:48:12 +00005064// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
5065// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
5066// one of the above mentioned nodes. It has to be wrapped because otherwise
5067// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
5068// be used to form addressing mode. These wrapped nodes will be selected
5069// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00005070SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005071X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005072 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00005073
Chris Lattner41621a22009-06-26 19:22:52 +00005074 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5075 // global base reg.
5076 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005077 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005078 CodeModel::Model M = getTargetMachine().getCodeModel();
5079
Chris Lattner4f066492009-07-11 20:29:19 +00005080 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005081 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005082 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005083 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005084 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005085 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005086 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005087
Evan Cheng1606e8e2009-03-13 07:51:59 +00005088 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00005089 CP->getAlignment(),
5090 CP->getOffset(), OpFlag);
5091 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00005092 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005093 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00005094 if (OpFlag) {
5095 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005096 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005097 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005098 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005099 }
5100
5101 return Result;
5102}
5103
Dan Gohmand858e902010-04-17 15:26:15 +00005104SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00005105 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00005106
Chris Lattner18c59872009-06-27 04:16:01 +00005107 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5108 // global base reg.
5109 unsigned char OpFlag = 0;
5110 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005111 CodeModel::Model M = getTargetMachine().getCodeModel();
5112
Chris Lattner4f066492009-07-11 20:29:19 +00005113 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005114 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005115 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005116 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005117 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005118 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005119 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005120
Chris Lattner18c59872009-06-27 04:16:01 +00005121 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
5122 OpFlag);
5123 DebugLoc DL = JT->getDebugLoc();
5124 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005125
Chris Lattner18c59872009-06-27 04:16:01 +00005126 // With PIC, the address is actually $g + Offset.
5127 if (OpFlag) {
5128 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5129 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005130 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00005131 Result);
5132 }
Eric Christopherfd179292009-08-27 18:07:15 +00005133
Chris Lattner18c59872009-06-27 04:16:01 +00005134 return Result;
5135}
5136
5137SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005138X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00005139 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00005140
Chris Lattner18c59872009-06-27 04:16:01 +00005141 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5142 // global base reg.
5143 unsigned char OpFlag = 0;
5144 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005145 CodeModel::Model M = getTargetMachine().getCodeModel();
5146
Chris Lattner4f066492009-07-11 20:29:19 +00005147 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005148 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005149 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005150 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005151 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005152 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005153 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005154
Chris Lattner18c59872009-06-27 04:16:01 +00005155 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00005156
Chris Lattner18c59872009-06-27 04:16:01 +00005157 DebugLoc DL = Op.getDebugLoc();
5158 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005159
5160
Chris Lattner18c59872009-06-27 04:16:01 +00005161 // With PIC, the address is actually $g + Offset.
5162 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00005163 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00005164 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5165 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005166 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00005167 Result);
5168 }
Eric Christopherfd179292009-08-27 18:07:15 +00005169
Chris Lattner18c59872009-06-27 04:16:01 +00005170 return Result;
5171}
5172
Dan Gohman475871a2008-07-27 21:46:04 +00005173SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005174X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00005175 // Create the TargetBlockAddressAddress node.
5176 unsigned char OpFlags =
5177 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00005178 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00005179 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00005180 DebugLoc dl = Op.getDebugLoc();
5181 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
5182 /*isTarget=*/true, OpFlags);
5183
Dan Gohmanf705adb2009-10-30 01:28:02 +00005184 if (Subtarget->isPICStyleRIPRel() &&
5185 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00005186 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
5187 else
5188 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00005189
Dan Gohman29cbade2009-11-20 23:18:13 +00005190 // With PIC, the address is actually $g + Offset.
5191 if (isGlobalRelativeToPICBase(OpFlags)) {
5192 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5193 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
5194 Result);
5195 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00005196
5197 return Result;
5198}
5199
5200SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00005201X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00005202 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00005203 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00005204 // Create the TargetGlobalAddress node, folding in the constant
5205 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00005206 unsigned char OpFlags =
5207 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005208 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00005209 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005210 if (OpFlags == X86II::MO_NO_FLAG &&
5211 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00005212 // A direct static reference to a global.
Dale Johannesen60b3ba02009-07-21 00:12:29 +00005213 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00005214 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005215 } else {
Chris Lattnerb1acd682009-06-27 05:39:56 +00005216 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00005217 }
Eric Christopherfd179292009-08-27 18:07:15 +00005218
Chris Lattner4f066492009-07-11 20:29:19 +00005219 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005220 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00005221 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
5222 else
5223 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00005224
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005225 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00005226 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00005227 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5228 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005229 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005230 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005231
Chris Lattner36c25012009-07-10 07:34:39 +00005232 // For globals that require a load from a stub to get the address, emit the
5233 // load.
5234 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00005235 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
David Greene67c9d422010-02-15 16:53:33 +00005236 PseudoSourceValue::getGOT(), 0, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005237
Dan Gohman6520e202008-10-18 02:06:02 +00005238 // If there was a non-zero offset that we didn't fold, create an explicit
5239 // addition for it.
5240 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00005241 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00005242 DAG.getConstant(Offset, getPointerTy()));
5243
Evan Cheng0db9fe62006-04-25 20:13:52 +00005244 return Result;
5245}
5246
Evan Chengda43bcf2008-09-24 00:05:32 +00005247SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005248X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00005249 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00005250 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005251 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00005252}
5253
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005254static SDValue
5255GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00005256 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00005257 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00005258 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Owen Anderson825b72b2009-08-11 20:47:22 +00005259 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005260 DebugLoc dl = GA->getDebugLoc();
5261 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
5262 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00005263 GA->getOffset(),
5264 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005265 if (InFlag) {
5266 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00005267 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005268 } else {
5269 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00005270 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005271 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00005272
5273 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00005274 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00005275
Rafael Espindola15f1b662009-04-24 12:59:40 +00005276 SDValue Flag = Chain.getValue(1);
5277 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005278}
5279
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005280// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00005281static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005282LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005283 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005284 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00005285 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
5286 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005287 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005288 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005289 InFlag = Chain.getValue(1);
5290
Chris Lattnerb903bed2009-06-26 21:20:29 +00005291 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005292}
5293
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005294// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00005295static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005296LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005297 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00005298 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
5299 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005300}
5301
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005302// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
5303// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00005304static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005305 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00005306 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00005307 DebugLoc dl = GA->getDebugLoc();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005308 // Get the Thread Pointer
Rafael Espindola094fad32009-04-08 21:14:34 +00005309 SDValue Base = DAG.getNode(X86ISD::SegmentBaseAddress,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005310 DebugLoc(), PtrVT,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00005311 DAG.getRegister(is64Bit? X86::FS : X86::GS,
Owen Anderson825b72b2009-08-11 20:47:22 +00005312 MVT::i32));
Rafael Espindola094fad32009-04-08 21:14:34 +00005313
5314 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Base,
David Greene67c9d422010-02-15 16:53:33 +00005315 NULL, 0, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00005316
Chris Lattnerb903bed2009-06-26 21:20:29 +00005317 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005318 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
5319 // initialexec.
5320 unsigned WrapperKind = X86ISD::Wrapper;
5321 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00005322 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00005323 } else if (is64Bit) {
5324 assert(model == TLSModel::InitialExec);
5325 OperandFlags = X86II::MO_GOTTPOFF;
5326 WrapperKind = X86ISD::WrapperRIP;
5327 } else {
5328 assert(model == TLSModel::InitialExec);
5329 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00005330 }
Eric Christopherfd179292009-08-27 18:07:15 +00005331
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005332 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
5333 // exec)
Chris Lattner4150c082009-06-21 02:22:34 +00005334 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00005335 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00005336 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00005337
Rafael Espindola9a580232009-02-27 13:37:18 +00005338 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00005339 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
David Greene67c9d422010-02-15 16:53:33 +00005340 PseudoSourceValue::getGOT(), 0, false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00005341
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005342 // The address of the thread local variable is the add of the thread
5343 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00005344 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005345}
5346
Dan Gohman475871a2008-07-27 21:46:04 +00005347SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005348X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005349 // TODO: implement the "local dynamic" model
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00005350 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005351 assert(Subtarget->isTargetELF() &&
5352 "TLS not implemented for non-ELF targets");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005353 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00005354 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00005355
Chris Lattnerb903bed2009-06-26 21:20:29 +00005356 // If GV is an alias then use the aliasee for determining
5357 // thread-localness.
5358 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
5359 GV = GA->resolveAliasedGlobal(false);
Eric Christopherfd179292009-08-27 18:07:15 +00005360
Chris Lattnerb903bed2009-06-26 21:20:29 +00005361 TLSModel::Model model = getTLSModel(GV,
5362 getTargetMachine().getRelocationModel());
Eric Christopherfd179292009-08-27 18:07:15 +00005363
Chris Lattnerb903bed2009-06-26 21:20:29 +00005364 switch (model) {
5365 case TLSModel::GeneralDynamic:
5366 case TLSModel::LocalDynamic: // not implemented
5367 if (Subtarget->is64Bit())
Rafael Espindola9a580232009-02-27 13:37:18 +00005368 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
Chris Lattnerb903bed2009-06-26 21:20:29 +00005369 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00005370
Chris Lattnerb903bed2009-06-26 21:20:29 +00005371 case TLSModel::InitialExec:
5372 case TLSModel::LocalExec:
5373 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
5374 Subtarget->is64Bit());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005375 }
Eric Christopherfd179292009-08-27 18:07:15 +00005376
Torok Edwinc23197a2009-07-14 16:55:14 +00005377 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00005378 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005379}
5380
Evan Cheng0db9fe62006-04-25 20:13:52 +00005381
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005382/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00005383/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00005384SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00005385 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00005386 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005387 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005388 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005389 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00005390 SDValue ShOpLo = Op.getOperand(0);
5391 SDValue ShOpHi = Op.getOperand(1);
5392 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00005393 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00005394 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00005395 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00005396
Dan Gohman475871a2008-07-27 21:46:04 +00005397 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005398 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00005399 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
5400 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005401 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005402 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
5403 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005404 }
Evan Chenge3413162006-01-09 18:33:28 +00005405
Owen Anderson825b72b2009-08-11 20:47:22 +00005406 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
5407 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00005408 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00005409 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00005410
Dan Gohman475871a2008-07-27 21:46:04 +00005411 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00005412 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00005413 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
5414 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00005415
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005416 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00005417 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
5418 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005419 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005420 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
5421 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005422 }
5423
Dan Gohman475871a2008-07-27 21:46:04 +00005424 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00005425 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005426}
Evan Chenga3195e82006-01-12 22:54:21 +00005427
Dan Gohmand858e902010-04-17 15:26:15 +00005428SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
5429 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005430 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00005431
5432 if (SrcVT.isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005433 if (SrcVT == MVT::v2i32 && Op.getValueType() == MVT::v2f64) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005434 return Op;
5435 }
5436 return SDValue();
5437 }
5438
Owen Anderson825b72b2009-08-11 20:47:22 +00005439 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00005440 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00005441
Eli Friedman36df4992009-05-27 00:47:34 +00005442 // These are really Legal; return the operand so the caller accepts it as
5443 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00005444 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00005445 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00005446 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00005447 Subtarget->is64Bit()) {
5448 return Op;
5449 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005450
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005451 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005452 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005453 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005454 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005455 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00005456 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00005457 StackSlot,
David Greene67c9d422010-02-15 16:53:33 +00005458 PseudoSourceValue::getFixedStack(SSFI), 0,
5459 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00005460 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
5461}
Evan Cheng0db9fe62006-04-25 20:13:52 +00005462
Owen Andersone50ed302009-08-10 22:56:29 +00005463SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Dale Johannesen8d908eb2010-05-15 18:51:12 +00005464 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00005465 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005466 // Build the FILD
Eli Friedman948e95a2009-05-23 09:59:16 +00005467 DebugLoc dl = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00005468 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00005469 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005470 if (useSSE)
Owen Anderson825b72b2009-08-11 20:47:22 +00005471 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00005472 else
Owen Anderson825b72b2009-08-11 20:47:22 +00005473 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005474 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Dale Johannesenace16102009-02-03 19:33:06 +00005475 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD, dl,
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005476 Tys, Ops, array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005477
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005478 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005479 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00005480 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005481
5482 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
5483 // shouldn't be necessary except that RFP cannot be live across
5484 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005485 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005486 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005487 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00005488 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005489 SDValue Ops[] = {
5490 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
5491 };
5492 Chain = DAG.getNode(X86ISD::FST, dl, Tys, Ops, array_lengthof(Ops));
Dale Johannesenace16102009-02-03 19:33:06 +00005493 Result = DAG.getLoad(Op.getValueType(), dl, Chain, StackSlot,
David Greene67c9d422010-02-15 16:53:33 +00005494 PseudoSourceValue::getFixedStack(SSFI), 0,
5495 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005496 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005497
Evan Cheng0db9fe62006-04-25 20:13:52 +00005498 return Result;
5499}
5500
Bill Wendling8b8a6362009-01-17 03:56:04 +00005501// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00005502SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
5503 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00005504 // This algorithm is not obvious. Here it is in C code, more or less:
5505 /*
5506 double uint64_to_double( uint32_t hi, uint32_t lo ) {
5507 static const __m128i exp = { 0x4330000045300000ULL, 0 };
5508 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00005509
Bill Wendling8b8a6362009-01-17 03:56:04 +00005510 // Copy ints to xmm registers.
5511 __m128i xh = _mm_cvtsi32_si128( hi );
5512 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00005513
Bill Wendling8b8a6362009-01-17 03:56:04 +00005514 // Combine into low half of a single xmm register.
5515 __m128i x = _mm_unpacklo_epi32( xh, xl );
5516 __m128d d;
5517 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00005518
Bill Wendling8b8a6362009-01-17 03:56:04 +00005519 // Merge in appropriate exponents to give the integer bits the right
5520 // magnitude.
5521 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00005522
Bill Wendling8b8a6362009-01-17 03:56:04 +00005523 // Subtract away the biases to deal with the IEEE-754 double precision
5524 // implicit 1.
5525 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00005526
Bill Wendling8b8a6362009-01-17 03:56:04 +00005527 // All conversions up to here are exact. The correctly rounded result is
5528 // calculated using the current rounding mode using the following
5529 // horizontal add.
5530 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
5531 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
5532 // store doesn't really need to be here (except
5533 // maybe to zero the other double)
5534 return sd;
5535 }
5536 */
Dale Johannesen040225f2008-10-21 23:07:49 +00005537
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005538 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00005539 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00005540
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005541 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00005542 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00005543 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
5544 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
5545 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
5546 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00005547 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005548 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005549
Bill Wendling8b8a6362009-01-17 03:56:04 +00005550 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00005551 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005552 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00005553 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005554 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00005555 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005556 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005557
Owen Anderson825b72b2009-08-11 20:47:22 +00005558 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5559 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00005560 Op.getOperand(0),
5561 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005562 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5563 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00005564 Op.getOperand(0),
5565 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005566 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
5567 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005568 PseudoSourceValue::getConstantPool(), 0,
David Greene67c9d422010-02-15 16:53:33 +00005569 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00005570 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
5571 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Unpck2);
5572 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005573 PseudoSourceValue::getConstantPool(), 0,
David Greene67c9d422010-02-15 16:53:33 +00005574 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00005575 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005576
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005577 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00005578 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00005579 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
5580 DAG.getUNDEF(MVT::v2f64), ShufMask);
5581 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
5582 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005583 DAG.getIntPtrConstant(0));
5584}
5585
Bill Wendling8b8a6362009-01-17 03:56:04 +00005586// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00005587SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
5588 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005589 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00005590 // FP constant to bias correct the final result.
5591 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00005592 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005593
5594 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00005595 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5596 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005597 Op.getOperand(0),
5598 DAG.getIntPtrConstant(0)));
5599
Owen Anderson825b72b2009-08-11 20:47:22 +00005600 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5601 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00005602 DAG.getIntPtrConstant(0));
5603
5604 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00005605 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
5606 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005607 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005608 MVT::v2f64, Load)),
5609 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005610 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005611 MVT::v2f64, Bias)));
5612 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5613 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00005614 DAG.getIntPtrConstant(0));
5615
5616 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00005617 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005618
5619 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00005620 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00005621
Owen Anderson825b72b2009-08-11 20:47:22 +00005622 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005623 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00005624 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00005625 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005626 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00005627 }
5628
5629 // Handle final rounding.
5630 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00005631}
5632
Dan Gohmand858e902010-04-17 15:26:15 +00005633SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
5634 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00005635 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005636 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00005637
Dale Johannesen8d908eb2010-05-15 18:51:12 +00005638 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00005639 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
5640 // the optimization here.
5641 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00005642 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00005643
Owen Andersone50ed302009-08-10 22:56:29 +00005644 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00005645 EVT DstVT = Op.getValueType();
5646 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00005647 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00005648 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00005649 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00005650
5651 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00005652 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00005653 if (SrcVT == MVT::i32) {
5654 SDValue WordOff = DAG.getConstant(4, getPointerTy());
5655 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
5656 getPointerTy(), StackSlot, WordOff);
5657 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
5658 StackSlot, NULL, 0, false, false, 0);
5659 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
5660 OffsetSlot, NULL, 0, false, false, 0);
5661 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
5662 return Fild;
5663 }
5664
5665 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
5666 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
David Greene67c9d422010-02-15 16:53:33 +00005667 StackSlot, NULL, 0, false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00005668 // For i64 source, we need to add the appropriate power of 2 if the input
5669 // was negative. This is the same as the optimization in
5670 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
5671 // we must be careful to do the computation in x87 extended precision, not
5672 // in SSE. (The generic code can't know it's OK to do this, or how to.)
5673 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
5674 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
5675 SDValue Fild = DAG.getNode(X86ISD::FILD, dl, Tys, Ops, 3);
5676
5677 APInt FF(32, 0x5F800000ULL);
5678
5679 // Check whether the sign bit is set.
5680 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
5681 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
5682 ISD::SETLT);
5683
5684 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
5685 SDValue FudgePtr = DAG.getConstantPool(
5686 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
5687 getPointerTy());
5688
5689 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
5690 SDValue Zero = DAG.getIntPtrConstant(0);
5691 SDValue Four = DAG.getIntPtrConstant(4);
5692 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
5693 Zero, Four);
5694 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
5695
5696 // Load the value out, extending it from f32 to f80.
5697 // FIXME: Avoid the extend by constructing the right constant pool?
5698 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
5699 FudgePtr, PseudoSourceValue::getConstantPool(),
5700 0, MVT::f32, false, false, 4);
5701 // Extend everything to 80 bits to force it to be done on x87.
5702 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
5703 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00005704}
5705
Dan Gohman475871a2008-07-27 21:46:04 +00005706std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00005707FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005708 DebugLoc dl = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00005709
Owen Andersone50ed302009-08-10 22:56:29 +00005710 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00005711
5712 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005713 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
5714 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00005715 }
5716
Owen Anderson825b72b2009-08-11 20:47:22 +00005717 assert(DstTy.getSimpleVT() <= MVT::i64 &&
5718 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00005719 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00005720
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005721 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00005722 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00005723 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005724 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00005725 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00005726 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00005727 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005728 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005729
Evan Cheng87c89352007-10-15 20:11:21 +00005730 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
5731 // stack slot.
5732 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00005733 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00005734 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005735 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00005736
Evan Cheng0db9fe62006-04-25 20:13:52 +00005737 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00005738 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005739 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005740 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
5741 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
5742 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005743 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005744
Dan Gohman475871a2008-07-27 21:46:04 +00005745 SDValue Chain = DAG.getEntryNode();
5746 SDValue Value = Op.getOperand(0);
Chris Lattner78631162008-01-16 06:24:21 +00005747 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005748 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dale Johannesenace16102009-02-03 19:33:06 +00005749 Chain = DAG.getStore(Chain, dl, Value, StackSlot,
David Greene67c9d422010-02-15 16:53:33 +00005750 PseudoSourceValue::getFixedStack(SSFI), 0,
5751 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005752 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00005753 SDValue Ops[] = {
Chris Lattner5a88b832007-02-25 07:10:00 +00005754 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
5755 };
Dale Johannesenace16102009-02-03 19:33:06 +00005756 Value = DAG.getNode(X86ISD::FLD, dl, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005757 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00005758 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005759 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5760 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005761
Evan Cheng0db9fe62006-04-25 20:13:52 +00005762 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00005763 SDValue Ops[] = { Chain, Value, StackSlot };
Owen Anderson825b72b2009-08-11 20:47:22 +00005764 SDValue FIST = DAG.getNode(Opc, dl, MVT::Other, Ops, 3);
Evan Chengd9558e02006-01-06 00:43:03 +00005765
Chris Lattner27a6c732007-11-24 07:07:01 +00005766 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005767}
5768
Dan Gohmand858e902010-04-17 15:26:15 +00005769SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
5770 SelectionDAG &DAG) const {
Eli Friedman23ef1052009-06-06 03:57:58 +00005771 if (Op.getValueType().isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005772 if (Op.getValueType() == MVT::v2i32 &&
5773 Op.getOperand(0).getValueType() == MVT::v2f64) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005774 return Op;
5775 }
5776 return SDValue();
5777 }
5778
Eli Friedman948e95a2009-05-23 09:59:16 +00005779 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00005780 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00005781 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
5782 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00005783
Chris Lattner27a6c732007-11-24 07:07:01 +00005784 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005785 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
David Greene67c9d422010-02-15 16:53:33 +00005786 FIST, StackSlot, NULL, 0, false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00005787}
5788
Dan Gohmand858e902010-04-17 15:26:15 +00005789SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
5790 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00005791 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
5792 SDValue FIST = Vals.first, StackSlot = Vals.second;
5793 assert(FIST.getNode() && "Unexpected failure");
5794
5795 // Load the result.
5796 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
David Greene67c9d422010-02-15 16:53:33 +00005797 FIST, StackSlot, NULL, 0, false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00005798}
5799
Dan Gohmand858e902010-04-17 15:26:15 +00005800SDValue X86TargetLowering::LowerFABS(SDValue Op,
5801 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005802 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005803 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005804 EVT VT = Op.getValueType();
5805 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005806 if (VT.isVector())
5807 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005808 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005809 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005810 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00005811 CV.push_back(C);
5812 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005813 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005814 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00005815 CV.push_back(C);
5816 CV.push_back(C);
5817 CV.push_back(C);
5818 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005819 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005820 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005821 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005822 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005823 PseudoSourceValue::getConstantPool(), 0,
5824 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005825 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005826}
5827
Dan Gohmand858e902010-04-17 15:26:15 +00005828SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005829 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005830 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005831 EVT VT = Op.getValueType();
5832 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00005833 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00005834 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005835 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005836 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005837 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00005838 CV.push_back(C);
5839 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005840 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005841 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00005842 CV.push_back(C);
5843 CV.push_back(C);
5844 CV.push_back(C);
5845 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005846 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005847 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005848 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005849 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005850 PseudoSourceValue::getConstantPool(), 0,
5851 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005852 if (VT.isVector()) {
Dale Johannesenace16102009-02-03 19:33:06 +00005853 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00005854 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
5855 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005856 Op.getOperand(0)),
Owen Anderson825b72b2009-08-11 20:47:22 +00005857 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00005858 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005859 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00005860 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005861}
5862
Dan Gohmand858e902010-04-17 15:26:15 +00005863SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005864 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00005865 SDValue Op0 = Op.getOperand(0);
5866 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005867 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005868 EVT VT = Op.getValueType();
5869 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00005870
5871 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005872 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005873 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005874 SrcVT = VT;
5875 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005876 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005877 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005878 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005879 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005880 }
5881
5882 // At this point the operands and the result should have the same
5883 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00005884
Evan Cheng68c47cb2007-01-05 07:55:56 +00005885 // First get the sign bit of second operand.
5886 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005887 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005888 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
5889 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005890 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005891 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
5892 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5893 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5894 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005895 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005896 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005897 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005898 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005899 PseudoSourceValue::getConstantPool(), 0,
5900 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005901 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005902
5903 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005904 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005905 // Op0 is MVT::f32, Op1 is MVT::f64.
5906 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
5907 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
5908 DAG.getConstant(32, MVT::i32));
5909 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, SignBit);
5910 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00005911 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005912 }
5913
Evan Cheng73d6cf12007-01-05 21:37:56 +00005914 // Clear first operand sign bit.
5915 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00005916 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005917 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
5918 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005919 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005920 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
5921 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5922 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5923 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005924 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005925 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005926 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005927 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005928 PseudoSourceValue::getConstantPool(), 0,
5929 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005930 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005931
5932 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00005933 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005934}
5935
Dan Gohman076aee32009-03-04 19:44:21 +00005936/// Emit nodes that will be selected as "test Op0,Op0", or something
5937/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005938SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00005939 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00005940 DebugLoc dl = Op.getDebugLoc();
5941
Dan Gohman31125812009-03-07 01:58:32 +00005942 // CF and OF aren't always set the way we want. Determine which
5943 // of these we need.
5944 bool NeedCF = false;
5945 bool NeedOF = false;
5946 switch (X86CC) {
5947 case X86::COND_A: case X86::COND_AE:
5948 case X86::COND_B: case X86::COND_BE:
5949 NeedCF = true;
5950 break;
5951 case X86::COND_G: case X86::COND_GE:
5952 case X86::COND_L: case X86::COND_LE:
5953 case X86::COND_O: case X86::COND_NO:
5954 NeedOF = true;
5955 break;
5956 default: break;
5957 }
5958
Dan Gohman076aee32009-03-04 19:44:21 +00005959 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00005960 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
5961 // we prove that the arithmetic won't overflow, we can't use OF or CF.
5962 if (Op.getResNo() == 0 && !NeedOF && !NeedCF) {
Dan Gohman076aee32009-03-04 19:44:21 +00005963 unsigned Opcode = 0;
Dan Gohman51bb4742009-03-05 21:29:28 +00005964 unsigned NumOperands = 0;
Dan Gohman076aee32009-03-04 19:44:21 +00005965 switch (Op.getNode()->getOpcode()) {
5966 case ISD::ADD:
Stuart Hastings5a6a65b2010-04-28 00:35:10 +00005967 // Due to an isel shortcoming, be conservative if this add is
5968 // likely to be selected as part of a load-modify-store
5969 // instruction. When the root node in a match is a store, isel
5970 // doesn't know how to remap non-chain non-flag uses of other
5971 // nodes in the match, such as the ADD in this case. This leads
5972 // to the ADD being left around and reselected, with the result
5973 // being two adds in the output. Alas, even if none our users
5974 // are stores, that doesn't prove we're O.K. Ergo, if we have
5975 // any parents that aren't CopyToReg or SETCC, eschew INC/DEC.
5976 // A better fix seems to require climbing the DAG back to the
5977 // root, and it doesn't seem to be worth the effort.
Dan Gohman076aee32009-03-04 19:44:21 +00005978 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Stuart Hastings5a6a65b2010-04-28 00:35:10 +00005979 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5980 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
Dan Gohman076aee32009-03-04 19:44:21 +00005981 goto default_case;
Dan Gohman076aee32009-03-04 19:44:21 +00005982 if (ConstantSDNode *C =
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005983 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
5984 // An add of one will be selected as an INC.
Dan Gohman076aee32009-03-04 19:44:21 +00005985 if (C->getAPIntValue() == 1) {
5986 Opcode = X86ISD::INC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005987 NumOperands = 1;
Dan Gohman076aee32009-03-04 19:44:21 +00005988 break;
5989 }
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005990 // An add of negative one (subtract of one) will be selected as a DEC.
5991 if (C->getAPIntValue().isAllOnesValue()) {
5992 Opcode = X86ISD::DEC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005993 NumOperands = 1;
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005994 break;
5995 }
5996 }
Dan Gohman076aee32009-03-04 19:44:21 +00005997 // Otherwise use a regular EFLAGS-setting add.
5998 Opcode = X86ISD::ADD;
Dan Gohman51bb4742009-03-05 21:29:28 +00005999 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00006000 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00006001 case ISD::AND: {
6002 // If the primary and result isn't used, don't bother using X86ISD::AND,
6003 // because a TEST instruction will be better.
6004 bool NonFlagUse = false;
6005 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Evan Cheng17751da2010-01-07 00:54:06 +00006006 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
6007 SDNode *User = *UI;
6008 unsigned UOpNo = UI.getOperandNo();
6009 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
6010 // Look pass truncate.
6011 UOpNo = User->use_begin().getOperandNo();
6012 User = *User->use_begin();
6013 }
6014 if (User->getOpcode() != ISD::BRCOND &&
6015 User->getOpcode() != ISD::SETCC &&
6016 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
Dan Gohmane220c4b2009-09-18 19:59:53 +00006017 NonFlagUse = true;
6018 break;
6019 }
Evan Cheng17751da2010-01-07 00:54:06 +00006020 }
Dan Gohmane220c4b2009-09-18 19:59:53 +00006021 if (!NonFlagUse)
6022 break;
6023 }
6024 // FALL THROUGH
Dan Gohman076aee32009-03-04 19:44:21 +00006025 case ISD::SUB:
Dan Gohmane220c4b2009-09-18 19:59:53 +00006026 case ISD::OR:
6027 case ISD::XOR:
6028 // Due to the ISEL shortcoming noted above, be conservative if this op is
Dan Gohman076aee32009-03-04 19:44:21 +00006029 // likely to be selected as part of a load-modify-store instruction.
6030 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
6031 UE = Op.getNode()->use_end(); UI != UE; ++UI)
6032 if (UI->getOpcode() == ISD::STORE)
6033 goto default_case;
Dan Gohmane220c4b2009-09-18 19:59:53 +00006034 // Otherwise use a regular EFLAGS-setting instruction.
6035 switch (Op.getNode()->getOpcode()) {
6036 case ISD::SUB: Opcode = X86ISD::SUB; break;
6037 case ISD::OR: Opcode = X86ISD::OR; break;
6038 case ISD::XOR: Opcode = X86ISD::XOR; break;
6039 case ISD::AND: Opcode = X86ISD::AND; break;
6040 default: llvm_unreachable("unexpected operator!");
6041 }
Dan Gohman51bb4742009-03-05 21:29:28 +00006042 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00006043 break;
6044 case X86ISD::ADD:
6045 case X86ISD::SUB:
6046 case X86ISD::INC:
6047 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +00006048 case X86ISD::OR:
6049 case X86ISD::XOR:
6050 case X86ISD::AND:
Dan Gohman076aee32009-03-04 19:44:21 +00006051 return SDValue(Op.getNode(), 1);
6052 default:
6053 default_case:
6054 break;
6055 }
6056 if (Opcode != 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006057 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
Dan Gohman076aee32009-03-04 19:44:21 +00006058 SmallVector<SDValue, 4> Ops;
Dan Gohman31125812009-03-07 01:58:32 +00006059 for (unsigned i = 0; i != NumOperands; ++i)
Dan Gohman076aee32009-03-04 19:44:21 +00006060 Ops.push_back(Op.getOperand(i));
Dan Gohmanfc166572009-04-09 23:54:40 +00006061 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
Dan Gohman076aee32009-03-04 19:44:21 +00006062 DAG.ReplaceAllUsesWith(Op, New);
6063 return SDValue(New.getNode(), 1);
6064 }
6065 }
6066
6067 // Otherwise just emit a CMP with 0, which is the TEST pattern.
Owen Anderson825b72b2009-08-11 20:47:22 +00006068 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
Dan Gohman076aee32009-03-04 19:44:21 +00006069 DAG.getConstant(0, Op.getValueType()));
6070}
6071
6072/// Emit nodes that will be selected as "cmp Op0,Op1", or something
6073/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00006074SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00006075 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00006076 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
6077 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00006078 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00006079
6080 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00006081 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00006082}
6083
Evan Chengd40d03e2010-01-06 19:38:29 +00006084/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
6085/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00006086SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
6087 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00006088 SDValue Op0 = And.getOperand(0);
6089 SDValue Op1 = And.getOperand(1);
6090 if (Op0.getOpcode() == ISD::TRUNCATE)
6091 Op0 = Op0.getOperand(0);
6092 if (Op1.getOpcode() == ISD::TRUNCATE)
6093 Op1 = Op1.getOperand(0);
6094
Evan Chengd40d03e2010-01-06 19:38:29 +00006095 SDValue LHS, RHS;
Evan Cheng2c755ba2010-02-27 07:36:59 +00006096 if (Op1.getOpcode() == ISD::SHL) {
6097 if (ConstantSDNode *And10C = dyn_cast<ConstantSDNode>(Op1.getOperand(0)))
6098 if (And10C->getZExtValue() == 1) {
6099 LHS = Op0;
6100 RHS = Op1.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00006101 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00006102 } else if (Op0.getOpcode() == ISD::SHL) {
6103 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
6104 if (And00C->getZExtValue() == 1) {
6105 LHS = Op1;
6106 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00006107 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00006108 } else if (Op1.getOpcode() == ISD::Constant) {
6109 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
6110 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00006111 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
6112 LHS = AndLHS.getOperand(0);
6113 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00006114 }
Evan Chengd40d03e2010-01-06 19:38:29 +00006115 }
Evan Cheng0488db92007-09-25 01:57:46 +00006116
Evan Chengd40d03e2010-01-06 19:38:29 +00006117 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00006118 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00006119 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00006120 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00006121 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00006122 // Also promote i16 to i32 for performance / code size reason.
6123 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00006124 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00006125 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00006126
Evan Chengd40d03e2010-01-06 19:38:29 +00006127 // If the operand types disagree, extend the shift amount to match. Since
6128 // BT ignores high bits (like shifts) we can use anyextend.
6129 if (LHS.getValueType() != RHS.getValueType())
6130 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00006131
Evan Chengd40d03e2010-01-06 19:38:29 +00006132 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
6133 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
6134 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6135 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00006136 }
6137
Evan Cheng54de3ea2010-01-05 06:52:31 +00006138 return SDValue();
6139}
6140
Dan Gohmand858e902010-04-17 15:26:15 +00006141SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng54de3ea2010-01-05 06:52:31 +00006142 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
6143 SDValue Op0 = Op.getOperand(0);
6144 SDValue Op1 = Op.getOperand(1);
6145 DebugLoc dl = Op.getDebugLoc();
6146 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
6147
6148 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00006149 // Lower (X & (1 << N)) == 0 to BT(X, N).
6150 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
6151 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
6152 if (Op0.getOpcode() == ISD::AND &&
6153 Op0.hasOneUse() &&
6154 Op1.getOpcode() == ISD::Constant &&
6155 cast<ConstantSDNode>(Op1)->getZExtValue() == 0 &&
6156 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
6157 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
6158 if (NewSetCC.getNode())
6159 return NewSetCC;
6160 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00006161
Evan Cheng2c755ba2010-02-27 07:36:59 +00006162 // Look for "(setcc) == / != 1" to avoid unncessary setcc.
6163 if (Op0.getOpcode() == X86ISD::SETCC &&
6164 Op1.getOpcode() == ISD::Constant &&
6165 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
6166 cast<ConstantSDNode>(Op1)->isNullValue()) &&
6167 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
6168 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
6169 bool Invert = (CC == ISD::SETNE) ^
6170 cast<ConstantSDNode>(Op1)->isNullValue();
6171 if (Invert)
6172 CCode = X86::GetOppositeBranchCondition(CCode);
6173 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6174 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
6175 }
6176
Evan Chenge5b51ac2010-04-17 06:13:15 +00006177 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00006178 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00006179 if (X86CC == X86::COND_INVALID)
6180 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00006181
Evan Cheng552f09a2010-04-26 19:06:11 +00006182 SDValue Cond = EmitCmp(Op0, Op1, X86CC, DAG);
Evan Chengad9c0a32009-12-15 00:53:42 +00006183
6184 // Use sbb x, x to materialize carry bit into a GPR.
Evan Cheng2e489c42009-12-16 00:53:11 +00006185 if (X86CC == X86::COND_B)
Evan Chengad9c0a32009-12-15 00:53:42 +00006186 return DAG.getNode(ISD::AND, dl, MVT::i8,
6187 DAG.getNode(X86ISD::SETCC_CARRY, dl, MVT::i8,
6188 DAG.getConstant(X86CC, MVT::i8), Cond),
6189 DAG.getConstant(1, MVT::i8));
Evan Chengad9c0a32009-12-15 00:53:42 +00006190
Owen Anderson825b72b2009-08-11 20:47:22 +00006191 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6192 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00006193}
6194
Dan Gohmand858e902010-04-17 15:26:15 +00006195SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00006196 SDValue Cond;
6197 SDValue Op0 = Op.getOperand(0);
6198 SDValue Op1 = Op.getOperand(1);
6199 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00006200 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00006201 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
6202 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006203 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00006204
6205 if (isFP) {
6206 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00006207 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00006208 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
6209 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00006210 bool Swap = false;
6211
6212 switch (SetCCOpcode) {
6213 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00006214 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00006215 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00006216 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00006217 case ISD::SETGT: Swap = true; // Fallthrough
6218 case ISD::SETLT:
6219 case ISD::SETOLT: SSECC = 1; break;
6220 case ISD::SETOGE:
6221 case ISD::SETGE: Swap = true; // Fallthrough
6222 case ISD::SETLE:
6223 case ISD::SETOLE: SSECC = 2; break;
6224 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00006225 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00006226 case ISD::SETNE: SSECC = 4; break;
6227 case ISD::SETULE: Swap = true;
6228 case ISD::SETUGE: SSECC = 5; break;
6229 case ISD::SETULT: Swap = true;
6230 case ISD::SETUGT: SSECC = 6; break;
6231 case ISD::SETO: SSECC = 7; break;
6232 }
6233 if (Swap)
6234 std::swap(Op0, Op1);
6235
Nate Begemanfb8ead02008-07-25 19:05:58 +00006236 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00006237 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00006238 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00006239 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00006240 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
6241 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00006242 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00006243 }
6244 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00006245 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00006246 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
6247 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00006248 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00006249 }
Torok Edwinc23197a2009-07-14 16:55:14 +00006250 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00006251 }
6252 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00006253 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00006254 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006255
Nate Begeman30a0de92008-07-17 16:51:19 +00006256 // We are handling one of the integer comparisons here. Since SSE only has
6257 // GT and EQ comparisons for integer, swapping operands and multiple
6258 // operations may be required for some comparisons.
6259 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
6260 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00006261
Owen Anderson825b72b2009-08-11 20:47:22 +00006262 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00006263 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00006264 case MVT::v8i8:
6265 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
6266 case MVT::v4i16:
6267 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
6268 case MVT::v2i32:
6269 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
6270 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00006271 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006272
Nate Begeman30a0de92008-07-17 16:51:19 +00006273 switch (SetCCOpcode) {
6274 default: break;
6275 case ISD::SETNE: Invert = true;
6276 case ISD::SETEQ: Opc = EQOpc; break;
6277 case ISD::SETLT: Swap = true;
6278 case ISD::SETGT: Opc = GTOpc; break;
6279 case ISD::SETGE: Swap = true;
6280 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
6281 case ISD::SETULT: Swap = true;
6282 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
6283 case ISD::SETUGE: Swap = true;
6284 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
6285 }
6286 if (Swap)
6287 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006288
Nate Begeman30a0de92008-07-17 16:51:19 +00006289 // Since SSE has no unsigned integer comparisons, we need to flip the sign
6290 // bits of the inputs before performing those operations.
6291 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00006292 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00006293 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
6294 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00006295 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00006296 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
6297 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00006298 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
6299 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00006300 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006301
Dale Johannesenace16102009-02-03 19:33:06 +00006302 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00006303
6304 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00006305 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00006306 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00006307
Nate Begeman30a0de92008-07-17 16:51:19 +00006308 return Result;
6309}
Evan Cheng0488db92007-09-25 01:57:46 +00006310
Evan Cheng370e5342008-12-03 08:38:43 +00006311// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00006312static bool isX86LogicalCmp(SDValue Op) {
6313 unsigned Opc = Op.getNode()->getOpcode();
6314 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
6315 return true;
6316 if (Op.getResNo() == 1 &&
6317 (Opc == X86ISD::ADD ||
6318 Opc == X86ISD::SUB ||
6319 Opc == X86ISD::SMUL ||
6320 Opc == X86ISD::UMUL ||
6321 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00006322 Opc == X86ISD::DEC ||
6323 Opc == X86ISD::OR ||
6324 Opc == X86ISD::XOR ||
6325 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00006326 return true;
6327
6328 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00006329}
6330
Dan Gohmand858e902010-04-17 15:26:15 +00006331SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00006332 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00006333 SDValue Cond = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006334 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00006335 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00006336
Dan Gohman1a492952009-10-20 16:22:37 +00006337 if (Cond.getOpcode() == ISD::SETCC) {
6338 SDValue NewCond = LowerSETCC(Cond, DAG);
6339 if (NewCond.getNode())
6340 Cond = NewCond;
6341 }
Evan Cheng734503b2006-09-11 02:19:56 +00006342
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006343 // (select (x == 0), -1, 0) -> (sign_bit (x - 1))
6344 SDValue Op1 = Op.getOperand(1);
6345 SDValue Op2 = Op.getOperand(2);
6346 if (Cond.getOpcode() == X86ISD::SETCC &&
6347 cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue() == X86::COND_E) {
6348 SDValue Cmp = Cond.getOperand(1);
6349 if (Cmp.getOpcode() == X86ISD::CMP) {
6350 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op1);
6351 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
6352 ConstantSDNode *RHSC =
6353 dyn_cast<ConstantSDNode>(Cmp.getOperand(1).getNode());
6354 if (N1C && N1C->isAllOnesValue() &&
6355 N2C && N2C->isNullValue() &&
6356 RHSC && RHSC->isNullValue()) {
6357 SDValue CmpOp0 = Cmp.getOperand(0);
Chris Lattnerda0688e2010-03-14 18:44:35 +00006358 Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006359 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
6360 return DAG.getNode(X86ISD::SETCC_CARRY, dl, Op.getValueType(),
6361 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
6362 }
6363 }
6364 }
6365
Evan Chengad9c0a32009-12-15 00:53:42 +00006366 // Look pass (and (setcc_carry (cmp ...)), 1).
6367 if (Cond.getOpcode() == ISD::AND &&
6368 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
6369 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
6370 if (C && C->getAPIntValue() == 1)
6371 Cond = Cond.getOperand(0);
6372 }
6373
Evan Cheng3f41d662007-10-08 22:16:29 +00006374 // If condition flag is set by a X86ISD::CMP, then use it as the condition
6375 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00006376 if (Cond.getOpcode() == X86ISD::SETCC ||
6377 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00006378 CC = Cond.getOperand(0);
6379
Dan Gohman475871a2008-07-27 21:46:04 +00006380 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00006381 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00006382 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00006383
Evan Cheng3f41d662007-10-08 22:16:29 +00006384 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006385 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00006386 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00006387 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00006388
Chris Lattnerd1980a52009-03-12 06:52:53 +00006389 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
6390 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00006391 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00006392 addTest = false;
6393 }
6394 }
6395
6396 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00006397 // Look pass the truncate.
6398 if (Cond.getOpcode() == ISD::TRUNCATE)
6399 Cond = Cond.getOperand(0);
6400
6401 // We know the result of AND is compared against zero. Try to match
6402 // it to BT.
6403 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
6404 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
6405 if (NewSetCC.getNode()) {
6406 CC = NewSetCC.getOperand(0);
6407 Cond = NewSetCC.getOperand(1);
6408 addTest = false;
6409 }
6410 }
6411 }
6412
6413 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006414 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00006415 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00006416 }
6417
Evan Cheng0488db92007-09-25 01:57:46 +00006418 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
6419 // condition is true.
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006420 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
6421 SDValue Ops[] = { Op2, Op1, CC, Cond };
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006422 return DAG.getNode(X86ISD::CMOV, dl, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00006423}
6424
Evan Cheng370e5342008-12-03 08:38:43 +00006425// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
6426// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
6427// from the AND / OR.
6428static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
6429 Opc = Op.getOpcode();
6430 if (Opc != ISD::OR && Opc != ISD::AND)
6431 return false;
6432 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
6433 Op.getOperand(0).hasOneUse() &&
6434 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
6435 Op.getOperand(1).hasOneUse());
6436}
6437
Evan Cheng961d6d42009-02-02 08:19:07 +00006438// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
6439// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00006440static bool isXor1OfSetCC(SDValue Op) {
6441 if (Op.getOpcode() != ISD::XOR)
6442 return false;
6443 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6444 if (N1C && N1C->getAPIntValue() == 1) {
6445 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
6446 Op.getOperand(0).hasOneUse();
6447 }
6448 return false;
6449}
6450
Dan Gohmand858e902010-04-17 15:26:15 +00006451SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00006452 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00006453 SDValue Chain = Op.getOperand(0);
6454 SDValue Cond = Op.getOperand(1);
6455 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006456 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00006457 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00006458
Dan Gohman1a492952009-10-20 16:22:37 +00006459 if (Cond.getOpcode() == ISD::SETCC) {
6460 SDValue NewCond = LowerSETCC(Cond, DAG);
6461 if (NewCond.getNode())
6462 Cond = NewCond;
6463 }
Chris Lattnere55484e2008-12-25 05:34:37 +00006464#if 0
6465 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00006466 else if (Cond.getOpcode() == X86ISD::ADD ||
6467 Cond.getOpcode() == X86ISD::SUB ||
6468 Cond.getOpcode() == X86ISD::SMUL ||
6469 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00006470 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00006471#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00006472
Evan Chengad9c0a32009-12-15 00:53:42 +00006473 // Look pass (and (setcc_carry (cmp ...)), 1).
6474 if (Cond.getOpcode() == ISD::AND &&
6475 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
6476 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
6477 if (C && C->getAPIntValue() == 1)
6478 Cond = Cond.getOperand(0);
6479 }
6480
Evan Cheng3f41d662007-10-08 22:16:29 +00006481 // If condition flag is set by a X86ISD::CMP, then use it as the condition
6482 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00006483 if (Cond.getOpcode() == X86ISD::SETCC ||
6484 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00006485 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006486
Dan Gohman475871a2008-07-27 21:46:04 +00006487 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00006488 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00006489 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00006490 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00006491 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00006492 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00006493 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00006494 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00006495 default: break;
6496 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00006497 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00006498 // These can only come from an arithmetic instruction with overflow,
6499 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00006500 Cond = Cond.getNode()->getOperand(1);
6501 addTest = false;
6502 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00006503 }
Evan Cheng0488db92007-09-25 01:57:46 +00006504 }
Evan Cheng370e5342008-12-03 08:38:43 +00006505 } else {
6506 unsigned CondOpc;
6507 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
6508 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00006509 if (CondOpc == ISD::OR) {
6510 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
6511 // two branches instead of an explicit OR instruction with a
6512 // separate test.
6513 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00006514 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00006515 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006516 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00006517 Chain, Dest, CC, Cmp);
6518 CC = Cond.getOperand(1).getOperand(0);
6519 Cond = Cmp;
6520 addTest = false;
6521 }
6522 } else { // ISD::AND
6523 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
6524 // two branches instead of an explicit AND instruction with a
6525 // separate test. However, we only do this if this block doesn't
6526 // have a fall-through edge, because this requires an explicit
6527 // jmp when the condition is false.
6528 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00006529 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00006530 Op.getNode()->hasOneUse()) {
6531 X86::CondCode CCode =
6532 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
6533 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006534 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00006535 SDValue User = SDValue(*Op.getNode()->use_begin(), 0);
6536 // Look for an unconditional branch following this conditional branch.
6537 // We need this because we need to reverse the successors in order
6538 // to implement FCMP_OEQ.
6539 if (User.getOpcode() == ISD::BR) {
6540 SDValue FalseBB = User.getOperand(1);
6541 SDValue NewBR =
6542 DAG.UpdateNodeOperands(User, User.getOperand(0), Dest);
6543 assert(NewBR == User);
6544 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00006545
Dale Johannesene4d209d2009-02-03 20:21:25 +00006546 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00006547 Chain, Dest, CC, Cmp);
6548 X86::CondCode CCode =
6549 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
6550 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006551 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00006552 Cond = Cmp;
6553 addTest = false;
6554 }
6555 }
Dan Gohman279c22e2008-10-21 03:29:32 +00006556 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00006557 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
6558 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
6559 // It should be transformed during dag combiner except when the condition
6560 // is set by a arithmetics with overflow node.
6561 X86::CondCode CCode =
6562 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
6563 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006564 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00006565 Cond = Cond.getOperand(0).getOperand(1);
6566 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00006567 }
Evan Cheng0488db92007-09-25 01:57:46 +00006568 }
6569
6570 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00006571 // Look pass the truncate.
6572 if (Cond.getOpcode() == ISD::TRUNCATE)
6573 Cond = Cond.getOperand(0);
6574
6575 // We know the result of AND is compared against zero. Try to match
6576 // it to BT.
6577 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
6578 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
6579 if (NewSetCC.getNode()) {
6580 CC = NewSetCC.getOperand(0);
6581 Cond = NewSetCC.getOperand(1);
6582 addTest = false;
6583 }
6584 }
6585 }
6586
6587 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006588 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00006589 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00006590 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00006591 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00006592 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00006593}
6594
Anton Korobeynikove060b532007-04-17 19:34:00 +00006595
6596// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
6597// Calls to _alloca is needed to probe the stack when allocating more than 4k
6598// bytes in one go. Touching the stack at 4K increments is necessary to ensure
6599// that the guard pages used by the OS virtual memory manager are allocated in
6600// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00006601SDValue
6602X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006603 SelectionDAG &DAG) const {
Anton Korobeynikove060b532007-04-17 19:34:00 +00006604 assert(Subtarget->isTargetCygMing() &&
6605 "This should be used only on Cygwin/Mingw targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006606 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006607
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006608 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00006609 SDValue Chain = Op.getOperand(0);
6610 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006611 // FIXME: Ensure alignment here
6612
Dan Gohman475871a2008-07-27 21:46:04 +00006613 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006614
Owen Andersone50ed302009-08-10 22:56:29 +00006615 EVT IntPtr = getPointerTy();
Owen Anderson825b72b2009-08-11 20:47:22 +00006616 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006617
Dale Johannesendd64c412009-02-04 00:33:20 +00006618 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006619 Flag = Chain.getValue(1);
6620
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00006621 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006622
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00006623 Chain = DAG.getNode(X86ISD::MINGW_ALLOCA, dl, NodeTys, Chain, Flag);
6624 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006625
Dale Johannesendd64c412009-02-04 00:33:20 +00006626 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006627
Dan Gohman475871a2008-07-27 21:46:04 +00006628 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006629 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006630}
6631
Dan Gohmand858e902010-04-17 15:26:15 +00006632SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00006633 MachineFunction &MF = DAG.getMachineFunction();
6634 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
6635
Dan Gohman69de1932008-02-06 22:27:42 +00006636 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006637 DebugLoc dl = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00006638
Evan Cheng25ab6902006-09-08 06:48:29 +00006639 if (!Subtarget->is64Bit()) {
6640 // vastart just stores the address of the VarArgsFrameIndex slot into the
6641 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00006642 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
6643 getPointerTy());
David Greene67c9d422010-02-15 16:53:33 +00006644 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0,
6645 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006646 }
6647
6648 // __va_list_tag:
6649 // gp_offset (0 - 6 * 8)
6650 // fp_offset (48 - 48 + 8 * 16)
6651 // overflow_arg_area (point to parameters coming in memory).
6652 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00006653 SmallVector<SDValue, 8> MemOps;
6654 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00006655 // Store gp_offset
Dale Johannesene4d209d2009-02-03 20:21:25 +00006656 SDValue Store = DAG.getStore(Op.getOperand(0), dl,
Dan Gohman1e93df62010-04-17 14:41:14 +00006657 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
6658 MVT::i32),
David Greene67c9d422010-02-15 16:53:33 +00006659 FIN, SV, 0, false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006660 MemOps.push_back(Store);
6661
6662 // Store fp_offset
Scott Michelfdc40a02009-02-17 22:15:04 +00006663 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006664 FIN, DAG.getIntPtrConstant(4));
6665 Store = DAG.getStore(Op.getOperand(0), dl,
Dan Gohman1e93df62010-04-17 14:41:14 +00006666 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
6667 MVT::i32),
David Greene67c9d422010-02-15 16:53:33 +00006668 FIN, SV, 0, false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006669 MemOps.push_back(Store);
6670
6671 // Store ptr to overflow_arg_area
Scott Michelfdc40a02009-02-17 22:15:04 +00006672 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006673 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00006674 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
6675 getPointerTy());
David Greene67c9d422010-02-15 16:53:33 +00006676 Store = DAG.getStore(Op.getOperand(0), dl, OVFIN, FIN, SV, 0,
6677 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006678 MemOps.push_back(Store);
6679
6680 // Store ptr to reg_save_area.
Scott Michelfdc40a02009-02-17 22:15:04 +00006681 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006682 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00006683 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
6684 getPointerTy());
David Greene67c9d422010-02-15 16:53:33 +00006685 Store = DAG.getStore(Op.getOperand(0), dl, RSFIN, FIN, SV, 0,
6686 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006687 MemOps.push_back(Store);
Owen Anderson825b72b2009-08-11 20:47:22 +00006688 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006689 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00006690}
6691
Dan Gohmand858e902010-04-17 15:26:15 +00006692SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman9018e832008-05-10 01:26:14 +00006693 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
6694 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman475871a2008-07-27 21:46:04 +00006695 SDValue Chain = Op.getOperand(0);
6696 SDValue SrcPtr = Op.getOperand(1);
6697 SDValue SrcSV = Op.getOperand(2);
Dan Gohman9018e832008-05-10 01:26:14 +00006698
Chris Lattner75361b62010-04-07 22:58:41 +00006699 report_fatal_error("VAArgInst is not yet implemented for x86-64!");
Dan Gohman475871a2008-07-27 21:46:04 +00006700 return SDValue();
Dan Gohman9018e832008-05-10 01:26:14 +00006701}
6702
Dan Gohmand858e902010-04-17 15:26:15 +00006703SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00006704 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00006705 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00006706 SDValue Chain = Op.getOperand(0);
6707 SDValue DstPtr = Op.getOperand(1);
6708 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00006709 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
6710 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006711 DebugLoc dl = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00006712
Dale Johannesendd64c412009-02-04 00:33:20 +00006713 return DAG.getMemcpy(Chain, dl, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00006714 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
6715 false, DstSV, 0, SrcSV, 0);
Evan Chengae642192007-03-02 23:16:35 +00006716}
6717
Dan Gohman475871a2008-07-27 21:46:04 +00006718SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006719X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006720 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006721 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006722 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00006723 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00006724 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00006725 case Intrinsic::x86_sse_comieq_ss:
6726 case Intrinsic::x86_sse_comilt_ss:
6727 case Intrinsic::x86_sse_comile_ss:
6728 case Intrinsic::x86_sse_comigt_ss:
6729 case Intrinsic::x86_sse_comige_ss:
6730 case Intrinsic::x86_sse_comineq_ss:
6731 case Intrinsic::x86_sse_ucomieq_ss:
6732 case Intrinsic::x86_sse_ucomilt_ss:
6733 case Intrinsic::x86_sse_ucomile_ss:
6734 case Intrinsic::x86_sse_ucomigt_ss:
6735 case Intrinsic::x86_sse_ucomige_ss:
6736 case Intrinsic::x86_sse_ucomineq_ss:
6737 case Intrinsic::x86_sse2_comieq_sd:
6738 case Intrinsic::x86_sse2_comilt_sd:
6739 case Intrinsic::x86_sse2_comile_sd:
6740 case Intrinsic::x86_sse2_comigt_sd:
6741 case Intrinsic::x86_sse2_comige_sd:
6742 case Intrinsic::x86_sse2_comineq_sd:
6743 case Intrinsic::x86_sse2_ucomieq_sd:
6744 case Intrinsic::x86_sse2_ucomilt_sd:
6745 case Intrinsic::x86_sse2_ucomile_sd:
6746 case Intrinsic::x86_sse2_ucomigt_sd:
6747 case Intrinsic::x86_sse2_ucomige_sd:
6748 case Intrinsic::x86_sse2_ucomineq_sd: {
6749 unsigned Opc = 0;
6750 ISD::CondCode CC = ISD::SETCC_INVALID;
6751 switch (IntNo) {
6752 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006753 case Intrinsic::x86_sse_comieq_ss:
6754 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006755 Opc = X86ISD::COMI;
6756 CC = ISD::SETEQ;
6757 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006758 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006759 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006760 Opc = X86ISD::COMI;
6761 CC = ISD::SETLT;
6762 break;
6763 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006764 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006765 Opc = X86ISD::COMI;
6766 CC = ISD::SETLE;
6767 break;
6768 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006769 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006770 Opc = X86ISD::COMI;
6771 CC = ISD::SETGT;
6772 break;
6773 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006774 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006775 Opc = X86ISD::COMI;
6776 CC = ISD::SETGE;
6777 break;
6778 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006779 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006780 Opc = X86ISD::COMI;
6781 CC = ISD::SETNE;
6782 break;
6783 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006784 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006785 Opc = X86ISD::UCOMI;
6786 CC = ISD::SETEQ;
6787 break;
6788 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006789 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006790 Opc = X86ISD::UCOMI;
6791 CC = ISD::SETLT;
6792 break;
6793 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006794 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006795 Opc = X86ISD::UCOMI;
6796 CC = ISD::SETLE;
6797 break;
6798 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006799 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006800 Opc = X86ISD::UCOMI;
6801 CC = ISD::SETGT;
6802 break;
6803 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006804 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006805 Opc = X86ISD::UCOMI;
6806 CC = ISD::SETGE;
6807 break;
6808 case Intrinsic::x86_sse_ucomineq_ss:
6809 case Intrinsic::x86_sse2_ucomineq_sd:
6810 Opc = X86ISD::UCOMI;
6811 CC = ISD::SETNE;
6812 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006813 }
Evan Cheng734503b2006-09-11 02:19:56 +00006814
Dan Gohman475871a2008-07-27 21:46:04 +00006815 SDValue LHS = Op.getOperand(1);
6816 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00006817 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00006818 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00006819 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
6820 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6821 DAG.getConstant(X86CC, MVT::i8), Cond);
6822 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00006823 }
Eric Christopher71c67532009-07-29 00:28:05 +00006824 // ptest intrinsics. The intrinsic these come from are designed to return
Eric Christopher794bfed2009-07-29 01:01:19 +00006825 // an integer value, not just an instruction so lower it to the ptest
6826 // pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00006827 case Intrinsic::x86_sse41_ptestz:
6828 case Intrinsic::x86_sse41_ptestc:
6829 case Intrinsic::x86_sse41_ptestnzc:{
6830 unsigned X86CC = 0;
6831 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00006832 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Eric Christopher71c67532009-07-29 00:28:05 +00006833 case Intrinsic::x86_sse41_ptestz:
6834 // ZF = 1
6835 X86CC = X86::COND_E;
6836 break;
6837 case Intrinsic::x86_sse41_ptestc:
6838 // CF = 1
6839 X86CC = X86::COND_B;
6840 break;
Eric Christopherfd179292009-08-27 18:07:15 +00006841 case Intrinsic::x86_sse41_ptestnzc:
Eric Christopher71c67532009-07-29 00:28:05 +00006842 // ZF and CF = 0
6843 X86CC = X86::COND_A;
6844 break;
6845 }
Eric Christopherfd179292009-08-27 18:07:15 +00006846
Eric Christopher71c67532009-07-29 00:28:05 +00006847 SDValue LHS = Op.getOperand(1);
6848 SDValue RHS = Op.getOperand(2);
Owen Anderson825b72b2009-08-11 20:47:22 +00006849 SDValue Test = DAG.getNode(X86ISD::PTEST, dl, MVT::i32, LHS, RHS);
6850 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
6851 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
6852 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00006853 }
Evan Cheng5759f972008-05-04 09:15:50 +00006854
6855 // Fix vector shift instructions where the last operand is a non-immediate
6856 // i32 value.
6857 case Intrinsic::x86_sse2_pslli_w:
6858 case Intrinsic::x86_sse2_pslli_d:
6859 case Intrinsic::x86_sse2_pslli_q:
6860 case Intrinsic::x86_sse2_psrli_w:
6861 case Intrinsic::x86_sse2_psrli_d:
6862 case Intrinsic::x86_sse2_psrli_q:
6863 case Intrinsic::x86_sse2_psrai_w:
6864 case Intrinsic::x86_sse2_psrai_d:
6865 case Intrinsic::x86_mmx_pslli_w:
6866 case Intrinsic::x86_mmx_pslli_d:
6867 case Intrinsic::x86_mmx_pslli_q:
6868 case Intrinsic::x86_mmx_psrli_w:
6869 case Intrinsic::x86_mmx_psrli_d:
6870 case Intrinsic::x86_mmx_psrli_q:
6871 case Intrinsic::x86_mmx_psrai_w:
6872 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00006873 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00006874 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00006875 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00006876
6877 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00006878 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00006879 switch (IntNo) {
6880 case Intrinsic::x86_sse2_pslli_w:
6881 NewIntNo = Intrinsic::x86_sse2_psll_w;
6882 break;
6883 case Intrinsic::x86_sse2_pslli_d:
6884 NewIntNo = Intrinsic::x86_sse2_psll_d;
6885 break;
6886 case Intrinsic::x86_sse2_pslli_q:
6887 NewIntNo = Intrinsic::x86_sse2_psll_q;
6888 break;
6889 case Intrinsic::x86_sse2_psrli_w:
6890 NewIntNo = Intrinsic::x86_sse2_psrl_w;
6891 break;
6892 case Intrinsic::x86_sse2_psrli_d:
6893 NewIntNo = Intrinsic::x86_sse2_psrl_d;
6894 break;
6895 case Intrinsic::x86_sse2_psrli_q:
6896 NewIntNo = Intrinsic::x86_sse2_psrl_q;
6897 break;
6898 case Intrinsic::x86_sse2_psrai_w:
6899 NewIntNo = Intrinsic::x86_sse2_psra_w;
6900 break;
6901 case Intrinsic::x86_sse2_psrai_d:
6902 NewIntNo = Intrinsic::x86_sse2_psra_d;
6903 break;
6904 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00006905 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00006906 switch (IntNo) {
6907 case Intrinsic::x86_mmx_pslli_w:
6908 NewIntNo = Intrinsic::x86_mmx_psll_w;
6909 break;
6910 case Intrinsic::x86_mmx_pslli_d:
6911 NewIntNo = Intrinsic::x86_mmx_psll_d;
6912 break;
6913 case Intrinsic::x86_mmx_pslli_q:
6914 NewIntNo = Intrinsic::x86_mmx_psll_q;
6915 break;
6916 case Intrinsic::x86_mmx_psrli_w:
6917 NewIntNo = Intrinsic::x86_mmx_psrl_w;
6918 break;
6919 case Intrinsic::x86_mmx_psrli_d:
6920 NewIntNo = Intrinsic::x86_mmx_psrl_d;
6921 break;
6922 case Intrinsic::x86_mmx_psrli_q:
6923 NewIntNo = Intrinsic::x86_mmx_psrl_q;
6924 break;
6925 case Intrinsic::x86_mmx_psrai_w:
6926 NewIntNo = Intrinsic::x86_mmx_psra_w;
6927 break;
6928 case Intrinsic::x86_mmx_psrai_d:
6929 NewIntNo = Intrinsic::x86_mmx_psra_d;
6930 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006931 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00006932 }
6933 break;
6934 }
6935 }
Mon P Wangefa42202009-09-03 19:56:25 +00006936
6937 // The vector shift intrinsics with scalars uses 32b shift amounts but
6938 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
6939 // to be zero.
6940 SDValue ShOps[4];
6941 ShOps[0] = ShAmt;
6942 ShOps[1] = DAG.getConstant(0, MVT::i32);
6943 if (ShAmtVT == MVT::v4i32) {
6944 ShOps[2] = DAG.getUNDEF(MVT::i32);
6945 ShOps[3] = DAG.getUNDEF(MVT::i32);
6946 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
6947 } else {
6948 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
6949 }
6950
Owen Andersone50ed302009-08-10 22:56:29 +00006951 EVT VT = Op.getValueType();
Mon P Wangefa42202009-09-03 19:56:25 +00006952 ShAmt = DAG.getNode(ISD::BIT_CONVERT, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006953 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00006954 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00006955 Op.getOperand(1), ShAmt);
6956 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00006957 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006958}
Evan Cheng72261582005-12-20 06:22:03 +00006959
Dan Gohmand858e902010-04-17 15:26:15 +00006960SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
6961 SelectionDAG &DAG) const {
Bill Wendling64e87322009-01-16 19:25:27 +00006962 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006963 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00006964
6965 if (Depth > 0) {
6966 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
6967 SDValue Offset =
6968 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00006969 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006970 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00006971 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006972 FrameAddr, Offset),
David Greene67c9d422010-02-15 16:53:33 +00006973 NULL, 0, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00006974 }
6975
6976 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00006977 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00006978 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
David Greene67c9d422010-02-15 16:53:33 +00006979 RetAddrFI, NULL, 0, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00006980}
6981
Dan Gohmand858e902010-04-17 15:26:15 +00006982SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00006983 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6984 MFI->setFrameAddressIsTaken(true);
Owen Andersone50ed302009-08-10 22:56:29 +00006985 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006986 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00006987 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6988 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00006989 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00006990 while (Depth--)
David Greene67c9d422010-02-15 16:53:33 +00006991 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0,
6992 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00006993 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00006994}
6995
Dan Gohman475871a2008-07-27 21:46:04 +00006996SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006997 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006998 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006999}
7000
Dan Gohmand858e902010-04-17 15:26:15 +00007001SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007002 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00007003 SDValue Chain = Op.getOperand(0);
7004 SDValue Offset = Op.getOperand(1);
7005 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007006 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007007
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007008 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
7009 getPointerTy());
7010 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007011
Dale Johannesene4d209d2009-02-03 20:21:25 +00007012 SDValue StoreAddr = DAG.getNode(ISD::SUB, dl, getPointerTy(), Frame,
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00007013 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007014 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
David Greene67c9d422010-02-15 16:53:33 +00007015 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, NULL, 0, false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00007016 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007017 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007018
Dale Johannesene4d209d2009-02-03 20:21:25 +00007019 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007020 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007021 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007022}
7023
Dan Gohman475871a2008-07-27 21:46:04 +00007024SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007025 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007026 SDValue Root = Op.getOperand(0);
7027 SDValue Trmp = Op.getOperand(1); // trampoline
7028 SDValue FPtr = Op.getOperand(2); // nested function
7029 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007030 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00007031
Dan Gohman69de1932008-02-06 22:27:42 +00007032 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00007033
7034 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00007035 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00007036
7037 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00007038 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
7039 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00007040
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00007041 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
7042 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00007043
7044 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
7045
7046 // Load the pointer to the nested function into R11.
7047 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00007048 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00007049 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
David Greene67c9d422010-02-15 16:53:33 +00007050 Addr, TrmpAddr, 0, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007051
Owen Anderson825b72b2009-08-11 20:47:22 +00007052 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7053 DAG.getConstant(2, MVT::i64));
David Greene67c9d422010-02-15 16:53:33 +00007054 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr, TrmpAddr, 2,
7055 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00007056
7057 // Load the 'nest' parameter value into R10.
7058 // R10 is specified in X86CallingConv.td
7059 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00007060 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7061 DAG.getConstant(10, MVT::i64));
7062 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
David Greene67c9d422010-02-15 16:53:33 +00007063 Addr, TrmpAddr, 10, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007064
Owen Anderson825b72b2009-08-11 20:47:22 +00007065 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7066 DAG.getConstant(12, MVT::i64));
David Greene67c9d422010-02-15 16:53:33 +00007067 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 12,
7068 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00007069
7070 // Jump to the nested function.
7071 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00007072 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7073 DAG.getConstant(20, MVT::i64));
7074 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
David Greene67c9d422010-02-15 16:53:33 +00007075 Addr, TrmpAddr, 20, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007076
7077 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00007078 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7079 DAG.getConstant(22, MVT::i64));
7080 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
David Greene67c9d422010-02-15 16:53:33 +00007081 TrmpAddr, 22, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007082
Dan Gohman475871a2008-07-27 21:46:04 +00007083 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00007084 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007085 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007086 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00007087 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00007088 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00007089 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00007090 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007091
7092 switch (CC) {
7093 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00007094 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00007095 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00007096 case CallingConv::X86_StdCall: {
7097 // Pass 'nest' parameter in ECX.
7098 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00007099 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007100
7101 // Check that ECX wasn't needed by an 'inreg' parameter.
7102 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00007103 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00007104
Chris Lattner58d74912008-03-12 17:45:29 +00007105 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00007106 unsigned InRegCount = 0;
7107 unsigned Idx = 1;
7108
7109 for (FunctionType::param_iterator I = FTy->param_begin(),
7110 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00007111 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00007112 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00007113 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007114
7115 if (InRegCount > 2) {
Chris Lattner75361b62010-04-07 22:58:41 +00007116 report_fatal_error("Nest register in use - reduce number of inreg parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00007117 }
7118 }
7119 break;
7120 }
7121 case CallingConv::X86_FastCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00007122 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00007123 // Pass 'nest' parameter in EAX.
7124 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00007125 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007126 break;
7127 }
7128
Dan Gohman475871a2008-07-27 21:46:04 +00007129 SDValue OutChains[4];
7130 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007131
Owen Anderson825b72b2009-08-11 20:47:22 +00007132 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7133 DAG.getConstant(10, MVT::i32));
7134 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007135
Chris Lattnera62fe662010-02-05 19:20:30 +00007136 // This is storing the opcode for MOV32ri.
7137 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00007138 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00007139 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007140 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
David Greene67c9d422010-02-15 16:53:33 +00007141 Trmp, TrmpAddr, 0, false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007142
Owen Anderson825b72b2009-08-11 20:47:22 +00007143 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7144 DAG.getConstant(1, MVT::i32));
David Greene67c9d422010-02-15 16:53:33 +00007145 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 1,
7146 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007147
Chris Lattnera62fe662010-02-05 19:20:30 +00007148 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00007149 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7150 DAG.getConstant(5, MVT::i32));
7151 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
David Greene67c9d422010-02-15 16:53:33 +00007152 TrmpAddr, 5, false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007153
Owen Anderson825b72b2009-08-11 20:47:22 +00007154 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7155 DAG.getConstant(6, MVT::i32));
David Greene67c9d422010-02-15 16:53:33 +00007156 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr, TrmpAddr, 6,
7157 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007158
Dan Gohman475871a2008-07-27 21:46:04 +00007159 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00007160 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007161 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007162 }
7163}
7164
Dan Gohmand858e902010-04-17 15:26:15 +00007165SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
7166 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007167 /*
7168 The rounding mode is in bits 11:10 of FPSR, and has the following
7169 settings:
7170 00 Round to nearest
7171 01 Round to -inf
7172 10 Round to +inf
7173 11 Round to 0
7174
7175 FLT_ROUNDS, on the other hand, expects the following:
7176 -1 Undefined
7177 0 Round to 0
7178 1 Round to nearest
7179 2 Round to +inf
7180 3 Round to -inf
7181
7182 To perform the conversion, we do:
7183 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
7184 */
7185
7186 MachineFunction &MF = DAG.getMachineFunction();
7187 const TargetMachine &TM = MF.getTarget();
7188 const TargetFrameInfo &TFI = *TM.getFrameInfo();
7189 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00007190 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007191 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007192
7193 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00007194 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007195 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007196
Owen Anderson825b72b2009-08-11 20:47:22 +00007197 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, dl, MVT::Other,
Evan Cheng8a186ae2008-09-24 23:26:36 +00007198 DAG.getEntryNode(), StackSlot);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007199
7200 // Load FP Control Word from stack slot
David Greene67c9d422010-02-15 16:53:33 +00007201 SDValue CWD = DAG.getLoad(MVT::i16, dl, Chain, StackSlot, NULL, 0,
7202 false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007203
7204 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00007205 SDValue CWD1 =
Owen Anderson825b72b2009-08-11 20:47:22 +00007206 DAG.getNode(ISD::SRL, dl, MVT::i16,
7207 DAG.getNode(ISD::AND, dl, MVT::i16,
7208 CWD, DAG.getConstant(0x800, MVT::i16)),
7209 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00007210 SDValue CWD2 =
Owen Anderson825b72b2009-08-11 20:47:22 +00007211 DAG.getNode(ISD::SRL, dl, MVT::i16,
7212 DAG.getNode(ISD::AND, dl, MVT::i16,
7213 CWD, DAG.getConstant(0x400, MVT::i16)),
7214 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007215
Dan Gohman475871a2008-07-27 21:46:04 +00007216 SDValue RetVal =
Owen Anderson825b72b2009-08-11 20:47:22 +00007217 DAG.getNode(ISD::AND, dl, MVT::i16,
7218 DAG.getNode(ISD::ADD, dl, MVT::i16,
7219 DAG.getNode(ISD::OR, dl, MVT::i16, CWD1, CWD2),
7220 DAG.getConstant(1, MVT::i16)),
7221 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007222
7223
Duncan Sands83ec4b62008-06-06 12:08:01 +00007224 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007225 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007226}
7227
Dan Gohmand858e902010-04-17 15:26:15 +00007228SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007229 EVT VT = Op.getValueType();
7230 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007231 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007232 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00007233
7234 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007235 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00007236 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00007237 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007238 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007239 }
Evan Cheng18efe262007-12-14 02:13:44 +00007240
Evan Cheng152804e2007-12-14 08:30:15 +00007241 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007242 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007243 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00007244
7245 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007246 SDValue Ops[] = {
7247 Op,
7248 DAG.getConstant(NumBits+NumBits-1, OpVT),
7249 DAG.getConstant(X86::COND_E, MVT::i8),
7250 Op.getValue(1)
7251 };
7252 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00007253
7254 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00007255 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00007256
Owen Anderson825b72b2009-08-11 20:47:22 +00007257 if (VT == MVT::i8)
7258 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007259 return Op;
7260}
7261
Dan Gohmand858e902010-04-17 15:26:15 +00007262SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007263 EVT VT = Op.getValueType();
7264 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007265 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007266 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00007267
7268 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007269 if (VT == MVT::i8) {
7270 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007271 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007272 }
Evan Cheng152804e2007-12-14 08:30:15 +00007273
7274 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007275 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007276 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00007277
7278 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007279 SDValue Ops[] = {
7280 Op,
7281 DAG.getConstant(NumBits, OpVT),
7282 DAG.getConstant(X86::COND_E, MVT::i8),
7283 Op.getValue(1)
7284 };
7285 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00007286
Owen Anderson825b72b2009-08-11 20:47:22 +00007287 if (VT == MVT::i8)
7288 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007289 return Op;
7290}
7291
Dan Gohmand858e902010-04-17 15:26:15 +00007292SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007293 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007294 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007295 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00007296
Mon P Wangaf9b9522008-12-18 21:42:19 +00007297 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
7298 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
7299 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
7300 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
7301 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
7302 //
7303 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
7304 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
7305 // return AloBlo + AloBhi + AhiBlo;
7306
7307 SDValue A = Op.getOperand(0);
7308 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007309
Dale Johannesene4d209d2009-02-03 20:21:25 +00007310 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007311 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7312 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007313 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007314 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7315 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007316 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007317 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007318 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007319 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007320 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007321 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007322 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007323 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007324 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007325 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007326 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7327 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007328 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007329 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7330 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007331 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
7332 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00007333 return Res;
7334}
7335
7336
Dan Gohmand858e902010-04-17 15:26:15 +00007337SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +00007338 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
7339 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00007340 // looks for this combo and may remove the "setcc" instruction if the "setcc"
7341 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00007342 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00007343 SDValue LHS = N->getOperand(0);
7344 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00007345 unsigned BaseOp = 0;
7346 unsigned Cond = 0;
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007347 DebugLoc dl = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00007348
7349 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007350 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00007351 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00007352 // A subtract of one will be selected as a INC. Note that INC doesn't
7353 // set CF, so we can't do this for UADDO.
7354 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
7355 if (C->getAPIntValue() == 1) {
7356 BaseOp = X86ISD::INC;
7357 Cond = X86::COND_O;
7358 break;
7359 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007360 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00007361 Cond = X86::COND_O;
7362 break;
7363 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007364 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00007365 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007366 break;
7367 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00007368 // A subtract of one will be selected as a DEC. Note that DEC doesn't
7369 // set CF, so we can't do this for USUBO.
7370 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
7371 if (C->getAPIntValue() == 1) {
7372 BaseOp = X86ISD::DEC;
7373 Cond = X86::COND_O;
7374 break;
7375 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007376 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00007377 Cond = X86::COND_O;
7378 break;
7379 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007380 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00007381 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007382 break;
7383 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00007384 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00007385 Cond = X86::COND_O;
7386 break;
7387 case ISD::UMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00007388 BaseOp = X86ISD::UMUL;
Dan Gohman653456c2009-01-07 00:15:08 +00007389 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007390 break;
7391 }
Bill Wendling3fafd932008-11-26 22:37:40 +00007392
Bill Wendling61edeb52008-12-02 01:06:39 +00007393 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007394 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007395 SDValue Sum = DAG.getNode(BaseOp, dl, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00007396
Bill Wendling61edeb52008-12-02 01:06:39 +00007397 SDValue SetCC =
Dale Johannesene4d209d2009-02-03 20:21:25 +00007398 DAG.getNode(X86ISD::SETCC, dl, N->getValueType(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00007399 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00007400
Bill Wendling61edeb52008-12-02 01:06:39 +00007401 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
7402 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00007403}
7404
Dan Gohmand858e902010-04-17 15:26:15 +00007405SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007406 EVT T = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007407 DebugLoc dl = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00007408 unsigned Reg = 0;
7409 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00007410 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007411 default:
7412 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007413 case MVT::i8: Reg = X86::AL; size = 1; break;
7414 case MVT::i16: Reg = X86::AX; size = 2; break;
7415 case MVT::i32: Reg = X86::EAX; size = 4; break;
7416 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00007417 assert(Subtarget->is64Bit() && "Node not type legal!");
7418 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00007419 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007420 }
Dale Johannesendd64c412009-02-04 00:33:20 +00007421 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), dl, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00007422 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00007423 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00007424 Op.getOperand(1),
7425 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00007426 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00007427 cpIn.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00007428 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007429 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, dl, Tys, Ops, 5);
Scott Michelfdc40a02009-02-17 22:15:04 +00007430 SDValue cpOut =
Dale Johannesendd64c412009-02-04 00:33:20 +00007431 DAG.getCopyFromReg(Result.getValue(0), dl, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00007432 return cpOut;
7433}
7434
Duncan Sands1607f052008-12-01 11:39:25 +00007435SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007436 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +00007437 assert(Subtarget->is64Bit() && "Result not type legalized?");
Owen Anderson825b72b2009-08-11 20:47:22 +00007438 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00007439 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007440 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007441 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007442 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
7443 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00007444 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00007445 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
7446 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00007447 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00007448 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00007449 rdx.getValue(1)
7450 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007451 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007452}
7453
Dan Gohmand858e902010-04-17 15:26:15 +00007454SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +00007455 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007456 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007457 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007458 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00007459 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007460 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007461 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00007462 Node->getOperand(0),
7463 Node->getOperand(1), negOp,
7464 cast<AtomicSDNode>(Node)->getSrcValue(),
7465 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00007466}
7467
Evan Cheng0db9fe62006-04-25 20:13:52 +00007468/// LowerOperation - Provide custom lowering hooks for some operations.
7469///
Dan Gohmand858e902010-04-17 15:26:15 +00007470SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007471 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007472 default: llvm_unreachable("Should not custom lower this!");
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007473 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
7474 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007475 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00007476 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007477 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
7478 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
7479 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
7480 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
7481 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
7482 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007483 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00007484 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007485 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007486 case ISD::SHL_PARTS:
7487 case ISD::SRA_PARTS:
7488 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
7489 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007490 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007491 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00007492 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007493 case ISD::FABS: return LowerFABS(Op, DAG);
7494 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007495 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00007496 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00007497 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00007498 case ISD::SELECT: return LowerSELECT(Op, DAG);
7499 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007500 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007501 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00007502 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00007503 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007504 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00007505 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
7506 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007507 case ISD::FRAME_TO_ARGS_OFFSET:
7508 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007509 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007510 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007511 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00007512 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00007513 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
7514 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00007515 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00007516 case ISD::SADDO:
7517 case ISD::UADDO:
7518 case ISD::SSUBO:
7519 case ISD::USUBO:
7520 case ISD::SMULO:
7521 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00007522 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007523 }
Chris Lattner27a6c732007-11-24 07:07:01 +00007524}
7525
Duncan Sands1607f052008-12-01 11:39:25 +00007526void X86TargetLowering::
7527ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00007528 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007529 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007530 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00007531 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00007532
7533 SDValue Chain = Node->getOperand(0);
7534 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007535 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007536 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007537 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007538 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00007539 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00007540 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00007541 SDValue Result =
7542 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
7543 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00007544 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00007545 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007546 Results.push_back(Result.getValue(2));
7547}
7548
Duncan Sands126d9072008-07-04 11:47:58 +00007549/// ReplaceNodeResults - Replace a node with an illegal result type
7550/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00007551void X86TargetLowering::ReplaceNodeResults(SDNode *N,
7552 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00007553 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007554 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00007555 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00007556 default:
Duncan Sands1607f052008-12-01 11:39:25 +00007557 assert(false && "Do not know how to custom type legalize this operation!");
7558 return;
7559 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00007560 std::pair<SDValue,SDValue> Vals =
7561 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00007562 SDValue FIST = Vals.first, StackSlot = Vals.second;
7563 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00007564 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00007565 // Return a load from the stack slot.
David Greene67c9d422010-02-15 16:53:33 +00007566 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot, NULL, 0,
7567 false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00007568 }
7569 return;
7570 }
7571 case ISD::READCYCLECOUNTER: {
Owen Anderson825b72b2009-08-11 20:47:22 +00007572 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00007573 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007574 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007575 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00007576 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007577 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007578 eax.getValue(2));
7579 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
7580 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00007581 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007582 Results.push_back(edx.getValue(1));
7583 return;
7584 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007585 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00007586 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007587 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00007588 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00007589 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
7590 DAG.getConstant(0, MVT::i32));
7591 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
7592 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00007593 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
7594 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00007595 cpInL.getValue(1));
7596 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00007597 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
7598 DAG.getConstant(0, MVT::i32));
7599 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
7600 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00007601 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00007602 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00007603 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00007604 swapInL.getValue(1));
7605 SDValue Ops[] = { swapInH.getValue(0),
7606 N->getOperand(1),
7607 swapInH.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00007608 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007609 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, dl, Tys, Ops, 3);
Dale Johannesendd64c412009-02-04 00:33:20 +00007610 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00007611 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00007612 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00007613 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00007614 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00007615 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007616 Results.push_back(cpOutH.getValue(1));
7617 return;
7618 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007619 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00007620 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
7621 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007622 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00007623 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
7624 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007625 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00007626 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
7627 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007628 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00007629 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
7630 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007631 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00007632 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
7633 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007634 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00007635 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
7636 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007637 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00007638 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
7639 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00007640 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007641}
7642
Evan Cheng72261582005-12-20 06:22:03 +00007643const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
7644 switch (Opcode) {
7645 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00007646 case X86ISD::BSF: return "X86ISD::BSF";
7647 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00007648 case X86ISD::SHLD: return "X86ISD::SHLD";
7649 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00007650 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00007651 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00007652 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00007653 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00007654 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00007655 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00007656 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
7657 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
7658 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00007659 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00007660 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00007661 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00007662 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00007663 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00007664 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00007665 case X86ISD::COMI: return "X86ISD::COMI";
7666 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00007667 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00007668 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Evan Cheng72261582005-12-20 06:22:03 +00007669 case X86ISD::CMOV: return "X86ISD::CMOV";
7670 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00007671 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00007672 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
7673 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00007674 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00007675 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00007676 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00007677 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00007678 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00007679 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
7680 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00007681 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007682 case X86ISD::MMX_PINSRW: return "X86ISD::MMX_PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00007683 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Evan Cheng8ca29322006-11-10 21:43:37 +00007684 case X86ISD::FMAX: return "X86ISD::FMAX";
7685 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00007686 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
7687 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007688 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Rafael Espindola094fad32009-04-08 21:14:34 +00007689 case X86ISD::SegmentBaseAddress: return "X86ISD::SegmentBaseAddress";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007690 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00007691 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007692 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00007693 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
7694 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007695 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
7696 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
7697 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
7698 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
7699 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
7700 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00007701 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
7702 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00007703 case X86ISD::VSHL: return "X86ISD::VSHL";
7704 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00007705 case X86ISD::CMPPD: return "X86ISD::CMPPD";
7706 case X86ISD::CMPPS: return "X86ISD::CMPPS";
7707 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
7708 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
7709 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
7710 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
7711 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
7712 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
7713 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
7714 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007715 case X86ISD::ADD: return "X86ISD::ADD";
7716 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingd350e022008-12-12 21:15:41 +00007717 case X86ISD::SMUL: return "X86ISD::SMUL";
7718 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00007719 case X86ISD::INC: return "X86ISD::INC";
7720 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00007721 case X86ISD::OR: return "X86ISD::OR";
7722 case X86ISD::XOR: return "X86ISD::XOR";
7723 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00007724 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00007725 case X86ISD::PTEST: return "X86ISD::PTEST";
Dan Gohmand6708ea2009-08-15 01:38:56 +00007726 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00007727 case X86ISD::MINGW_ALLOCA: return "X86ISD::MINGW_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00007728 }
7729}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007730
Chris Lattnerc9addb72007-03-30 23:15:24 +00007731// isLegalAddressingMode - Return true if the addressing mode represented
7732// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00007733bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00007734 const Type *Ty) const {
7735 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007736 CodeModel::Model M = getTargetMachine().getCodeModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00007737
Chris Lattnerc9addb72007-03-30 23:15:24 +00007738 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007739 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00007740 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007741
Chris Lattnerc9addb72007-03-30 23:15:24 +00007742 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00007743 unsigned GVFlags =
7744 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007745
Chris Lattnerdfed4132009-07-10 07:38:24 +00007746 // If a reference to this global requires an extra load, we can't fold it.
7747 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00007748 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007749
Chris Lattnerdfed4132009-07-10 07:38:24 +00007750 // If BaseGV requires a register for the PIC base, we cannot also have a
7751 // BaseReg specified.
7752 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00007753 return false;
Evan Cheng52787842007-08-01 23:46:47 +00007754
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007755 // If lower 4G is not available, then we must use rip-relative addressing.
7756 if (Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
7757 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00007758 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007759
Chris Lattnerc9addb72007-03-30 23:15:24 +00007760 switch (AM.Scale) {
7761 case 0:
7762 case 1:
7763 case 2:
7764 case 4:
7765 case 8:
7766 // These scales always work.
7767 break;
7768 case 3:
7769 case 5:
7770 case 9:
7771 // These scales are formed with basereg+scalereg. Only accept if there is
7772 // no basereg yet.
7773 if (AM.HasBaseReg)
7774 return false;
7775 break;
7776 default: // Other stuff never works.
7777 return false;
7778 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007779
Chris Lattnerc9addb72007-03-30 23:15:24 +00007780 return true;
7781}
7782
7783
Evan Cheng2bd122c2007-10-26 01:56:11 +00007784bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00007785 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00007786 return false;
Evan Chenge127a732007-10-29 07:57:50 +00007787 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
7788 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00007789 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00007790 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00007791 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00007792}
7793
Owen Andersone50ed302009-08-10 22:56:29 +00007794bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007795 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007796 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007797 unsigned NumBits1 = VT1.getSizeInBits();
7798 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00007799 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007800 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00007801 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007802}
Evan Cheng2bd122c2007-10-26 01:56:11 +00007803
Dan Gohman97121ba2009-04-08 00:15:30 +00007804bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00007805 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00007806 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00007807}
7808
Owen Andersone50ed302009-08-10 22:56:29 +00007809bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00007810 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00007811 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00007812}
7813
Owen Andersone50ed302009-08-10 22:56:29 +00007814bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00007815 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00007816 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00007817}
7818
Evan Cheng60c07e12006-07-05 22:17:51 +00007819/// isShuffleMaskLegal - Targets can use this to indicate that they only
7820/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
7821/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
7822/// are assumed to be legal.
7823bool
Eric Christopherfd179292009-08-27 18:07:15 +00007824X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00007825 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +00007826 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +00007827 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +00007828 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +00007829
Nate Begemana09008b2009-10-19 02:17:23 +00007830 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00007831 return (VT.getVectorNumElements() == 2 ||
7832 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
7833 isMOVLMask(M, VT) ||
7834 isSHUFPMask(M, VT) ||
7835 isPSHUFDMask(M, VT) ||
7836 isPSHUFHWMask(M, VT) ||
7837 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00007838 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00007839 isUNPCKLMask(M, VT) ||
7840 isUNPCKHMask(M, VT) ||
7841 isUNPCKL_v_undef_Mask(M, VT) ||
7842 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00007843}
7844
Dan Gohman7d8143f2008-04-09 20:09:42 +00007845bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00007846X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00007847 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00007848 unsigned NumElts = VT.getVectorNumElements();
7849 // FIXME: This collection of masks seems suspect.
7850 if (NumElts == 2)
7851 return true;
7852 if (NumElts == 4 && VT.getSizeInBits() == 128) {
7853 return (isMOVLMask(Mask, VT) ||
7854 isCommutedMOVLMask(Mask, VT, true) ||
7855 isSHUFPMask(Mask, VT) ||
7856 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00007857 }
7858 return false;
7859}
7860
7861//===----------------------------------------------------------------------===//
7862// X86 Scheduler Hooks
7863//===----------------------------------------------------------------------===//
7864
Mon P Wang63307c32008-05-05 19:05:59 +00007865// private utility function
7866MachineBasicBlock *
7867X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
7868 MachineBasicBlock *MBB,
7869 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007870 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007871 unsigned LoadOpc,
7872 unsigned CXchgOpc,
7873 unsigned copyOpc,
7874 unsigned notOpc,
7875 unsigned EAXreg,
7876 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007877 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00007878 // For the atomic bitwise operator, we generate
7879 // thisMBB:
7880 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00007881 // ld t1 = [bitinstr.addr]
7882 // op t2 = t1, [bitinstr.val]
7883 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00007884 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
7885 // bz newMBB
7886 // fallthrough -->nextMBB
7887 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7888 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007889 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00007890 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007891
Mon P Wang63307c32008-05-05 19:05:59 +00007892 /// First build the CFG
7893 MachineFunction *F = MBB->getParent();
7894 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007895 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7896 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7897 F->insert(MBBIter, newMBB);
7898 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007899
Mon P Wang63307c32008-05-05 19:05:59 +00007900 // Move all successors to thisMBB to nextMBB
7901 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007902
Mon P Wang63307c32008-05-05 19:05:59 +00007903 // Update thisMBB to fall through to newMBB
7904 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007905
Mon P Wang63307c32008-05-05 19:05:59 +00007906 // newMBB jumps to itself and fall through to nextMBB
7907 newMBB->addSuccessor(nextMBB);
7908 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007909
Mon P Wang63307c32008-05-05 19:05:59 +00007910 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007911 assert(bInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00007912 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00007913 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00007914 MachineOperand& destOper = bInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007915 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00007916 int numArgs = bInstr->getNumOperands() - 1;
7917 for (int i=0; i < numArgs; ++i)
7918 argOpers[i] = &bInstr->getOperand(i+1);
7919
7920 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007921 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
7922 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00007923
Dale Johannesen140be2d2008-08-19 18:47:28 +00007924 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007925 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00007926 for (int i=0; i <= lastAddrIndx; ++i)
7927 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007928
Dale Johannesen140be2d2008-08-19 18:47:28 +00007929 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007930 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007931 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007932 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007933 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007934 tt = t1;
7935
Dale Johannesen140be2d2008-08-19 18:47:28 +00007936 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00007937 assert((argOpers[valArgIndx]->isReg() ||
7938 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00007939 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00007940 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007941 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00007942 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007943 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007944 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00007945 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007946
Dale Johannesene4d209d2009-02-03 20:21:25 +00007947 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00007948 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007949
Dale Johannesene4d209d2009-02-03 20:21:25 +00007950 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00007951 for (int i=0; i <= lastAddrIndx; ++i)
7952 (*MIB).addOperand(*argOpers[i]);
7953 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00007954 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00007955 (*MIB).setMemRefs(bInstr->memoperands_begin(),
7956 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00007957
Dale Johannesene4d209d2009-02-03 20:21:25 +00007958 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00007959 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00007960
Mon P Wang63307c32008-05-05 19:05:59 +00007961 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00007962 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00007963
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007964 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00007965 return nextMBB;
7966}
7967
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00007968// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00007969MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007970X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
7971 MachineBasicBlock *MBB,
7972 unsigned regOpcL,
7973 unsigned regOpcH,
7974 unsigned immOpcL,
7975 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007976 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007977 // For the atomic bitwise operator, we generate
7978 // thisMBB (instructions are in pairs, except cmpxchg8b)
7979 // ld t1,t2 = [bitinstr.addr]
7980 // newMBB:
7981 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
7982 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00007983 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007984 // mov ECX, EBX <- t5, t6
7985 // mov EAX, EDX <- t1, t2
7986 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
7987 // mov t3, t4 <- EAX, EDX
7988 // bz newMBB
7989 // result in out1, out2
7990 // fallthrough -->nextMBB
7991
7992 const TargetRegisterClass *RC = X86::GR32RegisterClass;
7993 const unsigned LoadOpc = X86::MOV32rm;
7994 const unsigned copyOpc = X86::MOV32rr;
7995 const unsigned NotOpc = X86::NOT32r;
7996 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7997 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
7998 MachineFunction::iterator MBBIter = MBB;
7999 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00008000
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008001 /// First build the CFG
8002 MachineFunction *F = MBB->getParent();
8003 MachineBasicBlock *thisMBB = MBB;
8004 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
8005 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
8006 F->insert(MBBIter, newMBB);
8007 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008008
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008009 // Move all successors to thisMBB to nextMBB
8010 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008011
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008012 // Update thisMBB to fall through to newMBB
8013 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008014
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008015 // newMBB jumps to itself and fall through to nextMBB
8016 newMBB->addSuccessor(nextMBB);
8017 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008018
Dale Johannesene4d209d2009-02-03 20:21:25 +00008019 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008020 // Insert instructions into newMBB based on incoming instruction
8021 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008022 assert(bInstr->getNumOperands() < X86AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00008023 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008024 MachineOperand& dest1Oper = bInstr->getOperand(0);
8025 MachineOperand& dest2Oper = bInstr->getOperand(1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008026 MachineOperand* argOpers[2 + X86AddrNumOperands];
Dan Gohman71ea4e52010-05-14 21:01:44 +00008027 for (int i=0; i < 2 + X86AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008028 argOpers[i] = &bInstr->getOperand(i+2);
8029
Dan Gohman71ea4e52010-05-14 21:01:44 +00008030 // We use some of the operands multiple times, so conservatively just
8031 // clear any kill flags that might be present.
8032 if (argOpers[i]->isReg() && argOpers[i]->isUse())
8033 argOpers[i]->setIsKill(false);
8034 }
8035
Evan Chengad5b52f2010-01-08 19:14:57 +00008036 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008037 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00008038
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008039 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008040 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008041 for (int i=0; i <= lastAddrIndx; ++i)
8042 (*MIB).addOperand(*argOpers[i]);
8043 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008044 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00008045 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00008046 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008047 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00008048 MachineOperand newOp3 = *(argOpers[3]);
8049 if (newOp3.isImm())
8050 newOp3.setImm(newOp3.getImm()+4);
8051 else
8052 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008053 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00008054 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008055
8056 // t3/4 are defined later, at the bottom of the loop
8057 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
8058 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008059 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008060 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008061 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008062 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
8063
Evan Cheng306b4ca2010-01-08 23:41:50 +00008064 // The subsequent operations should be using the destination registers of
8065 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00008066 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00008067 t1 = F->getRegInfo().createVirtualRegister(RC);
8068 t2 = F->getRegInfo().createVirtualRegister(RC);
8069 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
8070 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008071 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00008072 t1 = dest1Oper.getReg();
8073 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008074 }
8075
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008076 int valArgIndx = lastAddrIndx + 1;
8077 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00008078 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008079 "invalid operand");
8080 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
8081 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008082 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008083 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008084 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008085 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00008086 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00008087 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008088 (*MIB).addOperand(*argOpers[valArgIndx]);
8089 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00008090 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008091 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00008092 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008093 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008094 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008095 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008096 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00008097 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00008098 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008099 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008100
Dale Johannesene4d209d2009-02-03 20:21:25 +00008101 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008102 MIB.addReg(t1);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008103 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008104 MIB.addReg(t2);
8105
Dale Johannesene4d209d2009-02-03 20:21:25 +00008106 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008107 MIB.addReg(t5);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008108 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008109 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00008110
Dale Johannesene4d209d2009-02-03 20:21:25 +00008111 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008112 for (int i=0; i <= lastAddrIndx; ++i)
8113 (*MIB).addOperand(*argOpers[i]);
8114
8115 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00008116 (*MIB).setMemRefs(bInstr->memoperands_begin(),
8117 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008118
Dale Johannesene4d209d2009-02-03 20:21:25 +00008119 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008120 MIB.addReg(X86::EAX);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008121 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008122 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00008123
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008124 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008125 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008126
8127 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
8128 return nextMBB;
8129}
8130
8131// private utility function
8132MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00008133X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
8134 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00008135 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00008136 // For the atomic min/max operator, we generate
8137 // thisMBB:
8138 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00008139 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00008140 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00008141 // cmp t1, t2
8142 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00008143 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00008144 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
8145 // bz newMBB
8146 // fallthrough -->nextMBB
8147 //
8148 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8149 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008150 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00008151 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00008152
Mon P Wang63307c32008-05-05 19:05:59 +00008153 /// First build the CFG
8154 MachineFunction *F = MBB->getParent();
8155 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008156 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
8157 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
8158 F->insert(MBBIter, newMBB);
8159 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008160
Dan Gohmand6708ea2009-08-15 01:38:56 +00008161 // Move all successors of thisMBB to nextMBB
Mon P Wang63307c32008-05-05 19:05:59 +00008162 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008163
Mon P Wang63307c32008-05-05 19:05:59 +00008164 // Update thisMBB to fall through to newMBB
8165 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008166
Mon P Wang63307c32008-05-05 19:05:59 +00008167 // newMBB jumps to newMBB and fall through to nextMBB
8168 newMBB->addSuccessor(nextMBB);
8169 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008170
Dale Johannesene4d209d2009-02-03 20:21:25 +00008171 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00008172 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008173 assert(mInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00008174 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00008175 MachineOperand& destOper = mInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008176 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00008177 int numArgs = mInstr->getNumOperands() - 1;
8178 for (int i=0; i < numArgs; ++i)
8179 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008180
Mon P Wang63307c32008-05-05 19:05:59 +00008181 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008182 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
8183 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00008184
Mon P Wangab3e7472008-05-05 22:56:23 +00008185 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008186 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00008187 for (int i=0; i <= lastAddrIndx; ++i)
8188 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00008189
Mon P Wang63307c32008-05-05 19:05:59 +00008190 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00008191 assert((argOpers[valArgIndx]->isReg() ||
8192 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00008193 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00008194
8195 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00008196 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008197 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00008198 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008199 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00008200 (*MIB).addOperand(*argOpers[valArgIndx]);
8201
Dale Johannesene4d209d2009-02-03 20:21:25 +00008202 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00008203 MIB.addReg(t1);
8204
Dale Johannesene4d209d2009-02-03 20:21:25 +00008205 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00008206 MIB.addReg(t1);
8207 MIB.addReg(t2);
8208
8209 // Generate movc
8210 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008211 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00008212 MIB.addReg(t2);
8213 MIB.addReg(t1);
8214
8215 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00008216 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00008217 for (int i=0; i <= lastAddrIndx; ++i)
8218 (*MIB).addOperand(*argOpers[i]);
8219 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00008220 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00008221 (*MIB).setMemRefs(mInstr->memoperands_begin(),
8222 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +00008223
Dale Johannesene4d209d2009-02-03 20:21:25 +00008224 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00008225 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00008226
Mon P Wang63307c32008-05-05 19:05:59 +00008227 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008228 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00008229
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008230 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00008231 return nextMBB;
8232}
8233
Eric Christopherf83a5de2009-08-27 18:08:16 +00008234// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
8235// all of this code can be replaced with that in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +00008236MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +00008237X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +00008238 unsigned numArgs, bool memArg) const {
Eric Christopherb120ab42009-08-18 22:50:32 +00008239
8240 MachineFunction *F = BB->getParent();
8241 DebugLoc dl = MI->getDebugLoc();
8242 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8243
8244 unsigned Opc;
Evan Chengce319102009-09-19 09:51:03 +00008245 if (memArg)
8246 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
8247 else
8248 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
Eric Christopherb120ab42009-08-18 22:50:32 +00008249
8250 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(Opc));
8251
8252 for (unsigned i = 0; i < numArgs; ++i) {
8253 MachineOperand &Op = MI->getOperand(i+1);
8254
8255 if (!(Op.isReg() && Op.isImplicit()))
8256 MIB.addOperand(Op);
8257 }
8258
8259 BuildMI(BB, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
8260 .addReg(X86::XMM0);
8261
8262 F->DeleteMachineInstr(MI);
8263
8264 return BB;
8265}
8266
8267MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +00008268X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
8269 MachineInstr *MI,
8270 MachineBasicBlock *MBB) const {
8271 // Emit code to save XMM registers to the stack. The ABI says that the
8272 // number of registers to save is given in %al, so it's theoretically
8273 // possible to do an indirect jump trick to avoid saving all of them,
8274 // however this code takes a simpler approach and just executes all
8275 // of the stores if %al is non-zero. It's less code, and it's probably
8276 // easier on the hardware branch predictor, and stores aren't all that
8277 // expensive anyway.
8278
8279 // Create the new basic blocks. One block contains all the XMM stores,
8280 // and one block is the final destination regardless of whether any
8281 // stores were performed.
8282 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
8283 MachineFunction *F = MBB->getParent();
8284 MachineFunction::iterator MBBIter = MBB;
8285 ++MBBIter;
8286 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
8287 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
8288 F->insert(MBBIter, XMMSaveMBB);
8289 F->insert(MBBIter, EndMBB);
8290
8291 // Set up the CFG.
8292 // Move any original successors of MBB to the end block.
8293 EndMBB->transferSuccessors(MBB);
8294 // The original block will now fall through to the XMM save block.
8295 MBB->addSuccessor(XMMSaveMBB);
8296 // The XMMSaveMBB will fall through to the end block.
8297 XMMSaveMBB->addSuccessor(EndMBB);
8298
8299 // Now add the instructions.
8300 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8301 DebugLoc DL = MI->getDebugLoc();
8302
8303 unsigned CountReg = MI->getOperand(0).getReg();
8304 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
8305 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
8306
8307 if (!Subtarget->isTargetWin64()) {
8308 // If %al is 0, branch around the XMM save block.
8309 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008310 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008311 MBB->addSuccessor(EndMBB);
8312 }
8313
8314 // In the XMM save block, save all the XMM argument registers.
8315 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
8316 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +00008317 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +00008318 F->getMachineMemOperand(
8319 PseudoSourceValue::getFixedStack(RegSaveFrameIndex),
8320 MachineMemOperand::MOStore, Offset,
8321 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008322 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
8323 .addFrameIndex(RegSaveFrameIndex)
8324 .addImm(/*Scale=*/1)
8325 .addReg(/*IndexReg=*/0)
8326 .addImm(/*Disp=*/Offset)
8327 .addReg(/*Segment=*/0)
8328 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +00008329 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008330 }
8331
8332 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8333
8334 return EndMBB;
8335}
Mon P Wang63307c32008-05-05 19:05:59 +00008336
Evan Cheng60c07e12006-07-05 22:17:51 +00008337MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +00008338X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00008339 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +00008340 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8341 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +00008342
Chris Lattner52600972009-09-02 05:57:00 +00008343 // To "insert" a SELECT_CC instruction, we actually have to insert the
8344 // diamond control-flow pattern. The incoming instruction knows the
8345 // destination vreg to set, the condition code register to branch on, the
8346 // true/false values to select between, and a branch opcode to use.
8347 const BasicBlock *LLVM_BB = BB->getBasicBlock();
8348 MachineFunction::iterator It = BB;
8349 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +00008350
Chris Lattner52600972009-09-02 05:57:00 +00008351 // thisMBB:
8352 // ...
8353 // TrueVal = ...
8354 // cmpTY ccX, r1, r2
8355 // bCC copy1MBB
8356 // fallthrough --> copy0MBB
8357 MachineBasicBlock *thisMBB = BB;
8358 MachineFunction *F = BB->getParent();
8359 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
8360 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
8361 unsigned Opc =
8362 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
8363 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
8364 F->insert(It, copy0MBB);
8365 F->insert(It, sinkMBB);
Evan Chengce319102009-09-19 09:51:03 +00008366 // Update machine-CFG edges by first adding all successors of the current
Chris Lattner52600972009-09-02 05:57:00 +00008367 // block to the new block which will contain the Phi node for the select.
Daniel Dunbara279bc32009-09-20 02:20:51 +00008368 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00008369 E = BB->succ_end(); I != E; ++I)
Evan Chengce319102009-09-19 09:51:03 +00008370 sinkMBB->addSuccessor(*I);
Evan Chengce319102009-09-19 09:51:03 +00008371 // Next, remove all successors of the current block, and add the true
8372 // and fallthrough blocks as its successors.
8373 while (!BB->succ_empty())
8374 BB->removeSuccessor(BB->succ_begin());
Chris Lattner52600972009-09-02 05:57:00 +00008375 // Add the true and fallthrough blocks as its successors.
8376 BB->addSuccessor(copy0MBB);
8377 BB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00008378
Chris Lattner52600972009-09-02 05:57:00 +00008379 // copy0MBB:
8380 // %FalseValue = ...
8381 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +00008382 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00008383
Chris Lattner52600972009-09-02 05:57:00 +00008384 // sinkMBB:
8385 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
8386 // ...
Dan Gohman3335a222010-04-30 20:14:26 +00008387 BuildMI(sinkMBB, DL, TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +00008388 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
8389 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
8390
8391 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +00008392 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +00008393}
8394
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008395MachineBasicBlock *
8396X86TargetLowering::EmitLoweredMingwAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00008397 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008398 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8399 DebugLoc DL = MI->getDebugLoc();
8400 MachineFunction *F = BB->getParent();
8401
8402 // The lowering is pretty easy: we're just emitting the call to _alloca. The
8403 // non-trivial part is impdef of ESP.
8404 // FIXME: The code should be tweaked as soon as we'll try to do codegen for
8405 // mingw-w64.
8406
8407 BuildMI(BB, DL, TII->get(X86::CALLpcrel32))
8408 .addExternalSymbol("_alloca")
8409 .addReg(X86::EAX, RegState::Implicit)
8410 .addReg(X86::ESP, RegState::Implicit)
8411 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
8412 .addReg(X86::ESP, RegState::Define | RegState::Implicit);
8413
8414 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8415 return BB;
8416}
Chris Lattner52600972009-09-02 05:57:00 +00008417
8418MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00008419X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00008420 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00008421 switch (MI->getOpcode()) {
8422 default: assert(false && "Unexpected instr type to insert");
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008423 case X86::MINGW_ALLOCA:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00008424 return EmitLoweredMingwAlloca(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +00008425 case X86::CMOV_GR8:
Mon P Wang9e5ecb82008-12-12 01:25:51 +00008426 case X86::CMOV_V1I64:
Evan Cheng60c07e12006-07-05 22:17:51 +00008427 case X86::CMOV_FR32:
8428 case X86::CMOV_FR64:
8429 case X86::CMOV_V4F32:
8430 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +00008431 case X86::CMOV_V2I64:
Chris Lattner314a1132010-03-14 18:31:44 +00008432 case X86::CMOV_GR16:
8433 case X86::CMOV_GR32:
8434 case X86::CMOV_RFP32:
8435 case X86::CMOV_RFP64:
8436 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00008437 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +00008438
Dale Johannesen849f2142007-07-03 00:53:03 +00008439 case X86::FP32_TO_INT16_IN_MEM:
8440 case X86::FP32_TO_INT32_IN_MEM:
8441 case X86::FP32_TO_INT64_IN_MEM:
8442 case X86::FP64_TO_INT16_IN_MEM:
8443 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00008444 case X86::FP64_TO_INT64_IN_MEM:
8445 case X86::FP80_TO_INT16_IN_MEM:
8446 case X86::FP80_TO_INT32_IN_MEM:
8447 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +00008448 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8449 DebugLoc DL = MI->getDebugLoc();
8450
Evan Cheng60c07e12006-07-05 22:17:51 +00008451 // Change the floating point control register to use "round towards zero"
8452 // mode when truncating to an integer value.
8453 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +00008454 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Chris Lattner52600972009-09-02 05:57:00 +00008455 addFrameReference(BuildMI(BB, DL, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008456
8457 // Load the old value of the high byte of the control word...
8458 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +00008459 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Chris Lattner52600972009-09-02 05:57:00 +00008460 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008461 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008462
8463 // Set the high part to be round to zero...
Chris Lattner52600972009-09-02 05:57:00 +00008464 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00008465 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00008466
8467 // Reload the modified control word now...
Chris Lattner52600972009-09-02 05:57:00 +00008468 addFrameReference(BuildMI(BB, DL, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008469
8470 // Restore the memory image of control word to original value
Chris Lattner52600972009-09-02 05:57:00 +00008471 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00008472 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00008473
8474 // Get the X86 opcode to use.
8475 unsigned Opc;
8476 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008477 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00008478 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
8479 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
8480 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
8481 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
8482 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
8483 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00008484 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
8485 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
8486 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00008487 }
8488
8489 X86AddressMode AM;
8490 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +00008491 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00008492 AM.BaseType = X86AddressMode::RegBase;
8493 AM.Base.Reg = Op.getReg();
8494 } else {
8495 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +00008496 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +00008497 }
8498 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +00008499 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00008500 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008501 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +00008502 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00008503 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008504 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +00008505 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00008506 AM.GV = Op.getGlobal();
8507 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00008508 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008509 }
Chris Lattner52600972009-09-02 05:57:00 +00008510 addFullAddress(BuildMI(BB, DL, TII->get(Opc)), AM)
Rafael Espindola8ef2b892009-04-08 08:09:33 +00008511 .addReg(MI->getOperand(X86AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00008512
8513 // Reload the original control word now.
Chris Lattner52600972009-09-02 05:57:00 +00008514 addFrameReference(BuildMI(BB, DL, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008515
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008516 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00008517 return BB;
8518 }
Eric Christopherb120ab42009-08-18 22:50:32 +00008519 // String/text processing lowering.
8520 case X86::PCMPISTRM128REG:
8521 return EmitPCMP(MI, BB, 3, false /* in-mem */);
8522 case X86::PCMPISTRM128MEM:
8523 return EmitPCMP(MI, BB, 3, true /* in-mem */);
8524 case X86::PCMPESTRM128REG:
8525 return EmitPCMP(MI, BB, 5, false /* in mem */);
8526 case X86::PCMPESTRM128MEM:
8527 return EmitPCMP(MI, BB, 5, true /* in mem */);
8528
8529 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +00008530 case X86::ATOMAND32:
8531 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008532 X86::AND32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008533 X86::LCMPXCHG32, X86::MOV32rr,
8534 X86::NOT32r, X86::EAX,
8535 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00008536 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +00008537 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
8538 X86::OR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008539 X86::LCMPXCHG32, X86::MOV32rr,
8540 X86::NOT32r, X86::EAX,
8541 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00008542 case X86::ATOMXOR32:
8543 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008544 X86::XOR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008545 X86::LCMPXCHG32, X86::MOV32rr,
8546 X86::NOT32r, X86::EAX,
8547 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008548 case X86::ATOMNAND32:
8549 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008550 X86::AND32ri, X86::MOV32rm,
8551 X86::LCMPXCHG32, X86::MOV32rr,
8552 X86::NOT32r, X86::EAX,
8553 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +00008554 case X86::ATOMMIN32:
8555 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
8556 case X86::ATOMMAX32:
8557 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
8558 case X86::ATOMUMIN32:
8559 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
8560 case X86::ATOMUMAX32:
8561 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +00008562
8563 case X86::ATOMAND16:
8564 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
8565 X86::AND16ri, X86::MOV16rm,
8566 X86::LCMPXCHG16, X86::MOV16rr,
8567 X86::NOT16r, X86::AX,
8568 X86::GR16RegisterClass);
8569 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +00008570 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008571 X86::OR16ri, X86::MOV16rm,
8572 X86::LCMPXCHG16, X86::MOV16rr,
8573 X86::NOT16r, X86::AX,
8574 X86::GR16RegisterClass);
8575 case X86::ATOMXOR16:
8576 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
8577 X86::XOR16ri, X86::MOV16rm,
8578 X86::LCMPXCHG16, X86::MOV16rr,
8579 X86::NOT16r, X86::AX,
8580 X86::GR16RegisterClass);
8581 case X86::ATOMNAND16:
8582 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
8583 X86::AND16ri, X86::MOV16rm,
8584 X86::LCMPXCHG16, X86::MOV16rr,
8585 X86::NOT16r, X86::AX,
8586 X86::GR16RegisterClass, true);
8587 case X86::ATOMMIN16:
8588 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
8589 case X86::ATOMMAX16:
8590 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
8591 case X86::ATOMUMIN16:
8592 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
8593 case X86::ATOMUMAX16:
8594 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
8595
8596 case X86::ATOMAND8:
8597 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
8598 X86::AND8ri, X86::MOV8rm,
8599 X86::LCMPXCHG8, X86::MOV8rr,
8600 X86::NOT8r, X86::AL,
8601 X86::GR8RegisterClass);
8602 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +00008603 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008604 X86::OR8ri, X86::MOV8rm,
8605 X86::LCMPXCHG8, X86::MOV8rr,
8606 X86::NOT8r, X86::AL,
8607 X86::GR8RegisterClass);
8608 case X86::ATOMXOR8:
8609 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
8610 X86::XOR8ri, X86::MOV8rm,
8611 X86::LCMPXCHG8, X86::MOV8rr,
8612 X86::NOT8r, X86::AL,
8613 X86::GR8RegisterClass);
8614 case X86::ATOMNAND8:
8615 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
8616 X86::AND8ri, X86::MOV8rm,
8617 X86::LCMPXCHG8, X86::MOV8rr,
8618 X86::NOT8r, X86::AL,
8619 X86::GR8RegisterClass, true);
8620 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008621 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +00008622 case X86::ATOMAND64:
8623 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008624 X86::AND64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008625 X86::LCMPXCHG64, X86::MOV64rr,
8626 X86::NOT64r, X86::RAX,
8627 X86::GR64RegisterClass);
8628 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +00008629 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
8630 X86::OR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008631 X86::LCMPXCHG64, X86::MOV64rr,
8632 X86::NOT64r, X86::RAX,
8633 X86::GR64RegisterClass);
8634 case X86::ATOMXOR64:
8635 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008636 X86::XOR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008637 X86::LCMPXCHG64, X86::MOV64rr,
8638 X86::NOT64r, X86::RAX,
8639 X86::GR64RegisterClass);
8640 case X86::ATOMNAND64:
8641 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
8642 X86::AND64ri32, X86::MOV64rm,
8643 X86::LCMPXCHG64, X86::MOV64rr,
8644 X86::NOT64r, X86::RAX,
8645 X86::GR64RegisterClass, true);
8646 case X86::ATOMMIN64:
8647 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
8648 case X86::ATOMMAX64:
8649 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
8650 case X86::ATOMUMIN64:
8651 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
8652 case X86::ATOMUMAX64:
8653 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008654
8655 // This group does 64-bit operations on a 32-bit host.
8656 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008657 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008658 X86::AND32rr, X86::AND32rr,
8659 X86::AND32ri, X86::AND32ri,
8660 false);
8661 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008662 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008663 X86::OR32rr, X86::OR32rr,
8664 X86::OR32ri, X86::OR32ri,
8665 false);
8666 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008667 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008668 X86::XOR32rr, X86::XOR32rr,
8669 X86::XOR32ri, X86::XOR32ri,
8670 false);
8671 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008672 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008673 X86::AND32rr, X86::AND32rr,
8674 X86::AND32ri, X86::AND32ri,
8675 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008676 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008677 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008678 X86::ADD32rr, X86::ADC32rr,
8679 X86::ADD32ri, X86::ADC32ri,
8680 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008681 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008682 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008683 X86::SUB32rr, X86::SBB32rr,
8684 X86::SUB32ri, X86::SBB32ri,
8685 false);
Dale Johannesen880ae362008-10-03 22:25:52 +00008686 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008687 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +00008688 X86::MOV32rr, X86::MOV32rr,
8689 X86::MOV32ri, X86::MOV32ri,
8690 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008691 case X86::VASTART_SAVE_XMM_REGS:
8692 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +00008693 }
8694}
8695
8696//===----------------------------------------------------------------------===//
8697// X86 Optimization Hooks
8698//===----------------------------------------------------------------------===//
8699
Dan Gohman475871a2008-07-27 21:46:04 +00008700void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00008701 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008702 APInt &KnownZero,
8703 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00008704 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00008705 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008706 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00008707 assert((Opc >= ISD::BUILTIN_OP_END ||
8708 Opc == ISD::INTRINSIC_WO_CHAIN ||
8709 Opc == ISD::INTRINSIC_W_CHAIN ||
8710 Opc == ISD::INTRINSIC_VOID) &&
8711 "Should use MaskedValueIsZero if you don't know whether Op"
8712 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008713
Dan Gohmanf4f92f52008-02-13 23:07:24 +00008714 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008715 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00008716 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +00008717 case X86ISD::ADD:
8718 case X86ISD::SUB:
8719 case X86ISD::SMUL:
8720 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +00008721 case X86ISD::INC:
8722 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +00008723 case X86ISD::OR:
8724 case X86ISD::XOR:
8725 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +00008726 // These nodes' second result is a boolean.
8727 if (Op.getResNo() == 0)
8728 break;
8729 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008730 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008731 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
8732 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +00008733 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008734 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008735}
Chris Lattner259e97c2006-01-31 19:43:35 +00008736
Evan Cheng206ee9d2006-07-07 08:33:52 +00008737/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +00008738/// node is a GlobalAddress + offset.
8739bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +00008740 const GlobalValue* &GA,
8741 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +00008742 if (N->getOpcode() == X86ISD::Wrapper) {
8743 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00008744 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00008745 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +00008746 return true;
8747 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00008748 }
Evan Chengad4196b2008-05-12 19:56:52 +00008749 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +00008750}
8751
Evan Cheng206ee9d2006-07-07 08:33:52 +00008752/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
8753/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
8754/// if the load addresses are consecutive, non-overlapping, and in the right
Nate Begemanfdea31a2010-03-24 20:49:50 +00008755/// order.
Dan Gohman475871a2008-07-27 21:46:04 +00008756static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00008757 const TargetLowering &TLI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00008758 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008759 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00008760 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
Mon P Wang1e955802009-04-03 02:43:30 +00008761
Eli Friedman7a5e5552009-06-07 06:52:44 +00008762 if (VT.getSizeInBits() != 128)
8763 return SDValue();
8764
Nate Begemanfdea31a2010-03-24 20:49:50 +00008765 SmallVector<SDValue, 16> Elts;
8766 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
8767 Elts.push_back(DAG.getShuffleScalarElt(SVN, i));
8768
8769 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00008770}
Evan Chengd880b972008-05-09 21:53:03 +00008771
Dan Gohman1bbf72b2010-03-15 23:23:03 +00008772/// PerformShuffleCombine - Detect vector gather/scatter index generation
8773/// and convert it from being a bunch of shuffles and extracts to a simple
8774/// store and scalar loads to extract the elements.
8775static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
8776 const TargetLowering &TLI) {
8777 SDValue InputVector = N->getOperand(0);
8778
8779 // Only operate on vectors of 4 elements, where the alternative shuffling
8780 // gets to be more expensive.
8781 if (InputVector.getValueType() != MVT::v4i32)
8782 return SDValue();
8783
8784 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
8785 // single use which is a sign-extend or zero-extend, and all elements are
8786 // used.
8787 SmallVector<SDNode *, 4> Uses;
8788 unsigned ExtractedElements = 0;
8789 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
8790 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
8791 if (UI.getUse().getResNo() != InputVector.getResNo())
8792 return SDValue();
8793
8794 SDNode *Extract = *UI;
8795 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8796 return SDValue();
8797
8798 if (Extract->getValueType(0) != MVT::i32)
8799 return SDValue();
8800 if (!Extract->hasOneUse())
8801 return SDValue();
8802 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
8803 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
8804 return SDValue();
8805 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
8806 return SDValue();
8807
8808 // Record which element was extracted.
8809 ExtractedElements |=
8810 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
8811
8812 Uses.push_back(Extract);
8813 }
8814
8815 // If not all the elements were used, this may not be worthwhile.
8816 if (ExtractedElements != 15)
8817 return SDValue();
8818
8819 // Ok, we've now decided to do the transformation.
8820 DebugLoc dl = InputVector.getDebugLoc();
8821
8822 // Store the value to a temporary stack slot.
8823 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
8824 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr, NULL, 0,
8825 false, false, 0);
8826
8827 // Replace each use (extract) with a load of the appropriate element.
8828 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
8829 UE = Uses.end(); UI != UE; ++UI) {
8830 SDNode *Extract = *UI;
8831
8832 // Compute the element's address.
8833 SDValue Idx = Extract->getOperand(1);
8834 unsigned EltSize =
8835 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
8836 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
8837 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
8838
8839 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), OffsetVal, StackPtr);
8840
8841 // Load the scalar.
8842 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch, ScalarAddr,
8843 NULL, 0, false, false, 0);
8844
8845 // Replace the exact with the load.
8846 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
8847 }
8848
8849 // The replacement was made in place; don't return anything.
8850 return SDValue();
8851}
8852
Chris Lattner83e6c992006-10-04 06:57:07 +00008853/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008854static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +00008855 const X86Subtarget *Subtarget) {
8856 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008857 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +00008858 // Get the LHS/RHS of the select.
8859 SDValue LHS = N->getOperand(1);
8860 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +00008861
Dan Gohman670e5392009-09-21 18:03:22 +00008862 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +00008863 // instructions match the semantics of the common C idiom x<y?x:y but not
8864 // x<=y?x:y, because of how they handle negative zero (which can be
8865 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +00008866 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00008867 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +00008868 Cond.getOpcode() == ISD::SETCC) {
8869 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008870
Chris Lattner47b4ce82009-03-11 05:48:52 +00008871 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +00008872 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +00008873 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
8874 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +00008875 switch (CC) {
8876 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +00008877 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +00008878 // Converting this to a min would handle NaNs incorrectly, and swapping
8879 // the operands would cause it to handle comparisons between positive
8880 // and negative zero incorrectly.
8881 if (!FiniteOnlyFPMath() &&
8882 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))) {
8883 if (!UnsafeFPMath &&
8884 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8885 break;
8886 std::swap(LHS, RHS);
8887 }
Dan Gohman670e5392009-09-21 18:03:22 +00008888 Opcode = X86ISD::FMIN;
8889 break;
8890 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +00008891 // Converting this to a min would handle comparisons between positive
8892 // and negative zero incorrectly.
8893 if (!UnsafeFPMath &&
8894 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
8895 break;
Dan Gohman670e5392009-09-21 18:03:22 +00008896 Opcode = X86ISD::FMIN;
8897 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +00008898 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +00008899 // Converting this to a min would handle both negative zeros and NaNs
8900 // incorrectly, but we can swap the operands to fix both.
8901 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00008902 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008903 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +00008904 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008905 Opcode = X86ISD::FMIN;
8906 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008907
Dan Gohman670e5392009-09-21 18:03:22 +00008908 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +00008909 // Converting this to a max would handle comparisons between positive
8910 // and negative zero incorrectly.
8911 if (!UnsafeFPMath &&
8912 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
8913 break;
Dan Gohman670e5392009-09-21 18:03:22 +00008914 Opcode = X86ISD::FMAX;
8915 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +00008916 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +00008917 // Converting this to a max would handle NaNs incorrectly, and swapping
8918 // the operands would cause it to handle comparisons between positive
8919 // and negative zero incorrectly.
8920 if (!FiniteOnlyFPMath() &&
8921 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))) {
8922 if (!UnsafeFPMath &&
8923 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8924 break;
8925 std::swap(LHS, RHS);
8926 }
Dan Gohman670e5392009-09-21 18:03:22 +00008927 Opcode = X86ISD::FMAX;
8928 break;
8929 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +00008930 // Converting this to a max would handle both negative zeros and NaNs
8931 // incorrectly, but we can swap the operands to fix both.
8932 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00008933 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008934 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008935 case ISD::SETGE:
8936 Opcode = X86ISD::FMAX;
8937 break;
Chris Lattner83e6c992006-10-04 06:57:07 +00008938 }
Dan Gohman670e5392009-09-21 18:03:22 +00008939 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +00008940 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
8941 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +00008942 switch (CC) {
8943 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +00008944 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +00008945 // Converting this to a min would handle comparisons between positive
8946 // and negative zero incorrectly, and swapping the operands would
8947 // cause it to handle NaNs incorrectly.
8948 if (!UnsafeFPMath &&
8949 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
8950 if (!FiniteOnlyFPMath() &&
8951 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
8952 break;
8953 std::swap(LHS, RHS);
8954 }
Dan Gohman670e5392009-09-21 18:03:22 +00008955 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +00008956 break;
Dan Gohman670e5392009-09-21 18:03:22 +00008957 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +00008958 // Converting this to a min would handle NaNs incorrectly.
8959 if (!UnsafeFPMath &&
8960 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
8961 break;
Dan Gohman670e5392009-09-21 18:03:22 +00008962 Opcode = X86ISD::FMIN;
8963 break;
8964 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +00008965 // Converting this to a min would handle both negative zeros and NaNs
8966 // incorrectly, but we can swap the operands to fix both.
8967 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00008968 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008969 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008970 case ISD::SETGE:
8971 Opcode = X86ISD::FMIN;
8972 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008973
Dan Gohman670e5392009-09-21 18:03:22 +00008974 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +00008975 // Converting this to a max would handle NaNs incorrectly.
8976 if (!FiniteOnlyFPMath() &&
8977 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
8978 break;
Dan Gohman670e5392009-09-21 18:03:22 +00008979 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +00008980 break;
Dan Gohman670e5392009-09-21 18:03:22 +00008981 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +00008982 // Converting this to a max would handle comparisons between positive
8983 // and negative zero incorrectly, and swapping the operands would
8984 // cause it to handle NaNs incorrectly.
8985 if (!UnsafeFPMath &&
8986 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
8987 if (!FiniteOnlyFPMath() &&
8988 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
8989 break;
8990 std::swap(LHS, RHS);
8991 }
Dan Gohman670e5392009-09-21 18:03:22 +00008992 Opcode = X86ISD::FMAX;
8993 break;
8994 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +00008995 // Converting this to a max would handle both negative zeros and NaNs
8996 // incorrectly, but we can swap the operands to fix both.
8997 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00008998 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008999 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +00009000 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009001 Opcode = X86ISD::FMAX;
9002 break;
9003 }
Chris Lattner83e6c992006-10-04 06:57:07 +00009004 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009005
Chris Lattner47b4ce82009-03-11 05:48:52 +00009006 if (Opcode)
9007 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +00009008 }
Eric Christopherfd179292009-08-27 18:07:15 +00009009
Chris Lattnerd1980a52009-03-12 06:52:53 +00009010 // If this is a select between two integer constants, try to do some
9011 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +00009012 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
9013 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +00009014 // Don't do this for crazy integer types.
9015 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
9016 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +00009017 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +00009018 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +00009019
Chris Lattnercee56e72009-03-13 05:53:31 +00009020 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +00009021 // Efficiently invertible.
9022 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
9023 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
9024 isa<ConstantSDNode>(Cond.getOperand(1))))) {
9025 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +00009026 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +00009027 }
Eric Christopherfd179292009-08-27 18:07:15 +00009028
Chris Lattnerd1980a52009-03-12 06:52:53 +00009029 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00009030 if (FalseC->getAPIntValue() == 0 &&
9031 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00009032 if (NeedsCondInvert) // Invert the condition if needed.
9033 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
9034 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009035
Chris Lattnerd1980a52009-03-12 06:52:53 +00009036 // Zero extend the condition if needed.
9037 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009038
Chris Lattnercee56e72009-03-13 05:53:31 +00009039 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +00009040 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +00009041 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +00009042 }
Eric Christopherfd179292009-08-27 18:07:15 +00009043
Chris Lattner97a29a52009-03-13 05:22:11 +00009044 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +00009045 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +00009046 if (NeedsCondInvert) // Invert the condition if needed.
9047 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
9048 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009049
Chris Lattner97a29a52009-03-13 05:22:11 +00009050 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00009051 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
9052 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00009053 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +00009054 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +00009055 }
Eric Christopherfd179292009-08-27 18:07:15 +00009056
Chris Lattnercee56e72009-03-13 05:53:31 +00009057 // Optimize cases that will turn into an LEA instruction. This requires
9058 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +00009059 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +00009060 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00009061 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +00009062
Chris Lattnercee56e72009-03-13 05:53:31 +00009063 bool isFastMultiplier = false;
9064 if (Diff < 10) {
9065 switch ((unsigned char)Diff) {
9066 default: break;
9067 case 1: // result = add base, cond
9068 case 2: // result = lea base( , cond*2)
9069 case 3: // result = lea base(cond, cond*2)
9070 case 4: // result = lea base( , cond*4)
9071 case 5: // result = lea base(cond, cond*4)
9072 case 8: // result = lea base( , cond*8)
9073 case 9: // result = lea base(cond, cond*8)
9074 isFastMultiplier = true;
9075 break;
9076 }
9077 }
Eric Christopherfd179292009-08-27 18:07:15 +00009078
Chris Lattnercee56e72009-03-13 05:53:31 +00009079 if (isFastMultiplier) {
9080 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
9081 if (NeedsCondInvert) // Invert the condition if needed.
9082 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
9083 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009084
Chris Lattnercee56e72009-03-13 05:53:31 +00009085 // Zero extend the condition if needed.
9086 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
9087 Cond);
9088 // Scale the condition by the difference.
9089 if (Diff != 1)
9090 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
9091 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009092
Chris Lattnercee56e72009-03-13 05:53:31 +00009093 // Add the base if non-zero.
9094 if (FalseC->getAPIntValue() != 0)
9095 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9096 SDValue(FalseC, 0));
9097 return Cond;
9098 }
Eric Christopherfd179292009-08-27 18:07:15 +00009099 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00009100 }
9101 }
Eric Christopherfd179292009-08-27 18:07:15 +00009102
Dan Gohman475871a2008-07-27 21:46:04 +00009103 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +00009104}
9105
Chris Lattnerd1980a52009-03-12 06:52:53 +00009106/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
9107static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
9108 TargetLowering::DAGCombinerInfo &DCI) {
9109 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +00009110
Chris Lattnerd1980a52009-03-12 06:52:53 +00009111 // If the flag operand isn't dead, don't touch this CMOV.
9112 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
9113 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +00009114
Chris Lattnerd1980a52009-03-12 06:52:53 +00009115 // If this is a select between two integer constants, try to do some
9116 // optimizations. Note that the operands are ordered the opposite of SELECT
9117 // operands.
9118 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
9119 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
9120 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
9121 // larger than FalseC (the false value).
9122 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
Eric Christopherfd179292009-08-27 18:07:15 +00009123
Chris Lattnerd1980a52009-03-12 06:52:53 +00009124 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
9125 CC = X86::GetOppositeBranchCondition(CC);
9126 std::swap(TrueC, FalseC);
9127 }
Eric Christopherfd179292009-08-27 18:07:15 +00009128
Chris Lattnerd1980a52009-03-12 06:52:53 +00009129 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00009130 // This is efficient for any integer data type (including i8/i16) and
9131 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +00009132 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
9133 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00009134 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9135 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009136
Chris Lattnerd1980a52009-03-12 06:52:53 +00009137 // Zero extend the condition if needed.
9138 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009139
Chris Lattnerd1980a52009-03-12 06:52:53 +00009140 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
9141 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +00009142 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +00009143 if (N->getNumValues() == 2) // Dead flag value?
9144 return DCI.CombineTo(N, Cond, SDValue());
9145 return Cond;
9146 }
Eric Christopherfd179292009-08-27 18:07:15 +00009147
Chris Lattnercee56e72009-03-13 05:53:31 +00009148 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
9149 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +00009150 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
9151 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00009152 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9153 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009154
Chris Lattner97a29a52009-03-13 05:22:11 +00009155 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00009156 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
9157 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00009158 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9159 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +00009160
Chris Lattner97a29a52009-03-13 05:22:11 +00009161 if (N->getNumValues() == 2) // Dead flag value?
9162 return DCI.CombineTo(N, Cond, SDValue());
9163 return Cond;
9164 }
Eric Christopherfd179292009-08-27 18:07:15 +00009165
Chris Lattnercee56e72009-03-13 05:53:31 +00009166 // Optimize cases that will turn into an LEA instruction. This requires
9167 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +00009168 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +00009169 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00009170 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +00009171
Chris Lattnercee56e72009-03-13 05:53:31 +00009172 bool isFastMultiplier = false;
9173 if (Diff < 10) {
9174 switch ((unsigned char)Diff) {
9175 default: break;
9176 case 1: // result = add base, cond
9177 case 2: // result = lea base( , cond*2)
9178 case 3: // result = lea base(cond, cond*2)
9179 case 4: // result = lea base( , cond*4)
9180 case 5: // result = lea base(cond, cond*4)
9181 case 8: // result = lea base( , cond*8)
9182 case 9: // result = lea base(cond, cond*8)
9183 isFastMultiplier = true;
9184 break;
9185 }
9186 }
Eric Christopherfd179292009-08-27 18:07:15 +00009187
Chris Lattnercee56e72009-03-13 05:53:31 +00009188 if (isFastMultiplier) {
9189 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
9190 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00009191 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9192 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +00009193 // Zero extend the condition if needed.
9194 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
9195 Cond);
9196 // Scale the condition by the difference.
9197 if (Diff != 1)
9198 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
9199 DAG.getConstant(Diff, Cond.getValueType()));
9200
9201 // Add the base if non-zero.
9202 if (FalseC->getAPIntValue() != 0)
9203 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9204 SDValue(FalseC, 0));
9205 if (N->getNumValues() == 2) // Dead flag value?
9206 return DCI.CombineTo(N, Cond, SDValue());
9207 return Cond;
9208 }
Eric Christopherfd179292009-08-27 18:07:15 +00009209 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00009210 }
9211 }
9212 return SDValue();
9213}
9214
9215
Evan Cheng0b0cd912009-03-28 05:57:29 +00009216/// PerformMulCombine - Optimize a single multiply with constant into two
9217/// in order to implement it with two cheaper instructions, e.g.
9218/// LEA + SHL, LEA + LEA.
9219static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
9220 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +00009221 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
9222 return SDValue();
9223
Owen Andersone50ed302009-08-10 22:56:29 +00009224 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009225 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +00009226 return SDValue();
9227
9228 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
9229 if (!C)
9230 return SDValue();
9231 uint64_t MulAmt = C->getZExtValue();
9232 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
9233 return SDValue();
9234
9235 uint64_t MulAmt1 = 0;
9236 uint64_t MulAmt2 = 0;
9237 if ((MulAmt % 9) == 0) {
9238 MulAmt1 = 9;
9239 MulAmt2 = MulAmt / 9;
9240 } else if ((MulAmt % 5) == 0) {
9241 MulAmt1 = 5;
9242 MulAmt2 = MulAmt / 5;
9243 } else if ((MulAmt % 3) == 0) {
9244 MulAmt1 = 3;
9245 MulAmt2 = MulAmt / 3;
9246 }
9247 if (MulAmt2 &&
9248 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
9249 DebugLoc DL = N->getDebugLoc();
9250
9251 if (isPowerOf2_64(MulAmt2) &&
9252 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
9253 // If second multiplifer is pow2, issue it first. We want the multiply by
9254 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
9255 // is an add.
9256 std::swap(MulAmt1, MulAmt2);
9257
9258 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +00009259 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +00009260 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00009261 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +00009262 else
Evan Cheng73f24c92009-03-30 21:36:47 +00009263 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +00009264 DAG.getConstant(MulAmt1, VT));
9265
Eric Christopherfd179292009-08-27 18:07:15 +00009266 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +00009267 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +00009268 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +00009269 else
Evan Cheng73f24c92009-03-30 21:36:47 +00009270 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +00009271 DAG.getConstant(MulAmt2, VT));
9272
9273 // Do not add new nodes to DAG combiner worklist.
9274 DCI.CombineTo(N, NewMul, false);
9275 }
9276 return SDValue();
9277}
9278
Evan Chengad9c0a32009-12-15 00:53:42 +00009279static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
9280 SDValue N0 = N->getOperand(0);
9281 SDValue N1 = N->getOperand(1);
9282 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
9283 EVT VT = N0.getValueType();
9284
9285 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
9286 // since the result of setcc_c is all zero's or all ones.
9287 if (N1C && N0.getOpcode() == ISD::AND &&
9288 N0.getOperand(1).getOpcode() == ISD::Constant) {
9289 SDValue N00 = N0.getOperand(0);
9290 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
9291 ((N00.getOpcode() == ISD::ANY_EXTEND ||
9292 N00.getOpcode() == ISD::ZERO_EXTEND) &&
9293 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
9294 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
9295 APInt ShAmt = N1C->getAPIntValue();
9296 Mask = Mask.shl(ShAmt);
9297 if (Mask != 0)
9298 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
9299 N00, DAG.getConstant(Mask, VT));
9300 }
9301 }
9302
9303 return SDValue();
9304}
Evan Cheng0b0cd912009-03-28 05:57:29 +00009305
Nate Begeman740ab032009-01-26 00:52:55 +00009306/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
9307/// when possible.
9308static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
9309 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +00009310 EVT VT = N->getValueType(0);
9311 if (!VT.isVector() && VT.isInteger() &&
9312 N->getOpcode() == ISD::SHL)
9313 return PerformSHLCombine(N, DAG);
9314
Nate Begeman740ab032009-01-26 00:52:55 +00009315 // On X86 with SSE2 support, we can transform this to a vector shift if
9316 // all elements are shifted by the same amount. We can't do this in legalize
9317 // because the a constant vector is typically transformed to a constant pool
9318 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009319 if (!Subtarget->hasSSE2())
9320 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009321
Owen Anderson825b72b2009-08-11 20:47:22 +00009322 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009323 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009324
Mon P Wang3becd092009-01-28 08:12:05 +00009325 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00009326 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +00009327 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +00009328 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +00009329 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
9330 unsigned NumElts = VT.getVectorNumElements();
9331 unsigned i = 0;
9332 for (; i != NumElts; ++i) {
9333 SDValue Arg = ShAmtOp.getOperand(i);
9334 if (Arg.getOpcode() == ISD::UNDEF) continue;
9335 BaseShAmt = Arg;
9336 break;
9337 }
9338 for (; i != NumElts; ++i) {
9339 SDValue Arg = ShAmtOp.getOperand(i);
9340 if (Arg.getOpcode() == ISD::UNDEF) continue;
9341 if (Arg != BaseShAmt) {
9342 return SDValue();
9343 }
9344 }
9345 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +00009346 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +00009347 SDValue InVec = ShAmtOp.getOperand(0);
9348 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
9349 unsigned NumElts = InVec.getValueType().getVectorNumElements();
9350 unsigned i = 0;
9351 for (; i != NumElts; ++i) {
9352 SDValue Arg = InVec.getOperand(i);
9353 if (Arg.getOpcode() == ISD::UNDEF) continue;
9354 BaseShAmt = Arg;
9355 break;
9356 }
9357 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
9358 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +00009359 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +00009360 if (C->getZExtValue() == SplatIdx)
9361 BaseShAmt = InVec.getOperand(1);
9362 }
9363 }
9364 if (BaseShAmt.getNode() == 0)
9365 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
9366 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +00009367 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009368 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +00009369
Mon P Wangefa42202009-09-03 19:56:25 +00009370 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +00009371 if (EltVT.bitsGT(MVT::i32))
9372 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
9373 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +00009374 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +00009375
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009376 // The shift amount is identical so we can do a vector shift.
9377 SDValue ValOp = N->getOperand(0);
9378 switch (N->getOpcode()) {
9379 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009380 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009381 break;
9382 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +00009383 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009384 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009385 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009386 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009387 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009388 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009389 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009390 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009391 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009392 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009393 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009394 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009395 break;
9396 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +00009397 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009398 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009399 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009400 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009401 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009402 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009403 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009404 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009405 break;
9406 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +00009407 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009408 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009409 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009410 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009411 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009412 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009413 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009414 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009415 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009416 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009417 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009418 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009419 break;
Nate Begeman740ab032009-01-26 00:52:55 +00009420 }
9421 return SDValue();
9422}
9423
Evan Cheng760d1942010-01-04 21:22:48 +00009424static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +00009425 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +00009426 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +00009427 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +00009428 return SDValue();
9429
Evan Cheng760d1942010-01-04 21:22:48 +00009430 EVT VT = N->getValueType(0);
Evan Cheng8b1190a2010-04-28 01:18:01 +00009431 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
Evan Cheng760d1942010-01-04 21:22:48 +00009432 return SDValue();
9433
9434 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
9435 SDValue N0 = N->getOperand(0);
9436 SDValue N1 = N->getOperand(1);
9437 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
9438 std::swap(N0, N1);
9439 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
9440 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +00009441 if (!N0.hasOneUse() || !N1.hasOneUse())
9442 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +00009443
9444 SDValue ShAmt0 = N0.getOperand(1);
9445 if (ShAmt0.getValueType() != MVT::i8)
9446 return SDValue();
9447 SDValue ShAmt1 = N1.getOperand(1);
9448 if (ShAmt1.getValueType() != MVT::i8)
9449 return SDValue();
9450 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
9451 ShAmt0 = ShAmt0.getOperand(0);
9452 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
9453 ShAmt1 = ShAmt1.getOperand(0);
9454
9455 DebugLoc DL = N->getDebugLoc();
9456 unsigned Opc = X86ISD::SHLD;
9457 SDValue Op0 = N0.getOperand(0);
9458 SDValue Op1 = N1.getOperand(0);
9459 if (ShAmt0.getOpcode() == ISD::SUB) {
9460 Opc = X86ISD::SHRD;
9461 std::swap(Op0, Op1);
9462 std::swap(ShAmt0, ShAmt1);
9463 }
9464
Evan Cheng8b1190a2010-04-28 01:18:01 +00009465 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +00009466 if (ShAmt1.getOpcode() == ISD::SUB) {
9467 SDValue Sum = ShAmt1.getOperand(0);
9468 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Evan Cheng8b1190a2010-04-28 01:18:01 +00009469 if (SumC->getSExtValue() == Bits &&
Evan Cheng760d1942010-01-04 21:22:48 +00009470 ShAmt1.getOperand(1) == ShAmt0)
9471 return DAG.getNode(Opc, DL, VT,
9472 Op0, Op1,
9473 DAG.getNode(ISD::TRUNCATE, DL,
9474 MVT::i8, ShAmt0));
9475 }
9476 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
9477 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
9478 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +00009479 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +00009480 return DAG.getNode(Opc, DL, VT,
9481 N0.getOperand(0), N1.getOperand(0),
9482 DAG.getNode(ISD::TRUNCATE, DL,
9483 MVT::i8, ShAmt0));
9484 }
9485
9486 return SDValue();
9487}
9488
Chris Lattner149a4e52008-02-22 02:09:43 +00009489/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009490static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +00009491 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +00009492 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
9493 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +00009494 // A preferable solution to the general problem is to figure out the right
9495 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +00009496
9497 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +00009498 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +00009499 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +00009500 if (VT.getSizeInBits() != 64)
9501 return SDValue();
9502
Devang Patel578efa92009-06-05 21:57:13 +00009503 const Function *F = DAG.getMachineFunction().getFunction();
9504 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +00009505 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +00009506 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +00009507 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +00009508 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +00009509 isa<LoadSDNode>(St->getValue()) &&
9510 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
9511 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00009512 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009513 LoadSDNode *Ld = 0;
9514 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +00009515 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +00009516 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009517 // Must be a store of a load. We currently handle two cases: the load
9518 // is a direct child, and it's under an intervening TokenFactor. It is
9519 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +00009520 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +00009521 Ld = cast<LoadSDNode>(St->getChain());
9522 else if (St->getValue().hasOneUse() &&
9523 ChainVal->getOpcode() == ISD::TokenFactor) {
9524 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +00009525 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +00009526 TokenFactorIndex = i;
9527 Ld = cast<LoadSDNode>(St->getValue());
9528 } else
9529 Ops.push_back(ChainVal->getOperand(i));
9530 }
9531 }
Dale Johannesen079f2a62008-02-25 19:20:14 +00009532
Evan Cheng536e6672009-03-12 05:59:15 +00009533 if (!Ld || !ISD::isNormalLoad(Ld))
9534 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009535
Evan Cheng536e6672009-03-12 05:59:15 +00009536 // If this is not the MMX case, i.e. we are just turning i64 load/store
9537 // into f64 load/store, avoid the transformation if there are multiple
9538 // uses of the loaded value.
9539 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
9540 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009541
Evan Cheng536e6672009-03-12 05:59:15 +00009542 DebugLoc LdDL = Ld->getDebugLoc();
9543 DebugLoc StDL = N->getDebugLoc();
9544 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
9545 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
9546 // pair instead.
9547 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009548 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Evan Cheng536e6672009-03-12 05:59:15 +00009549 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(),
9550 Ld->getBasePtr(), Ld->getSrcValue(),
9551 Ld->getSrcValueOffset(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +00009552 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +00009553 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +00009554 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +00009555 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +00009556 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +00009557 Ops.size());
9558 }
Evan Cheng536e6672009-03-12 05:59:15 +00009559 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner149a4e52008-02-22 02:09:43 +00009560 St->getSrcValue(), St->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00009561 St->isVolatile(), St->isNonTemporal(),
9562 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +00009563 }
Evan Cheng536e6672009-03-12 05:59:15 +00009564
9565 // Otherwise, lower to two pairs of 32-bit loads / stores.
9566 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +00009567 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
9568 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +00009569
Owen Anderson825b72b2009-08-11 20:47:22 +00009570 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Evan Cheng536e6672009-03-12 05:59:15 +00009571 Ld->getSrcValue(), Ld->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00009572 Ld->isVolatile(), Ld->isNonTemporal(),
9573 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00009574 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Evan Cheng536e6672009-03-12 05:59:15 +00009575 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
David Greene67c9d422010-02-15 16:53:33 +00009576 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +00009577 MinAlign(Ld->getAlignment(), 4));
9578
9579 SDValue NewChain = LoLd.getValue(1);
9580 if (TokenFactorIndex != -1) {
9581 Ops.push_back(LoLd);
9582 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +00009583 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +00009584 Ops.size());
9585 }
9586
9587 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +00009588 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
9589 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +00009590
9591 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
9592 St->getSrcValue(), St->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00009593 St->isVolatile(), St->isNonTemporal(),
9594 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +00009595 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
9596 St->getSrcValue(),
9597 St->getSrcValueOffset() + 4,
9598 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +00009599 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +00009600 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +00009601 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +00009602 }
Dan Gohman475871a2008-07-27 21:46:04 +00009603 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +00009604}
9605
Chris Lattner6cf73262008-01-25 06:14:17 +00009606/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
9607/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009608static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +00009609 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
9610 // F[X]OR(0.0, x) -> x
9611 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +00009612 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
9613 if (C->getValueAPF().isPosZero())
9614 return N->getOperand(1);
9615 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
9616 if (C->getValueAPF().isPosZero())
9617 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +00009618 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00009619}
9620
9621/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009622static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +00009623 // FAND(0.0, x) -> 0.0
9624 // FAND(x, 0.0) -> 0.0
9625 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
9626 if (C->getValueAPF().isPosZero())
9627 return N->getOperand(0);
9628 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
9629 if (C->getValueAPF().isPosZero())
9630 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +00009631 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00009632}
9633
Dan Gohmane5af2d32009-01-29 01:59:02 +00009634static SDValue PerformBTCombine(SDNode *N,
9635 SelectionDAG &DAG,
9636 TargetLowering::DAGCombinerInfo &DCI) {
9637 // BT ignores high bits in the bit index operand.
9638 SDValue Op1 = N->getOperand(1);
9639 if (Op1.hasOneUse()) {
9640 unsigned BitWidth = Op1.getValueSizeInBits();
9641 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
9642 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +00009643 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
9644 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +00009645 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +00009646 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
9647 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
9648 DCI.CommitTargetLoweringOpt(TLO);
9649 }
9650 return SDValue();
9651}
Chris Lattner83e6c992006-10-04 06:57:07 +00009652
Eli Friedman7a5e5552009-06-07 06:52:44 +00009653static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
9654 SDValue Op = N->getOperand(0);
9655 if (Op.getOpcode() == ISD::BIT_CONVERT)
9656 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00009657 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +00009658 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +00009659 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +00009660 OpVT.getVectorElementType().getSizeInBits()) {
9661 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT, Op);
9662 }
9663 return SDValue();
9664}
9665
Owen Anderson99177002009-06-29 18:04:45 +00009666// On X86 and X86-64, atomic operations are lowered to locked instructions.
9667// Locked instructions, in turn, have implicit fence semantics (all memory
9668// operations are flushed before issuing the locked instruction, and the
Eric Christopherfd179292009-08-27 18:07:15 +00009669// are not buffered), so we can fold away the common pattern of
Owen Anderson99177002009-06-29 18:04:45 +00009670// fence-atomic-fence.
9671static SDValue PerformMEMBARRIERCombine(SDNode* N, SelectionDAG &DAG) {
9672 SDValue atomic = N->getOperand(0);
9673 switch (atomic.getOpcode()) {
9674 case ISD::ATOMIC_CMP_SWAP:
9675 case ISD::ATOMIC_SWAP:
9676 case ISD::ATOMIC_LOAD_ADD:
9677 case ISD::ATOMIC_LOAD_SUB:
9678 case ISD::ATOMIC_LOAD_AND:
9679 case ISD::ATOMIC_LOAD_OR:
9680 case ISD::ATOMIC_LOAD_XOR:
9681 case ISD::ATOMIC_LOAD_NAND:
9682 case ISD::ATOMIC_LOAD_MIN:
9683 case ISD::ATOMIC_LOAD_MAX:
9684 case ISD::ATOMIC_LOAD_UMIN:
9685 case ISD::ATOMIC_LOAD_UMAX:
9686 break;
9687 default:
9688 return SDValue();
9689 }
Eric Christopherfd179292009-08-27 18:07:15 +00009690
Owen Anderson99177002009-06-29 18:04:45 +00009691 SDValue fence = atomic.getOperand(0);
9692 if (fence.getOpcode() != ISD::MEMBARRIER)
9693 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +00009694
Owen Anderson99177002009-06-29 18:04:45 +00009695 switch (atomic.getOpcode()) {
9696 case ISD::ATOMIC_CMP_SWAP:
9697 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
9698 atomic.getOperand(1), atomic.getOperand(2),
9699 atomic.getOperand(3));
9700 case ISD::ATOMIC_SWAP:
9701 case ISD::ATOMIC_LOAD_ADD:
9702 case ISD::ATOMIC_LOAD_SUB:
9703 case ISD::ATOMIC_LOAD_AND:
9704 case ISD::ATOMIC_LOAD_OR:
9705 case ISD::ATOMIC_LOAD_XOR:
9706 case ISD::ATOMIC_LOAD_NAND:
9707 case ISD::ATOMIC_LOAD_MIN:
9708 case ISD::ATOMIC_LOAD_MAX:
9709 case ISD::ATOMIC_LOAD_UMIN:
9710 case ISD::ATOMIC_LOAD_UMAX:
9711 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
9712 atomic.getOperand(1), atomic.getOperand(2));
9713 default:
9714 return SDValue();
9715 }
9716}
9717
Evan Cheng2e489c42009-12-16 00:53:11 +00009718static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
9719 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
9720 // (and (i32 x86isd::setcc_carry), 1)
9721 // This eliminates the zext. This transformation is necessary because
9722 // ISD::SETCC is always legalized to i8.
9723 DebugLoc dl = N->getDebugLoc();
9724 SDValue N0 = N->getOperand(0);
9725 EVT VT = N->getValueType(0);
9726 if (N0.getOpcode() == ISD::AND &&
9727 N0.hasOneUse() &&
9728 N0.getOperand(0).hasOneUse()) {
9729 SDValue N00 = N0.getOperand(0);
9730 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
9731 return SDValue();
9732 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
9733 if (!C || C->getZExtValue() != 1)
9734 return SDValue();
9735 return DAG.getNode(ISD::AND, dl, VT,
9736 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
9737 N00.getOperand(0), N00.getOperand(1)),
9738 DAG.getConstant(1, VT));
9739 }
9740
9741 return SDValue();
9742}
9743
Dan Gohman475871a2008-07-27 21:46:04 +00009744SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +00009745 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +00009746 SelectionDAG &DAG = DCI.DAG;
9747 switch (N->getOpcode()) {
9748 default: break;
Evan Chengad4196b2008-05-12 19:56:52 +00009749 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00009750 case ISD::EXTRACT_VECTOR_ELT:
9751 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +00009752 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +00009753 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +00009754 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +00009755 case ISD::SHL:
9756 case ISD::SRA:
9757 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +00009758 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +00009759 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +00009760 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +00009761 case X86ISD::FOR: return PerformFORCombine(N, DAG);
9762 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +00009763 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +00009764 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Owen Anderson99177002009-06-29 18:04:45 +00009765 case ISD::MEMBARRIER: return PerformMEMBARRIERCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +00009766 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Evan Cheng206ee9d2006-07-07 08:33:52 +00009767 }
9768
Dan Gohman475871a2008-07-27 21:46:04 +00009769 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00009770}
9771
Evan Chenge5b51ac2010-04-17 06:13:15 +00009772/// isTypeDesirableForOp - Return true if the target has native support for
9773/// the specified value type and it is 'desirable' to use the type for the
9774/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
9775/// instruction encodings are longer and some i16 instructions are slow.
9776bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
9777 if (!isTypeLegal(VT))
9778 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00009779 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +00009780 return true;
9781
9782 switch (Opc) {
9783 default:
9784 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +00009785 case ISD::LOAD:
9786 case ISD::SIGN_EXTEND:
9787 case ISD::ZERO_EXTEND:
9788 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +00009789 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +00009790 case ISD::SRL:
9791 case ISD::SUB:
9792 case ISD::ADD:
9793 case ISD::MUL:
9794 case ISD::AND:
9795 case ISD::OR:
9796 case ISD::XOR:
9797 return false;
9798 }
9799}
9800
Evan Chengc82c20b2010-04-24 04:44:57 +00009801static bool MayFoldLoad(SDValue Op) {
9802 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
9803}
9804
9805static bool MayFoldIntoStore(SDValue Op) {
9806 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
9807}
9808
Evan Chenge5b51ac2010-04-17 06:13:15 +00009809/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +00009810/// beneficial for dag combiner to promote the specified node. If true, it
9811/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +00009812bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +00009813 EVT VT = Op.getValueType();
9814 if (VT != MVT::i16)
9815 return false;
9816
Evan Cheng4c26e932010-04-19 19:29:22 +00009817 bool Promote = false;
9818 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +00009819 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +00009820 default: break;
9821 case ISD::LOAD: {
9822 LoadSDNode *LD = cast<LoadSDNode>(Op);
9823 // If the non-extending load has a single use and it's not live out, then it
9824 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00009825 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
9826 Op.hasOneUse()*/) {
9827 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
9828 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
9829 // The only case where we'd want to promote LOAD (rather then it being
9830 // promoted as an operand is when it's only use is liveout.
9831 if (UI->getOpcode() != ISD::CopyToReg)
9832 return false;
9833 }
9834 }
Evan Cheng4c26e932010-04-19 19:29:22 +00009835 Promote = true;
9836 break;
9837 }
9838 case ISD::SIGN_EXTEND:
9839 case ISD::ZERO_EXTEND:
9840 case ISD::ANY_EXTEND:
9841 Promote = true;
9842 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +00009843 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00009844 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +00009845 SDValue N0 = Op.getOperand(0);
9846 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +00009847 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +00009848 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +00009849 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +00009850 break;
9851 }
Evan Cheng64b7bf72010-04-16 06:14:10 +00009852 case ISD::ADD:
9853 case ISD::MUL:
9854 case ISD::AND:
9855 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +00009856 case ISD::XOR:
9857 Commute = true;
9858 // fallthrough
9859 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +00009860 SDValue N0 = Op.getOperand(0);
9861 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +00009862 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +00009863 return false;
9864 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +00009865 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +00009866 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +00009867 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +00009868 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +00009869 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +00009870 }
9871 }
9872
9873 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +00009874 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +00009875}
9876
Evan Cheng60c07e12006-07-05 22:17:51 +00009877//===----------------------------------------------------------------------===//
9878// X86 Inline Assembly Support
9879//===----------------------------------------------------------------------===//
9880
Chris Lattnerb8105652009-07-20 17:51:36 +00009881static bool LowerToBSwap(CallInst *CI) {
9882 // FIXME: this should verify that we are targetting a 486 or better. If not,
9883 // we will turn this bswap into something that will be lowered to logical ops
9884 // instead of emitting the bswap asm. For now, we don't support 486 or lower
9885 // so don't worry about this.
Eric Christopherfd179292009-08-27 18:07:15 +00009886
Chris Lattnerb8105652009-07-20 17:51:36 +00009887 // Verify this is a simple bswap.
9888 if (CI->getNumOperands() != 2 ||
Eric Christopher551754c2010-04-16 23:37:20 +00009889 CI->getType() != CI->getOperand(1)->getType() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009890 !CI->getType()->isIntegerTy())
Chris Lattnerb8105652009-07-20 17:51:36 +00009891 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00009892
Chris Lattnerb8105652009-07-20 17:51:36 +00009893 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
9894 if (!Ty || Ty->getBitWidth() % 16 != 0)
9895 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00009896
Chris Lattnerb8105652009-07-20 17:51:36 +00009897 // Okay, we can do this xform, do so now.
9898 const Type *Tys[] = { Ty };
9899 Module *M = CI->getParent()->getParent()->getParent();
9900 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
Eric Christopherfd179292009-08-27 18:07:15 +00009901
Eric Christopher551754c2010-04-16 23:37:20 +00009902 Value *Op = CI->getOperand(1);
Chris Lattnerb8105652009-07-20 17:51:36 +00009903 Op = CallInst::Create(Int, Op, CI->getName(), CI);
Eric Christopherfd179292009-08-27 18:07:15 +00009904
Chris Lattnerb8105652009-07-20 17:51:36 +00009905 CI->replaceAllUsesWith(Op);
9906 CI->eraseFromParent();
9907 return true;
9908}
9909
9910bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
9911 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
9912 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
9913
9914 std::string AsmStr = IA->getAsmString();
9915
9916 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +00009917 SmallVector<StringRef, 4> AsmPieces;
Chris Lattnerb8105652009-07-20 17:51:36 +00009918 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
9919
9920 switch (AsmPieces.size()) {
9921 default: return false;
9922 case 1:
9923 AsmStr = AsmPieces[0];
9924 AsmPieces.clear();
9925 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
9926
9927 // bswap $0
9928 if (AsmPieces.size() == 2 &&
9929 (AsmPieces[0] == "bswap" ||
9930 AsmPieces[0] == "bswapq" ||
9931 AsmPieces[0] == "bswapl") &&
9932 (AsmPieces[1] == "$0" ||
9933 AsmPieces[1] == "${0:q}")) {
9934 // No need to check constraints, nothing other than the equivalent of
9935 // "=r,0" would be valid here.
9936 return LowerToBSwap(CI);
9937 }
9938 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009939 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +00009940 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +00009941 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +00009942 AsmPieces[1] == "$$8," &&
9943 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +00009944 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
9945 AsmPieces.clear();
Benjamin Kramer018cbd52010-03-12 13:54:59 +00009946 const std::string &Constraints = IA->getConstraintString();
9947 SplitString(StringRef(Constraints).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +00009948 std::sort(AsmPieces.begin(), AsmPieces.end());
9949 if (AsmPieces.size() == 4 &&
9950 AsmPieces[0] == "~{cc}" &&
9951 AsmPieces[1] == "~{dirflag}" &&
9952 AsmPieces[2] == "~{flags}" &&
9953 AsmPieces[3] == "~{fpsr}") {
9954 return LowerToBSwap(CI);
9955 }
Chris Lattnerb8105652009-07-20 17:51:36 +00009956 }
9957 break;
9958 case 3:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009959 if (CI->getType()->isIntegerTy(64) &&
Owen Anderson1d0be152009-08-13 21:58:54 +00009960 Constraints.size() >= 2 &&
Chris Lattnerb8105652009-07-20 17:51:36 +00009961 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
9962 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
9963 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramerd4f19592010-01-11 18:03:24 +00009964 SmallVector<StringRef, 4> Words;
Chris Lattnerb8105652009-07-20 17:51:36 +00009965 SplitString(AsmPieces[0], Words, " \t");
9966 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
9967 Words.clear();
9968 SplitString(AsmPieces[1], Words, " \t");
9969 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
9970 Words.clear();
9971 SplitString(AsmPieces[2], Words, " \t,");
9972 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
9973 Words[2] == "%edx") {
9974 return LowerToBSwap(CI);
9975 }
9976 }
9977 }
9978 }
9979 break;
9980 }
9981 return false;
9982}
9983
9984
9985
Chris Lattnerf4dff842006-07-11 02:54:03 +00009986/// getConstraintType - Given a constraint letter, return the type of
9987/// constraint it is for this target.
9988X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00009989X86TargetLowering::getConstraintType(const std::string &Constraint) const {
9990 if (Constraint.size() == 1) {
9991 switch (Constraint[0]) {
9992 case 'A':
Dale Johannesen330169f2008-11-13 21:52:36 +00009993 return C_Register;
Chris Lattnerfce84ac2008-03-11 19:06:29 +00009994 case 'f':
Chris Lattner4234f572007-03-25 02:14:49 +00009995 case 'r':
9996 case 'R':
9997 case 'l':
9998 case 'q':
9999 case 'Q':
10000 case 'x':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000010001 case 'y':
Chris Lattner4234f572007-03-25 02:14:49 +000010002 case 'Y':
10003 return C_RegisterClass;
Dale Johannesen78e3e522009-02-12 20:58:09 +000010004 case 'e':
10005 case 'Z':
10006 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000010007 default:
10008 break;
10009 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000010010 }
Chris Lattner4234f572007-03-25 02:14:49 +000010011 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000010012}
10013
Dale Johannesenba2a0b92008-01-29 02:21:21 +000010014/// LowerXConstraint - try to replace an X constraint, which matches anything,
10015/// with another that has more specific requirements based on the type of the
10016/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000010017const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000010018LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000010019 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
10020 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000010021 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesenba2a0b92008-01-29 02:21:21 +000010022 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +000010023 return "Y";
10024 if (Subtarget->hasSSE1())
10025 return "x";
10026 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010027
Chris Lattner5e764232008-04-26 23:02:14 +000010028 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000010029}
10030
Chris Lattner48884cd2007-08-25 00:47:38 +000010031/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
10032/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000010033void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +000010034 char Constraint,
Evan Chengda43bcf2008-09-24 00:05:32 +000010035 bool hasMemory,
Dan Gohman475871a2008-07-27 21:46:04 +000010036 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000010037 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010038 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000010039
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010040 switch (Constraint) {
10041 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000010042 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000010043 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000010044 if (C->getZExtValue() <= 31) {
10045 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000010046 break;
10047 }
Devang Patel84f7fd22007-03-17 00:13:28 +000010048 }
Chris Lattner48884cd2007-08-25 00:47:38 +000010049 return;
Evan Cheng364091e2008-09-22 23:57:37 +000010050 case 'J':
10051 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000010052 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000010053 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
10054 break;
10055 }
10056 }
10057 return;
10058 case 'K':
10059 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000010060 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000010061 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
10062 break;
10063 }
10064 }
10065 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000010066 case 'N':
10067 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000010068 if (C->getZExtValue() <= 255) {
10069 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000010070 break;
10071 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000010072 }
Chris Lattner48884cd2007-08-25 00:47:38 +000010073 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000010074 case 'e': {
10075 // 32-bit signed value
10076 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
10077 const ConstantInt *CI = C->getConstantIntValue();
Owen Anderson1d0be152009-08-13 21:58:54 +000010078 if (CI->isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
10079 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000010080 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000010081 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000010082 break;
10083 }
10084 // FIXME gcc accepts some relocatable values here too, but only in certain
10085 // memory models; it's complicated.
10086 }
10087 return;
10088 }
10089 case 'Z': {
10090 // 32-bit unsigned value
10091 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
10092 const ConstantInt *CI = C->getConstantIntValue();
Owen Anderson1d0be152009-08-13 21:58:54 +000010093 if (CI->isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
10094 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000010095 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
10096 break;
10097 }
10098 }
10099 // FIXME gcc accepts some relocatable values here too, but only in certain
10100 // memory models; it's complicated.
10101 return;
10102 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000010103 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010104 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000010105 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000010106 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000010107 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000010108 break;
10109 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010110
Chris Lattnerdc43a882007-05-03 16:52:29 +000010111 // If we are in non-pic codegen mode, we allow the address of a global (with
10112 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000010113 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000010114 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000010115
Chris Lattner49921962009-05-08 18:23:14 +000010116 // Match either (GA), (GA+C), (GA+C1+C2), etc.
10117 while (1) {
10118 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
10119 Offset += GA->getOffset();
10120 break;
10121 } else if (Op.getOpcode() == ISD::ADD) {
10122 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
10123 Offset += C->getZExtValue();
10124 Op = Op.getOperand(0);
10125 continue;
10126 }
10127 } else if (Op.getOpcode() == ISD::SUB) {
10128 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
10129 Offset += -C->getZExtValue();
10130 Op = Op.getOperand(0);
10131 continue;
10132 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000010133 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000010134
Chris Lattner49921962009-05-08 18:23:14 +000010135 // Otherwise, this isn't something we can handle, reject it.
10136 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000010137 }
Eric Christopherfd179292009-08-27 18:07:15 +000010138
Dan Gohman46510a72010-04-15 01:51:59 +000010139 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000010140 // If we require an extra load to get this address, as in PIC mode, we
10141 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000010142 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
10143 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000010144 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000010145
Dale Johannesen60b3ba02009-07-21 00:12:29 +000010146 if (hasMemory)
10147 Op = LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
10148 else
10149 Op = DAG.getTargetGlobalAddress(GV, GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000010150 Result = Op;
10151 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010152 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000010153 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010154
Gabor Greifba36cb52008-08-28 21:40:38 +000010155 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000010156 Ops.push_back(Result);
10157 return;
10158 }
Evan Chengda43bcf2008-09-24 00:05:32 +000010159 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
10160 Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010161}
10162
Chris Lattner259e97c2006-01-31 19:43:35 +000010163std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +000010164getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000010165 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +000010166 if (Constraint.size() == 1) {
10167 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +000010168 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +000010169 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +000010170 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
10171 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000010172 if (VT == MVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010173 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
10174 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
10175 X86::R10D,X86::R11D,X86::R12D,
10176 X86::R13D,X86::R14D,X86::R15D,
10177 X86::EBP, X86::ESP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010178 else if (VT == MVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010179 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
10180 X86::SI, X86::DI, X86::R8W,X86::R9W,
10181 X86::R10W,X86::R11W,X86::R12W,
10182 X86::R13W,X86::R14W,X86::R15W,
10183 X86::BP, X86::SP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010184 else if (VT == MVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010185 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
10186 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
10187 X86::R10B,X86::R11B,X86::R12B,
10188 X86::R13B,X86::R14B,X86::R15B,
10189 X86::BPL, X86::SPL, 0);
10190
Owen Anderson825b72b2009-08-11 20:47:22 +000010191 else if (VT == MVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010192 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
10193 X86::RSI, X86::RDI, X86::R8, X86::R9,
10194 X86::R10, X86::R11, X86::R12,
10195 X86::R13, X86::R14, X86::R15,
10196 X86::RBP, X86::RSP, 0);
10197
10198 break;
10199 }
Eric Christopherfd179292009-08-27 18:07:15 +000010200 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +000010201 case 'Q': // Q_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000010202 if (VT == MVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000010203 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010204 else if (VT == MVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000010205 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010206 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +000010207 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010208 else if (VT == MVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +000010209 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
10210 break;
Chris Lattner259e97c2006-01-31 19:43:35 +000010211 }
10212 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010213
Chris Lattner1efa40f2006-02-22 00:56:39 +000010214 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +000010215}
Chris Lattnerf76d1802006-07-31 23:26:50 +000010216
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010217std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000010218X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000010219 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000010220 // First, see if this is a constraint that directly corresponds to an LLVM
10221 // register class.
10222 if (Constraint.size() == 1) {
10223 // GCC Constraint Letters
10224 switch (Constraint[0]) {
10225 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000010226 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000010227 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000010228 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +000010229 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010230 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000010231 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010232 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000010233 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000010234 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000010235 case 'R': // LEGACY_REGS
10236 if (VT == MVT::i8)
10237 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
10238 if (VT == MVT::i16)
10239 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
10240 if (VT == MVT::i32 || !Subtarget->is64Bit())
10241 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
10242 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010243 case 'f': // FP Stack registers.
10244 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
10245 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000010246 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010247 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010248 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010249 return std::make_pair(0U, X86::RFP64RegisterClass);
10250 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000010251 case 'y': // MMX_REGS if MMX allowed.
10252 if (!Subtarget->hasMMX()) break;
10253 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000010254 case 'Y': // SSE_REGS if SSE2 allowed
10255 if (!Subtarget->hasSSE2()) break;
10256 // FALL THROUGH.
10257 case 'x': // SSE_REGS if SSE1 allowed
10258 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010259
Owen Anderson825b72b2009-08-11 20:47:22 +000010260 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000010261 default: break;
10262 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000010263 case MVT::f32:
10264 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000010265 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010266 case MVT::f64:
10267 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000010268 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000010269 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000010270 case MVT::v16i8:
10271 case MVT::v8i16:
10272 case MVT::v4i32:
10273 case MVT::v2i64:
10274 case MVT::v4f32:
10275 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000010276 return std::make_pair(0U, X86::VR128RegisterClass);
10277 }
Chris Lattnerad043e82007-04-09 05:11:28 +000010278 break;
10279 }
10280 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010281
Chris Lattnerf76d1802006-07-31 23:26:50 +000010282 // Use the default implementation in TargetLowering to convert the register
10283 // constraint into a member of a register class.
10284 std::pair<unsigned, const TargetRegisterClass*> Res;
10285 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000010286
10287 // Not found as a standard register?
10288 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000010289 // Map st(0) -> st(7) -> ST0
10290 if (Constraint.size() == 7 && Constraint[0] == '{' &&
10291 tolower(Constraint[1]) == 's' &&
10292 tolower(Constraint[2]) == 't' &&
10293 Constraint[3] == '(' &&
10294 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
10295 Constraint[5] == ')' &&
10296 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000010297
Chris Lattner56d77c72009-09-13 22:41:48 +000010298 Res.first = X86::ST0+Constraint[4]-'0';
10299 Res.second = X86::RFP80RegisterClass;
10300 return Res;
10301 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000010302
Chris Lattner56d77c72009-09-13 22:41:48 +000010303 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000010304 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000010305 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000010306 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000010307 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000010308 }
Chris Lattner56d77c72009-09-13 22:41:48 +000010309
10310 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000010311 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000010312 Res.first = X86::EFLAGS;
10313 Res.second = X86::CCRRegisterClass;
10314 return Res;
10315 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000010316
Dale Johannesen330169f2008-11-13 21:52:36 +000010317 // 'A' means EAX + EDX.
10318 if (Constraint == "A") {
10319 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000010320 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000010321 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000010322 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000010323 return Res;
10324 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010325
Chris Lattnerf76d1802006-07-31 23:26:50 +000010326 // Otherwise, check to see if this is a register class of the wrong value
10327 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
10328 // turn into {ax},{dx}.
10329 if (Res.second->hasType(VT))
10330 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010331
Chris Lattnerf76d1802006-07-31 23:26:50 +000010332 // All of the single-register GCC register classes map their values onto
10333 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
10334 // really want an 8-bit or 32-bit register, map to the appropriate register
10335 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000010336 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000010337 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010338 unsigned DestReg = 0;
10339 switch (Res.first) {
10340 default: break;
10341 case X86::AX: DestReg = X86::AL; break;
10342 case X86::DX: DestReg = X86::DL; break;
10343 case X86::CX: DestReg = X86::CL; break;
10344 case X86::BX: DestReg = X86::BL; break;
10345 }
10346 if (DestReg) {
10347 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010348 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010349 }
Owen Anderson825b72b2009-08-11 20:47:22 +000010350 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010351 unsigned DestReg = 0;
10352 switch (Res.first) {
10353 default: break;
10354 case X86::AX: DestReg = X86::EAX; break;
10355 case X86::DX: DestReg = X86::EDX; break;
10356 case X86::CX: DestReg = X86::ECX; break;
10357 case X86::BX: DestReg = X86::EBX; break;
10358 case X86::SI: DestReg = X86::ESI; break;
10359 case X86::DI: DestReg = X86::EDI; break;
10360 case X86::BP: DestReg = X86::EBP; break;
10361 case X86::SP: DestReg = X86::ESP; break;
10362 }
10363 if (DestReg) {
10364 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010365 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010366 }
Owen Anderson825b72b2009-08-11 20:47:22 +000010367 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010368 unsigned DestReg = 0;
10369 switch (Res.first) {
10370 default: break;
10371 case X86::AX: DestReg = X86::RAX; break;
10372 case X86::DX: DestReg = X86::RDX; break;
10373 case X86::CX: DestReg = X86::RCX; break;
10374 case X86::BX: DestReg = X86::RBX; break;
10375 case X86::SI: DestReg = X86::RSI; break;
10376 case X86::DI: DestReg = X86::RDI; break;
10377 case X86::BP: DestReg = X86::RBP; break;
10378 case X86::SP: DestReg = X86::RSP; break;
10379 }
10380 if (DestReg) {
10381 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010382 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010383 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000010384 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000010385 } else if (Res.second == X86::FR32RegisterClass ||
10386 Res.second == X86::FR64RegisterClass ||
10387 Res.second == X86::VR128RegisterClass) {
10388 // Handle references to XMM physical registers that got mapped into the
10389 // wrong class. This can happen with constraints like {xmm0} where the
10390 // target independent register mapper will just pick the first match it can
10391 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000010392 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000010393 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000010394 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000010395 Res.second = X86::FR64RegisterClass;
10396 else if (X86::VR128RegisterClass->hasType(VT))
10397 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000010398 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010399
Chris Lattnerf76d1802006-07-31 23:26:50 +000010400 return Res;
10401}