blob: 167176047281b8f29d4bd42cef02d464a76979fd [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 Cheng211ffa12010-05-19 20:19:50 +000097 setSchedulingPreference(Sched::RegPressure);
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 Johannesenacbf6342010-05-21 18:44:47 +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);
Dale Johannesene39859a2010-05-21 18:40:15 +0000220 if (Subtarget->is64Bit()) {
Dale Johannesen7d07b482010-05-21 00:52:33 +0000221 setOperationAction(ISD::BIT_CONVERT , MVT::f64 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000222 // Without SSE, i64->f64 goes through memory; i64->MMX is Legal.
223 if (Subtarget->hasMMX() && !DisableMMX)
224 setOperationAction(ISD::BIT_CONVERT , MVT::i64 , Custom);
225 else
226 setOperationAction(ISD::BIT_CONVERT , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000227 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000228 }
Chris Lattner21f66852005-12-23 05:15:23 +0000229
Dan Gohmanb00ee212008-02-18 19:34:53 +0000230 // Scalar integer divide and remainder are lowered to use operations that
231 // produce two results, to match the available instructions. This exposes
232 // the two-result form to trivial CSE, which is able to combine x/y and x%y
233 // into a single instruction.
234 //
235 // Scalar integer multiply-high is also lowered to use two-result
236 // operations, to match the available instructions. However, plain multiply
237 // (low) operations are left as Legal, as there are single-result
238 // instructions for this in x86. Using the two-result multiply instructions
239 // when both high and low results are needed must be arranged by dagcombine.
Owen Anderson825b72b2009-08-11 20:47:22 +0000240 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
241 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
242 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
243 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
244 setOperationAction(ISD::SREM , MVT::i8 , Expand);
245 setOperationAction(ISD::UREM , MVT::i8 , Expand);
246 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
247 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
248 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
249 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
250 setOperationAction(ISD::SREM , MVT::i16 , Expand);
251 setOperationAction(ISD::UREM , MVT::i16 , Expand);
252 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
253 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
254 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
255 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
256 setOperationAction(ISD::SREM , MVT::i32 , Expand);
257 setOperationAction(ISD::UREM , MVT::i32 , Expand);
258 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
259 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
260 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
261 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
262 setOperationAction(ISD::SREM , MVT::i64 , Expand);
263 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohmana37c9f72007-09-25 18:23:27 +0000264
Owen Anderson825b72b2009-08-11 20:47:22 +0000265 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
266 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
267 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
268 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000269 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000270 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
271 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
272 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
273 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
274 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
275 setOperationAction(ISD::FREM , MVT::f32 , Expand);
276 setOperationAction(ISD::FREM , MVT::f64 , Expand);
277 setOperationAction(ISD::FREM , MVT::f80 , Expand);
278 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000279
Owen Anderson825b72b2009-08-11 20:47:22 +0000280 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
281 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
282 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
283 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000284 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
285 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000286 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
287 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
288 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000289 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000290 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
291 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
292 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000293 }
294
Owen Anderson825b72b2009-08-11 20:47:22 +0000295 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
296 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000297
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000298 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000299 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000300 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000301 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000302 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000303 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
304 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
305 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
306 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
307 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000308 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000309 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
310 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
311 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
312 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000313 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000314 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
315 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000316 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000317 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000318
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000319 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000320 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
321 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
322 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
323 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000324 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000325 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
326 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000327 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000328 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000329 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
330 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
331 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
332 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000333 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000334 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000335 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000336 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
337 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
338 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000339 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000340 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
341 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
342 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000343 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000344
Evan Chengd2cde682008-03-10 19:38:10 +0000345 if (Subtarget->hasSSE1())
Owen Anderson825b72b2009-08-11 20:47:22 +0000346 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000347
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000348 if (!Subtarget->hasSSE2())
Owen Anderson825b72b2009-08-11 20:47:22 +0000349 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000350 // On X86 and X86-64, atomic operations are lowered to locked instructions.
351 // Locked instructions, in turn, have implicit fence semantics (all memory
352 // operations are flushed before issuing the locked instruction, and they
353 // are not buffered), so we can fold away the common pattern of
354 // fence-atomic-fence.
355 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000356
Mon P Wang63307c32008-05-05 19:05:59 +0000357 // Expand certain atomics
Owen Anderson825b72b2009-08-11 20:47:22 +0000358 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Custom);
359 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Custom);
360 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
361 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Bill Wendling5bf1b4e2008-08-20 00:28:16 +0000362
Owen Anderson825b72b2009-08-11 20:47:22 +0000363 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Custom);
364 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Custom);
365 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
366 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000367
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000368 if (!Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000369 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
370 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
371 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
372 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
373 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
374 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
375 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000376 }
377
Evan Cheng3c992d22006-03-07 02:02:57 +0000378 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000379 if (!Subtarget->isTargetDarwin() &&
380 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000381 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000382 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000383 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000384
Owen Anderson825b72b2009-08-11 20:47:22 +0000385 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
386 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
387 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
388 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000389 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000390 setExceptionPointerRegister(X86::RAX);
391 setExceptionSelectorRegister(X86::RDX);
392 } else {
393 setExceptionPointerRegister(X86::EAX);
394 setExceptionSelectorRegister(X86::EDX);
395 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000396 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
397 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000398
Owen Anderson825b72b2009-08-11 20:47:22 +0000399 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000400
Owen Anderson825b72b2009-08-11 20:47:22 +0000401 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000402
Nate Begemanacc398c2006-01-25 18:21:52 +0000403 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000404 setOperationAction(ISD::VASTART , MVT::Other, Custom);
405 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000406 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000407 setOperationAction(ISD::VAARG , MVT::Other, Custom);
408 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000409 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000410 setOperationAction(ISD::VAARG , MVT::Other, Expand);
411 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000412 }
Evan Chengae642192007-03-02 23:16:35 +0000413
Owen Anderson825b72b2009-08-11 20:47:22 +0000414 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
415 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000416 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000417 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000418 if (Subtarget->isTargetCygMing())
Owen Anderson825b72b2009-08-11 20:47:22 +0000419 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000420 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000421 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000422
Evan Chengc7ce29b2009-02-13 22:36:38 +0000423 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000424 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000425 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000426 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
427 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000428
Evan Cheng223547a2006-01-31 22:28:30 +0000429 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000430 setOperationAction(ISD::FABS , MVT::f64, Custom);
431 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000432
433 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000434 setOperationAction(ISD::FNEG , MVT::f64, Custom);
435 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000436
Evan Cheng68c47cb2007-01-05 07:55:56 +0000437 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000438 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
439 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000440
Evan Chengd25e9e82006-02-02 00:28:23 +0000441 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000442 setOperationAction(ISD::FSIN , MVT::f64, Expand);
443 setOperationAction(ISD::FCOS , MVT::f64, Expand);
444 setOperationAction(ISD::FSIN , MVT::f32, Expand);
445 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000446
Chris Lattnera54aa942006-01-29 06:26:08 +0000447 // Expand FP immediates into loads from the stack, except for the special
448 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000449 addLegalFPImmediate(APFloat(+0.0)); // xorpd
450 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000451 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000452 // Use SSE for f32, x87 for f64.
453 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000454 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
455 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000456
457 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000458 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000459
460 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000461 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000462
Owen Anderson825b72b2009-08-11 20:47:22 +0000463 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000464
465 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000466 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
467 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000468
469 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000470 setOperationAction(ISD::FSIN , MVT::f32, Expand);
471 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000472
Nate Begemane1795842008-02-14 08:57:00 +0000473 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000474 addLegalFPImmediate(APFloat(+0.0f)); // xorps
475 addLegalFPImmediate(APFloat(+0.0)); // FLD0
476 addLegalFPImmediate(APFloat(+1.0)); // FLD1
477 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
478 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
479
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000480 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000481 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
482 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000483 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000484 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000485 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000486 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000487 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
488 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000489
Owen Anderson825b72b2009-08-11 20:47:22 +0000490 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
491 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
492 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
493 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000494
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000495 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000496 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
497 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000498 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000499 addLegalFPImmediate(APFloat(+0.0)); // FLD0
500 addLegalFPImmediate(APFloat(+1.0)); // FLD1
501 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
502 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000503 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
504 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
505 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
506 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000507 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000508
Dale Johannesen59a58732007-08-05 18:49:15 +0000509 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000510 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000511 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
512 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
513 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000514 {
515 bool ignored;
516 APFloat TmpFlt(+0.0);
517 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
518 &ignored);
519 addLegalFPImmediate(TmpFlt); // FLD0
520 TmpFlt.changeSign();
521 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
522 APFloat TmpFlt2(+1.0);
523 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
524 &ignored);
525 addLegalFPImmediate(TmpFlt2); // FLD1
526 TmpFlt2.changeSign();
527 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
528 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000529
Evan Chengc7ce29b2009-02-13 22:36:38 +0000530 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000531 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
532 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000533 }
Dale Johannesen2f429012007-09-26 21:10:55 +0000534 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000535
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000536 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000537 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
538 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
539 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000540
Owen Anderson825b72b2009-08-11 20:47:22 +0000541 setOperationAction(ISD::FLOG, MVT::f80, Expand);
542 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
543 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
544 setOperationAction(ISD::FEXP, MVT::f80, Expand);
545 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000546
Mon P Wangf007a8b2008-11-06 05:31:54 +0000547 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000548 // (for widening) or expand (for scalarization). Then we will selectively
549 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000550 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
551 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
552 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
553 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
554 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
555 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
556 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
557 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
558 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
559 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
560 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
561 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
562 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
563 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
564 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
565 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
566 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
567 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
568 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
569 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
570 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
571 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
572 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
573 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
574 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
575 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
576 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
577 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
578 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
579 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
580 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
581 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
582 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
583 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
584 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
585 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
586 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
587 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
588 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
589 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
590 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
591 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
592 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
593 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
594 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
595 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
596 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
597 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
598 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
599 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000600 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000601 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
602 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
603 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
604 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
605 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
606 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
607 setTruncStoreAction((MVT::SimpleValueType)VT,
608 (MVT::SimpleValueType)InnerVT, Expand);
609 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
610 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
611 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000612 }
613
Evan Chengc7ce29b2009-02-13 22:36:38 +0000614 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
615 // with -msoft-float, disable use of MMX as well.
Evan Cheng92722532009-03-26 23:06:32 +0000616 if (!UseSoftFloat && !DisableMMX && Subtarget->hasMMX()) {
Dale Johannesen76090172010-04-20 22:34:09 +0000617 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass, false);
618 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass, false);
619 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass, false);
Chris Lattnere35d9842010-07-04 22:57:10 +0000620
Dale Johannesen76090172010-04-20 22:34:09 +0000621 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass, false);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000622
Owen Anderson825b72b2009-08-11 20:47:22 +0000623 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
624 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
625 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
626 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000627
Owen Anderson825b72b2009-08-11 20:47:22 +0000628 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
629 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
630 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
631 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000632
Owen Anderson825b72b2009-08-11 20:47:22 +0000633 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
634 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
Bill Wendling74027e92007-03-15 21:24:36 +0000635
Owen Anderson825b72b2009-08-11 20:47:22 +0000636 setOperationAction(ISD::AND, MVT::v8i8, Promote);
637 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
638 setOperationAction(ISD::AND, MVT::v4i16, Promote);
639 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
640 setOperationAction(ISD::AND, MVT::v2i32, Promote);
641 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
642 setOperationAction(ISD::AND, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000643
Owen Anderson825b72b2009-08-11 20:47:22 +0000644 setOperationAction(ISD::OR, MVT::v8i8, Promote);
645 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
646 setOperationAction(ISD::OR, MVT::v4i16, Promote);
647 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
648 setOperationAction(ISD::OR, MVT::v2i32, Promote);
649 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
650 setOperationAction(ISD::OR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000651
Owen Anderson825b72b2009-08-11 20:47:22 +0000652 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
653 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
654 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
655 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
656 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
657 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
658 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000659
Owen Anderson825b72b2009-08-11 20:47:22 +0000660 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
661 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
662 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
663 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
664 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
665 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
Owen Anderson825b72b2009-08-11 20:47:22 +0000666 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000667
Owen Anderson825b72b2009-08-11 20:47:22 +0000668 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
669 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
670 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000671 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
Bill Wendlinga348c562007-03-22 18:42:45 +0000672
Owen Anderson825b72b2009-08-11 20:47:22 +0000673 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
674 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
675 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
676 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000677
Owen Anderson825b72b2009-08-11 20:47:22 +0000678 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
679 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
680 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Bill Wendling3180e202008-07-20 02:32:23 +0000681
Owen Anderson825b72b2009-08-11 20:47:22 +0000682 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
Mon P Wang9e5ecb82008-12-12 01:25:51 +0000683
Owen Anderson825b72b2009-08-11 20:47:22 +0000684 setOperationAction(ISD::SELECT, MVT::v8i8, Promote);
685 setOperationAction(ISD::SELECT, MVT::v4i16, Promote);
686 setOperationAction(ISD::SELECT, MVT::v2i32, Promote);
687 setOperationAction(ISD::SELECT, MVT::v1i64, Custom);
688 setOperationAction(ISD::VSETCC, MVT::v8i8, Custom);
689 setOperationAction(ISD::VSETCC, MVT::v4i16, Custom);
690 setOperationAction(ISD::VSETCC, MVT::v2i32, Custom);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000691
692 if (!X86ScalarSSEf64 && Subtarget->is64Bit()) {
693 setOperationAction(ISD::BIT_CONVERT, MVT::v8i8, Custom);
694 setOperationAction(ISD::BIT_CONVERT, MVT::v4i16, Custom);
695 setOperationAction(ISD::BIT_CONVERT, MVT::v2i32, Custom);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000696 setOperationAction(ISD::BIT_CONVERT, MVT::v1i64, Custom);
697 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000698 }
699
Evan Cheng92722532009-03-26 23:06:32 +0000700 if (!UseSoftFloat && Subtarget->hasSSE1()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000701 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000702
Owen Anderson825b72b2009-08-11 20:47:22 +0000703 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
704 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
705 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
706 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
707 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
708 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
709 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
710 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
711 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
712 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
713 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
714 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000715 }
716
Evan Cheng92722532009-03-26 23:06:32 +0000717 if (!UseSoftFloat && Subtarget->hasSSE2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000718 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000719
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000720 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
721 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000722 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
723 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
724 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
725 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000726
Owen Anderson825b72b2009-08-11 20:47:22 +0000727 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
728 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
729 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
730 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
731 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
732 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
733 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
734 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
735 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
736 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
737 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
738 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
739 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
740 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
741 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
742 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000743
Owen Anderson825b72b2009-08-11 20:47:22 +0000744 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
745 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
746 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
747 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000748
Owen Anderson825b72b2009-08-11 20:47:22 +0000749 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
750 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
751 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
752 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
753 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000754
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000755 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
756 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
757 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
758 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
759 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
760
Evan Cheng2c3ae372006-04-12 21:21:57 +0000761 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000762 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
763 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000764 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000765 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000766 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000767 // Do not attempt to custom lower non-128-bit vectors
768 if (!VT.is128BitVector())
769 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000770 setOperationAction(ISD::BUILD_VECTOR,
771 VT.getSimpleVT().SimpleTy, Custom);
772 setOperationAction(ISD::VECTOR_SHUFFLE,
773 VT.getSimpleVT().SimpleTy, Custom);
774 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
775 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000776 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000777
Owen Anderson825b72b2009-08-11 20:47:22 +0000778 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
779 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
780 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
781 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
782 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
783 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000784
Nate Begemancdd1eec2008-02-12 22:51:28 +0000785 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000786 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
787 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000788 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000789
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000790 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000791 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
792 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000793 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000794
795 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000796 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000797 continue;
Eric Christopher4bd24c22010-03-30 01:04:59 +0000798
Owen Andersond6662ad2009-08-10 20:46:15 +0000799 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000800 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000801 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000802 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000803 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000804 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000805 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000806 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000807 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000808 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000809 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000810
Owen Anderson825b72b2009-08-11 20:47:22 +0000811 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000812
Evan Cheng2c3ae372006-04-12 21:21:57 +0000813 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000814 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
815 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
816 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
817 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000818
Owen Anderson825b72b2009-08-11 20:47:22 +0000819 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
820 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Eli Friedman23ef1052009-06-06 03:57:58 +0000821 if (!DisableMMX && Subtarget->hasMMX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000822 setOperationAction(ISD::FP_TO_SINT, MVT::v2i32, Custom);
823 setOperationAction(ISD::SINT_TO_FP, MVT::v2i32, Custom);
Eli Friedman23ef1052009-06-06 03:57:58 +0000824 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000825 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000826
Nate Begeman14d12ca2008-02-11 04:19:36 +0000827 if (Subtarget->hasSSE41()) {
Dale Johannesen54feef22010-05-27 20:12:41 +0000828 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
829 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
830 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
831 setOperationAction(ISD::FRINT, MVT::f32, Legal);
832 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
833 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
834 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
835 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
836 setOperationAction(ISD::FRINT, MVT::f64, Legal);
837 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
838
Nate Begeman14d12ca2008-02-11 04:19:36 +0000839 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000840 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000841
842 // i8 and i16 vectors are custom , because the source register and source
843 // source memory operand types are not the same width. f32 vectors are
844 // custom since the immediate controlling the insert encodes additional
845 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000846 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
847 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
848 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
849 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000850
Owen Anderson825b72b2009-08-11 20:47:22 +0000851 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
852 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
853 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
854 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000855
856 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000857 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
858 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000859 }
860 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000861
Nate Begeman30a0de92008-07-17 16:51:19 +0000862 if (Subtarget->hasSSE42()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000863 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
Nate Begeman30a0de92008-07-17 16:51:19 +0000864 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000865
David Greene9b9838d2009-06-29 16:47:10 +0000866 if (!UseSoftFloat && Subtarget->hasAVX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000867 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
868 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
869 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
870 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000871
Owen Anderson825b72b2009-08-11 20:47:22 +0000872 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
873 setOperationAction(ISD::LOAD, MVT::v8i32, Legal);
874 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
875 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
876 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
877 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
878 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
879 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
880 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
881 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
882 //setOperationAction(ISD::BUILD_VECTOR, MVT::v8f32, Custom);
883 //setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Custom);
884 //setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8f32, Custom);
885 //setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
886 //setOperationAction(ISD::VSETCC, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000887
888 // Operations to consider commented out -v16i16 v32i8
Owen Anderson825b72b2009-08-11 20:47:22 +0000889 //setOperationAction(ISD::ADD, MVT::v16i16, Legal);
890 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
891 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
892 //setOperationAction(ISD::SUB, MVT::v32i8, Legal);
893 //setOperationAction(ISD::SUB, MVT::v16i16, Legal);
894 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
895 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
896 //setOperationAction(ISD::MUL, MVT::v16i16, Legal);
897 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
898 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
899 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
900 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
901 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
902 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000903
Owen Anderson825b72b2009-08-11 20:47:22 +0000904 setOperationAction(ISD::VSETCC, MVT::v4f64, Custom);
905 // setOperationAction(ISD::VSETCC, MVT::v32i8, Custom);
906 // setOperationAction(ISD::VSETCC, MVT::v16i16, Custom);
907 setOperationAction(ISD::VSETCC, MVT::v8i32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000908
Owen Anderson825b72b2009-08-11 20:47:22 +0000909 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v32i8, Custom);
910 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i16, Custom);
911 // setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i16, Custom);
912 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i32, Custom);
913 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000914
Owen Anderson825b72b2009-08-11 20:47:22 +0000915 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f64, Custom);
916 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i64, Custom);
917 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f64, Custom);
918 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i64, Custom);
919 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f64, Custom);
920 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000921
922#if 0
923 // Not sure we want to do this since there are no 256-bit integer
924 // operations in AVX
925
926 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
927 // This includes 256-bit vectors
Owen Anderson825b72b2009-08-11 20:47:22 +0000928 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; ++i) {
929 EVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000930
931 // Do not attempt to custom lower non-power-of-2 vectors
932 if (!isPowerOf2_32(VT.getVectorNumElements()))
933 continue;
934
935 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
936 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
937 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
938 }
939
940 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000941 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i64, Custom);
942 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i64, Custom);
Eric Christopherfd179292009-08-27 18:07:15 +0000943 }
David Greene9b9838d2009-06-29 16:47:10 +0000944#endif
945
946#if 0
947 // Not sure we want to do this since there are no 256-bit integer
948 // operations in AVX
949
950 // Promote v32i8, v16i16, v8i32 load, select, and, or, xor to v4i64.
951 // Including 256-bit vectors
Owen Anderson825b72b2009-08-11 20:47:22 +0000952 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; i++) {
953 EVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000954
955 if (!VT.is256BitVector()) {
956 continue;
957 }
958 setOperationAction(ISD::AND, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000959 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000960 setOperationAction(ISD::OR, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000961 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000962 setOperationAction(ISD::XOR, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000963 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000964 setOperationAction(ISD::LOAD, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000965 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000966 setOperationAction(ISD::SELECT, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000967 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000968 }
969
Owen Anderson825b72b2009-08-11 20:47:22 +0000970 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
David Greene9b9838d2009-06-29 16:47:10 +0000971#endif
972 }
973
Evan Cheng6be2c582006-04-05 23:38:46 +0000974 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000975 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +0000976
Bill Wendling74c37652008-12-09 22:08:41 +0000977 // Add/Sub/Mul with overflow operations are custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000978 setOperationAction(ISD::SADDO, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000979 setOperationAction(ISD::UADDO, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000980 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000981 setOperationAction(ISD::USUBO, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000982 setOperationAction(ISD::SMULO, MVT::i32, Custom);
Dan Gohman71c62a22010-06-02 19:13:40 +0000983
Eli Friedman962f5492010-06-02 19:35:46 +0000984 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
985 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +0000986 //
Eli Friedman962f5492010-06-02 19:35:46 +0000987 // FIXME: We really should do custom legalization for addition and
988 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
989 // than generic legalization for 64-bit multiplication-with-overflow, though.
Eli Friedmana993f0a2010-06-02 00:27:18 +0000990 if (Subtarget->is64Bit()) {
991 setOperationAction(ISD::SADDO, MVT::i64, Custom);
992 setOperationAction(ISD::UADDO, MVT::i64, Custom);
993 setOperationAction(ISD::SSUBO, MVT::i64, Custom);
994 setOperationAction(ISD::USUBO, MVT::i64, Custom);
995 setOperationAction(ISD::SMULO, MVT::i64, Custom);
996 }
Bill Wendling41ea7e72008-11-24 19:21:46 +0000997
Evan Chengd54f2d52009-03-31 19:38:51 +0000998 if (!Subtarget->is64Bit()) {
999 // These libcalls are not available in 32-bit.
1000 setLibcallName(RTLIB::SHL_I128, 0);
1001 setLibcallName(RTLIB::SRL_I128, 0);
1002 setLibcallName(RTLIB::SRA_I128, 0);
1003 }
1004
Evan Cheng206ee9d2006-07-07 08:33:52 +00001005 // We have target-specific dag combine patterns for the following nodes:
1006 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001007 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +00001008 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +00001009 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001010 setTargetDAGCombine(ISD::SHL);
1011 setTargetDAGCombine(ISD::SRA);
1012 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001013 setTargetDAGCombine(ISD::OR);
Chris Lattner149a4e52008-02-22 02:09:43 +00001014 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001015 setTargetDAGCombine(ISD::ZERO_EXTEND);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001016 if (Subtarget->is64Bit())
1017 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001018
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001019 computeRegisterProperties();
1020
Evan Cheng87ed7162006-02-14 08:25:08 +00001021 // FIXME: These should be based on subtarget info. Plus, the values should
1022 // be smaller when we are in optimizing for size mode.
Dan Gohman87060f52008-06-30 21:00:56 +00001023 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng255f20f2010-04-01 06:04:33 +00001024 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Dan Gohman87060f52008-06-30 21:00:56 +00001025 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Evan Chengfb8075d2008-02-28 00:43:03 +00001026 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001027 benefitFromCodePlacementOpt = true;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001028}
1029
Scott Michel5b8f82e2008-03-10 15:42:14 +00001030
Owen Anderson825b72b2009-08-11 20:47:22 +00001031MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
1032 return MVT::i8;
Scott Michel5b8f82e2008-03-10 15:42:14 +00001033}
1034
1035
Evan Cheng29286502008-01-23 23:17:41 +00001036/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1037/// the desired ByVal argument alignment.
1038static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
1039 if (MaxAlign == 16)
1040 return;
1041 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
1042 if (VTy->getBitWidth() == 128)
1043 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +00001044 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1045 unsigned EltAlign = 0;
1046 getMaxByValAlign(ATy->getElementType(), EltAlign);
1047 if (EltAlign > MaxAlign)
1048 MaxAlign = EltAlign;
1049 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1050 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1051 unsigned EltAlign = 0;
1052 getMaxByValAlign(STy->getElementType(i), EltAlign);
1053 if (EltAlign > MaxAlign)
1054 MaxAlign = EltAlign;
1055 if (MaxAlign == 16)
1056 break;
1057 }
1058 }
1059 return;
1060}
1061
1062/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1063/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001064/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1065/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +00001066unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001067 if (Subtarget->is64Bit()) {
1068 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001069 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001070 if (TyAlign > 8)
1071 return TyAlign;
1072 return 8;
1073 }
1074
Evan Cheng29286502008-01-23 23:17:41 +00001075 unsigned Align = 4;
Dale Johannesen0c191872008-02-08 19:48:20 +00001076 if (Subtarget->hasSSE1())
1077 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001078 return Align;
1079}
Chris Lattner2b02a442007-02-25 08:29:00 +00001080
Evan Chengf0df0312008-05-15 08:39:06 +00001081/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001082/// and store operations as a result of memset, memcpy, and memmove
1083/// lowering. If DstAlign is zero that means it's safe to destination
1084/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1085/// means there isn't a need to check it against alignment requirement,
1086/// probably because the source does not need to be loaded. If
1087/// 'NonScalarIntSafe' is true, that means it's safe to return a
1088/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1089/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1090/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001091/// It returns EVT::Other if the type should be determined using generic
1092/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001093EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001094X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1095 unsigned DstAlign, unsigned SrcAlign,
Evan Chengf28f8bc2010-04-02 19:36:14 +00001096 bool NonScalarIntSafe,
Evan Chengc3b0c342010-04-08 07:37:57 +00001097 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001098 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001099 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1100 // linux. This is because the stack realignment code can't handle certain
1101 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001102 const Function *F = MF.getFunction();
Evan Chengf28f8bc2010-04-02 19:36:14 +00001103 if (NonScalarIntSafe &&
1104 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001105 if (Size >= 16 &&
1106 (Subtarget->isUnalignedMemAccessFast() ||
Chandler Carruthae1d41c2010-04-02 01:31:24 +00001107 ((DstAlign == 0 || DstAlign >= 16) &&
1108 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001109 Subtarget->getStackAlignment() >= 16) {
1110 if (Subtarget->hasSSE2())
1111 return MVT::v4i32;
Evan Chengf28f8bc2010-04-02 19:36:14 +00001112 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001113 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001114 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001115 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001116 Subtarget->getStackAlignment() >= 8 &&
Evan Chengc3b0c342010-04-08 07:37:57 +00001117 Subtarget->hasSSE2()) {
1118 // Do not use f64 to lower memcpy if source is string constant. It's
1119 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001120 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001121 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001122 }
Evan Chengf0df0312008-05-15 08:39:06 +00001123 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001124 return MVT::i64;
1125 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001126}
1127
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001128/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1129/// current function. The returned value is a member of the
1130/// MachineJumpTableInfo::JTEntryKind enum.
1131unsigned X86TargetLowering::getJumpTableEncoding() const {
1132 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1133 // symbol.
1134 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1135 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001136 return MachineJumpTableInfo::EK_Custom32;
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001137
1138 // Otherwise, use the normal jump table encoding heuristics.
1139 return TargetLowering::getJumpTableEncoding();
1140}
1141
Chris Lattner589c6f62010-01-26 06:28:43 +00001142/// getPICBaseSymbol - Return the X86-32 PIC base.
1143MCSymbol *
1144X86TargetLowering::getPICBaseSymbol(const MachineFunction *MF,
1145 MCContext &Ctx) const {
1146 const MCAsmInfo &MAI = *getTargetMachine().getMCAsmInfo();
Chris Lattner9b97a732010-03-30 18:10:53 +00001147 return Ctx.GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix())+
1148 Twine(MF->getFunctionNumber())+"$pb");
Chris Lattner589c6f62010-01-26 06:28:43 +00001149}
1150
1151
Chris Lattnerc64daab2010-01-26 05:02:42 +00001152const MCExpr *
1153X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1154 const MachineBasicBlock *MBB,
1155 unsigned uid,MCContext &Ctx) const{
1156 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1157 Subtarget->isPICStyleGOT());
1158 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1159 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001160 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1161 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001162}
1163
Evan Chengcc415862007-11-09 01:32:10 +00001164/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1165/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001166SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001167 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001168 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001169 // This doesn't have DebugLoc associated with it, but is not really the
1170 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001171 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001172 return Table;
1173}
1174
Chris Lattner589c6f62010-01-26 06:28:43 +00001175/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1176/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1177/// MCExpr.
1178const MCExpr *X86TargetLowering::
1179getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1180 MCContext &Ctx) const {
1181 // X86-64 uses RIP relative addressing based on the jump table label.
1182 if (Subtarget->isPICStyleRIPRel())
1183 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1184
1185 // Otherwise, the reference is relative to the PIC base.
1186 return MCSymbolRefExpr::Create(getPICBaseSymbol(MF, Ctx), Ctx);
1187}
1188
Bill Wendlingb4202b82009-07-01 18:50:55 +00001189/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +00001190unsigned X86TargetLowering::getFunctionAlignment(const Function *F) const {
Dan Gohman25103a22009-08-18 00:20:06 +00001191 return F->hasFnAttr(Attribute::OptimizeForSize) ? 0 : 4;
Bill Wendling20c568f2009-06-30 22:38:32 +00001192}
1193
Chris Lattner2b02a442007-02-25 08:29:00 +00001194//===----------------------------------------------------------------------===//
1195// Return Value Calling Convention Implementation
1196//===----------------------------------------------------------------------===//
1197
Chris Lattner59ed56b2007-02-28 04:55:35 +00001198#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001199
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001200bool
1201X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv, bool isVarArg,
1202 const SmallVectorImpl<EVT> &OutTys,
1203 const SmallVectorImpl<ISD::ArgFlagsTy> &ArgsFlags,
Dan Gohmand858e902010-04-17 15:26:15 +00001204 SelectionDAG &DAG) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001205 SmallVector<CCValAssign, 16> RVLocs;
1206 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1207 RVLocs, *DAG.getContext());
1208 return CCInfo.CheckReturn(OutTys, ArgsFlags, RetCC_X86);
1209}
1210
Dan Gohman98ca4f22009-08-05 01:29:28 +00001211SDValue
1212X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001213 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001214 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmand858e902010-04-17 15:26:15 +00001215 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001216 MachineFunction &MF = DAG.getMachineFunction();
1217 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001218
Chris Lattner9774c912007-02-27 05:28:59 +00001219 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001220 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1221 RVLocs, *DAG.getContext());
1222 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001223
Evan Chengdcea1632010-02-04 02:40:39 +00001224 // Add the regs to the liveout set for the function.
1225 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1226 for (unsigned i = 0; i != RVLocs.size(); ++i)
1227 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1228 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001229
Dan Gohman475871a2008-07-27 21:46:04 +00001230 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001231
Dan Gohman475871a2008-07-27 21:46:04 +00001232 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001233 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1234 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001235 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1236 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001237
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001238 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001239 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1240 CCValAssign &VA = RVLocs[i];
1241 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman98ca4f22009-08-05 01:29:28 +00001242 SDValue ValToCopy = Outs[i].Val;
Scott Michelfdc40a02009-02-17 22:15:04 +00001243
Chris Lattner447ff682008-03-11 03:23:40 +00001244 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1245 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001246 if (VA.getLocReg() == X86::ST0 ||
1247 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001248 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1249 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001250 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001251 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001252 RetOps.push_back(ValToCopy);
1253 // Don't emit a copytoreg.
1254 continue;
1255 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001256
Evan Cheng242b38b2009-02-23 09:03:22 +00001257 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1258 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001259 if (Subtarget->is64Bit()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001260 EVT ValVT = ValToCopy.getValueType();
Evan Cheng242b38b2009-02-23 09:03:22 +00001261 if (ValVT.isVector() && ValVT.getSizeInBits() == 64) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001262 ValToCopy = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, ValToCopy);
Evan Cheng242b38b2009-02-23 09:03:22 +00001263 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1)
Owen Anderson825b72b2009-08-11 20:47:22 +00001264 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, ValToCopy);
Evan Cheng242b38b2009-02-23 09:03:22 +00001265 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001266 }
1267
Dale Johannesendd64c412009-02-04 00:33:20 +00001268 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001269 Flag = Chain.getValue(1);
1270 }
Dan Gohman61a92132008-04-21 23:59:07 +00001271
1272 // The x86-64 ABI for returning structs by value requires that we copy
1273 // the sret argument into %rax for the return. We saved the argument into
1274 // a virtual register in the entry block, so now we copy the value out
1275 // and into %rax.
1276 if (Subtarget->is64Bit() &&
1277 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1278 MachineFunction &MF = DAG.getMachineFunction();
1279 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1280 unsigned Reg = FuncInfo->getSRetReturnReg();
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001281 assert(Reg &&
1282 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001283 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001284
Dale Johannesendd64c412009-02-04 00:33:20 +00001285 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001286 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001287
1288 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001289 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001290 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001291
Chris Lattner447ff682008-03-11 03:23:40 +00001292 RetOps[0] = Chain; // Update chain.
1293
1294 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001295 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001296 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001297
1298 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001299 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001300}
1301
Dan Gohman98ca4f22009-08-05 01:29:28 +00001302/// LowerCallResult - Lower the result values of a call into the
1303/// appropriate copies out of appropriate physical registers.
1304///
1305SDValue
1306X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001307 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001308 const SmallVectorImpl<ISD::InputArg> &Ins,
1309 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001310 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001311
Chris Lattnere32bbf62007-02-28 07:09:55 +00001312 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001313 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001314 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001315 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001316 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001317 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001318
Chris Lattner3085e152007-02-25 08:59:22 +00001319 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001320 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001321 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001322 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001323
Torok Edwin3f142c32009-02-01 18:15:56 +00001324 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001325 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Dan Gohman98ca4f22009-08-05 01:29:28 +00001326 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001327 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001328 }
1329
Chris Lattner8e6da152008-03-10 21:08:41 +00001330 // If this is a call to a function that returns an fp value on the floating
1331 // point stack, but where we prefer to use the value in xmm registers, copy
1332 // 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 +00001333 if ((VA.getLocReg() == X86::ST0 ||
1334 VA.getLocReg() == X86::ST1) &&
1335 isScalarFPTypeInSSEReg(VA.getValVT())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001336 CopyVT = MVT::f80;
Chris Lattner3085e152007-02-25 08:59:22 +00001337 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001338
Evan Cheng79fb3b42009-02-20 20:43:02 +00001339 SDValue Val;
1340 if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001341 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1342 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1343 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001344 MVT::v2i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001345 Val = Chain.getValue(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001346 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1347 Val, DAG.getConstant(0, MVT::i64));
Evan Cheng242b38b2009-02-23 09:03:22 +00001348 } else {
1349 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001350 MVT::i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001351 Val = Chain.getValue(0);
1352 }
Evan Cheng79fb3b42009-02-20 20:43:02 +00001353 Val = DAG.getNode(ISD::BIT_CONVERT, dl, CopyVT, Val);
1354 } else {
1355 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1356 CopyVT, InFlag).getValue(1);
1357 Val = Chain.getValue(0);
1358 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001359 InFlag = Chain.getValue(2);
Chris Lattner112dedc2007-12-29 06:41:28 +00001360
Dan Gohman37eed792009-02-04 17:28:58 +00001361 if (CopyVT != VA.getValVT()) {
Chris Lattner8e6da152008-03-10 21:08:41 +00001362 // Round the F80 the right size, which also moves to the appropriate xmm
1363 // register.
Dan Gohman37eed792009-02-04 17:28:58 +00001364 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
Chris Lattner8e6da152008-03-10 21:08:41 +00001365 // This truncation won't change the value.
1366 DAG.getIntPtrConstant(1));
1367 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001368
Dan Gohman98ca4f22009-08-05 01:29:28 +00001369 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001370 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001371
Dan Gohman98ca4f22009-08-05 01:29:28 +00001372 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001373}
1374
1375
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001376//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001377// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001378//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001379// StdCall calling convention seems to be standard for many Windows' API
1380// routines and around. It differs from C calling convention just a little:
1381// callee should clean up the stack, not caller. Symbols should be also
1382// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001383// For info on fast calling convention see Fast Calling Convention (tail call)
1384// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001385
Dan Gohman98ca4f22009-08-05 01:29:28 +00001386/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001387/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001388static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1389 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001390 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001391
Dan Gohman98ca4f22009-08-05 01:29:28 +00001392 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001393}
1394
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001395/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001396/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001397static bool
1398ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1399 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001400 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001401
Dan Gohman98ca4f22009-08-05 01:29:28 +00001402 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001403}
1404
Dan Gohman095cc292008-09-13 01:54:27 +00001405/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1406/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001407CCAssignFn *X86TargetLowering::CCAssignFnForNode(CallingConv::ID CC) const {
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001408 if (Subtarget->is64Bit()) {
Chris Lattner29689432010-03-11 00:22:57 +00001409 if (CC == CallingConv::GHC)
1410 return CC_X86_64_GHC;
1411 else if (Subtarget->isTargetWin64())
Anton Korobeynikov8f88cb02008-03-22 20:37:30 +00001412 return CC_X86_Win64_C;
Evan Chenge9ac9e62008-09-07 09:07:23 +00001413 else
1414 return CC_X86_64_C;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001415 }
1416
Gordon Henriksen86737662008-01-05 16:56:59 +00001417 if (CC == CallingConv::X86_FastCall)
1418 return CC_X86_32_FastCall;
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001419 else if (CC == CallingConv::X86_ThisCall)
1420 return CC_X86_32_ThisCall;
Evan Chengb188dd92008-09-10 18:25:29 +00001421 else if (CC == CallingConv::Fast)
1422 return CC_X86_32_FastCC;
Chris Lattner29689432010-03-11 00:22:57 +00001423 else if (CC == CallingConv::GHC)
1424 return CC_X86_32_GHC;
Gordon Henriksen86737662008-01-05 16:56:59 +00001425 else
1426 return CC_X86_32_C;
1427}
1428
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001429/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1430/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001431/// the specific parameter attribute. The copy will be passed as a byval
1432/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001433static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001434CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001435 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1436 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001437 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesendd64c412009-02-04 00:33:20 +00001438 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001439 /*isVolatile*/false, /*AlwaysInline=*/true,
1440 NULL, 0, NULL, 0);
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001441}
1442
Chris Lattner29689432010-03-11 00:22:57 +00001443/// IsTailCallConvention - Return true if the calling convention is one that
1444/// supports tail call optimization.
1445static bool IsTailCallConvention(CallingConv::ID CC) {
1446 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1447}
1448
Evan Cheng0c439eb2010-01-27 00:07:07 +00001449/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1450/// a tailcall target by changing its ABI.
1451static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001452 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001453}
1454
Dan Gohman98ca4f22009-08-05 01:29:28 +00001455SDValue
1456X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001457 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001458 const SmallVectorImpl<ISD::InputArg> &Ins,
1459 DebugLoc dl, SelectionDAG &DAG,
1460 const CCValAssign &VA,
1461 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001462 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001463 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001464 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001465 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001466 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001467 EVT ValVT;
1468
1469 // If value is passed by pointer we have address passed instead of the value
1470 // itself.
1471 if (VA.getLocInfo() == CCValAssign::Indirect)
1472 ValVT = VA.getLocVT();
1473 else
1474 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001475
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001476 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001477 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001478 // In case of tail call optimization mark all arguments mutable. Since they
1479 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001480 if (Flags.isByVal()) {
1481 int FI = MFI->CreateFixedObject(Flags.getByValSize(),
Evan Chenged2ae132010-07-03 00:40:23 +00001482 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001483 return DAG.getFrameIndex(FI, getPointerTy());
1484 } else {
1485 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001486 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001487 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1488 return DAG.getLoad(ValVT, dl, Chain, FIN,
David Greene67c9d422010-02-15 16:53:33 +00001489 PseudoSourceValue::getFixedStack(FI), 0,
1490 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001491 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001492}
1493
Dan Gohman475871a2008-07-27 21:46:04 +00001494SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001495X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001496 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001497 bool isVarArg,
1498 const SmallVectorImpl<ISD::InputArg> &Ins,
1499 DebugLoc dl,
1500 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001501 SmallVectorImpl<SDValue> &InVals)
1502 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001503 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001504 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001505
Gordon Henriksen86737662008-01-05 16:56:59 +00001506 const Function* Fn = MF.getFunction();
1507 if (Fn->hasExternalLinkage() &&
1508 Subtarget->isTargetCygMing() &&
1509 Fn->getName() == "main")
1510 FuncInfo->setForceFramePointer(true);
1511
Evan Cheng1bc78042006-04-26 01:20:17 +00001512 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001513 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001514 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001515
Chris Lattner29689432010-03-11 00:22:57 +00001516 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1517 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001518
Chris Lattner638402b2007-02-28 07:00:42 +00001519 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001520 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001521 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1522 ArgLocs, *DAG.getContext());
1523 CCInfo.AnalyzeFormalArguments(Ins, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001524
Chris Lattnerf39f7712007-02-28 05:46:49 +00001525 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001526 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001527 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1528 CCValAssign &VA = ArgLocs[i];
1529 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1530 // places.
1531 assert(VA.getValNo() != LastVal &&
1532 "Don't support value assigned to multiple locs yet");
1533 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001534
Chris Lattnerf39f7712007-02-28 05:46:49 +00001535 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001536 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001537 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001538 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001539 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001540 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001541 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001542 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001543 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001544 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001545 RC = X86::FR64RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001546 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001547 RC = X86::VR128RegisterClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001548 else if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
1549 RC = X86::VR64RegisterClass;
1550 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001551 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001552
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001553 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001554 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001555
Chris Lattnerf39f7712007-02-28 05:46:49 +00001556 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1557 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1558 // right size.
1559 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001560 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001561 DAG.getValueType(VA.getValVT()));
1562 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001563 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001564 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001565 else if (VA.getLocInfo() == CCValAssign::BCvt)
Anton Korobeynikov6dde14b2009-08-03 08:14:14 +00001566 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001567
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001568 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001569 // Handle MMX values passed in XMM regs.
1570 if (RegVT.isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001571 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1572 ArgValue, DAG.getConstant(0, MVT::i64));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001573 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1574 } else
1575 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001576 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001577 } else {
1578 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001579 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001580 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001581
1582 // If value is passed via pointer - do a load.
1583 if (VA.getLocInfo() == CCValAssign::Indirect)
David Greene67c9d422010-02-15 16:53:33 +00001584 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue, NULL, 0,
1585 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001586
Dan Gohman98ca4f22009-08-05 01:29:28 +00001587 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001588 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001589
Dan Gohman61a92132008-04-21 23:59:07 +00001590 // The x86-64 ABI for returning structs by value requires that we copy
1591 // the sret argument into %rax for the return. Save the argument into
1592 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001593 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001594 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1595 unsigned Reg = FuncInfo->getSRetReturnReg();
1596 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001597 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001598 FuncInfo->setSRetReturnReg(Reg);
1599 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001600 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001601 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001602 }
1603
Chris Lattnerf39f7712007-02-28 05:46:49 +00001604 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001605 // Align stack specially for tail calls.
1606 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001607 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001608
Evan Cheng1bc78042006-04-26 01:20:17 +00001609 // If the function takes variable number of arguments, make a frame index for
1610 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001611 if (isVarArg) {
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001612 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1613 CallConv != CallingConv::X86_ThisCall)) {
Evan Chenged2ae132010-07-03 00:40:23 +00001614 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001615 }
1616 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001617 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1618
1619 // FIXME: We should really autogenerate these arrays
1620 static const unsigned GPR64ArgRegsWin64[] = {
1621 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001622 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001623 static const unsigned XMMArgRegsWin64[] = {
1624 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1625 };
1626 static const unsigned GPR64ArgRegs64Bit[] = {
1627 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1628 };
1629 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001630 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1631 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1632 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001633 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1634
1635 if (IsWin64) {
1636 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1637 GPR64ArgRegs = GPR64ArgRegsWin64;
1638 XMMArgRegs = XMMArgRegsWin64;
1639 } else {
1640 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1641 GPR64ArgRegs = GPR64ArgRegs64Bit;
1642 XMMArgRegs = XMMArgRegs64Bit;
1643 }
1644 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1645 TotalNumIntRegs);
1646 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1647 TotalNumXMMRegs);
1648
Devang Patel578efa92009-06-05 21:57:13 +00001649 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Evan Chengc7ce29b2009-02-13 22:36:38 +00001650 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001651 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001652 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001653 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001654 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00001655 // Kernel mode asks for SSE to be disabled, so don't push them
1656 // on the stack.
1657 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001658
Gordon Henriksen86737662008-01-05 16:56:59 +00001659 // For X86-64, if there are vararg parameters that are passed via
1660 // registers, then we must store them to their spots on the stack so they
1661 // may be loaded by deferencing the result of va_next.
Dan Gohman1e93df62010-04-17 14:41:14 +00001662 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1663 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1664 FuncInfo->setRegSaveFrameIndex(
1665 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
1666 false));
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001667
Gordon Henriksen86737662008-01-05 16:56:59 +00001668 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001669 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001670 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1671 getPointerTy());
1672 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001673 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001674 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1675 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001676 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
1677 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001678 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001679 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001680 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Dan Gohman1e93df62010-04-17 14:41:14 +00001681 PseudoSourceValue::getFixedStack(
1682 FuncInfo->getRegSaveFrameIndex()),
David Greene67c9d422010-02-15 16:53:33 +00001683 Offset, false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001684 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001685 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001686 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001687
Dan Gohmanface41a2009-08-16 21:24:25 +00001688 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1689 // Now store the XMM (fp + vector) parameter registers.
1690 SmallVector<SDValue, 11> SaveXMMOps;
1691 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001692
Dan Gohmanface41a2009-08-16 21:24:25 +00001693 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
1694 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1695 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001696
Dan Gohman1e93df62010-04-17 14:41:14 +00001697 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1698 FuncInfo->getRegSaveFrameIndex()));
1699 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1700 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001701
Dan Gohmanface41a2009-08-16 21:24:25 +00001702 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
1703 unsigned VReg = MF.addLiveIn(XMMArgRegs[NumXMMRegs],
1704 X86::VR128RegisterClass);
1705 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1706 SaveXMMOps.push_back(Val);
1707 }
1708 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1709 MVT::Other,
1710 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001711 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001712
1713 if (!MemOps.empty())
1714 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1715 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001716 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001717 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001718
Gordon Henriksen86737662008-01-05 16:56:59 +00001719 // Some CCs need callee pop.
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001720 if (Subtarget->IsCalleePop(isVarArg, CallConv)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001721 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001722 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001723 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001724 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001725 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001726 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001727 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001728
Gordon Henriksen86737662008-01-05 16:56:59 +00001729 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001730 // RegSaveFrameIndex is X86-64 only.
1731 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001732 if (CallConv == CallingConv::X86_FastCall ||
1733 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001734 // fastcc functions can't have varargs.
1735 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001736 }
Evan Cheng25caf632006-05-23 21:06:34 +00001737
Dan Gohman98ca4f22009-08-05 01:29:28 +00001738 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001739}
1740
Dan Gohman475871a2008-07-27 21:46:04 +00001741SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001742X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1743 SDValue StackPtr, SDValue Arg,
1744 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001745 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001746 ISD::ArgFlagsTy Flags) const {
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +00001747 const unsigned FirstStackArgOffset = (Subtarget->isTargetWin64() ? 32 : 0);
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +00001748 unsigned LocMemOffset = FirstStackArgOffset + VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001749 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001750 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001751 if (Flags.isByVal()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001752 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Evan Chengdffbd832008-01-10 00:09:10 +00001753 }
Dale Johannesenace16102009-02-03 19:33:06 +00001754 return DAG.getStore(Chain, dl, Arg, PtrOff,
David Greene67c9d422010-02-15 16:53:33 +00001755 PseudoSourceValue::getStack(), LocMemOffset,
1756 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001757}
1758
Bill Wendling64e87322009-01-16 19:25:27 +00001759/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001760/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001761SDValue
1762X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001763 SDValue &OutRetAddr, SDValue Chain,
1764 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001765 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001766 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001767 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001768 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001769
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001770 // Load the "old" Return address.
David Greene67c9d422010-02-15 16:53:33 +00001771 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, NULL, 0, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001772 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001773}
1774
1775/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1776/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001777static SDValue
1778EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001779 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001780 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001781 // Store the return address to the appropriate stack slot.
1782 if (!FPDiff) return Chain;
1783 // Calculate the new stack slot for the return address.
1784 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001785 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00001786 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001787 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001788 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001789 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
David Greene67c9d422010-02-15 16:53:33 +00001790 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0,
1791 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001792 return Chain;
1793}
1794
Dan Gohman98ca4f22009-08-05 01:29:28 +00001795SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001796X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001797 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001798 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001799 const SmallVectorImpl<ISD::OutputArg> &Outs,
1800 const SmallVectorImpl<ISD::InputArg> &Ins,
1801 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001802 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001803 MachineFunction &MF = DAG.getMachineFunction();
1804 bool Is64Bit = Subtarget->is64Bit();
1805 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00001806 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001807
Evan Cheng5f941932010-02-05 02:21:12 +00001808 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001809 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00001810 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1811 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Evan Cheng022d9e12010-02-02 23:55:14 +00001812 Outs, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00001813
1814 // Sibcalls are automatically detected tailcalls which do not require
1815 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00001816 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00001817 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00001818
1819 if (isTailCall)
1820 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00001821 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00001822
Chris Lattner29689432010-03-11 00:22:57 +00001823 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1824 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001825
Chris Lattner638402b2007-02-28 07:00:42 +00001826 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001827 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001828 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1829 ArgLocs, *DAG.getContext());
1830 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001831
Chris Lattner423c5f42007-02-28 05:31:48 +00001832 // Get a count of how many bytes are to be pushed on the stack.
1833 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00001834 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00001835 // This is a sibcall. The memory operands are available in caller's
1836 // own caller's stack.
1837 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00001838 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00001839 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001840
Gordon Henriksen86737662008-01-05 16:56:59 +00001841 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00001842 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001843 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00001844 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00001845 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1846 FPDiff = NumBytesCallerPushed - NumBytes;
1847
1848 // Set the delta of movement of the returnaddr stackslot.
1849 // But only set if delta is greater than previous delta.
1850 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1851 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1852 }
1853
Evan Chengf22f9b32010-02-06 03:28:46 +00001854 if (!IsSibcall)
1855 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001856
Dan Gohman475871a2008-07-27 21:46:04 +00001857 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001858 // Load return adress for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00001859 if (isTailCall && FPDiff)
1860 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
1861 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001862
Dan Gohman475871a2008-07-27 21:46:04 +00001863 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1864 SmallVector<SDValue, 8> MemOpChains;
1865 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001866
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001867 // Walk the register/memloc assignments, inserting copies/loads. In the case
1868 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00001869 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1870 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001871 EVT RegVT = VA.getLocVT();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001872 SDValue Arg = Outs[i].Val;
1873 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00001874 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00001875
Chris Lattner423c5f42007-02-28 05:31:48 +00001876 // Promote the value if needed.
1877 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001878 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00001879 case CCValAssign::Full: break;
1880 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001881 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001882 break;
1883 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001884 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001885 break;
1886 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001887 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
1888 // Special case: passing MMX values in XMM registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001889 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
1890 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
1891 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001892 } else
1893 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
1894 break;
1895 case CCValAssign::BCvt:
1896 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001897 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001898 case CCValAssign::Indirect: {
1899 // Store the argument.
1900 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00001901 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001902 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
David Greene67c9d422010-02-15 16:53:33 +00001903 PseudoSourceValue::getFixedStack(FI), 0,
1904 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001905 Arg = SpillSlot;
1906 break;
1907 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00001908 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001909
Chris Lattner423c5f42007-02-28 05:31:48 +00001910 if (VA.isRegLoc()) {
1911 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Evan Chengf22f9b32010-02-06 03:28:46 +00001912 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00001913 assert(VA.isMemLoc());
1914 if (StackPtr.getNode() == 0)
1915 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
1916 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1917 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001918 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001919 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001920
Evan Cheng32fe1032006-05-25 00:59:30 +00001921 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001922 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001923 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001924
Evan Cheng347d5f72006-04-28 21:29:37 +00001925 // Build a sequence of copy-to-reg nodes chained together with token chain
1926 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001927 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001928 // Tail call byval lowering might overwrite argument registers so in case of
1929 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001930 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001931 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001932 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001933 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001934 InFlag = Chain.getValue(1);
1935 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001936
Chris Lattner88e1fd52009-07-09 04:24:46 +00001937 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001938 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1939 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001940 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001941 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
1942 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001943 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001944 InFlag);
1945 InFlag = Chain.getValue(1);
1946 } else {
1947 // If we are tail calling and generating PIC/GOT style code load the
1948 // address of the callee into ECX. The value in ecx is used as target of
1949 // the tail jump. This is done to circumvent the ebx/callee-saved problem
1950 // for tail calls on PIC/GOT architectures. Normally we would just put the
1951 // address of GOT into ebx and then call target@PLT. But for tail calls
1952 // ebx would be restored (since ebx is callee saved) before jumping to the
1953 // target@PLT.
1954
1955 // Note: The actual moving to ECX is done further down.
1956 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1957 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1958 !G->getGlobal()->hasProtectedVisibility())
1959 Callee = LowerGlobalAddress(Callee, DAG);
1960 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00001961 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001962 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001963 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001964
Gordon Henriksen86737662008-01-05 16:56:59 +00001965 if (Is64Bit && isVarArg) {
1966 // From AMD64 ABI document:
1967 // For calls that may call functions that use varargs or stdargs
1968 // (prototype-less calls or calls to functions containing ellipsis (...) in
1969 // the declaration) %al is used as hidden argument to specify the number
1970 // of SSE registers used. The contents of %al do not need to match exactly
1971 // the number of registers, but must be an ubound on the number of SSE
1972 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001973
1974 // FIXME: Verify this on Win64
Gordon Henriksen86737662008-01-05 16:56:59 +00001975 // Count the number of XMM registers allocated.
1976 static const unsigned XMMArgRegs[] = {
1977 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1978 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1979 };
1980 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Scott Michelfdc40a02009-02-17 22:15:04 +00001981 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00001982 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001983
Dale Johannesendd64c412009-02-04 00:33:20 +00001984 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00001985 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00001986 InFlag = Chain.getValue(1);
1987 }
1988
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001989
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001990 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001991 if (isTailCall) {
1992 // Force all the incoming stack arguments to be loaded from the stack
1993 // before any new outgoing arguments are stored to the stack, because the
1994 // outgoing stack slots may alias the incoming argument stack slots, and
1995 // the alias isn't otherwise explicit. This is slightly more conservative
1996 // than necessary, because it means that each store effectively depends
1997 // on every argument instead of just those arguments it would clobber.
1998 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
1999
Dan Gohman475871a2008-07-27 21:46:04 +00002000 SmallVector<SDValue, 8> MemOpChains2;
2001 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002002 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002003 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002004 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002005 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002006 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2007 CCValAssign &VA = ArgLocs[i];
2008 if (VA.isRegLoc())
2009 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002010 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00002011 SDValue Arg = Outs[i].Val;
2012 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002013 // Create frame index.
2014 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002015 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002016 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002017 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002018
Duncan Sands276dcbd2008-03-21 09:14:45 +00002019 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002020 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002021 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002022 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002023 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002024 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002025 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002026
Dan Gohman98ca4f22009-08-05 01:29:28 +00002027 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2028 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002029 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002030 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002031 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002032 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002033 DAG.getStore(ArgChain, dl, Arg, FIN,
David Greene67c9d422010-02-15 16:53:33 +00002034 PseudoSourceValue::getFixedStack(FI), 0,
2035 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002036 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002037 }
2038 }
2039
2040 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002041 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002042 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002043
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002044 // Copy arguments to their registers.
2045 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002046 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002047 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002048 InFlag = Chain.getValue(1);
2049 }
Dan Gohman475871a2008-07-27 21:46:04 +00002050 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002051
Gordon Henriksen86737662008-01-05 16:56:59 +00002052 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002053 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002054 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002055 }
2056
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002057 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2058 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2059 // In the 64-bit large code model, we have to make all calls
2060 // through a register, since the call instruction's 32-bit
2061 // pc-relative offset may not be large enough to hold the whole
2062 // address.
2063 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002064 // If the callee is a GlobalAddress node (quite common, every direct call
2065 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2066 // it.
2067
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002068 // We should use extra load for direct calls to dllimported functions in
2069 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002070 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002071 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002072 unsigned char OpFlags = 0;
Eric Christopherfd179292009-08-27 18:07:15 +00002073
Chris Lattner48a7d022009-07-09 05:02:21 +00002074 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2075 // external symbols most go through the PLT in PIC mode. If the symbol
2076 // has hidden or protected visibility, or if it is static or local, then
2077 // we don't need to use the PLT - we can directly call it.
2078 if (Subtarget->isTargetELF() &&
2079 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002080 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002081 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002082 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002083 (GV->isDeclaration() || GV->isWeakForLinker()) &&
2084 Subtarget->getDarwinVers() < 9) {
2085 // PC-relative references to external symbols should go through $stub,
2086 // unless we're building with the leopard linker or later, which
2087 // automatically synthesizes these stubs.
2088 OpFlags = X86II::MO_DARWIN_STUB;
2089 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002090
Chris Lattner74e726e2009-07-09 05:27:35 +00002091 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002092 G->getOffset(), OpFlags);
2093 }
Bill Wendling056292f2008-09-16 21:48:12 +00002094 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002095 unsigned char OpFlags = 0;
2096
2097 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to external
2098 // symbols should go through the PLT.
2099 if (Subtarget->isTargetELF() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002100 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002101 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002102 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002103 Subtarget->getDarwinVers() < 9) {
2104 // PC-relative references to external symbols should go through $stub,
2105 // unless we're building with the leopard linker or later, which
2106 // automatically synthesizes these stubs.
2107 OpFlags = X86II::MO_DARWIN_STUB;
2108 }
Eric Christopherfd179292009-08-27 18:07:15 +00002109
Chris Lattner48a7d022009-07-09 05:02:21 +00002110 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2111 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002112 }
2113
Chris Lattnerd96d0722007-02-25 06:40:16 +00002114 // Returns a chain & a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +00002115 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00002116 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002117
Evan Chengf22f9b32010-02-06 03:28:46 +00002118 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002119 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2120 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002121 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002122 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002123
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002124 Ops.push_back(Chain);
2125 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002126
Dan Gohman98ca4f22009-08-05 01:29:28 +00002127 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002128 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002129
Gordon Henriksen86737662008-01-05 16:56:59 +00002130 // Add argument registers to the end of the list so that they are known live
2131 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002132 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2133 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2134 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002135
Evan Cheng586ccac2008-03-18 23:36:35 +00002136 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002137 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002138 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2139
2140 // Add an implicit use of AL for x86 vararg functions.
2141 if (Is64Bit && isVarArg)
Owen Anderson825b72b2009-08-11 20:47:22 +00002142 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002143
Gabor Greifba36cb52008-08-28 21:40:38 +00002144 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002145 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002146
Dan Gohman98ca4f22009-08-05 01:29:28 +00002147 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002148 // We used to do:
2149 //// If this is the first return lowered for this function, add the regs
2150 //// to the liveout set for the function.
2151 // This isn't right, although it's probably harmless on x86; liveouts
2152 // should be computed from returns not tail calls. Consider a void
2153 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002154 return DAG.getNode(X86ISD::TC_RETURN, dl,
2155 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002156 }
2157
Dale Johannesenace16102009-02-03 19:33:06 +00002158 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002159 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002160
Chris Lattner2d297092006-05-23 18:50:38 +00002161 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002162 unsigned NumBytesForCalleeToPush;
Dan Gohman4d3d6e12010-05-27 18:43:40 +00002163 if (Subtarget->IsCalleePop(isVarArg, CallConv))
Gordon Henriksen86737662008-01-05 16:56:59 +00002164 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002165 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002166 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002167 // pops the hidden struct pointer, so we have to push it back.
2168 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002169 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002170 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002171 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002172
Gordon Henriksenae636f82008-01-03 16:47:34 +00002173 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002174 if (!IsSibcall) {
2175 Chain = DAG.getCALLSEQ_END(Chain,
2176 DAG.getIntPtrConstant(NumBytes, true),
2177 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2178 true),
2179 InFlag);
2180 InFlag = Chain.getValue(1);
2181 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002182
Chris Lattner3085e152007-02-25 08:59:22 +00002183 // Handle result values, copying them out of physregs into vregs that we
2184 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002185 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2186 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002187}
2188
Evan Cheng25ab6902006-09-08 06:48:29 +00002189
2190//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002191// Fast Calling Convention (tail call) implementation
2192//===----------------------------------------------------------------------===//
2193
2194// Like std call, callee cleans arguments, convention except that ECX is
2195// reserved for storing the tail called function address. Only 2 registers are
2196// free for argument passing (inreg). Tail call optimization is performed
2197// provided:
2198// * tailcallopt is enabled
2199// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002200// On X86_64 architecture with GOT-style position independent code only local
2201// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002202// To keep the stack aligned according to platform abi the function
2203// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2204// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002205// If a tail called function callee has more arguments than the caller the
2206// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002207// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002208// original REtADDR, but before the saved framepointer or the spilled registers
2209// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2210// stack layout:
2211// arg1
2212// arg2
2213// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002214// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002215// move area ]
2216// (possible EBP)
2217// ESI
2218// EDI
2219// local1 ..
2220
2221/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2222/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002223unsigned
2224X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2225 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002226 MachineFunction &MF = DAG.getMachineFunction();
2227 const TargetMachine &TM = MF.getTarget();
2228 const TargetFrameInfo &TFI = *TM.getFrameInfo();
2229 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002230 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002231 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002232 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002233 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2234 // Number smaller than 12 so just add the difference.
2235 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2236 } else {
2237 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002238 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002239 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002240 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002241 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002242}
2243
Evan Cheng5f941932010-02-05 02:21:12 +00002244/// MatchingStackOffset - Return true if the given stack call argument is
2245/// already available in the same position (relatively) of the caller's
2246/// incoming argument stack.
2247static
2248bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2249 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2250 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002251 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2252 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002253 if (Arg.getOpcode() == ISD::CopyFromReg) {
2254 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
2255 if (!VR || TargetRegisterInfo::isPhysicalRegister(VR))
2256 return false;
2257 MachineInstr *Def = MRI->getVRegDef(VR);
2258 if (!Def)
2259 return false;
2260 if (!Flags.isByVal()) {
2261 if (!TII->isLoadFromStackSlot(Def, FI))
2262 return false;
2263 } else {
2264 unsigned Opcode = Def->getOpcode();
2265 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2266 Def->getOperand(1).isFI()) {
2267 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002268 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002269 } else
2270 return false;
2271 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002272 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2273 if (Flags.isByVal())
2274 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002275 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002276 // define @foo(%struct.X* %A) {
2277 // tail call @bar(%struct.X* byval %A)
2278 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002279 return false;
2280 SDValue Ptr = Ld->getBasePtr();
2281 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2282 if (!FINode)
2283 return false;
2284 FI = FINode->getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002285 } else
2286 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002287
Evan Cheng4cae1332010-03-05 08:38:04 +00002288 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002289 if (!MFI->isFixedObjectIndex(FI))
2290 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002291 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002292}
2293
Dan Gohman98ca4f22009-08-05 01:29:28 +00002294/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2295/// for tail call optimization. Targets which want to do tail call
2296/// optimization should implement this function.
2297bool
2298X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002299 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002300 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002301 bool isCalleeStructRet,
2302 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002303 const SmallVectorImpl<ISD::OutputArg> &Outs,
2304 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002305 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002306 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002307 CalleeCC != CallingConv::C)
2308 return false;
2309
Evan Cheng7096ae42010-01-29 06:45:59 +00002310 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002311 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002312 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002313 CallingConv::ID CallerCC = CallerF->getCallingConv();
2314 bool CCMatch = CallerCC == CalleeCC;
2315
Dan Gohman1797ed52010-02-08 20:27:50 +00002316 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002317 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002318 return true;
2319 return false;
2320 }
2321
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002322 // Look for obvious safe cases to perform tail call optimization that do not
2323 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002324
Evan Cheng2c12cb42010-03-26 16:26:03 +00002325 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2326 // emit a special epilogue.
2327 if (RegInfo->needsStackRealignment(MF))
2328 return false;
2329
Evan Cheng3c262ee2010-03-26 02:13:13 +00002330 // Do not sibcall optimize vararg calls unless the call site is not passing any
2331 // arguments.
2332 if (isVarArg && !Outs.empty())
Evan Cheng843bd692010-01-31 06:44:49 +00002333 return false;
2334
Evan Chenga375d472010-03-15 18:54:48 +00002335 // Also avoid sibcall optimization if either caller or callee uses struct
2336 // return semantics.
2337 if (isCalleeStructRet || isCallerStructRet)
2338 return false;
2339
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002340 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2341 // Therefore if it's not used by the call it is not safe to optimize this into
2342 // a sibcall.
2343 bool Unused = false;
2344 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2345 if (!Ins[i].Used) {
2346 Unused = true;
2347 break;
2348 }
2349 }
2350 if (Unused) {
2351 SmallVector<CCValAssign, 16> RVLocs;
2352 CCState CCInfo(CalleeCC, false, getTargetMachine(),
2353 RVLocs, *DAG.getContext());
2354 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002355 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002356 CCValAssign &VA = RVLocs[i];
2357 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2358 return false;
2359 }
2360 }
2361
Evan Cheng13617962010-04-30 01:12:32 +00002362 // If the calling conventions do not match, then we'd better make sure the
2363 // results are returned in the same way as what the caller expects.
2364 if (!CCMatch) {
2365 SmallVector<CCValAssign, 16> RVLocs1;
2366 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
2367 RVLocs1, *DAG.getContext());
2368 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2369
2370 SmallVector<CCValAssign, 16> RVLocs2;
2371 CCState CCInfo2(CallerCC, false, getTargetMachine(),
2372 RVLocs2, *DAG.getContext());
2373 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2374
2375 if (RVLocs1.size() != RVLocs2.size())
2376 return false;
2377 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2378 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2379 return false;
2380 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2381 return false;
2382 if (RVLocs1[i].isRegLoc()) {
2383 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2384 return false;
2385 } else {
2386 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2387 return false;
2388 }
2389 }
2390 }
2391
Evan Chenga6bff982010-01-30 01:22:00 +00002392 // If the callee takes no arguments then go on to check the results of the
2393 // call.
2394 if (!Outs.empty()) {
2395 // Check if stack adjustment is needed. For now, do not do this if any
2396 // argument is passed on the stack.
2397 SmallVector<CCValAssign, 16> ArgLocs;
2398 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
2399 ArgLocs, *DAG.getContext());
2400 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CalleeCC));
Evan Chengb2c92902010-02-02 02:22:50 +00002401 if (CCInfo.getNextStackOffset()) {
2402 MachineFunction &MF = DAG.getMachineFunction();
2403 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2404 return false;
2405 if (Subtarget->isTargetWin64())
2406 // Win64 ABI has additional complications.
2407 return false;
2408
2409 // Check if the arguments are already laid out in the right way as
2410 // the caller's fixed stack objects.
2411 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002412 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2413 const X86InstrInfo *TII =
2414 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002415 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2416 CCValAssign &VA = ArgLocs[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002417 SDValue Arg = Outs[i].Val;
2418 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002419 if (VA.getLocInfo() == CCValAssign::Indirect)
2420 return false;
2421 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002422 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2423 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002424 return false;
2425 }
2426 }
2427 }
Evan Cheng9c044672010-05-29 01:35:22 +00002428
2429 // If the tailcall address may be in a register, then make sure it's
2430 // possible to register allocate for it. In 32-bit, the call address can
2431 // only target EAX, EDX, or ECX since the tail call must be scheduled after
2432 // callee-saved registers are restored. In 64-bit, it's RAX, RCX, RDX, RSI,
2433 // RDI, R8, R9, R11.
2434 if (!isa<GlobalAddressSDNode>(Callee) &&
2435 !isa<ExternalSymbolSDNode>(Callee)) {
2436 unsigned Limit = Subtarget->is64Bit() ? 8 : 3;
2437 unsigned NumInRegs = 0;
2438 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2439 CCValAssign &VA = ArgLocs[i];
2440 if (VA.isRegLoc()) {
2441 if (++NumInRegs == Limit)
2442 return false;
2443 }
2444 }
2445 }
Evan Chenga6bff982010-01-30 01:22:00 +00002446 }
Evan Chengb1712452010-01-27 06:25:16 +00002447
Evan Cheng86809cc2010-02-03 03:28:02 +00002448 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002449}
2450
Dan Gohman3df24e62008-09-03 23:12:08 +00002451FastISel *
Chris Lattnered3a8062010-04-05 06:05:26 +00002452X86TargetLowering::createFastISel(MachineFunction &mf,
Evan Chengddc419c2010-01-26 19:04:47 +00002453 DenseMap<const Value *, unsigned> &vm,
2454 DenseMap<const BasicBlock*, MachineBasicBlock*> &bm,
Dan Gohmanf81eca02010-04-22 20:46:50 +00002455 DenseMap<const AllocaInst *, int> &am,
2456 std::vector<std::pair<MachineInstr*, unsigned> > &pn
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002457#ifndef NDEBUG
Dan Gohman25208642010-04-14 19:53:31 +00002458 , SmallSet<const Instruction *, 8> &cil
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002459#endif
Dan Gohmand858e902010-04-17 15:26:15 +00002460 ) const {
Dan Gohmanf81eca02010-04-22 20:46:50 +00002461 return X86::createFastISel(mf, vm, bm, am, pn
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002462#ifndef NDEBUG
2463 , cil
2464#endif
2465 );
Dan Gohmand9f3c482008-08-19 21:32:53 +00002466}
2467
2468
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002469//===----------------------------------------------------------------------===//
2470// Other Lowering Hooks
2471//===----------------------------------------------------------------------===//
2472
2473
Dan Gohmand858e902010-04-17 15:26:15 +00002474SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002475 MachineFunction &MF = DAG.getMachineFunction();
2476 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2477 int ReturnAddrIndex = FuncInfo->getRAIndex();
2478
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002479 if (ReturnAddrIndex == 0) {
2480 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002481 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002482 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002483 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002484 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002485 }
2486
Evan Cheng25ab6902006-09-08 06:48:29 +00002487 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002488}
2489
2490
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002491bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2492 bool hasSymbolicDisplacement) {
2493 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002494 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002495 return false;
2496
2497 // If we don't have a symbolic displacement - we don't have any extra
2498 // restrictions.
2499 if (!hasSymbolicDisplacement)
2500 return true;
2501
2502 // FIXME: Some tweaks might be needed for medium code model.
2503 if (M != CodeModel::Small && M != CodeModel::Kernel)
2504 return false;
2505
2506 // For small code model we assume that latest object is 16MB before end of 31
2507 // bits boundary. We may also accept pretty large negative constants knowing
2508 // that all objects are in the positive half of address space.
2509 if (M == CodeModel::Small && Offset < 16*1024*1024)
2510 return true;
2511
2512 // For kernel code model we know that all object resist in the negative half
2513 // of 32bits address space. We may not accept negative offsets, since they may
2514 // be just off and we may accept pretty large positive ones.
2515 if (M == CodeModel::Kernel && Offset > 0)
2516 return true;
2517
2518 return false;
2519}
2520
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002521/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2522/// specific condition code, returning the condition code and the LHS/RHS of the
2523/// comparison to make.
2524static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2525 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002526 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002527 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2528 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2529 // X > -1 -> X == 0, jump !sign.
2530 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002531 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002532 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2533 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002534 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002535 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002536 // X < 1 -> X <= 0
2537 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002538 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002539 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002540 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002541
Evan Chengd9558e02006-01-06 00:43:03 +00002542 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002543 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002544 case ISD::SETEQ: return X86::COND_E;
2545 case ISD::SETGT: return X86::COND_G;
2546 case ISD::SETGE: return X86::COND_GE;
2547 case ISD::SETLT: return X86::COND_L;
2548 case ISD::SETLE: return X86::COND_LE;
2549 case ISD::SETNE: return X86::COND_NE;
2550 case ISD::SETULT: return X86::COND_B;
2551 case ISD::SETUGT: return X86::COND_A;
2552 case ISD::SETULE: return X86::COND_BE;
2553 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002554 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002555 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002556
Chris Lattner4c78e022008-12-23 23:42:27 +00002557 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002558
Chris Lattner4c78e022008-12-23 23:42:27 +00002559 // If LHS is a foldable load, but RHS is not, flip the condition.
2560 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2561 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2562 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2563 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002564 }
2565
Chris Lattner4c78e022008-12-23 23:42:27 +00002566 switch (SetCCOpcode) {
2567 default: break;
2568 case ISD::SETOLT:
2569 case ISD::SETOLE:
2570 case ISD::SETUGT:
2571 case ISD::SETUGE:
2572 std::swap(LHS, RHS);
2573 break;
2574 }
2575
2576 // On a floating point condition, the flags are set as follows:
2577 // ZF PF CF op
2578 // 0 | 0 | 0 | X > Y
2579 // 0 | 0 | 1 | X < Y
2580 // 1 | 0 | 0 | X == Y
2581 // 1 | 1 | 1 | unordered
2582 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002583 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002584 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002585 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002586 case ISD::SETOLT: // flipped
2587 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002588 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002589 case ISD::SETOLE: // flipped
2590 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002591 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002592 case ISD::SETUGT: // flipped
2593 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002594 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002595 case ISD::SETUGE: // flipped
2596 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002597 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002598 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002599 case ISD::SETNE: return X86::COND_NE;
2600 case ISD::SETUO: return X86::COND_P;
2601 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002602 case ISD::SETOEQ:
2603 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002604 }
Evan Chengd9558e02006-01-06 00:43:03 +00002605}
2606
Evan Cheng4a460802006-01-11 00:33:36 +00002607/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2608/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002609/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002610static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002611 switch (X86CC) {
2612 default:
2613 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002614 case X86::COND_B:
2615 case X86::COND_BE:
2616 case X86::COND_E:
2617 case X86::COND_P:
2618 case X86::COND_A:
2619 case X86::COND_AE:
2620 case X86::COND_NE:
2621 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002622 return true;
2623 }
2624}
2625
Evan Chengeb2f9692009-10-27 19:56:55 +00002626/// isFPImmLegal - Returns true if the target can instruction select the
2627/// specified FP immediate natively. If false, the legalizer will
2628/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002629bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00002630 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
2631 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
2632 return true;
2633 }
2634 return false;
2635}
2636
Nate Begeman9008ca62009-04-27 18:41:29 +00002637/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2638/// the specified range (L, H].
2639static bool isUndefOrInRange(int Val, int Low, int Hi) {
2640 return (Val < 0) || (Val >= Low && Val < Hi);
2641}
2642
2643/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2644/// specified value.
2645static bool isUndefOrEqual(int Val, int CmpVal) {
2646 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002647 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002648 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002649}
2650
Nate Begeman9008ca62009-04-27 18:41:29 +00002651/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2652/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2653/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00002654static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002655 if (VT == MVT::v4f32 || VT == MVT::v4i32 || VT == MVT::v4i16)
Nate Begeman9008ca62009-04-27 18:41:29 +00002656 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002657 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00002658 return (Mask[0] < 2 && Mask[1] < 2);
2659 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002660}
2661
Nate Begeman9008ca62009-04-27 18:41:29 +00002662bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002663 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002664 N->getMask(M);
2665 return ::isPSHUFDMask(M, N->getValueType(0));
2666}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002667
Nate Begeman9008ca62009-04-27 18:41:29 +00002668/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2669/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00002670static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002671 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002672 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002673
Nate Begeman9008ca62009-04-27 18:41:29 +00002674 // Lower quadword copied in order or undef.
2675 for (int i = 0; i != 4; ++i)
2676 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002677 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002678
Evan Cheng506d3df2006-03-29 23:07:14 +00002679 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002680 for (int i = 4; i != 8; ++i)
2681 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002682 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002683
Evan Cheng506d3df2006-03-29 23:07:14 +00002684 return true;
2685}
2686
Nate Begeman9008ca62009-04-27 18:41:29 +00002687bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002688 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002689 N->getMask(M);
2690 return ::isPSHUFHWMask(M, N->getValueType(0));
2691}
Evan Cheng506d3df2006-03-29 23:07:14 +00002692
Nate Begeman9008ca62009-04-27 18:41:29 +00002693/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2694/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00002695static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002696 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002697 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002698
Rafael Espindola15684b22009-04-24 12:40:33 +00002699 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00002700 for (int i = 4; i != 8; ++i)
2701 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00002702 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002703
Rafael Espindola15684b22009-04-24 12:40:33 +00002704 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002705 for (int i = 0; i != 4; ++i)
2706 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00002707 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002708
Rafael Espindola15684b22009-04-24 12:40:33 +00002709 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002710}
2711
Nate Begeman9008ca62009-04-27 18:41:29 +00002712bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002713 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002714 N->getMask(M);
2715 return ::isPSHUFLWMask(M, N->getValueType(0));
2716}
2717
Nate Begemana09008b2009-10-19 02:17:23 +00002718/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
2719/// is suitable for input to PALIGNR.
2720static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
2721 bool hasSSSE3) {
2722 int i, e = VT.getVectorNumElements();
2723
2724 // Do not handle v2i64 / v2f64 shuffles with palignr.
2725 if (e < 4 || !hasSSSE3)
2726 return false;
2727
2728 for (i = 0; i != e; ++i)
2729 if (Mask[i] >= 0)
2730 break;
2731
2732 // All undef, not a palignr.
2733 if (i == e)
2734 return false;
2735
2736 // Determine if it's ok to perform a palignr with only the LHS, since we
2737 // don't have access to the actual shuffle elements to see if RHS is undef.
2738 bool Unary = Mask[i] < (int)e;
2739 bool NeedsUnary = false;
2740
2741 int s = Mask[i] - i;
2742
2743 // Check the rest of the elements to see if they are consecutive.
2744 for (++i; i != e; ++i) {
2745 int m = Mask[i];
2746 if (m < 0)
2747 continue;
2748
2749 Unary = Unary && (m < (int)e);
2750 NeedsUnary = NeedsUnary || (m < s);
2751
2752 if (NeedsUnary && !Unary)
2753 return false;
2754 if (Unary && m != ((s+i) & (e-1)))
2755 return false;
2756 if (!Unary && m != (s+i))
2757 return false;
2758 }
2759 return true;
2760}
2761
2762bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
2763 SmallVector<int, 8> M;
2764 N->getMask(M);
2765 return ::isPALIGNRMask(M, N->getValueType(0), true);
2766}
2767
Evan Cheng14aed5e2006-03-24 01:18:28 +00002768/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2769/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00002770static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002771 int NumElems = VT.getVectorNumElements();
2772 if (NumElems != 2 && NumElems != 4)
2773 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002774
Nate Begeman9008ca62009-04-27 18:41:29 +00002775 int Half = NumElems / 2;
2776 for (int i = 0; i < Half; ++i)
2777 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002778 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002779 for (int i = Half; i < NumElems; ++i)
2780 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002781 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002782
Evan Cheng14aed5e2006-03-24 01:18:28 +00002783 return true;
2784}
2785
Nate Begeman9008ca62009-04-27 18:41:29 +00002786bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
2787 SmallVector<int, 8> M;
2788 N->getMask(M);
2789 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002790}
2791
Evan Cheng213d2cf2007-05-17 18:45:50 +00002792/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002793/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2794/// half elements to come from vector 1 (which would equal the dest.) and
2795/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00002796static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002797 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002798
2799 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00002800 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002801
Nate Begeman9008ca62009-04-27 18:41:29 +00002802 int Half = NumElems / 2;
2803 for (int i = 0; i < Half; ++i)
2804 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002805 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002806 for (int i = Half; i < NumElems; ++i)
2807 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002808 return false;
2809 return true;
2810}
2811
Nate Begeman9008ca62009-04-27 18:41:29 +00002812static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
2813 SmallVector<int, 8> M;
2814 N->getMask(M);
2815 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002816}
2817
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002818/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2819/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002820bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
2821 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002822 return false;
2823
Evan Cheng2064a2b2006-03-28 06:50:32 +00002824 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00002825 return isUndefOrEqual(N->getMaskElt(0), 6) &&
2826 isUndefOrEqual(N->getMaskElt(1), 7) &&
2827 isUndefOrEqual(N->getMaskElt(2), 2) &&
2828 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002829}
2830
Nate Begeman0b10b912009-11-07 23:17:15 +00002831/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2832/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2833/// <2, 3, 2, 3>
2834bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
2835 unsigned NumElems = N->getValueType(0).getVectorNumElements();
2836
2837 if (NumElems != 4)
2838 return false;
2839
2840 return isUndefOrEqual(N->getMaskElt(0), 2) &&
2841 isUndefOrEqual(N->getMaskElt(1), 3) &&
2842 isUndefOrEqual(N->getMaskElt(2), 2) &&
2843 isUndefOrEqual(N->getMaskElt(3), 3);
2844}
2845
Evan Cheng5ced1d82006-04-06 23:23:56 +00002846/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2847/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00002848bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
2849 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002850
Evan Cheng5ced1d82006-04-06 23:23:56 +00002851 if (NumElems != 2 && NumElems != 4)
2852 return false;
2853
Evan Chengc5cdff22006-04-07 21:53:05 +00002854 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002855 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002856 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002857
Evan Chengc5cdff22006-04-07 21:53:05 +00002858 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002859 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002860 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002861
2862 return true;
2863}
2864
Nate Begeman0b10b912009-11-07 23:17:15 +00002865/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
2866/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
2867bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002868 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002869
Evan Cheng5ced1d82006-04-06 23:23:56 +00002870 if (NumElems != 2 && NumElems != 4)
2871 return false;
2872
Evan Chengc5cdff22006-04-07 21:53:05 +00002873 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002874 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002875 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002876
Nate Begeman9008ca62009-04-27 18:41:29 +00002877 for (unsigned i = 0; i < NumElems/2; ++i)
2878 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002879 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002880
2881 return true;
2882}
2883
Evan Cheng0038e592006-03-28 00:39:58 +00002884/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2885/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00002886static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002887 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002888 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002889 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00002890 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002891
Nate Begeman9008ca62009-04-27 18:41:29 +00002892 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2893 int BitI = Mask[i];
2894 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002895 if (!isUndefOrEqual(BitI, j))
2896 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002897 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002898 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002899 return false;
2900 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002901 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002902 return false;
2903 }
Evan Cheng0038e592006-03-28 00:39:58 +00002904 }
Evan Cheng0038e592006-03-28 00:39:58 +00002905 return true;
2906}
2907
Nate Begeman9008ca62009-04-27 18:41:29 +00002908bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2909 SmallVector<int, 8> M;
2910 N->getMask(M);
2911 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002912}
2913
Evan Cheng4fcb9222006-03-28 02:43:26 +00002914/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2915/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00002916static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002917 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002918 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002919 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00002920 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002921
Nate Begeman9008ca62009-04-27 18:41:29 +00002922 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2923 int BitI = Mask[i];
2924 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00002925 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00002926 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002927 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002928 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002929 return false;
2930 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002931 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002932 return false;
2933 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002934 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002935 return true;
2936}
2937
Nate Begeman9008ca62009-04-27 18:41:29 +00002938bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2939 SmallVector<int, 8> M;
2940 N->getMask(M);
2941 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002942}
2943
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002944/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2945/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2946/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00002947static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002948 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002949 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002950 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002951
Nate Begeman9008ca62009-04-27 18:41:29 +00002952 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
2953 int BitI = Mask[i];
2954 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002955 if (!isUndefOrEqual(BitI, j))
2956 return false;
2957 if (!isUndefOrEqual(BitI1, j))
2958 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002959 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002960 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002961}
2962
Nate Begeman9008ca62009-04-27 18:41:29 +00002963bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
2964 SmallVector<int, 8> M;
2965 N->getMask(M);
2966 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
2967}
2968
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002969/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2970/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2971/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00002972static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002973 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002974 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2975 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002976
Nate Begeman9008ca62009-04-27 18:41:29 +00002977 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2978 int BitI = Mask[i];
2979 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002980 if (!isUndefOrEqual(BitI, j))
2981 return false;
2982 if (!isUndefOrEqual(BitI1, j))
2983 return false;
2984 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002985 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002986}
2987
Nate Begeman9008ca62009-04-27 18:41:29 +00002988bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
2989 SmallVector<int, 8> M;
2990 N->getMask(M);
2991 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
2992}
2993
Evan Cheng017dcc62006-04-21 01:05:10 +00002994/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2995/// specifies a shuffle of elements that is suitable for input to MOVSS,
2996/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00002997static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00002998 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002999 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003000
3001 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003002
Nate Begeman9008ca62009-04-27 18:41:29 +00003003 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003004 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003005
Nate Begeman9008ca62009-04-27 18:41:29 +00003006 for (int i = 1; i < NumElts; ++i)
3007 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003008 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003009
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003010 return true;
3011}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003012
Nate Begeman9008ca62009-04-27 18:41:29 +00003013bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3014 SmallVector<int, 8> M;
3015 N->getMask(M);
3016 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003017}
3018
Evan Cheng017dcc62006-04-21 01:05:10 +00003019/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3020/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003021/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003022static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003023 bool V2IsSplat = false, bool V2IsUndef = false) {
3024 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003025 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003026 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003027
Nate Begeman9008ca62009-04-27 18:41:29 +00003028 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003029 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003030
Nate Begeman9008ca62009-04-27 18:41:29 +00003031 for (int i = 1; i < NumOps; ++i)
3032 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3033 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3034 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003035 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003036
Evan Cheng39623da2006-04-20 08:58:49 +00003037 return true;
3038}
3039
Nate Begeman9008ca62009-04-27 18:41:29 +00003040static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003041 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003042 SmallVector<int, 8> M;
3043 N->getMask(M);
3044 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003045}
3046
Evan Chengd9539472006-04-14 21:59:03 +00003047/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3048/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003049bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
3050 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003051 return false;
3052
3053 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00003054 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003055 int Elt = N->getMaskElt(i);
3056 if (Elt >= 0 && Elt != 1)
3057 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00003058 }
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 != 3)
3064 return false;
3065 if (Elt == 3)
3066 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003067 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003068 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00003069 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003070 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003071}
3072
3073/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3074/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003075bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
3076 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003077 return false;
3078
3079 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00003080 for (unsigned i = 0; i < 2; ++i)
3081 if (N->getMaskElt(i) > 0)
3082 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003083
3084 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003085 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003086 int Elt = N->getMaskElt(i);
3087 if (Elt >= 0 && Elt != 2)
3088 return false;
3089 if (Elt == 2)
3090 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003091 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003092 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003093 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003094}
3095
Evan Cheng0b457f02008-09-25 20:50:48 +00003096/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3097/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003098bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3099 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003100
Nate Begeman9008ca62009-04-27 18:41:29 +00003101 for (int i = 0; i < e; ++i)
3102 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003103 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003104 for (int i = 0; i < e; ++i)
3105 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003106 return false;
3107 return true;
3108}
3109
Evan Cheng63d33002006-03-22 08:01:21 +00003110/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003111/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003112unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003113 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3114 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3115
Evan Chengb9df0ca2006-03-22 02:53:00 +00003116 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3117 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003118 for (int i = 0; i < NumOperands; ++i) {
3119 int Val = SVOp->getMaskElt(NumOperands-i-1);
3120 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003121 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003122 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003123 if (i != NumOperands - 1)
3124 Mask <<= Shift;
3125 }
Evan Cheng63d33002006-03-22 08:01:21 +00003126 return Mask;
3127}
3128
Evan Cheng506d3df2006-03-29 23:07:14 +00003129/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003130/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003131unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003132 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003133 unsigned Mask = 0;
3134 // 8 nodes, but we only care about the last 4.
3135 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003136 int Val = SVOp->getMaskElt(i);
3137 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003138 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003139 if (i != 4)
3140 Mask <<= 2;
3141 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003142 return Mask;
3143}
3144
3145/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003146/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003147unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003148 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003149 unsigned Mask = 0;
3150 // 8 nodes, but we only care about the first 4.
3151 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003152 int Val = SVOp->getMaskElt(i);
3153 if (Val >= 0)
3154 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003155 if (i != 0)
3156 Mask <<= 2;
3157 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003158 return Mask;
3159}
3160
Nate Begemana09008b2009-10-19 02:17:23 +00003161/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3162/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3163unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3164 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3165 EVT VVT = N->getValueType(0);
3166 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3167 int Val = 0;
3168
3169 unsigned i, e;
3170 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3171 Val = SVOp->getMaskElt(i);
3172 if (Val >= 0)
3173 break;
3174 }
3175 return (Val - i) * EltSize;
3176}
3177
Evan Cheng37b73872009-07-30 08:33:02 +00003178/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3179/// constant +0.0.
3180bool X86::isZeroNode(SDValue Elt) {
3181 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00003182 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00003183 (isa<ConstantFPSDNode>(Elt) &&
3184 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3185}
3186
Nate Begeman9008ca62009-04-27 18:41:29 +00003187/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3188/// their permute mask.
3189static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3190 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003191 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003192 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003193 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003194
Nate Begeman5a5ca152009-04-29 05:20:52 +00003195 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003196 int idx = SVOp->getMaskElt(i);
3197 if (idx < 0)
3198 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003199 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003200 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003201 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003202 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003203 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003204 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3205 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003206}
3207
Evan Cheng779ccea2007-12-07 21:30:01 +00003208/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3209/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003210static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003211 unsigned NumElems = VT.getVectorNumElements();
3212 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003213 int idx = Mask[i];
3214 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003215 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003216 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003217 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003218 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003219 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003220 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003221}
3222
Evan Cheng533a0aa2006-04-19 20:35:22 +00003223/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3224/// match movhlps. The lower half elements should come from upper half of
3225/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003226/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003227static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3228 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003229 return false;
3230 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003231 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003232 return false;
3233 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003234 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003235 return false;
3236 return true;
3237}
3238
Evan Cheng5ced1d82006-04-06 23:23:56 +00003239/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003240/// is promoted to a vector. It also returns the LoadSDNode by reference if
3241/// required.
3242static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003243 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3244 return false;
3245 N = N->getOperand(0).getNode();
3246 if (!ISD::isNON_EXTLoad(N))
3247 return false;
3248 if (LD)
3249 *LD = cast<LoadSDNode>(N);
3250 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003251}
3252
Evan Cheng533a0aa2006-04-19 20:35:22 +00003253/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3254/// match movlp{s|d}. The lower half elements should come from lower half of
3255/// V1 (and in order), and the upper half elements should come from the upper
3256/// half of V2 (and in order). And since V1 will become the source of the
3257/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003258static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3259 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003260 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003261 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003262 // Is V2 is a vector load, don't do this transformation. We will try to use
3263 // load folding shufps op.
3264 if (ISD::isNON_EXTLoad(V2))
3265 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003266
Nate Begeman5a5ca152009-04-29 05:20:52 +00003267 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003268
Evan Cheng533a0aa2006-04-19 20:35:22 +00003269 if (NumElems != 2 && NumElems != 4)
3270 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003271 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003272 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003273 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003274 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003275 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003276 return false;
3277 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003278}
3279
Evan Cheng39623da2006-04-20 08:58:49 +00003280/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3281/// all the same.
3282static bool isSplatVector(SDNode *N) {
3283 if (N->getOpcode() != ISD::BUILD_VECTOR)
3284 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003285
Dan Gohman475871a2008-07-27 21:46:04 +00003286 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003287 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3288 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003289 return false;
3290 return true;
3291}
3292
Evan Cheng213d2cf2007-05-17 18:45:50 +00003293/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003294/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003295/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003296static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003297 SDValue V1 = N->getOperand(0);
3298 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003299 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3300 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003301 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003302 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003303 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003304 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3305 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003306 if (Opc != ISD::BUILD_VECTOR ||
3307 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003308 return false;
3309 } else if (Idx >= 0) {
3310 unsigned Opc = V1.getOpcode();
3311 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3312 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003313 if (Opc != ISD::BUILD_VECTOR ||
3314 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003315 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003316 }
3317 }
3318 return true;
3319}
3320
3321/// getZeroVector - Returns a vector of specified type with all zero elements.
3322///
Owen Andersone50ed302009-08-10 22:56:29 +00003323static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003324 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003325 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003326
Chris Lattner8a594482007-11-25 00:24:49 +00003327 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3328 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003329 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003330 if (VT.getSizeInBits() == 64) { // MMX
Owen Anderson825b72b2009-08-11 20:47:22 +00003331 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3332 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003333 } else if (HasSSE2) { // SSE2
Owen Anderson825b72b2009-08-11 20:47:22 +00003334 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3335 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003336 } else { // SSE1
Owen Anderson825b72b2009-08-11 20:47:22 +00003337 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3338 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003339 }
Dale Johannesenace16102009-02-03 19:33:06 +00003340 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003341}
3342
Chris Lattner8a594482007-11-25 00:24:49 +00003343/// getOnesVector - Returns a vector of specified type with all bits set.
3344///
Owen Andersone50ed302009-08-10 22:56:29 +00003345static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003346 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003347
Chris Lattner8a594482007-11-25 00:24:49 +00003348 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3349 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003350 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003351 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003352 if (VT.getSizeInBits() == 64) // MMX
Owen Anderson825b72b2009-08-11 20:47:22 +00003353 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Chris Lattner8a594482007-11-25 00:24:49 +00003354 else // SSE
Owen Anderson825b72b2009-08-11 20:47:22 +00003355 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Dale Johannesenace16102009-02-03 19:33:06 +00003356 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003357}
3358
3359
Evan Cheng39623da2006-04-20 08:58:49 +00003360/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3361/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003362static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003363 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003364 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003365
Evan Cheng39623da2006-04-20 08:58:49 +00003366 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003367 SmallVector<int, 8> MaskVec;
3368 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003369
Nate Begeman5a5ca152009-04-29 05:20:52 +00003370 for (unsigned i = 0; i != NumElems; ++i) {
3371 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003372 MaskVec[i] = NumElems;
3373 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003374 }
Evan Cheng39623da2006-04-20 08:58:49 +00003375 }
Evan Cheng39623da2006-04-20 08:58:49 +00003376 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003377 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3378 SVOp->getOperand(1), &MaskVec[0]);
3379 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003380}
3381
Evan Cheng017dcc62006-04-21 01:05:10 +00003382/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3383/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003384static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003385 SDValue V2) {
3386 unsigned NumElems = VT.getVectorNumElements();
3387 SmallVector<int, 8> Mask;
3388 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003389 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003390 Mask.push_back(i);
3391 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003392}
3393
Nate Begeman9008ca62009-04-27 18:41:29 +00003394/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003395static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003396 SDValue V2) {
3397 unsigned NumElems = VT.getVectorNumElements();
3398 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003399 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003400 Mask.push_back(i);
3401 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003402 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003403 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003404}
3405
Nate Begeman9008ca62009-04-27 18:41:29 +00003406/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003407static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003408 SDValue V2) {
3409 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003410 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003411 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003412 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003413 Mask.push_back(i + Half);
3414 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003415 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003416 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003417}
3418
Evan Cheng0c0f83f2008-04-05 00:30:36 +00003419/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Eric Christopherfd179292009-08-27 18:07:15 +00003420static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00003421 bool HasSSE2) {
3422 if (SV->getValueType(0).getVectorNumElements() <= 4)
3423 return SDValue(SV, 0);
Eric Christopherfd179292009-08-27 18:07:15 +00003424
Owen Anderson825b72b2009-08-11 20:47:22 +00003425 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003426 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003427 DebugLoc dl = SV->getDebugLoc();
3428 SDValue V1 = SV->getOperand(0);
3429 int NumElems = VT.getVectorNumElements();
3430 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003431
Nate Begeman9008ca62009-04-27 18:41:29 +00003432 // unpack elements to the correct location
3433 while (NumElems > 4) {
3434 if (EltNo < NumElems/2) {
3435 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3436 } else {
3437 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3438 EltNo -= NumElems/2;
3439 }
3440 NumElems >>= 1;
3441 }
Eric Christopherfd179292009-08-27 18:07:15 +00003442
Nate Begeman9008ca62009-04-27 18:41:29 +00003443 // Perform the splat.
3444 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Dale Johannesenace16102009-02-03 19:33:06 +00003445 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003446 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
3447 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003448}
3449
Evan Chengba05f722006-04-21 23:03:30 +00003450/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003451/// vector of zero or undef vector. This produces a shuffle where the low
3452/// element of V2 is swizzled into the zero/undef vector, landing at element
3453/// 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 +00003454static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003455 bool isZero, bool HasSSE2,
3456 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003457 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003458 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003459 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3460 unsigned NumElems = VT.getVectorNumElements();
3461 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003462 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003463 // If this is the insertion idx, put the low elt of V2 here.
3464 MaskVec.push_back(i == Idx ? NumElems : i);
3465 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003466}
3467
Evan Chengf26ffe92008-05-29 08:22:04 +00003468/// getNumOfConsecutiveZeros - Return the number of elements in a result of
3469/// a shuffle that is zero.
3470static
Nate Begeman9008ca62009-04-27 18:41:29 +00003471unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, int NumElems,
3472 bool Low, SelectionDAG &DAG) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003473 unsigned NumZeros = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003474 for (int i = 0; i < NumElems; ++i) {
Evan Chengab262272008-06-25 20:52:59 +00003475 unsigned Index = Low ? i : NumElems-i-1;
Nate Begeman9008ca62009-04-27 18:41:29 +00003476 int Idx = SVOp->getMaskElt(Index);
3477 if (Idx < 0) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003478 ++NumZeros;
3479 continue;
3480 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003481 SDValue Elt = DAG.getShuffleScalarElt(SVOp, Index);
Evan Cheng37b73872009-07-30 08:33:02 +00003482 if (Elt.getNode() && X86::isZeroNode(Elt))
Evan Chengf26ffe92008-05-29 08:22:04 +00003483 ++NumZeros;
3484 else
3485 break;
3486 }
3487 return NumZeros;
3488}
3489
3490/// isVectorShift - Returns true if the shuffle can be implemented as a
3491/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003492/// FIXME: split into pslldqi, psrldqi, palignr variants.
3493static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00003494 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
John McCallb1fb4492010-04-07 01:49:15 +00003495 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
Evan Chengf26ffe92008-05-29 08:22:04 +00003496
3497 isLeft = true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003498 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, true, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003499 if (!NumZeros) {
3500 isLeft = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003501 NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, false, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003502 if (!NumZeros)
3503 return false;
3504 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003505 bool SeenV1 = false;
3506 bool SeenV2 = false;
John McCallb1fb4492010-04-07 01:49:15 +00003507 for (unsigned i = NumZeros; i < NumElems; ++i) {
3508 unsigned Val = isLeft ? (i - NumZeros) : i;
3509 int Idx_ = SVOp->getMaskElt(isLeft ? i : (i - NumZeros));
3510 if (Idx_ < 0)
Evan Chengf26ffe92008-05-29 08:22:04 +00003511 continue;
John McCallb1fb4492010-04-07 01:49:15 +00003512 unsigned Idx = (unsigned) Idx_;
Nate Begeman9008ca62009-04-27 18:41:29 +00003513 if (Idx < NumElems)
Evan Chengf26ffe92008-05-29 08:22:04 +00003514 SeenV1 = true;
3515 else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003516 Idx -= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00003517 SeenV2 = true;
3518 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003519 if (Idx != Val)
Evan Chengf26ffe92008-05-29 08:22:04 +00003520 return false;
3521 }
3522 if (SeenV1 && SeenV2)
3523 return false;
3524
Nate Begeman9008ca62009-04-27 18:41:29 +00003525 ShVal = SeenV1 ? SVOp->getOperand(0) : SVOp->getOperand(1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003526 ShAmt = NumZeros;
3527 return true;
3528}
3529
3530
Evan Chengc78d3b42006-04-24 18:01:45 +00003531/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3532///
Dan Gohman475871a2008-07-27 21:46:04 +00003533static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003534 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00003535 SelectionDAG &DAG,
3536 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003537 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00003538 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003539
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003540 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003541 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003542 bool First = true;
3543 for (unsigned i = 0; i < 16; ++i) {
3544 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3545 if (ThisIsNonZero && First) {
3546 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003547 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003548 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003549 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003550 First = false;
3551 }
3552
3553 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00003554 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003555 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3556 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003557 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003558 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00003559 }
3560 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003561 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
3562 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
3563 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00003564 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003565 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00003566 } else
3567 ThisElt = LastElt;
3568
Gabor Greifba36cb52008-08-28 21:40:38 +00003569 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00003570 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00003571 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00003572 }
3573 }
3574
Owen Anderson825b72b2009-08-11 20:47:22 +00003575 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00003576}
3577
Bill Wendlinga348c562007-03-22 18:42:45 +00003578/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003579///
Dan Gohman475871a2008-07-27 21:46:04 +00003580static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00003581 unsigned NumNonZero, unsigned NumZero,
3582 SelectionDAG &DAG,
3583 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003584 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00003585 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003586
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003587 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003588 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003589 bool First = true;
3590 for (unsigned i = 0; i < 8; ++i) {
3591 bool isNonZero = (NonZeros & (1 << i)) != 0;
3592 if (isNonZero) {
3593 if (First) {
3594 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003595 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003596 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003597 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003598 First = false;
3599 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003600 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003601 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00003602 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00003603 }
3604 }
3605
3606 return V;
3607}
3608
Evan Chengf26ffe92008-05-29 08:22:04 +00003609/// getVShift - Return a vector logical shift node.
3610///
Owen Andersone50ed302009-08-10 22:56:29 +00003611static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00003612 unsigned NumBits, SelectionDAG &DAG,
3613 const TargetLowering &TLI, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003614 bool isMMX = VT.getSizeInBits() == 64;
Owen Anderson825b72b2009-08-11 20:47:22 +00003615 EVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00003616 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Dale Johannesenace16102009-02-03 19:33:06 +00003617 SrcOp = DAG.getNode(ISD::BIT_CONVERT, dl, ShVT, SrcOp);
3618 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3619 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00003620 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00003621}
3622
Dan Gohman475871a2008-07-27 21:46:04 +00003623SDValue
Evan Chengc3630942009-12-09 21:00:30 +00003624X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00003625 SelectionDAG &DAG) const {
Evan Chengc3630942009-12-09 21:00:30 +00003626
3627 // Check if the scalar load can be widened into a vector load. And if
3628 // the address is "base + cst" see if the cst can be "absorbed" into
3629 // the shuffle mask.
3630 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
3631 SDValue Ptr = LD->getBasePtr();
3632 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
3633 return SDValue();
3634 EVT PVT = LD->getValueType(0);
3635 if (PVT != MVT::i32 && PVT != MVT::f32)
3636 return SDValue();
3637
3638 int FI = -1;
3639 int64_t Offset = 0;
3640 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
3641 FI = FINode->getIndex();
3642 Offset = 0;
3643 } else if (Ptr.getOpcode() == ISD::ADD &&
3644 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
3645 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
3646 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
3647 Offset = Ptr.getConstantOperandVal(1);
3648 Ptr = Ptr.getOperand(0);
3649 } else {
3650 return SDValue();
3651 }
3652
3653 SDValue Chain = LD->getChain();
3654 // Make sure the stack object alignment is at least 16.
3655 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3656 if (DAG.InferPtrAlignment(Ptr) < 16) {
3657 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00003658 // Can't change the alignment. FIXME: It's possible to compute
3659 // the exact stack offset and reference FI + adjust offset instead.
3660 // If someone *really* cares about this. That's the way to implement it.
3661 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00003662 } else {
3663 MFI->setObjectAlignment(FI, 16);
3664 }
3665 }
3666
3667 // (Offset % 16) must be multiple of 4. Then address is then
3668 // Ptr + (Offset & ~15).
3669 if (Offset < 0)
3670 return SDValue();
3671 if ((Offset % 16) & 3)
3672 return SDValue();
3673 int64_t StartOffset = Offset & ~15;
3674 if (StartOffset)
3675 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
3676 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
3677
3678 int EltNo = (Offset - StartOffset) >> 2;
3679 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
3680 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
David Greene67c9d422010-02-15 16:53:33 +00003681 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,LD->getSrcValue(),0,
3682 false, false, 0);
Evan Chengc3630942009-12-09 21:00:30 +00003683 // Canonicalize it to a v4i32 shuffle.
3684 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32, V1);
3685 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3686 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
3687 DAG.getUNDEF(MVT::v4i32), &Mask[0]));
3688 }
3689
3690 return SDValue();
3691}
3692
Nate Begeman1449f292010-03-24 22:19:06 +00003693/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
3694/// vector of type 'VT', see if the elements can be replaced by a single large
3695/// load which has the same value as a build_vector whose operands are 'elts'.
3696///
3697/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
3698///
3699/// FIXME: we'd also like to handle the case where the last elements are zero
3700/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
3701/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00003702static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
3703 DebugLoc &dl, SelectionDAG &DAG) {
3704 EVT EltVT = VT.getVectorElementType();
3705 unsigned NumElems = Elts.size();
3706
Nate Begemanfdea31a2010-03-24 20:49:50 +00003707 LoadSDNode *LDBase = NULL;
3708 unsigned LastLoadedElt = -1U;
Nate Begeman1449f292010-03-24 22:19:06 +00003709
3710 // For each element in the initializer, see if we've found a load or an undef.
3711 // If we don't find an initial load element, or later load elements are
3712 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00003713 for (unsigned i = 0; i < NumElems; ++i) {
3714 SDValue Elt = Elts[i];
3715
3716 if (!Elt.getNode() ||
3717 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
3718 return SDValue();
3719 if (!LDBase) {
3720 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
3721 return SDValue();
3722 LDBase = cast<LoadSDNode>(Elt.getNode());
3723 LastLoadedElt = i;
3724 continue;
3725 }
3726 if (Elt.getOpcode() == ISD::UNDEF)
3727 continue;
3728
3729 LoadSDNode *LD = cast<LoadSDNode>(Elt);
3730 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
3731 return SDValue();
3732 LastLoadedElt = i;
3733 }
Nate Begeman1449f292010-03-24 22:19:06 +00003734
3735 // If we have found an entire vector of loads and undefs, then return a large
3736 // load of the entire vector width starting at the base pointer. If we found
3737 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00003738 if (LastLoadedElt == NumElems - 1) {
3739 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
3740 return DAG.getLoad(VT, dl, LDBase->getChain(), LDBase->getBasePtr(),
3741 LDBase->getSrcValue(), LDBase->getSrcValueOffset(),
3742 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
3743 return DAG.getLoad(VT, dl, LDBase->getChain(), LDBase->getBasePtr(),
3744 LDBase->getSrcValue(), LDBase->getSrcValueOffset(),
3745 LDBase->isVolatile(), LDBase->isNonTemporal(),
3746 LDBase->getAlignment());
3747 } else if (NumElems == 4 && LastLoadedElt == 1) {
3748 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
3749 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
3750 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2);
3751 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, ResNode);
3752 }
3753 return SDValue();
3754}
3755
Evan Chengc3630942009-12-09 21:00:30 +00003756SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00003757X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003758 DebugLoc dl = Op.getDebugLoc();
Chris Lattner8a594482007-11-25 00:24:49 +00003759 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif327ef032008-08-28 23:19:51 +00003760 if (ISD::isBuildVectorAllZeros(Op.getNode())
3761 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattner8a594482007-11-25 00:24:49 +00003762 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3763 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3764 // eliminated on x86-32 hosts.
Owen Anderson825b72b2009-08-11 20:47:22 +00003765 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
Chris Lattner8a594482007-11-25 00:24:49 +00003766 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003767
Gabor Greifba36cb52008-08-28 21:40:38 +00003768 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00003769 return getOnesVector(Op.getValueType(), DAG, dl);
3770 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00003771 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003772
Owen Andersone50ed302009-08-10 22:56:29 +00003773 EVT VT = Op.getValueType();
3774 EVT ExtVT = VT.getVectorElementType();
3775 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003776
3777 unsigned NumElems = Op.getNumOperands();
3778 unsigned NumZero = 0;
3779 unsigned NumNonZero = 0;
3780 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003781 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00003782 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003783 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003784 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00003785 if (Elt.getOpcode() == ISD::UNDEF)
3786 continue;
3787 Values.insert(Elt);
3788 if (Elt.getOpcode() != ISD::Constant &&
3789 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003790 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00003791 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00003792 NumZero++;
3793 else {
3794 NonZeros |= (1 << i);
3795 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003796 }
3797 }
3798
Dan Gohman7f321562007-06-25 16:23:39 +00003799 if (NumNonZero == 0) {
Chris Lattner8a594482007-11-25 00:24:49 +00003800 // All undef vector. Return an UNDEF. All zero vectors were handled above.
Dale Johannesene8d72302009-02-06 23:05:02 +00003801 return DAG.getUNDEF(VT);
Dan Gohman7f321562007-06-25 16:23:39 +00003802 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003803
Chris Lattner67f453a2008-03-09 05:42:06 +00003804 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00003805 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003806 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00003807 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00003808
Chris Lattner62098042008-03-09 01:05:04 +00003809 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3810 // the value are obviously zero, truncate the value to i32 and do the
3811 // insertion that way. Only do this if the value is non-constant or if the
3812 // value is a constant being inserted into element 0. It is cheaper to do
3813 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00003814 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00003815 (!IsAllConstants || Idx == 0)) {
3816 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3817 // Handle MMX and SSE both.
Owen Anderson825b72b2009-08-11 20:47:22 +00003818 EVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3819 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Scott Michelfdc40a02009-02-17 22:15:04 +00003820
Chris Lattner62098042008-03-09 01:05:04 +00003821 // Truncate the value (which may itself be a constant) to i32, and
3822 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00003823 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00003824 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00003825 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3826 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00003827
Chris Lattner62098042008-03-09 01:05:04 +00003828 // Now we have our 32-bit value zero extended in the low element of
3829 // a vector. If Idx != 0, swizzle it into place.
3830 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003831 SmallVector<int, 4> Mask;
3832 Mask.push_back(Idx);
3833 for (unsigned i = 1; i != VecElts; ++i)
3834 Mask.push_back(i);
3835 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00003836 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00003837 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003838 }
Dale Johannesenace16102009-02-03 19:33:06 +00003839 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00003840 }
3841 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003842
Chris Lattner19f79692008-03-08 22:59:52 +00003843 // If we have a constant or non-constant insertion into the low element of
3844 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3845 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00003846 // depending on what the source datatype is.
3847 if (Idx == 0) {
3848 if (NumZero == 0) {
3849 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00003850 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
3851 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00003852 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
3853 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
3854 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
3855 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00003856 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
3857 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
3858 EVT MiddleVT = VT.getSizeInBits() == 64 ? MVT::v2i32 : MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00003859 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
3860 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3861 Subtarget->hasSSE2(), DAG);
3862 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Item);
3863 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003864 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003865
3866 // Is it a vector logical left shift?
3867 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00003868 X86::isZeroNode(Op.getOperand(0)) &&
3869 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003870 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00003871 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00003872 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00003873 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00003874 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00003875 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003876
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003877 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00003878 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003879
Chris Lattner19f79692008-03-08 22:59:52 +00003880 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3881 // is a non-constant being inserted into an element other than the low one,
3882 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3883 // movd/movss) to move this into the low element, then shuffle it into
3884 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003885 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00003886 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00003887
Evan Cheng0db9fe62006-04-25 20:13:52 +00003888 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003889 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3890 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00003891 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003892 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00003893 MaskVec.push_back(i == Idx ? 0 : 1);
3894 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003895 }
3896 }
3897
Chris Lattner67f453a2008-03-09 05:42:06 +00003898 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00003899 if (Values.size() == 1) {
3900 if (EVTBits == 32) {
3901 // Instead of a shuffle like this:
3902 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
3903 // Check if it's possible to issue this instead.
3904 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
3905 unsigned Idx = CountTrailingZeros_32(NonZeros);
3906 SDValue Item = Op.getOperand(Idx);
3907 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
3908 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
3909 }
Dan Gohman475871a2008-07-27 21:46:04 +00003910 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00003911 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003912
Dan Gohmana3941172007-07-24 22:55:08 +00003913 // A vector full of immediates; various special cases are already
3914 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003915 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00003916 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00003917
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003918 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003919 if (EVTBits == 64) {
3920 if (NumNonZero == 1) {
3921 // One half is zero or undef.
3922 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00003923 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003924 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00003925 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3926 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00003927 }
Dan Gohman475871a2008-07-27 21:46:04 +00003928 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00003929 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003930
3931 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00003932 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00003933 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003934 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003935 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003936 }
3937
Bill Wendling826f36f2007-03-28 00:57:11 +00003938 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00003939 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003940 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003941 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003942 }
3943
3944 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003945 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00003946 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003947 if (NumElems == 4 && NumZero > 0) {
3948 for (unsigned i = 0; i < 4; ++i) {
3949 bool isZero = !(NonZeros & (1 << i));
3950 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003951 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003952 else
Dale Johannesenace16102009-02-03 19:33:06 +00003953 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003954 }
3955
3956 for (unsigned i = 0; i < 2; ++i) {
3957 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3958 default: break;
3959 case 0:
3960 V[i] = V[i*2]; // Must be a zero vector.
3961 break;
3962 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00003963 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003964 break;
3965 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00003966 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003967 break;
3968 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00003969 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003970 break;
3971 }
3972 }
3973
Nate Begeman9008ca62009-04-27 18:41:29 +00003974 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003975 bool Reverse = (NonZeros & 0x3) == 2;
3976 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003977 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003978 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3979 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003980 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
3981 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003982 }
3983
Nate Begemanfdea31a2010-03-24 20:49:50 +00003984 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
3985 // Check for a build vector of consecutive loads.
3986 for (unsigned i = 0; i < NumElems; ++i)
3987 V[i] = Op.getOperand(i);
3988
3989 // Check for elements which are consecutive loads.
3990 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
3991 if (LD.getNode())
3992 return LD;
3993
3994 // For SSE 4.1, use inserts into undef.
3995 if (getSubtarget()->hasSSE41()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003996 V[0] = DAG.getUNDEF(VT);
3997 for (unsigned i = 0; i < NumElems; ++i)
3998 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
3999 V[0] = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, V[0],
4000 Op.getOperand(i), DAG.getIntPtrConstant(i));
4001 return V[0];
4002 }
Nate Begemanfdea31a2010-03-24 20:49:50 +00004003
4004 // Otherwise, expand into a number of unpckl*
Evan Cheng0db9fe62006-04-25 20:13:52 +00004005 // e.g. for v4f32
4006 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
4007 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
4008 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Evan Cheng0db9fe62006-04-25 20:13:52 +00004009 for (unsigned i = 0; i < NumElems; ++i)
Dale Johannesenace16102009-02-03 19:33:06 +00004010 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004011 NumElems >>= 1;
4012 while (NumElems != 0) {
4013 for (unsigned i = 0; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004014 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + NumElems]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004015 NumElems >>= 1;
4016 }
4017 return V[0];
4018 }
Dan Gohman475871a2008-07-27 21:46:04 +00004019 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004020}
4021
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004022SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004023X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004024 // We support concatenate two MMX registers and place them in a MMX
4025 // register. This is better than doing a stack convert.
4026 DebugLoc dl = Op.getDebugLoc();
4027 EVT ResVT = Op.getValueType();
4028 assert(Op.getNumOperands() == 2);
4029 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
4030 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
4031 int Mask[2];
4032 SDValue InVec = DAG.getNode(ISD::BIT_CONVERT,dl, MVT::v1i64, Op.getOperand(0));
4033 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4034 InVec = Op.getOperand(1);
4035 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4036 unsigned NumElts = ResVT.getVectorNumElements();
4037 VecOp = DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
4038 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
4039 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
4040 } else {
4041 InVec = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v1i64, InVec);
4042 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4043 Mask[0] = 0; Mask[1] = 2;
4044 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
4045 }
4046 return DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
4047}
4048
Nate Begemanb9a47b82009-02-23 08:49:38 +00004049// v8i16 shuffles - Prefer shuffles in the following order:
4050// 1. [all] pshuflw, pshufhw, optional move
4051// 2. [ssse3] 1 x pshufb
4052// 3. [ssse3] 2 x pshufb + 1 x por
4053// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004054static
Nate Begeman9008ca62009-04-27 18:41:29 +00004055SDValue LowerVECTOR_SHUFFLEv8i16(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00004056 SelectionDAG &DAG,
4057 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004058 SDValue V1 = SVOp->getOperand(0);
4059 SDValue V2 = SVOp->getOperand(1);
4060 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004061 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00004062
Nate Begemanb9a47b82009-02-23 08:49:38 +00004063 // Determine if more than 1 of the words in each of the low and high quadwords
4064 // of the result come from the same quadword of one of the two inputs. Undef
4065 // mask values count as coming from any quadword, for better codegen.
4066 SmallVector<unsigned, 4> LoQuad(4);
4067 SmallVector<unsigned, 4> HiQuad(4);
4068 BitVector InputQuads(4);
4069 for (unsigned i = 0; i < 8; ++i) {
4070 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00004071 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004072 MaskVals.push_back(EltIdx);
4073 if (EltIdx < 0) {
4074 ++Quad[0];
4075 ++Quad[1];
4076 ++Quad[2];
4077 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00004078 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004079 }
4080 ++Quad[EltIdx / 4];
4081 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00004082 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004083
Nate Begemanb9a47b82009-02-23 08:49:38 +00004084 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004085 unsigned MaxQuad = 1;
4086 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004087 if (LoQuad[i] > MaxQuad) {
4088 BestLoQuad = i;
4089 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004090 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004091 }
4092
Nate Begemanb9a47b82009-02-23 08:49:38 +00004093 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004094 MaxQuad = 1;
4095 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004096 if (HiQuad[i] > MaxQuad) {
4097 BestHiQuad = i;
4098 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004099 }
4100 }
4101
Nate Begemanb9a47b82009-02-23 08:49:38 +00004102 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00004103 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00004104 // single pshufb instruction is necessary. If There are more than 2 input
4105 // quads, disable the next transformation since it does not help SSSE3.
4106 bool V1Used = InputQuads[0] || InputQuads[1];
4107 bool V2Used = InputQuads[2] || InputQuads[3];
4108 if (TLI.getSubtarget()->hasSSSE3()) {
4109 if (InputQuads.count() == 2 && V1Used && V2Used) {
4110 BestLoQuad = InputQuads.find_first();
4111 BestHiQuad = InputQuads.find_next(BestLoQuad);
4112 }
4113 if (InputQuads.count() > 2) {
4114 BestLoQuad = -1;
4115 BestHiQuad = -1;
4116 }
4117 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004118
Nate Begemanb9a47b82009-02-23 08:49:38 +00004119 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
4120 // the shuffle mask. If a quad is scored as -1, that means that it contains
4121 // words from all 4 input quadwords.
4122 SDValue NewV;
4123 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004124 SmallVector<int, 8> MaskV;
4125 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
4126 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00004127 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004128 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V1),
4129 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V2), &MaskV[0]);
4130 NewV = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004131
Nate Begemanb9a47b82009-02-23 08:49:38 +00004132 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
4133 // source words for the shuffle, to aid later transformations.
4134 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00004135 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00004136 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004137 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00004138 if (idx != (int)i)
4139 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004140 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00004141 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004142 AllWordsInNewV = false;
4143 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00004144 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004145
Nate Begemanb9a47b82009-02-23 08:49:38 +00004146 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
4147 if (AllWordsInNewV) {
4148 for (int i = 0; i != 8; ++i) {
4149 int idx = MaskVals[i];
4150 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004151 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004152 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004153 if ((idx != i) && idx < 4)
4154 pshufhw = false;
4155 if ((idx != i) && idx > 3)
4156 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00004157 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00004158 V1 = NewV;
4159 V2Used = false;
4160 BestLoQuad = 0;
4161 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004162 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004163
Nate Begemanb9a47b82009-02-23 08:49:38 +00004164 // If we've eliminated the use of V2, and the new mask is a pshuflw or
4165 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00004166 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Eric Christopherfd179292009-08-27 18:07:15 +00004167 return DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00004168 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Evan Cheng14b32e12007-12-11 01:46:18 +00004169 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004170 }
Eric Christopherfd179292009-08-27 18:07:15 +00004171
Nate Begemanb9a47b82009-02-23 08:49:38 +00004172 // If we have SSSE3, and all words of the result are from 1 input vector,
4173 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
4174 // is present, fall back to case 4.
4175 if (TLI.getSubtarget()->hasSSSE3()) {
4176 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004177
Nate Begemanb9a47b82009-02-23 08:49:38 +00004178 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00004179 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00004180 // mask, and elements that come from V1 in the V2 mask, so that the two
4181 // results can be OR'd together.
4182 bool TwoInputs = V1Used && V2Used;
4183 for (unsigned i = 0; i != 8; ++i) {
4184 int EltIdx = MaskVals[i] * 2;
4185 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004186 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4187 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004188 continue;
4189 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004190 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
4191 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004192 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004193 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004194 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004195 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004196 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004197 if (!TwoInputs)
Owen Anderson825b72b2009-08-11 20:47:22 +00004198 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004199
Nate Begemanb9a47b82009-02-23 08:49:38 +00004200 // Calculate the shuffle mask for the second input, shuffle it, and
4201 // OR it with the first shuffled input.
4202 pshufbMask.clear();
4203 for (unsigned i = 0; i != 8; ++i) {
4204 int EltIdx = MaskVals[i] * 2;
4205 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004206 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4207 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004208 continue;
4209 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004210 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
4211 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004212 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004213 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00004214 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004215 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004216 MVT::v16i8, &pshufbMask[0], 16));
4217 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
4218 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004219 }
4220
4221 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
4222 // and update MaskVals with new element order.
4223 BitVector InOrder(8);
4224 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004225 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004226 for (int i = 0; i != 4; ++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) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004232 MaskV.push_back(idx & 3);
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 }
4238 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004239 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00004240 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004241 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004242 }
Eric Christopherfd179292009-08-27 18:07:15 +00004243
Nate Begemanb9a47b82009-02-23 08:49:38 +00004244 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
4245 // and update MaskVals with the new element order.
4246 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004247 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004248 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004249 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004250 for (unsigned i = 4; i != 8; ++i) {
4251 int idx = MaskVals[i];
4252 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004253 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004254 InOrder.set(i);
4255 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004256 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004257 InOrder.set(i);
4258 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004259 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004260 }
4261 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004262 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004263 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004264 }
Eric Christopherfd179292009-08-27 18:07:15 +00004265
Nate Begemanb9a47b82009-02-23 08:49:38 +00004266 // In case BestHi & BestLo were both -1, which means each quadword has a word
4267 // from each of the four input quadwords, calculate the InOrder bitvector now
4268 // before falling through to the insert/extract cleanup.
4269 if (BestLoQuad == -1 && BestHiQuad == -1) {
4270 NewV = V1;
4271 for (int i = 0; i != 8; ++i)
4272 if (MaskVals[i] < 0 || MaskVals[i] == i)
4273 InOrder.set(i);
4274 }
Eric Christopherfd179292009-08-27 18:07:15 +00004275
Nate Begemanb9a47b82009-02-23 08:49:38 +00004276 // The other elements are put in the right place using pextrw and pinsrw.
4277 for (unsigned i = 0; i != 8; ++i) {
4278 if (InOrder[i])
4279 continue;
4280 int EltIdx = MaskVals[i];
4281 if (EltIdx < 0)
4282 continue;
4283 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004284 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004285 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00004286 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004287 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004288 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004289 DAG.getIntPtrConstant(i));
4290 }
4291 return NewV;
4292}
4293
4294// v16i8 shuffles - Prefer shuffles in the following order:
4295// 1. [ssse3] 1 x pshufb
4296// 2. [ssse3] 2 x pshufb + 1 x por
4297// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
4298static
Nate Begeman9008ca62009-04-27 18:41:29 +00004299SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00004300 SelectionDAG &DAG,
4301 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004302 SDValue V1 = SVOp->getOperand(0);
4303 SDValue V2 = SVOp->getOperand(1);
4304 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004305 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00004306 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00004307
Nate Begemanb9a47b82009-02-23 08:49:38 +00004308 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00004309 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00004310 // present, fall back to case 3.
4311 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
4312 bool V1Only = true;
4313 bool V2Only = true;
4314 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004315 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00004316 if (EltIdx < 0)
4317 continue;
4318 if (EltIdx < 16)
4319 V2Only = false;
4320 else
4321 V1Only = false;
4322 }
Eric Christopherfd179292009-08-27 18:07:15 +00004323
Nate Begemanb9a47b82009-02-23 08:49:38 +00004324 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
4325 if (TLI.getSubtarget()->hasSSSE3()) {
4326 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004327
Nate Begemanb9a47b82009-02-23 08:49:38 +00004328 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00004329 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004330 //
4331 // Otherwise, we have elements from both input vectors, and must zero out
4332 // elements that come from V2 in the first mask, and V1 in the second mask
4333 // so that we can OR them together.
4334 bool TwoInputs = !(V1Only || V2Only);
4335 for (unsigned i = 0; i != 16; ++i) {
4336 int EltIdx = MaskVals[i];
4337 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004338 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004339 continue;
4340 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004341 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004342 }
4343 // If all the elements are from V2, assign it to V1 and return after
4344 // building the first pshufb.
4345 if (V2Only)
4346 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00004347 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004348 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004349 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004350 if (!TwoInputs)
4351 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00004352
Nate Begemanb9a47b82009-02-23 08:49:38 +00004353 // Calculate the shuffle mask for the second input, shuffle it, and
4354 // OR it with the first shuffled input.
4355 pshufbMask.clear();
4356 for (unsigned i = 0; i != 16; ++i) {
4357 int EltIdx = MaskVals[i];
4358 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004359 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004360 continue;
4361 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004362 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004363 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004364 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004365 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004366 MVT::v16i8, &pshufbMask[0], 16));
4367 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004368 }
Eric Christopherfd179292009-08-27 18:07:15 +00004369
Nate Begemanb9a47b82009-02-23 08:49:38 +00004370 // No SSSE3 - Calculate in place words and then fix all out of place words
4371 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
4372 // the 16 different words that comprise the two doublequadword input vectors.
Owen Anderson825b72b2009-08-11 20:47:22 +00004373 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
4374 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004375 SDValue NewV = V2Only ? V2 : V1;
4376 for (int i = 0; i != 8; ++i) {
4377 int Elt0 = MaskVals[i*2];
4378 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00004379
Nate Begemanb9a47b82009-02-23 08:49:38 +00004380 // This word of the result is all undef, skip it.
4381 if (Elt0 < 0 && Elt1 < 0)
4382 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004383
Nate Begemanb9a47b82009-02-23 08:49:38 +00004384 // This word of the result is already in the correct place, skip it.
4385 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
4386 continue;
4387 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
4388 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004389
Nate Begemanb9a47b82009-02-23 08:49:38 +00004390 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
4391 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
4392 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00004393
4394 // If Elt0 and Elt1 are defined, are consecutive, and can be load
4395 // using a single extract together, load it and store it.
4396 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004397 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004398 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00004399 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004400 DAG.getIntPtrConstant(i));
4401 continue;
4402 }
4403
Nate Begemanb9a47b82009-02-23 08:49:38 +00004404 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00004405 // source byte is not also odd, shift the extracted word left 8 bits
4406 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004407 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004408 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004409 DAG.getIntPtrConstant(Elt1 / 2));
4410 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004411 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004412 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004413 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004414 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
4415 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004416 }
4417 // If Elt0 is defined, extract it from the appropriate source. If the
4418 // source byte is not also even, shift the extracted word right 8 bits. If
4419 // Elt1 was also defined, OR the extracted values together before
4420 // inserting them in the result.
4421 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004422 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004423 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
4424 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004425 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004426 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004427 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004428 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
4429 DAG.getConstant(0x00FF, MVT::i16));
4430 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00004431 : InsElt0;
4432 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004433 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004434 DAG.getIntPtrConstant(i));
4435 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004436 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004437}
4438
Evan Cheng7a831ce2007-12-15 03:00:47 +00004439/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Chris Lattnerf172ecd2010-07-04 23:07:25 +00004440/// ones, or rewriting v4i32 / v2i32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00004441/// done when every pair / quad of shuffle mask elements point to elements in
4442/// the right sequence. e.g.
Evan Cheng14b32e12007-12-11 01:46:18 +00004443/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
4444static
Nate Begeman9008ca62009-04-27 18:41:29 +00004445SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
4446 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00004447 const TargetLowering &TLI, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00004448 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00004449 SDValue V1 = SVOp->getOperand(0);
4450 SDValue V2 = SVOp->getOperand(1);
4451 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00004452 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Owen Anderson825b72b2009-08-11 20:47:22 +00004453 EVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Owen Andersone50ed302009-08-10 22:56:29 +00004454 EVT NewVT = MaskVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00004455 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004456 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004457 case MVT::v4f32: NewVT = MVT::v2f64; break;
4458 case MVT::v4i32: NewVT = MVT::v2i64; break;
4459 case MVT::v8i16: NewVT = MVT::v4i32; break;
4460 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004461 }
4462
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004463 if (NewWidth == 2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004464 if (VT.isInteger())
Owen Anderson825b72b2009-08-11 20:47:22 +00004465 NewVT = MVT::v2i64;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004466 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004467 NewVT = MVT::v2f64;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004468 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004469 int Scale = NumElems / NewWidth;
4470 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00004471 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004472 int StartIdx = -1;
4473 for (int j = 0; j < Scale; ++j) {
4474 int EltIdx = SVOp->getMaskElt(i+j);
4475 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004476 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00004477 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00004478 StartIdx = EltIdx - (EltIdx % Scale);
4479 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00004480 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004481 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004482 if (StartIdx == -1)
4483 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00004484 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004485 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004486 }
4487
Dale Johannesenace16102009-02-03 19:33:06 +00004488 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V1);
4489 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00004490 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004491}
4492
Evan Chengd880b972008-05-09 21:53:03 +00004493/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004494///
Owen Andersone50ed302009-08-10 22:56:29 +00004495static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00004496 SDValue SrcOp, SelectionDAG &DAG,
4497 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004498 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004499 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00004500 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00004501 LD = dyn_cast<LoadSDNode>(SrcOp);
4502 if (!LD) {
4503 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
4504 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00004505 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
4506 if ((ExtVT.SimpleTy != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00004507 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4508 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00004509 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004510 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00004511 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Dale Johannesenace16102009-02-03 19:33:06 +00004512 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4513 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
4514 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
4515 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00004516 SrcOp.getOperand(0)
4517 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004518 }
4519 }
4520 }
4521
Dale Johannesenace16102009-02-03 19:33:06 +00004522 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4523 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004524 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00004525 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004526}
4527
Evan Chengace3c172008-07-22 21:13:36 +00004528/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
4529/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004530static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00004531LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
4532 SDValue V1 = SVOp->getOperand(0);
4533 SDValue V2 = SVOp->getOperand(1);
4534 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004535 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00004536
Evan Chengace3c172008-07-22 21:13:36 +00004537 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00004538 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00004539 SmallVector<int, 8> Mask1(4U, -1);
4540 SmallVector<int, 8> PermMask;
4541 SVOp->getMask(PermMask);
4542
Evan Chengace3c172008-07-22 21:13:36 +00004543 unsigned NumHi = 0;
4544 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00004545 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004546 int Idx = PermMask[i];
4547 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004548 Locs[i] = std::make_pair(-1, -1);
4549 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004550 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
4551 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004552 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00004553 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004554 NumLo++;
4555 } else {
4556 Locs[i] = std::make_pair(1, NumHi);
4557 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004558 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004559 NumHi++;
4560 }
4561 }
4562 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004563
Evan Chengace3c172008-07-22 21:13:36 +00004564 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004565 // If no more than two elements come from either vector. This can be
4566 // implemented with two shuffles. First shuffle gather the elements.
4567 // The second shuffle, which takes the first shuffle as both of its
4568 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00004569 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004570
Nate Begeman9008ca62009-04-27 18:41:29 +00004571 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00004572
Evan Chengace3c172008-07-22 21:13:36 +00004573 for (unsigned i = 0; i != 4; ++i) {
4574 if (Locs[i].first == -1)
4575 continue;
4576 else {
4577 unsigned Idx = (i < 2) ? 0 : 4;
4578 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004579 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004580 }
4581 }
4582
Nate Begeman9008ca62009-04-27 18:41:29 +00004583 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004584 } else if (NumLo == 3 || NumHi == 3) {
4585 // Otherwise, we must have three elements from one vector, call it X, and
4586 // one element from the other, call it Y. First, use a shufps to build an
4587 // intermediate vector with the one element from Y and the element from X
4588 // that will be in the same half in the final destination (the indexes don't
4589 // matter). Then, use a shufps to build the final vector, taking the half
4590 // containing the element from Y from the intermediate, and the other half
4591 // from X.
4592 if (NumHi == 3) {
4593 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00004594 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004595 std::swap(V1, V2);
4596 }
4597
4598 // Find the element from V2.
4599 unsigned HiIndex;
4600 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004601 int Val = PermMask[HiIndex];
4602 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004603 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004604 if (Val >= 4)
4605 break;
4606 }
4607
Nate Begeman9008ca62009-04-27 18:41:29 +00004608 Mask1[0] = PermMask[HiIndex];
4609 Mask1[1] = -1;
4610 Mask1[2] = PermMask[HiIndex^1];
4611 Mask1[3] = -1;
4612 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004613
4614 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004615 Mask1[0] = PermMask[0];
4616 Mask1[1] = PermMask[1];
4617 Mask1[2] = HiIndex & 1 ? 6 : 4;
4618 Mask1[3] = HiIndex & 1 ? 4 : 6;
4619 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004620 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004621 Mask1[0] = HiIndex & 1 ? 2 : 0;
4622 Mask1[1] = HiIndex & 1 ? 0 : 2;
4623 Mask1[2] = PermMask[2];
4624 Mask1[3] = PermMask[3];
4625 if (Mask1[2] >= 0)
4626 Mask1[2] += 4;
4627 if (Mask1[3] >= 0)
4628 Mask1[3] += 4;
4629 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004630 }
Evan Chengace3c172008-07-22 21:13:36 +00004631 }
4632
4633 // Break it into (shuffle shuffle_hi, shuffle_lo).
4634 Locs.clear();
Nate Begeman9008ca62009-04-27 18:41:29 +00004635 SmallVector<int,8> LoMask(4U, -1);
4636 SmallVector<int,8> HiMask(4U, -1);
4637
4638 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00004639 unsigned MaskIdx = 0;
4640 unsigned LoIdx = 0;
4641 unsigned HiIdx = 2;
4642 for (unsigned i = 0; i != 4; ++i) {
4643 if (i == 2) {
4644 MaskPtr = &HiMask;
4645 MaskIdx = 1;
4646 LoIdx = 0;
4647 HiIdx = 2;
4648 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004649 int Idx = PermMask[i];
4650 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004651 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00004652 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004653 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004654 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004655 LoIdx++;
4656 } else {
4657 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004658 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004659 HiIdx++;
4660 }
4661 }
4662
Nate Begeman9008ca62009-04-27 18:41:29 +00004663 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
4664 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
4665 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00004666 for (unsigned i = 0; i != 4; ++i) {
4667 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004668 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00004669 } else {
4670 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004671 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00004672 }
4673 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004674 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00004675}
4676
Dan Gohman475871a2008-07-27 21:46:04 +00004677SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004678X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00004679 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00004680 SDValue V1 = Op.getOperand(0);
4681 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00004682 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004683 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00004684 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004685 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004686 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
4687 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00004688 bool V1IsSplat = false;
4689 bool V2IsSplat = false;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004690
Nate Begeman9008ca62009-04-27 18:41:29 +00004691 if (isZeroShuffle(SVOp))
Dale Johannesenace16102009-02-03 19:33:06 +00004692 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004693
Nate Begeman9008ca62009-04-27 18:41:29 +00004694 // Promote splats to v4f32.
4695 if (SVOp->isSplat()) {
Eric Christopherfd179292009-08-27 18:07:15 +00004696 if (isMMX || NumElems < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004697 return Op;
4698 return PromoteSplat(SVOp, DAG, Subtarget->hasSSE2());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004699 }
4700
Evan Cheng7a831ce2007-12-15 03:00:47 +00004701 // If the shuffle can be profitably rewritten as a narrower shuffle, then
4702 // do it!
Owen Anderson825b72b2009-08-11 20:47:22 +00004703 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004704 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004705 if (NewOp.getNode())
Scott Michelfdc40a02009-02-17 22:15:04 +00004706 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004707 LowerVECTOR_SHUFFLE(NewOp, DAG));
Owen Anderson825b72b2009-08-11 20:47:22 +00004708 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Evan Cheng7a831ce2007-12-15 03:00:47 +00004709 // FIXME: Figure out a cleaner way to do this.
4710 // Try to make use of movq to zero out the top part.
Gabor Greifba36cb52008-08-28 21:40:38 +00004711 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004712 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004713 if (NewOp.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004714 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
4715 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
4716 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004717 }
Gabor Greifba36cb52008-08-28 21:40:38 +00004718 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004719 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
4720 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
Evan Chengd880b972008-05-09 21:53:03 +00004721 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Nate Begeman9008ca62009-04-27 18:41:29 +00004722 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004723 }
4724 }
Eric Christopherfd179292009-08-27 18:07:15 +00004725
Nate Begeman9008ca62009-04-27 18:41:29 +00004726 if (X86::isPSHUFDMask(SVOp))
4727 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004728
Evan Chengf26ffe92008-05-29 08:22:04 +00004729 // Check if this can be converted into a logical shift.
4730 bool isLeft = false;
4731 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00004732 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00004733 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00004734 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00004735 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004736 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00004737 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00004738 EVT EltVT = VT.getVectorElementType();
4739 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004740 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004741 }
Eric Christopherfd179292009-08-27 18:07:15 +00004742
Nate Begeman9008ca62009-04-27 18:41:29 +00004743 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004744 if (V1IsUndef)
4745 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00004746 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004747 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Nate Begemanfb8ead02008-07-25 19:05:58 +00004748 if (!isMMX)
4749 return Op;
Evan Cheng7e2ff772008-05-08 00:57:18 +00004750 }
Eric Christopherfd179292009-08-27 18:07:15 +00004751
Nate Begeman9008ca62009-04-27 18:41:29 +00004752 // FIXME: fold these into legal mask.
4753 if (!isMMX && (X86::isMOVSHDUPMask(SVOp) ||
4754 X86::isMOVSLDUPMask(SVOp) ||
4755 X86::isMOVHLPSMask(SVOp) ||
Nate Begeman0b10b912009-11-07 23:17:15 +00004756 X86::isMOVLHPSMask(SVOp) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00004757 X86::isMOVLPMask(SVOp)))
Evan Cheng9bbbb982006-10-25 20:48:19 +00004758 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004759
Nate Begeman9008ca62009-04-27 18:41:29 +00004760 if (ShouldXformToMOVHLPS(SVOp) ||
4761 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
4762 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004763
Evan Chengf26ffe92008-05-29 08:22:04 +00004764 if (isShift) {
4765 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00004766 EVT EltVT = VT.getVectorElementType();
4767 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004768 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004769 }
Eric Christopherfd179292009-08-27 18:07:15 +00004770
Evan Cheng9eca5e82006-10-25 21:49:50 +00004771 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00004772 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4773 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00004774 V1IsSplat = isSplatVector(V1.getNode());
4775 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00004776
Chris Lattner8a594482007-11-25 00:24:49 +00004777 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00004778 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004779 Op = CommuteVectorShuffle(SVOp, DAG);
4780 SVOp = cast<ShuffleVectorSDNode>(Op);
4781 V1 = SVOp->getOperand(0);
4782 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00004783 std::swap(V1IsSplat, V2IsSplat);
4784 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00004785 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00004786 }
4787
Nate Begeman9008ca62009-04-27 18:41:29 +00004788 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
4789 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00004790 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00004791 return V1;
4792 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
4793 // the instruction selector will not match, so get a canonical MOVL with
4794 // swapped operands to undo the commute.
4795 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00004796 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004797
Nate Begeman9008ca62009-04-27 18:41:29 +00004798 if (X86::isUNPCKL_v_undef_Mask(SVOp) ||
4799 X86::isUNPCKH_v_undef_Mask(SVOp) ||
4800 X86::isUNPCKLMask(SVOp) ||
4801 X86::isUNPCKHMask(SVOp))
Evan Chengd9b8e402006-10-16 06:36:00 +00004802 return Op;
Evan Chenge1113032006-10-04 18:33:38 +00004803
Evan Cheng9bbbb982006-10-25 20:48:19 +00004804 if (V2IsSplat) {
4805 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004806 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00004807 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00004808 SDValue NewMask = NormalizeMask(SVOp, DAG);
4809 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
4810 if (NSVOp != SVOp) {
4811 if (X86::isUNPCKLMask(NSVOp, true)) {
4812 return NewMask;
4813 } else if (X86::isUNPCKHMask(NSVOp, true)) {
4814 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004815 }
4816 }
4817 }
4818
Evan Cheng9eca5e82006-10-25 21:49:50 +00004819 if (Commuted) {
4820 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00004821 // FIXME: this seems wrong.
4822 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
4823 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
4824 if (X86::isUNPCKL_v_undef_Mask(NewSVOp) ||
4825 X86::isUNPCKH_v_undef_Mask(NewSVOp) ||
4826 X86::isUNPCKLMask(NewSVOp) ||
4827 X86::isUNPCKHMask(NewSVOp))
4828 return NewOp;
Evan Cheng9eca5e82006-10-25 21:49:50 +00004829 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004830
Nate Begemanb9a47b82009-02-23 08:49:38 +00004831 // FIXME: for mmx, bitcast v2i32 to v4i16 for shuffle.
Nate Begeman9008ca62009-04-27 18:41:29 +00004832
4833 // Normalize the node to match x86 shuffle ops if needed
4834 if (!isMMX && V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
4835 return CommuteVectorShuffle(SVOp, DAG);
4836
4837 // Check for legal shuffle and return?
4838 SmallVector<int, 16> PermMask;
4839 SVOp->getMask(PermMask);
4840 if (isShuffleMaskLegal(PermMask, VT))
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004841 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004842
Evan Cheng14b32e12007-12-11 01:46:18 +00004843 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00004844 if (VT == MVT::v8i16) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004845 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(SVOp, DAG, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004846 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00004847 return NewOp;
4848 }
4849
Owen Anderson825b72b2009-08-11 20:47:22 +00004850 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004851 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004852 if (NewOp.getNode())
4853 return NewOp;
4854 }
Eric Christopherfd179292009-08-27 18:07:15 +00004855
Evan Chengace3c172008-07-22 21:13:36 +00004856 // Handle all 4 wide cases with a number of shuffles except for MMX.
4857 if (NumElems == 4 && !isMMX)
Nate Begeman9008ca62009-04-27 18:41:29 +00004858 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004859
Dan Gohman475871a2008-07-27 21:46:04 +00004860 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004861}
4862
Dan Gohman475871a2008-07-27 21:46:04 +00004863SDValue
4864X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00004865 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00004866 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004867 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004868 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004869 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004870 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004871 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004872 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004873 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004874 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00004875 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
4876 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
4877 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004878 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4879 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004880 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004881 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00004882 Op.getOperand(0)),
4883 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00004884 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004885 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004886 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004887 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004888 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00004889 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00004890 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4891 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004892 // result has a single use which is a store or a bitcast to i32. And in
4893 // the case of a store, it's not worth it if the index is a constant 0,
4894 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00004895 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00004896 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004897 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004898 if ((User->getOpcode() != ISD::STORE ||
4899 (isa<ConstantSDNode>(Op.getOperand(1)) &&
4900 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman171c11e2008-04-16 02:32:24 +00004901 (User->getOpcode() != ISD::BIT_CONVERT ||
Owen Anderson825b72b2009-08-11 20:47:22 +00004902 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00004903 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00004904 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
4905 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004906 Op.getOperand(0)),
4907 Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004908 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Extract);
4909 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00004910 // ExtractPS works with constant index.
4911 if (isa<ConstantSDNode>(Op.getOperand(1)))
4912 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004913 }
Dan Gohman475871a2008-07-27 21:46:04 +00004914 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004915}
4916
4917
Dan Gohman475871a2008-07-27 21:46:04 +00004918SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004919X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
4920 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004921 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00004922 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004923
Evan Cheng62a3f152008-03-24 21:52:23 +00004924 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004925 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004926 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00004927 return Res;
4928 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00004929
Owen Andersone50ed302009-08-10 22:56:29 +00004930 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004931 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004932 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004933 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004934 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004935 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004936 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004937 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4938 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Scott Michelfdc40a02009-02-17 22:15:04 +00004939 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004940 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00004941 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004942 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00004943 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00004944 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004945 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00004946 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004947 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004948 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004949 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004950 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004951 if (Idx == 0)
4952 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004953
Evan Cheng0db9fe62006-04-25 20:13:52 +00004954 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00004955 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00004956 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00004957 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00004958 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004959 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004960 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004961 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004962 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4963 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4964 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004965 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004966 if (Idx == 0)
4967 return Op;
4968
4969 // UNPCKHPD the element to the lowest double word, then movsd.
4970 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4971 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00004972 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00004973 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00004974 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00004975 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004976 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004977 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004978 }
4979
Dan Gohman475871a2008-07-27 21:46:04 +00004980 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004981}
4982
Dan Gohman475871a2008-07-27 21:46:04 +00004983SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004984X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
4985 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00004986 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00004987 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004988 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004989
Dan Gohman475871a2008-07-27 21:46:04 +00004990 SDValue N0 = Op.getOperand(0);
4991 SDValue N1 = Op.getOperand(1);
4992 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004993
Dan Gohman8a55ce42009-09-23 21:02:20 +00004994 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00004995 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00004996 unsigned Opc;
4997 if (VT == MVT::v8i16)
4998 Opc = X86ISD::PINSRW;
4999 else if (VT == MVT::v4i16)
5000 Opc = X86ISD::MMX_PINSRW;
5001 else if (VT == MVT::v16i8)
5002 Opc = X86ISD::PINSRB;
5003 else
5004 Opc = X86ISD::PINSRB;
5005
Nate Begeman14d12ca2008-02-11 04:19:36 +00005006 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
5007 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00005008 if (N1.getValueType() != MVT::i32)
5009 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
5010 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005011 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00005012 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00005013 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00005014 // Bits [7:6] of the constant are the source select. This will always be
5015 // zero here. The DAG Combiner may combine an extract_elt index into these
5016 // bits. For example (insert (extract, 3), 2) could be matched by putting
5017 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00005018 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00005019 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00005020 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00005021 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005022 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00005023 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00005024 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00005025 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00005026 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00005027 // PINSR* works with constant index.
5028 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00005029 }
Dan Gohman475871a2008-07-27 21:46:04 +00005030 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005031}
5032
Dan Gohman475871a2008-07-27 21:46:04 +00005033SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005034X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005035 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00005036 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005037
5038 if (Subtarget->hasSSE41())
5039 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
5040
Dan Gohman8a55ce42009-09-23 21:02:20 +00005041 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00005042 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00005043
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005044 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005045 SDValue N0 = Op.getOperand(0);
5046 SDValue N1 = Op.getOperand(1);
5047 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00005048
Dan Gohman8a55ce42009-09-23 21:02:20 +00005049 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00005050 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
5051 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00005052 if (N1.getValueType() != MVT::i32)
5053 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
5054 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005055 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00005056 return DAG.getNode(VT == MVT::v8i16 ? X86ISD::PINSRW : X86ISD::MMX_PINSRW,
5057 dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005058 }
Dan Gohman475871a2008-07-27 21:46:04 +00005059 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005060}
5061
Dan Gohman475871a2008-07-27 21:46:04 +00005062SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005063X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005064 DebugLoc dl = Op.getDebugLoc();
Chris Lattnerf172ecd2010-07-04 23:07:25 +00005065
5066 if (Op.getValueType() == MVT::v1i64 &&
5067 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00005068 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00005069
Owen Anderson825b72b2009-08-11 20:47:22 +00005070 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
5071 EVT VT = MVT::v2i32;
5072 switch (Op.getValueType().getSimpleVT().SimpleTy) {
Evan Chengefec7512008-02-18 23:04:32 +00005073 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00005074 case MVT::v16i8:
5075 case MVT::v8i16:
5076 VT = MVT::v4i32;
Evan Chengefec7512008-02-18 23:04:32 +00005077 break;
5078 }
Dale Johannesenace16102009-02-03 19:33:06 +00005079 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(),
5080 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005081}
5082
Bill Wendling056292f2008-09-16 21:48:12 +00005083// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
5084// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
5085// one of the above mentioned nodes. It has to be wrapped because otherwise
5086// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
5087// be used to form addressing mode. These wrapped nodes will be selected
5088// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00005089SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005090X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005091 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00005092
Chris Lattner41621a22009-06-26 19:22:52 +00005093 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5094 // global base reg.
5095 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005096 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005097 CodeModel::Model M = getTargetMachine().getCodeModel();
5098
Chris Lattner4f066492009-07-11 20:29:19 +00005099 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005100 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005101 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005102 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005103 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005104 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005105 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005106
Evan Cheng1606e8e2009-03-13 07:51:59 +00005107 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00005108 CP->getAlignment(),
5109 CP->getOffset(), OpFlag);
5110 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00005111 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005112 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00005113 if (OpFlag) {
5114 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005115 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005116 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005117 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005118 }
5119
5120 return Result;
5121}
5122
Dan Gohmand858e902010-04-17 15:26:15 +00005123SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00005124 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00005125
Chris Lattner18c59872009-06-27 04:16:01 +00005126 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5127 // global base reg.
5128 unsigned char OpFlag = 0;
5129 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005130 CodeModel::Model M = getTargetMachine().getCodeModel();
5131
Chris Lattner4f066492009-07-11 20:29:19 +00005132 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005133 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005134 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005135 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005136 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005137 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005138 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005139
Chris Lattner18c59872009-06-27 04:16:01 +00005140 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
5141 OpFlag);
5142 DebugLoc DL = JT->getDebugLoc();
5143 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005144
Chris Lattner18c59872009-06-27 04:16:01 +00005145 // With PIC, the address is actually $g + Offset.
5146 if (OpFlag) {
5147 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5148 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005149 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00005150 Result);
5151 }
Eric Christopherfd179292009-08-27 18:07:15 +00005152
Chris Lattner18c59872009-06-27 04:16:01 +00005153 return Result;
5154}
5155
5156SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005157X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00005158 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00005159
Chris Lattner18c59872009-06-27 04:16:01 +00005160 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5161 // global base reg.
5162 unsigned char OpFlag = 0;
5163 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005164 CodeModel::Model M = getTargetMachine().getCodeModel();
5165
Chris Lattner4f066492009-07-11 20:29:19 +00005166 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005167 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005168 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005169 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005170 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005171 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005172 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005173
Chris Lattner18c59872009-06-27 04:16:01 +00005174 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00005175
Chris Lattner18c59872009-06-27 04:16:01 +00005176 DebugLoc DL = Op.getDebugLoc();
5177 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005178
5179
Chris Lattner18c59872009-06-27 04:16:01 +00005180 // With PIC, the address is actually $g + Offset.
5181 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00005182 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00005183 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5184 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005185 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00005186 Result);
5187 }
Eric Christopherfd179292009-08-27 18:07:15 +00005188
Chris Lattner18c59872009-06-27 04:16:01 +00005189 return Result;
5190}
5191
Dan Gohman475871a2008-07-27 21:46:04 +00005192SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005193X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00005194 // Create the TargetBlockAddressAddress node.
5195 unsigned char OpFlags =
5196 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00005197 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00005198 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00005199 DebugLoc dl = Op.getDebugLoc();
5200 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
5201 /*isTarget=*/true, OpFlags);
5202
Dan Gohmanf705adb2009-10-30 01:28:02 +00005203 if (Subtarget->isPICStyleRIPRel() &&
5204 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00005205 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
5206 else
5207 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00005208
Dan Gohman29cbade2009-11-20 23:18:13 +00005209 // With PIC, the address is actually $g + Offset.
5210 if (isGlobalRelativeToPICBase(OpFlags)) {
5211 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5212 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
5213 Result);
5214 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00005215
5216 return Result;
5217}
5218
5219SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00005220X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00005221 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00005222 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00005223 // Create the TargetGlobalAddress node, folding in the constant
5224 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00005225 unsigned char OpFlags =
5226 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005227 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00005228 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005229 if (OpFlags == X86II::MO_NO_FLAG &&
5230 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00005231 // A direct static reference to a global.
Dale Johannesen60b3ba02009-07-21 00:12:29 +00005232 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00005233 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005234 } else {
Chris Lattnerb1acd682009-06-27 05:39:56 +00005235 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00005236 }
Eric Christopherfd179292009-08-27 18:07:15 +00005237
Chris Lattner4f066492009-07-11 20:29:19 +00005238 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005239 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00005240 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
5241 else
5242 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00005243
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005244 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00005245 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00005246 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5247 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005248 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005249 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005250
Chris Lattner36c25012009-07-10 07:34:39 +00005251 // For globals that require a load from a stub to get the address, emit the
5252 // load.
5253 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00005254 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
David Greene67c9d422010-02-15 16:53:33 +00005255 PseudoSourceValue::getGOT(), 0, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005256
Dan Gohman6520e202008-10-18 02:06:02 +00005257 // If there was a non-zero offset that we didn't fold, create an explicit
5258 // addition for it.
5259 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00005260 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00005261 DAG.getConstant(Offset, getPointerTy()));
5262
Evan Cheng0db9fe62006-04-25 20:13:52 +00005263 return Result;
5264}
5265
Evan Chengda43bcf2008-09-24 00:05:32 +00005266SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005267X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00005268 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00005269 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005270 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00005271}
5272
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005273static SDValue
5274GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00005275 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00005276 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00005277 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Owen Anderson825b72b2009-08-11 20:47:22 +00005278 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005279 DebugLoc dl = GA->getDebugLoc();
5280 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
5281 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00005282 GA->getOffset(),
5283 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005284 if (InFlag) {
5285 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00005286 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005287 } else {
5288 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00005289 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005290 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00005291
5292 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00005293 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00005294
Rafael Espindola15f1b662009-04-24 12:59:40 +00005295 SDValue Flag = Chain.getValue(1);
5296 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005297}
5298
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005299// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00005300static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005301LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005302 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005303 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00005304 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
5305 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005306 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005307 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005308 InFlag = Chain.getValue(1);
5309
Chris Lattnerb903bed2009-06-26 21:20:29 +00005310 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005311}
5312
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005313// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00005314static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005315LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005316 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00005317 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
5318 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005319}
5320
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005321// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
5322// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00005323static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005324 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00005325 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00005326 DebugLoc dl = GA->getDebugLoc();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005327 // Get the Thread Pointer
Rafael Espindola094fad32009-04-08 21:14:34 +00005328 SDValue Base = DAG.getNode(X86ISD::SegmentBaseAddress,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005329 DebugLoc(), PtrVT,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00005330 DAG.getRegister(is64Bit? X86::FS : X86::GS,
Owen Anderson825b72b2009-08-11 20:47:22 +00005331 MVT::i32));
Rafael Espindola094fad32009-04-08 21:14:34 +00005332
5333 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Base,
David Greene67c9d422010-02-15 16:53:33 +00005334 NULL, 0, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00005335
Chris Lattnerb903bed2009-06-26 21:20:29 +00005336 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005337 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
5338 // initialexec.
5339 unsigned WrapperKind = X86ISD::Wrapper;
5340 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00005341 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00005342 } else if (is64Bit) {
5343 assert(model == TLSModel::InitialExec);
5344 OperandFlags = X86II::MO_GOTTPOFF;
5345 WrapperKind = X86ISD::WrapperRIP;
5346 } else {
5347 assert(model == TLSModel::InitialExec);
5348 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00005349 }
Eric Christopherfd179292009-08-27 18:07:15 +00005350
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005351 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
5352 // exec)
Chris Lattner4150c082009-06-21 02:22:34 +00005353 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00005354 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00005355 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00005356
Rafael Espindola9a580232009-02-27 13:37:18 +00005357 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00005358 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
David Greene67c9d422010-02-15 16:53:33 +00005359 PseudoSourceValue::getGOT(), 0, false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00005360
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005361 // The address of the thread local variable is the add of the thread
5362 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00005363 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005364}
5365
Dan Gohman475871a2008-07-27 21:46:04 +00005366SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005367X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Eric Christopher30ef0e52010-06-03 04:07:48 +00005368
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005369 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00005370 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00005371
Eric Christopher30ef0e52010-06-03 04:07:48 +00005372 if (Subtarget->isTargetELF()) {
5373 // TODO: implement the "local dynamic" model
5374 // TODO: implement the "initial exec"model for pic executables
5375
5376 // If GV is an alias then use the aliasee for determining
5377 // thread-localness.
5378 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
5379 GV = GA->resolveAliasedGlobal(false);
5380
5381 TLSModel::Model model
5382 = getTLSModel(GV, getTargetMachine().getRelocationModel());
5383
5384 switch (model) {
5385 case TLSModel::GeneralDynamic:
5386 case TLSModel::LocalDynamic: // not implemented
5387 if (Subtarget->is64Bit())
5388 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
5389 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
5390
5391 case TLSModel::InitialExec:
5392 case TLSModel::LocalExec:
5393 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
5394 Subtarget->is64Bit());
5395 }
5396 } else if (Subtarget->isTargetDarwin()) {
5397 // Darwin only has one model of TLS. Lower to that.
5398 unsigned char OpFlag = 0;
5399 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
5400 X86ISD::WrapperRIP : X86ISD::Wrapper;
5401
5402 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5403 // global base reg.
5404 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
5405 !Subtarget->is64Bit();
5406 if (PIC32)
5407 OpFlag = X86II::MO_TLVP_PIC_BASE;
5408 else
5409 OpFlag = X86II::MO_TLVP;
5410
5411 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(),
5412 getPointerTy(),
5413 GA->getOffset(), OpFlag);
5414
5415 DebugLoc DL = Op.getDebugLoc();
5416 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
5417
5418 // With PIC32, the address is actually $g + Offset.
5419 if (PIC32)
5420 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5421 DAG.getNode(X86ISD::GlobalBaseReg,
5422 DebugLoc(), getPointerTy()),
5423 Offset);
5424
5425 // Lowering the machine isd will make sure everything is in the right
5426 // location.
5427 SDValue Args[] = { Offset };
5428 SDValue Chain = DAG.getNode(X86ISD::TLSCALL, DL, MVT::Other, Args, 1);
5429
5430 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
5431 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
5432 MFI->setAdjustsStack(true);
Eric Christopherfd179292009-08-27 18:07:15 +00005433
Eric Christopher30ef0e52010-06-03 04:07:48 +00005434 // And our return value (tls address) is in the standard call return value
5435 // location.
5436 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
5437 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005438 }
Eric Christopher30ef0e52010-06-03 04:07:48 +00005439
5440 assert(false &&
5441 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00005442
Torok Edwinc23197a2009-07-14 16:55:14 +00005443 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00005444 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005445}
5446
Evan Cheng0db9fe62006-04-25 20:13:52 +00005447
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005448/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00005449/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00005450SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00005451 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00005452 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005453 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005454 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005455 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00005456 SDValue ShOpLo = Op.getOperand(0);
5457 SDValue ShOpHi = Op.getOperand(1);
5458 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00005459 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00005460 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00005461 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00005462
Dan Gohman475871a2008-07-27 21:46:04 +00005463 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005464 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00005465 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
5466 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005467 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005468 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
5469 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005470 }
Evan Chenge3413162006-01-09 18:33:28 +00005471
Owen Anderson825b72b2009-08-11 20:47:22 +00005472 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
5473 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00005474 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00005475 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00005476
Dan Gohman475871a2008-07-27 21:46:04 +00005477 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00005478 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00005479 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
5480 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00005481
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005482 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00005483 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
5484 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005485 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005486 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
5487 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005488 }
5489
Dan Gohman475871a2008-07-27 21:46:04 +00005490 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00005491 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005492}
Evan Chenga3195e82006-01-12 22:54:21 +00005493
Dan Gohmand858e902010-04-17 15:26:15 +00005494SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
5495 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005496 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00005497
5498 if (SrcVT.isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005499 if (SrcVT == MVT::v2i32 && Op.getValueType() == MVT::v2f64) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005500 return Op;
5501 }
5502 return SDValue();
5503 }
5504
Owen Anderson825b72b2009-08-11 20:47:22 +00005505 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00005506 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00005507
Eli Friedman36df4992009-05-27 00:47:34 +00005508 // These are really Legal; return the operand so the caller accepts it as
5509 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00005510 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00005511 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00005512 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00005513 Subtarget->is64Bit()) {
5514 return Op;
5515 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005516
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005517 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005518 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005519 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005520 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005521 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00005522 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00005523 StackSlot,
David Greene67c9d422010-02-15 16:53:33 +00005524 PseudoSourceValue::getFixedStack(SSFI), 0,
5525 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00005526 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
5527}
Evan Cheng0db9fe62006-04-25 20:13:52 +00005528
Owen Andersone50ed302009-08-10 22:56:29 +00005529SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Dale Johannesen8d908eb2010-05-15 18:51:12 +00005530 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00005531 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005532 // Build the FILD
Eli Friedman948e95a2009-05-23 09:59:16 +00005533 DebugLoc dl = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00005534 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00005535 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005536 if (useSSE)
Owen Anderson825b72b2009-08-11 20:47:22 +00005537 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00005538 else
Owen Anderson825b72b2009-08-11 20:47:22 +00005539 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005540 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Dale Johannesenace16102009-02-03 19:33:06 +00005541 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD, dl,
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005542 Tys, Ops, array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005543
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005544 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005545 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00005546 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005547
5548 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
5549 // shouldn't be necessary except that RFP cannot be live across
5550 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005551 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005552 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005553 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00005554 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005555 SDValue Ops[] = {
5556 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
5557 };
5558 Chain = DAG.getNode(X86ISD::FST, dl, Tys, Ops, array_lengthof(Ops));
Dale Johannesenace16102009-02-03 19:33:06 +00005559 Result = DAG.getLoad(Op.getValueType(), dl, Chain, StackSlot,
David Greene67c9d422010-02-15 16:53:33 +00005560 PseudoSourceValue::getFixedStack(SSFI), 0,
5561 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005562 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005563
Evan Cheng0db9fe62006-04-25 20:13:52 +00005564 return Result;
5565}
5566
Bill Wendling8b8a6362009-01-17 03:56:04 +00005567// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00005568SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
5569 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00005570 // This algorithm is not obvious. Here it is in C code, more or less:
5571 /*
5572 double uint64_to_double( uint32_t hi, uint32_t lo ) {
5573 static const __m128i exp = { 0x4330000045300000ULL, 0 };
5574 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00005575
Bill Wendling8b8a6362009-01-17 03:56:04 +00005576 // Copy ints to xmm registers.
5577 __m128i xh = _mm_cvtsi32_si128( hi );
5578 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00005579
Bill Wendling8b8a6362009-01-17 03:56:04 +00005580 // Combine into low half of a single xmm register.
5581 __m128i x = _mm_unpacklo_epi32( xh, xl );
5582 __m128d d;
5583 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00005584
Bill Wendling8b8a6362009-01-17 03:56:04 +00005585 // Merge in appropriate exponents to give the integer bits the right
5586 // magnitude.
5587 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00005588
Bill Wendling8b8a6362009-01-17 03:56:04 +00005589 // Subtract away the biases to deal with the IEEE-754 double precision
5590 // implicit 1.
5591 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00005592
Bill Wendling8b8a6362009-01-17 03:56:04 +00005593 // All conversions up to here are exact. The correctly rounded result is
5594 // calculated using the current rounding mode using the following
5595 // horizontal add.
5596 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
5597 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
5598 // store doesn't really need to be here (except
5599 // maybe to zero the other double)
5600 return sd;
5601 }
5602 */
Dale Johannesen040225f2008-10-21 23:07:49 +00005603
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005604 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00005605 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00005606
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005607 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00005608 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00005609 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
5610 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
5611 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
5612 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00005613 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005614 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005615
Bill Wendling8b8a6362009-01-17 03:56:04 +00005616 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00005617 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005618 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00005619 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005620 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00005621 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005622 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005623
Owen Anderson825b72b2009-08-11 20:47:22 +00005624 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5625 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00005626 Op.getOperand(0),
5627 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005628 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5629 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00005630 Op.getOperand(0),
5631 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005632 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
5633 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005634 PseudoSourceValue::getConstantPool(), 0,
David Greene67c9d422010-02-15 16:53:33 +00005635 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00005636 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
5637 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Unpck2);
5638 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005639 PseudoSourceValue::getConstantPool(), 0,
David Greene67c9d422010-02-15 16:53:33 +00005640 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00005641 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005642
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005643 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00005644 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00005645 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
5646 DAG.getUNDEF(MVT::v2f64), ShufMask);
5647 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
5648 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005649 DAG.getIntPtrConstant(0));
5650}
5651
Bill Wendling8b8a6362009-01-17 03:56:04 +00005652// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00005653SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
5654 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005655 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00005656 // FP constant to bias correct the final result.
5657 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00005658 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005659
5660 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00005661 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5662 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005663 Op.getOperand(0),
5664 DAG.getIntPtrConstant(0)));
5665
Owen Anderson825b72b2009-08-11 20:47:22 +00005666 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5667 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00005668 DAG.getIntPtrConstant(0));
5669
5670 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00005671 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
5672 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005673 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005674 MVT::v2f64, Load)),
5675 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005676 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005677 MVT::v2f64, Bias)));
5678 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5679 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00005680 DAG.getIntPtrConstant(0));
5681
5682 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00005683 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005684
5685 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00005686 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00005687
Owen Anderson825b72b2009-08-11 20:47:22 +00005688 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005689 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00005690 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00005691 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005692 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00005693 }
5694
5695 // Handle final rounding.
5696 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00005697}
5698
Dan Gohmand858e902010-04-17 15:26:15 +00005699SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
5700 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00005701 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005702 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00005703
Dale Johannesen8d908eb2010-05-15 18:51:12 +00005704 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00005705 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
5706 // the optimization here.
5707 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00005708 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00005709
Owen Andersone50ed302009-08-10 22:56:29 +00005710 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00005711 EVT DstVT = Op.getValueType();
5712 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00005713 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00005714 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00005715 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00005716
5717 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00005718 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00005719 if (SrcVT == MVT::i32) {
5720 SDValue WordOff = DAG.getConstant(4, getPointerTy());
5721 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
5722 getPointerTy(), StackSlot, WordOff);
5723 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
5724 StackSlot, NULL, 0, false, false, 0);
5725 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
5726 OffsetSlot, NULL, 0, false, false, 0);
5727 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
5728 return Fild;
5729 }
5730
5731 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
5732 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
David Greene67c9d422010-02-15 16:53:33 +00005733 StackSlot, NULL, 0, false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00005734 // For i64 source, we need to add the appropriate power of 2 if the input
5735 // was negative. This is the same as the optimization in
5736 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
5737 // we must be careful to do the computation in x87 extended precision, not
5738 // in SSE. (The generic code can't know it's OK to do this, or how to.)
5739 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
5740 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
5741 SDValue Fild = DAG.getNode(X86ISD::FILD, dl, Tys, Ops, 3);
5742
5743 APInt FF(32, 0x5F800000ULL);
5744
5745 // Check whether the sign bit is set.
5746 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
5747 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
5748 ISD::SETLT);
5749
5750 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
5751 SDValue FudgePtr = DAG.getConstantPool(
5752 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
5753 getPointerTy());
5754
5755 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
5756 SDValue Zero = DAG.getIntPtrConstant(0);
5757 SDValue Four = DAG.getIntPtrConstant(4);
5758 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
5759 Zero, Four);
5760 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
5761
5762 // Load the value out, extending it from f32 to f80.
5763 // FIXME: Avoid the extend by constructing the right constant pool?
5764 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
5765 FudgePtr, PseudoSourceValue::getConstantPool(),
5766 0, MVT::f32, false, false, 4);
5767 // Extend everything to 80 bits to force it to be done on x87.
5768 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
5769 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00005770}
5771
Dan Gohman475871a2008-07-27 21:46:04 +00005772std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00005773FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005774 DebugLoc dl = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00005775
Owen Andersone50ed302009-08-10 22:56:29 +00005776 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00005777
5778 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005779 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
5780 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00005781 }
5782
Owen Anderson825b72b2009-08-11 20:47:22 +00005783 assert(DstTy.getSimpleVT() <= MVT::i64 &&
5784 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00005785 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00005786
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005787 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00005788 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00005789 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005790 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00005791 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00005792 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00005793 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005794 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005795
Evan Cheng87c89352007-10-15 20:11:21 +00005796 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
5797 // stack slot.
5798 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00005799 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00005800 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005801 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00005802
Evan Cheng0db9fe62006-04-25 20:13:52 +00005803 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00005804 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005805 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005806 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
5807 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
5808 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005809 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005810
Dan Gohman475871a2008-07-27 21:46:04 +00005811 SDValue Chain = DAG.getEntryNode();
5812 SDValue Value = Op.getOperand(0);
Chris Lattner78631162008-01-16 06:24:21 +00005813 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005814 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dale Johannesenace16102009-02-03 19:33:06 +00005815 Chain = DAG.getStore(Chain, dl, Value, StackSlot,
David Greene67c9d422010-02-15 16:53:33 +00005816 PseudoSourceValue::getFixedStack(SSFI), 0,
5817 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005818 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00005819 SDValue Ops[] = {
Chris Lattner5a88b832007-02-25 07:10:00 +00005820 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
5821 };
Dale Johannesenace16102009-02-03 19:33:06 +00005822 Value = DAG.getNode(X86ISD::FLD, dl, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005823 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00005824 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005825 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5826 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005827
Evan Cheng0db9fe62006-04-25 20:13:52 +00005828 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00005829 SDValue Ops[] = { Chain, Value, StackSlot };
Owen Anderson825b72b2009-08-11 20:47:22 +00005830 SDValue FIST = DAG.getNode(Opc, dl, MVT::Other, Ops, 3);
Evan Chengd9558e02006-01-06 00:43:03 +00005831
Chris Lattner27a6c732007-11-24 07:07:01 +00005832 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005833}
5834
Dan Gohmand858e902010-04-17 15:26:15 +00005835SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
5836 SelectionDAG &DAG) const {
Eli Friedman23ef1052009-06-06 03:57:58 +00005837 if (Op.getValueType().isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005838 if (Op.getValueType() == MVT::v2i32 &&
5839 Op.getOperand(0).getValueType() == MVT::v2f64) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005840 return Op;
5841 }
5842 return SDValue();
5843 }
5844
Eli Friedman948e95a2009-05-23 09:59:16 +00005845 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00005846 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00005847 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
5848 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00005849
Chris Lattner27a6c732007-11-24 07:07:01 +00005850 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005851 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
David Greene67c9d422010-02-15 16:53:33 +00005852 FIST, StackSlot, NULL, 0, false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00005853}
5854
Dan Gohmand858e902010-04-17 15:26:15 +00005855SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
5856 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00005857 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
5858 SDValue FIST = Vals.first, StackSlot = Vals.second;
5859 assert(FIST.getNode() && "Unexpected failure");
5860
5861 // Load the result.
5862 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
David Greene67c9d422010-02-15 16:53:33 +00005863 FIST, StackSlot, NULL, 0, false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00005864}
5865
Dan Gohmand858e902010-04-17 15:26:15 +00005866SDValue X86TargetLowering::LowerFABS(SDValue Op,
5867 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005868 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005869 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005870 EVT VT = Op.getValueType();
5871 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005872 if (VT.isVector())
5873 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005874 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005875 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005876 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00005877 CV.push_back(C);
5878 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005879 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005880 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00005881 CV.push_back(C);
5882 CV.push_back(C);
5883 CV.push_back(C);
5884 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005885 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005886 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005887 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005888 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005889 PseudoSourceValue::getConstantPool(), 0,
5890 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005891 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005892}
5893
Dan Gohmand858e902010-04-17 15:26:15 +00005894SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005895 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005896 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005897 EVT VT = Op.getValueType();
5898 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00005899 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00005900 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005901 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005902 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005903 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00005904 CV.push_back(C);
5905 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005906 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005907 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00005908 CV.push_back(C);
5909 CV.push_back(C);
5910 CV.push_back(C);
5911 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005912 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005913 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005914 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005915 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005916 PseudoSourceValue::getConstantPool(), 0,
5917 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005918 if (VT.isVector()) {
Dale Johannesenace16102009-02-03 19:33:06 +00005919 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00005920 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
5921 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005922 Op.getOperand(0)),
Owen Anderson825b72b2009-08-11 20:47:22 +00005923 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00005924 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005925 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00005926 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005927}
5928
Dan Gohmand858e902010-04-17 15:26:15 +00005929SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005930 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00005931 SDValue Op0 = Op.getOperand(0);
5932 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005933 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005934 EVT VT = Op.getValueType();
5935 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00005936
5937 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005938 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005939 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005940 SrcVT = VT;
5941 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005942 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005943 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005944 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005945 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005946 }
5947
5948 // At this point the operands and the result should have the same
5949 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00005950
Evan Cheng68c47cb2007-01-05 07:55:56 +00005951 // First get the sign bit of second operand.
5952 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005953 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005954 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
5955 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005956 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005957 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
5958 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5959 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5960 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005961 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005962 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005963 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005964 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005965 PseudoSourceValue::getConstantPool(), 0,
5966 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005967 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005968
5969 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005970 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005971 // Op0 is MVT::f32, Op1 is MVT::f64.
5972 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
5973 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
5974 DAG.getConstant(32, MVT::i32));
5975 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, SignBit);
5976 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00005977 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005978 }
5979
Evan Cheng73d6cf12007-01-05 21:37:56 +00005980 // Clear first operand sign bit.
5981 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00005982 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005983 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
5984 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005985 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005986 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
5987 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5988 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5989 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005990 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005991 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005992 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005993 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005994 PseudoSourceValue::getConstantPool(), 0,
5995 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005996 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005997
5998 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00005999 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00006000}
6001
Dan Gohman076aee32009-03-04 19:44:21 +00006002/// Emit nodes that will be selected as "test Op0,Op0", or something
6003/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00006004SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00006005 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00006006 DebugLoc dl = Op.getDebugLoc();
6007
Dan Gohman31125812009-03-07 01:58:32 +00006008 // CF and OF aren't always set the way we want. Determine which
6009 // of these we need.
6010 bool NeedCF = false;
6011 bool NeedOF = false;
6012 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006013 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00006014 case X86::COND_A: case X86::COND_AE:
6015 case X86::COND_B: case X86::COND_BE:
6016 NeedCF = true;
6017 break;
6018 case X86::COND_G: case X86::COND_GE:
6019 case X86::COND_L: case X86::COND_LE:
6020 case X86::COND_O: case X86::COND_NO:
6021 NeedOF = true;
6022 break;
Dan Gohman31125812009-03-07 01:58:32 +00006023 }
6024
Dan Gohman076aee32009-03-04 19:44:21 +00006025 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00006026 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
6027 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006028 if (Op.getResNo() != 0 || NeedOF || NeedCF)
6029 // Emit a CMP with 0, which is the TEST pattern.
6030 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
6031 DAG.getConstant(0, Op.getValueType()));
6032
6033 unsigned Opcode = 0;
6034 unsigned NumOperands = 0;
6035 switch (Op.getNode()->getOpcode()) {
6036 case ISD::ADD:
6037 // Due to an isel shortcoming, be conservative if this add is likely to be
6038 // selected as part of a load-modify-store instruction. When the root node
6039 // in a match is a store, isel doesn't know how to remap non-chain non-flag
6040 // uses of other nodes in the match, such as the ADD in this case. This
6041 // leads to the ADD being left around and reselected, with the result being
6042 // two adds in the output. Alas, even if none our users are stores, that
6043 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
6044 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
6045 // climbing the DAG back to the root, and it doesn't seem to be worth the
6046 // effort.
6047 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00006048 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006049 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
6050 goto default_case;
6051
6052 if (ConstantSDNode *C =
6053 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
6054 // An add of one will be selected as an INC.
6055 if (C->getAPIntValue() == 1) {
6056 Opcode = X86ISD::INC;
6057 NumOperands = 1;
6058 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00006059 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006060
6061 // An add of negative one (subtract of one) will be selected as a DEC.
6062 if (C->getAPIntValue().isAllOnesValue()) {
6063 Opcode = X86ISD::DEC;
6064 NumOperands = 1;
6065 break;
6066 }
Dan Gohman076aee32009-03-04 19:44:21 +00006067 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006068
6069 // Otherwise use a regular EFLAGS-setting add.
6070 Opcode = X86ISD::ADD;
6071 NumOperands = 2;
6072 break;
6073 case ISD::AND: {
6074 // If the primary and result isn't used, don't bother using X86ISD::AND,
6075 // because a TEST instruction will be better.
6076 bool NonFlagUse = false;
6077 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
6078 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
6079 SDNode *User = *UI;
6080 unsigned UOpNo = UI.getOperandNo();
6081 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
6082 // Look pass truncate.
6083 UOpNo = User->use_begin().getOperandNo();
6084 User = *User->use_begin();
6085 }
6086
6087 if (User->getOpcode() != ISD::BRCOND &&
6088 User->getOpcode() != ISD::SETCC &&
6089 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
6090 NonFlagUse = true;
6091 break;
6092 }
Dan Gohman076aee32009-03-04 19:44:21 +00006093 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006094
6095 if (!NonFlagUse)
6096 break;
6097 }
6098 // FALL THROUGH
6099 case ISD::SUB:
6100 case ISD::OR:
6101 case ISD::XOR:
6102 // Due to the ISEL shortcoming noted above, be conservative if this op is
6103 // likely to be selected as part of a load-modify-store instruction.
6104 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
6105 UE = Op.getNode()->use_end(); UI != UE; ++UI)
6106 if (UI->getOpcode() == ISD::STORE)
6107 goto default_case;
6108
6109 // Otherwise use a regular EFLAGS-setting instruction.
6110 switch (Op.getNode()->getOpcode()) {
6111 default: llvm_unreachable("unexpected operator!");
6112 case ISD::SUB: Opcode = X86ISD::SUB; break;
6113 case ISD::OR: Opcode = X86ISD::OR; break;
6114 case ISD::XOR: Opcode = X86ISD::XOR; break;
6115 case ISD::AND: Opcode = X86ISD::AND; break;
6116 }
6117
6118 NumOperands = 2;
6119 break;
6120 case X86ISD::ADD:
6121 case X86ISD::SUB:
6122 case X86ISD::INC:
6123 case X86ISD::DEC:
6124 case X86ISD::OR:
6125 case X86ISD::XOR:
6126 case X86ISD::AND:
6127 return SDValue(Op.getNode(), 1);
6128 default:
6129 default_case:
6130 break;
Dan Gohman076aee32009-03-04 19:44:21 +00006131 }
6132
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006133 if (Opcode == 0)
6134 // Emit a CMP with 0, which is the TEST pattern.
6135 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
6136 DAG.getConstant(0, Op.getValueType()));
6137
6138 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
6139 SmallVector<SDValue, 4> Ops;
6140 for (unsigned i = 0; i != NumOperands; ++i)
6141 Ops.push_back(Op.getOperand(i));
6142
6143 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
6144 DAG.ReplaceAllUsesWith(Op, New);
6145 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00006146}
6147
6148/// Emit nodes that will be selected as "cmp Op0,Op1", or something
6149/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00006150SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00006151 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00006152 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
6153 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00006154 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00006155
6156 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00006157 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00006158}
6159
Evan Chengd40d03e2010-01-06 19:38:29 +00006160/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
6161/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00006162SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
6163 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00006164 SDValue Op0 = And.getOperand(0);
6165 SDValue Op1 = And.getOperand(1);
6166 if (Op0.getOpcode() == ISD::TRUNCATE)
6167 Op0 = Op0.getOperand(0);
6168 if (Op1.getOpcode() == ISD::TRUNCATE)
6169 Op1 = Op1.getOperand(0);
6170
Evan Chengd40d03e2010-01-06 19:38:29 +00006171 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00006172 if (Op1.getOpcode() == ISD::SHL)
6173 std::swap(Op0, Op1);
6174 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00006175 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
6176 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00006177 // If we looked past a truncate, check that it's only truncating away
6178 // known zeros.
6179 unsigned BitWidth = Op0.getValueSizeInBits();
6180 unsigned AndBitWidth = And.getValueSizeInBits();
6181 if (BitWidth > AndBitWidth) {
6182 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
6183 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
6184 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
6185 return SDValue();
6186 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00006187 LHS = Op1;
6188 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00006189 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00006190 } else if (Op1.getOpcode() == ISD::Constant) {
6191 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
6192 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00006193 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
6194 LHS = AndLHS.getOperand(0);
6195 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00006196 }
Evan Chengd40d03e2010-01-06 19:38:29 +00006197 }
Evan Cheng0488db92007-09-25 01:57:46 +00006198
Evan Chengd40d03e2010-01-06 19:38:29 +00006199 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00006200 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00006201 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00006202 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00006203 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00006204 // Also promote i16 to i32 for performance / code size reason.
6205 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00006206 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00006207 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00006208
Evan Chengd40d03e2010-01-06 19:38:29 +00006209 // If the operand types disagree, extend the shift amount to match. Since
6210 // BT ignores high bits (like shifts) we can use anyextend.
6211 if (LHS.getValueType() != RHS.getValueType())
6212 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00006213
Evan Chengd40d03e2010-01-06 19:38:29 +00006214 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
6215 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
6216 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6217 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00006218 }
6219
Evan Cheng54de3ea2010-01-05 06:52:31 +00006220 return SDValue();
6221}
6222
Dan Gohmand858e902010-04-17 15:26:15 +00006223SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng54de3ea2010-01-05 06:52:31 +00006224 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
6225 SDValue Op0 = Op.getOperand(0);
6226 SDValue Op1 = Op.getOperand(1);
6227 DebugLoc dl = Op.getDebugLoc();
6228 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
6229
6230 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00006231 // Lower (X & (1 << N)) == 0 to BT(X, N).
6232 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
6233 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
6234 if (Op0.getOpcode() == ISD::AND &&
6235 Op0.hasOneUse() &&
6236 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00006237 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00006238 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
6239 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
6240 if (NewSetCC.getNode())
6241 return NewSetCC;
6242 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00006243
Evan Cheng2c755ba2010-02-27 07:36:59 +00006244 // Look for "(setcc) == / != 1" to avoid unncessary setcc.
6245 if (Op0.getOpcode() == X86ISD::SETCC &&
6246 Op1.getOpcode() == ISD::Constant &&
6247 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
6248 cast<ConstantSDNode>(Op1)->isNullValue()) &&
6249 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
6250 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
6251 bool Invert = (CC == ISD::SETNE) ^
6252 cast<ConstantSDNode>(Op1)->isNullValue();
6253 if (Invert)
6254 CCode = X86::GetOppositeBranchCondition(CCode);
6255 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6256 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
6257 }
6258
Evan Chenge5b51ac2010-04-17 06:13:15 +00006259 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00006260 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00006261 if (X86CC == X86::COND_INVALID)
6262 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00006263
Evan Cheng552f09a2010-04-26 19:06:11 +00006264 SDValue Cond = EmitCmp(Op0, Op1, X86CC, DAG);
Evan Chengad9c0a32009-12-15 00:53:42 +00006265
6266 // Use sbb x, x to materialize carry bit into a GPR.
Evan Cheng2e489c42009-12-16 00:53:11 +00006267 if (X86CC == X86::COND_B)
Evan Chengad9c0a32009-12-15 00:53:42 +00006268 return DAG.getNode(ISD::AND, dl, MVT::i8,
6269 DAG.getNode(X86ISD::SETCC_CARRY, dl, MVT::i8,
6270 DAG.getConstant(X86CC, MVT::i8), Cond),
6271 DAG.getConstant(1, MVT::i8));
Evan Chengad9c0a32009-12-15 00:53:42 +00006272
Owen Anderson825b72b2009-08-11 20:47:22 +00006273 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6274 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00006275}
6276
Dan Gohmand858e902010-04-17 15:26:15 +00006277SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00006278 SDValue Cond;
6279 SDValue Op0 = Op.getOperand(0);
6280 SDValue Op1 = Op.getOperand(1);
6281 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00006282 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00006283 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
6284 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006285 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00006286
6287 if (isFP) {
6288 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00006289 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00006290 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
6291 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00006292 bool Swap = false;
6293
6294 switch (SetCCOpcode) {
6295 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00006296 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00006297 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00006298 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00006299 case ISD::SETGT: Swap = true; // Fallthrough
6300 case ISD::SETLT:
6301 case ISD::SETOLT: SSECC = 1; break;
6302 case ISD::SETOGE:
6303 case ISD::SETGE: Swap = true; // Fallthrough
6304 case ISD::SETLE:
6305 case ISD::SETOLE: SSECC = 2; break;
6306 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00006307 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00006308 case ISD::SETNE: SSECC = 4; break;
6309 case ISD::SETULE: Swap = true;
6310 case ISD::SETUGE: SSECC = 5; break;
6311 case ISD::SETULT: Swap = true;
6312 case ISD::SETUGT: SSECC = 6; break;
6313 case ISD::SETO: SSECC = 7; break;
6314 }
6315 if (Swap)
6316 std::swap(Op0, Op1);
6317
Nate Begemanfb8ead02008-07-25 19:05:58 +00006318 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00006319 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00006320 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00006321 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00006322 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
6323 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00006324 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00006325 }
6326 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00006327 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00006328 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
6329 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00006330 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00006331 }
Torok Edwinc23197a2009-07-14 16:55:14 +00006332 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00006333 }
6334 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00006335 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00006336 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006337
Nate Begeman30a0de92008-07-17 16:51:19 +00006338 // We are handling one of the integer comparisons here. Since SSE only has
6339 // GT and EQ comparisons for integer, swapping operands and multiple
6340 // operations may be required for some comparisons.
6341 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
6342 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00006343
Owen Anderson825b72b2009-08-11 20:47:22 +00006344 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00006345 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00006346 case MVT::v8i8:
6347 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
6348 case MVT::v4i16:
6349 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
6350 case MVT::v2i32:
6351 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
6352 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00006353 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006354
Nate Begeman30a0de92008-07-17 16:51:19 +00006355 switch (SetCCOpcode) {
6356 default: break;
6357 case ISD::SETNE: Invert = true;
6358 case ISD::SETEQ: Opc = EQOpc; break;
6359 case ISD::SETLT: Swap = true;
6360 case ISD::SETGT: Opc = GTOpc; break;
6361 case ISD::SETGE: Swap = true;
6362 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
6363 case ISD::SETULT: Swap = true;
6364 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
6365 case ISD::SETUGE: Swap = true;
6366 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
6367 }
6368 if (Swap)
6369 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006370
Nate Begeman30a0de92008-07-17 16:51:19 +00006371 // Since SSE has no unsigned integer comparisons, we need to flip the sign
6372 // bits of the inputs before performing those operations.
6373 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00006374 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00006375 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
6376 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00006377 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00006378 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
6379 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00006380 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
6381 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00006382 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006383
Dale Johannesenace16102009-02-03 19:33:06 +00006384 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00006385
6386 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00006387 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00006388 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00006389
Nate Begeman30a0de92008-07-17 16:51:19 +00006390 return Result;
6391}
Evan Cheng0488db92007-09-25 01:57:46 +00006392
Evan Cheng370e5342008-12-03 08:38:43 +00006393// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00006394static bool isX86LogicalCmp(SDValue Op) {
6395 unsigned Opc = Op.getNode()->getOpcode();
6396 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
6397 return true;
6398 if (Op.getResNo() == 1 &&
6399 (Opc == X86ISD::ADD ||
6400 Opc == X86ISD::SUB ||
6401 Opc == X86ISD::SMUL ||
6402 Opc == X86ISD::UMUL ||
6403 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00006404 Opc == X86ISD::DEC ||
6405 Opc == X86ISD::OR ||
6406 Opc == X86ISD::XOR ||
6407 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00006408 return true;
6409
6410 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00006411}
6412
Dan Gohmand858e902010-04-17 15:26:15 +00006413SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00006414 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00006415 SDValue Cond = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006416 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00006417 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00006418
Dan Gohman1a492952009-10-20 16:22:37 +00006419 if (Cond.getOpcode() == ISD::SETCC) {
6420 SDValue NewCond = LowerSETCC(Cond, DAG);
6421 if (NewCond.getNode())
6422 Cond = NewCond;
6423 }
Evan Cheng734503b2006-09-11 02:19:56 +00006424
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006425 // (select (x == 0), -1, 0) -> (sign_bit (x - 1))
6426 SDValue Op1 = Op.getOperand(1);
6427 SDValue Op2 = Op.getOperand(2);
6428 if (Cond.getOpcode() == X86ISD::SETCC &&
6429 cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue() == X86::COND_E) {
6430 SDValue Cmp = Cond.getOperand(1);
6431 if (Cmp.getOpcode() == X86ISD::CMP) {
6432 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op1);
6433 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
6434 ConstantSDNode *RHSC =
6435 dyn_cast<ConstantSDNode>(Cmp.getOperand(1).getNode());
6436 if (N1C && N1C->isAllOnesValue() &&
6437 N2C && N2C->isNullValue() &&
6438 RHSC && RHSC->isNullValue()) {
6439 SDValue CmpOp0 = Cmp.getOperand(0);
Chris Lattnerda0688e2010-03-14 18:44:35 +00006440 Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006441 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
6442 return DAG.getNode(X86ISD::SETCC_CARRY, dl, Op.getValueType(),
6443 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
6444 }
6445 }
6446 }
6447
Evan Chengad9c0a32009-12-15 00:53:42 +00006448 // Look pass (and (setcc_carry (cmp ...)), 1).
6449 if (Cond.getOpcode() == ISD::AND &&
6450 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
6451 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
6452 if (C && C->getAPIntValue() == 1)
6453 Cond = Cond.getOperand(0);
6454 }
6455
Evan Cheng3f41d662007-10-08 22:16:29 +00006456 // If condition flag is set by a X86ISD::CMP, then use it as the condition
6457 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00006458 if (Cond.getOpcode() == X86ISD::SETCC ||
6459 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00006460 CC = Cond.getOperand(0);
6461
Dan Gohman475871a2008-07-27 21:46:04 +00006462 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00006463 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00006464 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00006465
Evan Cheng3f41d662007-10-08 22:16:29 +00006466 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006467 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00006468 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00006469 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00006470
Chris Lattnerd1980a52009-03-12 06:52:53 +00006471 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
6472 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00006473 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00006474 addTest = false;
6475 }
6476 }
6477
6478 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00006479 // Look pass the truncate.
6480 if (Cond.getOpcode() == ISD::TRUNCATE)
6481 Cond = Cond.getOperand(0);
6482
6483 // We know the result of AND is compared against zero. Try to match
6484 // it to BT.
6485 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
6486 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
6487 if (NewSetCC.getNode()) {
6488 CC = NewSetCC.getOperand(0);
6489 Cond = NewSetCC.getOperand(1);
6490 addTest = false;
6491 }
6492 }
6493 }
6494
6495 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006496 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00006497 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00006498 }
6499
Evan Cheng0488db92007-09-25 01:57:46 +00006500 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
6501 // condition is true.
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006502 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
6503 SDValue Ops[] = { Op2, Op1, CC, Cond };
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006504 return DAG.getNode(X86ISD::CMOV, dl, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00006505}
6506
Evan Cheng370e5342008-12-03 08:38:43 +00006507// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
6508// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
6509// from the AND / OR.
6510static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
6511 Opc = Op.getOpcode();
6512 if (Opc != ISD::OR && Opc != ISD::AND)
6513 return false;
6514 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
6515 Op.getOperand(0).hasOneUse() &&
6516 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
6517 Op.getOperand(1).hasOneUse());
6518}
6519
Evan Cheng961d6d42009-02-02 08:19:07 +00006520// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
6521// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00006522static bool isXor1OfSetCC(SDValue Op) {
6523 if (Op.getOpcode() != ISD::XOR)
6524 return false;
6525 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6526 if (N1C && N1C->getAPIntValue() == 1) {
6527 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
6528 Op.getOperand(0).hasOneUse();
6529 }
6530 return false;
6531}
6532
Dan Gohmand858e902010-04-17 15:26:15 +00006533SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00006534 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00006535 SDValue Chain = Op.getOperand(0);
6536 SDValue Cond = Op.getOperand(1);
6537 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006538 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00006539 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00006540
Dan Gohman1a492952009-10-20 16:22:37 +00006541 if (Cond.getOpcode() == ISD::SETCC) {
6542 SDValue NewCond = LowerSETCC(Cond, DAG);
6543 if (NewCond.getNode())
6544 Cond = NewCond;
6545 }
Chris Lattnere55484e2008-12-25 05:34:37 +00006546#if 0
6547 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00006548 else if (Cond.getOpcode() == X86ISD::ADD ||
6549 Cond.getOpcode() == X86ISD::SUB ||
6550 Cond.getOpcode() == X86ISD::SMUL ||
6551 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00006552 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00006553#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00006554
Evan Chengad9c0a32009-12-15 00:53:42 +00006555 // Look pass (and (setcc_carry (cmp ...)), 1).
6556 if (Cond.getOpcode() == ISD::AND &&
6557 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
6558 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
6559 if (C && C->getAPIntValue() == 1)
6560 Cond = Cond.getOperand(0);
6561 }
6562
Evan Cheng3f41d662007-10-08 22:16:29 +00006563 // If condition flag is set by a X86ISD::CMP, then use it as the condition
6564 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00006565 if (Cond.getOpcode() == X86ISD::SETCC ||
6566 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00006567 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006568
Dan Gohman475871a2008-07-27 21:46:04 +00006569 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00006570 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00006571 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00006572 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00006573 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00006574 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00006575 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00006576 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00006577 default: break;
6578 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00006579 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00006580 // These can only come from an arithmetic instruction with overflow,
6581 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00006582 Cond = Cond.getNode()->getOperand(1);
6583 addTest = false;
6584 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00006585 }
Evan Cheng0488db92007-09-25 01:57:46 +00006586 }
Evan Cheng370e5342008-12-03 08:38:43 +00006587 } else {
6588 unsigned CondOpc;
6589 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
6590 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00006591 if (CondOpc == ISD::OR) {
6592 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
6593 // two branches instead of an explicit OR instruction with a
6594 // separate test.
6595 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00006596 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00006597 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006598 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00006599 Chain, Dest, CC, Cmp);
6600 CC = Cond.getOperand(1).getOperand(0);
6601 Cond = Cmp;
6602 addTest = false;
6603 }
6604 } else { // ISD::AND
6605 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
6606 // two branches instead of an explicit AND instruction with a
6607 // separate test. However, we only do this if this block doesn't
6608 // have a fall-through edge, because this requires an explicit
6609 // jmp when the condition is false.
6610 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00006611 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00006612 Op.getNode()->hasOneUse()) {
6613 X86::CondCode CCode =
6614 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
6615 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006616 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00006617 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00006618 // Look for an unconditional branch following this conditional branch.
6619 // We need this because we need to reverse the successors in order
6620 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00006621 if (User->getOpcode() == ISD::BR) {
6622 SDValue FalseBB = User->getOperand(1);
6623 SDNode *NewBR =
6624 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00006625 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00006626 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00006627 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00006628
Dale Johannesene4d209d2009-02-03 20:21:25 +00006629 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00006630 Chain, Dest, CC, Cmp);
6631 X86::CondCode CCode =
6632 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
6633 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006634 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00006635 Cond = Cmp;
6636 addTest = false;
6637 }
6638 }
Dan Gohman279c22e2008-10-21 03:29:32 +00006639 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00006640 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
6641 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
6642 // It should be transformed during dag combiner except when the condition
6643 // is set by a arithmetics with overflow node.
6644 X86::CondCode CCode =
6645 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
6646 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006647 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00006648 Cond = Cond.getOperand(0).getOperand(1);
6649 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00006650 }
Evan Cheng0488db92007-09-25 01:57:46 +00006651 }
6652
6653 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00006654 // Look pass the truncate.
6655 if (Cond.getOpcode() == ISD::TRUNCATE)
6656 Cond = Cond.getOperand(0);
6657
6658 // We know the result of AND is compared against zero. Try to match
6659 // it to BT.
6660 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
6661 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
6662 if (NewSetCC.getNode()) {
6663 CC = NewSetCC.getOperand(0);
6664 Cond = NewSetCC.getOperand(1);
6665 addTest = false;
6666 }
6667 }
6668 }
6669
6670 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006671 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00006672 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00006673 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00006674 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00006675 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00006676}
6677
Anton Korobeynikove060b532007-04-17 19:34:00 +00006678
6679// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
6680// Calls to _alloca is needed to probe the stack when allocating more than 4k
6681// bytes in one go. Touching the stack at 4K increments is necessary to ensure
6682// that the guard pages used by the OS virtual memory manager are allocated in
6683// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00006684SDValue
6685X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006686 SelectionDAG &DAG) const {
Anton Korobeynikove060b532007-04-17 19:34:00 +00006687 assert(Subtarget->isTargetCygMing() &&
6688 "This should be used only on Cygwin/Mingw targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006689 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006690
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006691 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00006692 SDValue Chain = Op.getOperand(0);
6693 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006694 // FIXME: Ensure alignment here
6695
Dan Gohman475871a2008-07-27 21:46:04 +00006696 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006697
Owen Anderson825b72b2009-08-11 20:47:22 +00006698 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006699
Dale Johannesendd64c412009-02-04 00:33:20 +00006700 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006701 Flag = Chain.getValue(1);
6702
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00006703 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006704
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00006705 Chain = DAG.getNode(X86ISD::MINGW_ALLOCA, dl, NodeTys, Chain, Flag);
6706 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006707
Dale Johannesendd64c412009-02-04 00:33:20 +00006708 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006709
Dan Gohman475871a2008-07-27 21:46:04 +00006710 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006711 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006712}
6713
Dan Gohmand858e902010-04-17 15:26:15 +00006714SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00006715 MachineFunction &MF = DAG.getMachineFunction();
6716 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
6717
Dan Gohman69de1932008-02-06 22:27:42 +00006718 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006719 DebugLoc dl = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00006720
Evan Cheng25ab6902006-09-08 06:48:29 +00006721 if (!Subtarget->is64Bit()) {
6722 // vastart just stores the address of the VarArgsFrameIndex slot into the
6723 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00006724 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
6725 getPointerTy());
David Greene67c9d422010-02-15 16:53:33 +00006726 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0,
6727 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006728 }
6729
6730 // __va_list_tag:
6731 // gp_offset (0 - 6 * 8)
6732 // fp_offset (48 - 48 + 8 * 16)
6733 // overflow_arg_area (point to parameters coming in memory).
6734 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00006735 SmallVector<SDValue, 8> MemOps;
6736 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00006737 // Store gp_offset
Dale Johannesene4d209d2009-02-03 20:21:25 +00006738 SDValue Store = DAG.getStore(Op.getOperand(0), dl,
Dan Gohman1e93df62010-04-17 14:41:14 +00006739 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
6740 MVT::i32),
David Greene67c9d422010-02-15 16:53:33 +00006741 FIN, SV, 0, false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006742 MemOps.push_back(Store);
6743
6744 // Store fp_offset
Scott Michelfdc40a02009-02-17 22:15:04 +00006745 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006746 FIN, DAG.getIntPtrConstant(4));
6747 Store = DAG.getStore(Op.getOperand(0), dl,
Dan Gohman1e93df62010-04-17 14:41:14 +00006748 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
6749 MVT::i32),
David Greene67c9d422010-02-15 16:53:33 +00006750 FIN, SV, 0, false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006751 MemOps.push_back(Store);
6752
6753 // Store ptr to overflow_arg_area
Scott Michelfdc40a02009-02-17 22:15:04 +00006754 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006755 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00006756 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
6757 getPointerTy());
David Greene67c9d422010-02-15 16:53:33 +00006758 Store = DAG.getStore(Op.getOperand(0), dl, OVFIN, FIN, SV, 0,
6759 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006760 MemOps.push_back(Store);
6761
6762 // Store ptr to reg_save_area.
Scott Michelfdc40a02009-02-17 22:15:04 +00006763 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006764 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00006765 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
6766 getPointerTy());
David Greene67c9d422010-02-15 16:53:33 +00006767 Store = DAG.getStore(Op.getOperand(0), dl, RSFIN, FIN, SV, 0,
6768 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006769 MemOps.push_back(Store);
Owen Anderson825b72b2009-08-11 20:47:22 +00006770 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006771 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00006772}
6773
Dan Gohmand858e902010-04-17 15:26:15 +00006774SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman9018e832008-05-10 01:26:14 +00006775 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
6776 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman9018e832008-05-10 01:26:14 +00006777
Chris Lattner75361b62010-04-07 22:58:41 +00006778 report_fatal_error("VAArgInst is not yet implemented for x86-64!");
Dan Gohman475871a2008-07-27 21:46:04 +00006779 return SDValue();
Dan Gohman9018e832008-05-10 01:26:14 +00006780}
6781
Dan Gohmand858e902010-04-17 15:26:15 +00006782SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00006783 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00006784 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00006785 SDValue Chain = Op.getOperand(0);
6786 SDValue DstPtr = Op.getOperand(1);
6787 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00006788 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
6789 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006790 DebugLoc dl = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00006791
Dale Johannesendd64c412009-02-04 00:33:20 +00006792 return DAG.getMemcpy(Chain, dl, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00006793 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
6794 false, DstSV, 0, SrcSV, 0);
Evan Chengae642192007-03-02 23:16:35 +00006795}
6796
Dan Gohman475871a2008-07-27 21:46:04 +00006797SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006798X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006799 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006800 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006801 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00006802 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00006803 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00006804 case Intrinsic::x86_sse_comieq_ss:
6805 case Intrinsic::x86_sse_comilt_ss:
6806 case Intrinsic::x86_sse_comile_ss:
6807 case Intrinsic::x86_sse_comigt_ss:
6808 case Intrinsic::x86_sse_comige_ss:
6809 case Intrinsic::x86_sse_comineq_ss:
6810 case Intrinsic::x86_sse_ucomieq_ss:
6811 case Intrinsic::x86_sse_ucomilt_ss:
6812 case Intrinsic::x86_sse_ucomile_ss:
6813 case Intrinsic::x86_sse_ucomigt_ss:
6814 case Intrinsic::x86_sse_ucomige_ss:
6815 case Intrinsic::x86_sse_ucomineq_ss:
6816 case Intrinsic::x86_sse2_comieq_sd:
6817 case Intrinsic::x86_sse2_comilt_sd:
6818 case Intrinsic::x86_sse2_comile_sd:
6819 case Intrinsic::x86_sse2_comigt_sd:
6820 case Intrinsic::x86_sse2_comige_sd:
6821 case Intrinsic::x86_sse2_comineq_sd:
6822 case Intrinsic::x86_sse2_ucomieq_sd:
6823 case Intrinsic::x86_sse2_ucomilt_sd:
6824 case Intrinsic::x86_sse2_ucomile_sd:
6825 case Intrinsic::x86_sse2_ucomigt_sd:
6826 case Intrinsic::x86_sse2_ucomige_sd:
6827 case Intrinsic::x86_sse2_ucomineq_sd: {
6828 unsigned Opc = 0;
6829 ISD::CondCode CC = ISD::SETCC_INVALID;
6830 switch (IntNo) {
6831 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006832 case Intrinsic::x86_sse_comieq_ss:
6833 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006834 Opc = X86ISD::COMI;
6835 CC = ISD::SETEQ;
6836 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006837 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006838 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006839 Opc = X86ISD::COMI;
6840 CC = ISD::SETLT;
6841 break;
6842 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006843 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006844 Opc = X86ISD::COMI;
6845 CC = ISD::SETLE;
6846 break;
6847 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006848 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006849 Opc = X86ISD::COMI;
6850 CC = ISD::SETGT;
6851 break;
6852 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006853 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006854 Opc = X86ISD::COMI;
6855 CC = ISD::SETGE;
6856 break;
6857 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006858 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006859 Opc = X86ISD::COMI;
6860 CC = ISD::SETNE;
6861 break;
6862 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006863 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006864 Opc = X86ISD::UCOMI;
6865 CC = ISD::SETEQ;
6866 break;
6867 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006868 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006869 Opc = X86ISD::UCOMI;
6870 CC = ISD::SETLT;
6871 break;
6872 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006873 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006874 Opc = X86ISD::UCOMI;
6875 CC = ISD::SETLE;
6876 break;
6877 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006878 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006879 Opc = X86ISD::UCOMI;
6880 CC = ISD::SETGT;
6881 break;
6882 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006883 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006884 Opc = X86ISD::UCOMI;
6885 CC = ISD::SETGE;
6886 break;
6887 case Intrinsic::x86_sse_ucomineq_ss:
6888 case Intrinsic::x86_sse2_ucomineq_sd:
6889 Opc = X86ISD::UCOMI;
6890 CC = ISD::SETNE;
6891 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006892 }
Evan Cheng734503b2006-09-11 02:19:56 +00006893
Dan Gohman475871a2008-07-27 21:46:04 +00006894 SDValue LHS = Op.getOperand(1);
6895 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00006896 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00006897 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00006898 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
6899 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6900 DAG.getConstant(X86CC, MVT::i8), Cond);
6901 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00006902 }
Eric Christopher71c67532009-07-29 00:28:05 +00006903 // ptest intrinsics. The intrinsic these come from are designed to return
Eric Christopher794bfed2009-07-29 01:01:19 +00006904 // an integer value, not just an instruction so lower it to the ptest
6905 // pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00006906 case Intrinsic::x86_sse41_ptestz:
6907 case Intrinsic::x86_sse41_ptestc:
6908 case Intrinsic::x86_sse41_ptestnzc:{
6909 unsigned X86CC = 0;
6910 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00006911 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Eric Christopher71c67532009-07-29 00:28:05 +00006912 case Intrinsic::x86_sse41_ptestz:
6913 // ZF = 1
6914 X86CC = X86::COND_E;
6915 break;
6916 case Intrinsic::x86_sse41_ptestc:
6917 // CF = 1
6918 X86CC = X86::COND_B;
6919 break;
Eric Christopherfd179292009-08-27 18:07:15 +00006920 case Intrinsic::x86_sse41_ptestnzc:
Eric Christopher71c67532009-07-29 00:28:05 +00006921 // ZF and CF = 0
6922 X86CC = X86::COND_A;
6923 break;
6924 }
Eric Christopherfd179292009-08-27 18:07:15 +00006925
Eric Christopher71c67532009-07-29 00:28:05 +00006926 SDValue LHS = Op.getOperand(1);
6927 SDValue RHS = Op.getOperand(2);
Owen Anderson825b72b2009-08-11 20:47:22 +00006928 SDValue Test = DAG.getNode(X86ISD::PTEST, dl, MVT::i32, LHS, RHS);
6929 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
6930 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
6931 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00006932 }
Evan Cheng5759f972008-05-04 09:15:50 +00006933
6934 // Fix vector shift instructions where the last operand is a non-immediate
6935 // i32 value.
6936 case Intrinsic::x86_sse2_pslli_w:
6937 case Intrinsic::x86_sse2_pslli_d:
6938 case Intrinsic::x86_sse2_pslli_q:
6939 case Intrinsic::x86_sse2_psrli_w:
6940 case Intrinsic::x86_sse2_psrli_d:
6941 case Intrinsic::x86_sse2_psrli_q:
6942 case Intrinsic::x86_sse2_psrai_w:
6943 case Intrinsic::x86_sse2_psrai_d:
6944 case Intrinsic::x86_mmx_pslli_w:
6945 case Intrinsic::x86_mmx_pslli_d:
6946 case Intrinsic::x86_mmx_pslli_q:
6947 case Intrinsic::x86_mmx_psrli_w:
6948 case Intrinsic::x86_mmx_psrli_d:
6949 case Intrinsic::x86_mmx_psrli_q:
6950 case Intrinsic::x86_mmx_psrai_w:
6951 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00006952 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00006953 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00006954 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00006955
6956 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00006957 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00006958 switch (IntNo) {
6959 case Intrinsic::x86_sse2_pslli_w:
6960 NewIntNo = Intrinsic::x86_sse2_psll_w;
6961 break;
6962 case Intrinsic::x86_sse2_pslli_d:
6963 NewIntNo = Intrinsic::x86_sse2_psll_d;
6964 break;
6965 case Intrinsic::x86_sse2_pslli_q:
6966 NewIntNo = Intrinsic::x86_sse2_psll_q;
6967 break;
6968 case Intrinsic::x86_sse2_psrli_w:
6969 NewIntNo = Intrinsic::x86_sse2_psrl_w;
6970 break;
6971 case Intrinsic::x86_sse2_psrli_d:
6972 NewIntNo = Intrinsic::x86_sse2_psrl_d;
6973 break;
6974 case Intrinsic::x86_sse2_psrli_q:
6975 NewIntNo = Intrinsic::x86_sse2_psrl_q;
6976 break;
6977 case Intrinsic::x86_sse2_psrai_w:
6978 NewIntNo = Intrinsic::x86_sse2_psra_w;
6979 break;
6980 case Intrinsic::x86_sse2_psrai_d:
6981 NewIntNo = Intrinsic::x86_sse2_psra_d;
6982 break;
6983 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00006984 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00006985 switch (IntNo) {
6986 case Intrinsic::x86_mmx_pslli_w:
6987 NewIntNo = Intrinsic::x86_mmx_psll_w;
6988 break;
6989 case Intrinsic::x86_mmx_pslli_d:
6990 NewIntNo = Intrinsic::x86_mmx_psll_d;
6991 break;
6992 case Intrinsic::x86_mmx_pslli_q:
6993 NewIntNo = Intrinsic::x86_mmx_psll_q;
6994 break;
6995 case Intrinsic::x86_mmx_psrli_w:
6996 NewIntNo = Intrinsic::x86_mmx_psrl_w;
6997 break;
6998 case Intrinsic::x86_mmx_psrli_d:
6999 NewIntNo = Intrinsic::x86_mmx_psrl_d;
7000 break;
7001 case Intrinsic::x86_mmx_psrli_q:
7002 NewIntNo = Intrinsic::x86_mmx_psrl_q;
7003 break;
7004 case Intrinsic::x86_mmx_psrai_w:
7005 NewIntNo = Intrinsic::x86_mmx_psra_w;
7006 break;
7007 case Intrinsic::x86_mmx_psrai_d:
7008 NewIntNo = Intrinsic::x86_mmx_psra_d;
7009 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00007010 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00007011 }
7012 break;
7013 }
7014 }
Mon P Wangefa42202009-09-03 19:56:25 +00007015
7016 // The vector shift intrinsics with scalars uses 32b shift amounts but
7017 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
7018 // to be zero.
7019 SDValue ShOps[4];
7020 ShOps[0] = ShAmt;
7021 ShOps[1] = DAG.getConstant(0, MVT::i32);
7022 if (ShAmtVT == MVT::v4i32) {
7023 ShOps[2] = DAG.getUNDEF(MVT::i32);
7024 ShOps[3] = DAG.getUNDEF(MVT::i32);
7025 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
7026 } else {
7027 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
7028 }
7029
Owen Andersone50ed302009-08-10 22:56:29 +00007030 EVT VT = Op.getValueType();
Mon P Wangefa42202009-09-03 19:56:25 +00007031 ShAmt = DAG.getNode(ISD::BIT_CONVERT, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007032 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007033 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00007034 Op.getOperand(1), ShAmt);
7035 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00007036 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007037}
Evan Cheng72261582005-12-20 06:22:03 +00007038
Dan Gohmand858e902010-04-17 15:26:15 +00007039SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
7040 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00007041 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7042 MFI->setReturnAddressIsTaken(true);
7043
Bill Wendling64e87322009-01-16 19:25:27 +00007044 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007045 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00007046
7047 if (Depth > 0) {
7048 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
7049 SDValue Offset =
7050 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00007051 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007052 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00007053 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007054 FrameAddr, Offset),
David Greene67c9d422010-02-15 16:53:33 +00007055 NULL, 0, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00007056 }
7057
7058 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00007059 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00007060 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
David Greene67c9d422010-02-15 16:53:33 +00007061 RetAddrFI, NULL, 0, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00007062}
7063
Dan Gohmand858e902010-04-17 15:26:15 +00007064SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00007065 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7066 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00007067
Owen Andersone50ed302009-08-10 22:56:29 +00007068 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007069 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00007070 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
7071 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00007072 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00007073 while (Depth--)
David Greene67c9d422010-02-15 16:53:33 +00007074 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0,
7075 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00007076 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00007077}
7078
Dan Gohman475871a2008-07-27 21:46:04 +00007079SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007080 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00007081 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007082}
7083
Dan Gohmand858e902010-04-17 15:26:15 +00007084SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007085 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00007086 SDValue Chain = Op.getOperand(0);
7087 SDValue Offset = Op.getOperand(1);
7088 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007089 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007090
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007091 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
7092 getPointerTy());
7093 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007094
Dale Johannesene4d209d2009-02-03 20:21:25 +00007095 SDValue StoreAddr = DAG.getNode(ISD::SUB, dl, getPointerTy(), Frame,
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00007096 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007097 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
David Greene67c9d422010-02-15 16:53:33 +00007098 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, NULL, 0, false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00007099 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007100 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007101
Dale Johannesene4d209d2009-02-03 20:21:25 +00007102 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007103 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007104 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007105}
7106
Dan Gohman475871a2008-07-27 21:46:04 +00007107SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007108 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007109 SDValue Root = Op.getOperand(0);
7110 SDValue Trmp = Op.getOperand(1); // trampoline
7111 SDValue FPtr = Op.getOperand(2); // nested function
7112 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007113 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00007114
Dan Gohman69de1932008-02-06 22:27:42 +00007115 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00007116
7117 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00007118 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00007119
7120 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00007121 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
7122 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00007123
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00007124 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
7125 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00007126
7127 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
7128
7129 // Load the pointer to the nested function into R11.
7130 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00007131 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00007132 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
David Greene67c9d422010-02-15 16:53:33 +00007133 Addr, TrmpAddr, 0, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007134
Owen Anderson825b72b2009-08-11 20:47:22 +00007135 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7136 DAG.getConstant(2, MVT::i64));
David Greene67c9d422010-02-15 16:53:33 +00007137 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr, TrmpAddr, 2,
7138 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00007139
7140 // Load the 'nest' parameter value into R10.
7141 // R10 is specified in X86CallingConv.td
7142 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00007143 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7144 DAG.getConstant(10, MVT::i64));
7145 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
David Greene67c9d422010-02-15 16:53:33 +00007146 Addr, TrmpAddr, 10, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007147
Owen Anderson825b72b2009-08-11 20:47:22 +00007148 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7149 DAG.getConstant(12, MVT::i64));
David Greene67c9d422010-02-15 16:53:33 +00007150 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 12,
7151 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00007152
7153 // Jump to the nested function.
7154 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00007155 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7156 DAG.getConstant(20, MVT::i64));
7157 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
David Greene67c9d422010-02-15 16:53:33 +00007158 Addr, TrmpAddr, 20, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007159
7160 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00007161 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7162 DAG.getConstant(22, MVT::i64));
7163 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
David Greene67c9d422010-02-15 16:53:33 +00007164 TrmpAddr, 22, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007165
Dan Gohman475871a2008-07-27 21:46:04 +00007166 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00007167 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007168 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007169 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00007170 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00007171 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00007172 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00007173 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007174
7175 switch (CC) {
7176 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00007177 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00007178 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00007179 case CallingConv::X86_StdCall: {
7180 // Pass 'nest' parameter in ECX.
7181 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00007182 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007183
7184 // Check that ECX wasn't needed by an 'inreg' parameter.
7185 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00007186 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00007187
Chris Lattner58d74912008-03-12 17:45:29 +00007188 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00007189 unsigned InRegCount = 0;
7190 unsigned Idx = 1;
7191
7192 for (FunctionType::param_iterator I = FTy->param_begin(),
7193 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00007194 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00007195 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00007196 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007197
7198 if (InRegCount > 2) {
Chris Lattner75361b62010-04-07 22:58:41 +00007199 report_fatal_error("Nest register in use - reduce number of inreg parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00007200 }
7201 }
7202 break;
7203 }
7204 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00007205 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00007206 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00007207 // Pass 'nest' parameter in EAX.
7208 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00007209 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007210 break;
7211 }
7212
Dan Gohman475871a2008-07-27 21:46:04 +00007213 SDValue OutChains[4];
7214 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007215
Owen Anderson825b72b2009-08-11 20:47:22 +00007216 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7217 DAG.getConstant(10, MVT::i32));
7218 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007219
Chris Lattnera62fe662010-02-05 19:20:30 +00007220 // This is storing the opcode for MOV32ri.
7221 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00007222 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00007223 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007224 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
David Greene67c9d422010-02-15 16:53:33 +00007225 Trmp, TrmpAddr, 0, false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007226
Owen Anderson825b72b2009-08-11 20:47:22 +00007227 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7228 DAG.getConstant(1, MVT::i32));
David Greene67c9d422010-02-15 16:53:33 +00007229 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 1,
7230 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007231
Chris Lattnera62fe662010-02-05 19:20:30 +00007232 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00007233 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7234 DAG.getConstant(5, MVT::i32));
7235 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
David Greene67c9d422010-02-15 16:53:33 +00007236 TrmpAddr, 5, false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007237
Owen Anderson825b72b2009-08-11 20:47:22 +00007238 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7239 DAG.getConstant(6, MVT::i32));
David Greene67c9d422010-02-15 16:53:33 +00007240 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr, TrmpAddr, 6,
7241 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007242
Dan Gohman475871a2008-07-27 21:46:04 +00007243 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00007244 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007245 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007246 }
7247}
7248
Dan Gohmand858e902010-04-17 15:26:15 +00007249SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
7250 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007251 /*
7252 The rounding mode is in bits 11:10 of FPSR, and has the following
7253 settings:
7254 00 Round to nearest
7255 01 Round to -inf
7256 10 Round to +inf
7257 11 Round to 0
7258
7259 FLT_ROUNDS, on the other hand, expects the following:
7260 -1 Undefined
7261 0 Round to 0
7262 1 Round to nearest
7263 2 Round to +inf
7264 3 Round to -inf
7265
7266 To perform the conversion, we do:
7267 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
7268 */
7269
7270 MachineFunction &MF = DAG.getMachineFunction();
7271 const TargetMachine &TM = MF.getTarget();
7272 const TargetFrameInfo &TFI = *TM.getFrameInfo();
7273 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00007274 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007275 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007276
7277 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00007278 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007279 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007280
Owen Anderson825b72b2009-08-11 20:47:22 +00007281 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, dl, MVT::Other,
Evan Cheng8a186ae2008-09-24 23:26:36 +00007282 DAG.getEntryNode(), StackSlot);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007283
7284 // Load FP Control Word from stack slot
David Greene67c9d422010-02-15 16:53:33 +00007285 SDValue CWD = DAG.getLoad(MVT::i16, dl, Chain, StackSlot, NULL, 0,
7286 false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007287
7288 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00007289 SDValue CWD1 =
Owen Anderson825b72b2009-08-11 20:47:22 +00007290 DAG.getNode(ISD::SRL, dl, MVT::i16,
7291 DAG.getNode(ISD::AND, dl, MVT::i16,
7292 CWD, DAG.getConstant(0x800, MVT::i16)),
7293 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00007294 SDValue CWD2 =
Owen Anderson825b72b2009-08-11 20:47:22 +00007295 DAG.getNode(ISD::SRL, dl, MVT::i16,
7296 DAG.getNode(ISD::AND, dl, MVT::i16,
7297 CWD, DAG.getConstant(0x400, MVT::i16)),
7298 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007299
Dan Gohman475871a2008-07-27 21:46:04 +00007300 SDValue RetVal =
Owen Anderson825b72b2009-08-11 20:47:22 +00007301 DAG.getNode(ISD::AND, dl, MVT::i16,
7302 DAG.getNode(ISD::ADD, dl, MVT::i16,
7303 DAG.getNode(ISD::OR, dl, MVT::i16, CWD1, CWD2),
7304 DAG.getConstant(1, MVT::i16)),
7305 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007306
7307
Duncan Sands83ec4b62008-06-06 12:08:01 +00007308 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007309 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007310}
7311
Dan Gohmand858e902010-04-17 15:26:15 +00007312SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007313 EVT VT = Op.getValueType();
7314 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007315 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007316 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00007317
7318 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007319 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00007320 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00007321 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007322 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007323 }
Evan Cheng18efe262007-12-14 02:13:44 +00007324
Evan Cheng152804e2007-12-14 08:30:15 +00007325 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007326 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007327 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00007328
7329 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007330 SDValue Ops[] = {
7331 Op,
7332 DAG.getConstant(NumBits+NumBits-1, OpVT),
7333 DAG.getConstant(X86::COND_E, MVT::i8),
7334 Op.getValue(1)
7335 };
7336 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00007337
7338 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00007339 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00007340
Owen Anderson825b72b2009-08-11 20:47:22 +00007341 if (VT == MVT::i8)
7342 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007343 return Op;
7344}
7345
Dan Gohmand858e902010-04-17 15:26:15 +00007346SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007347 EVT VT = Op.getValueType();
7348 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007349 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007350 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00007351
7352 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007353 if (VT == MVT::i8) {
7354 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007355 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007356 }
Evan Cheng152804e2007-12-14 08:30:15 +00007357
7358 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007359 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007360 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00007361
7362 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007363 SDValue Ops[] = {
7364 Op,
7365 DAG.getConstant(NumBits, OpVT),
7366 DAG.getConstant(X86::COND_E, MVT::i8),
7367 Op.getValue(1)
7368 };
7369 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00007370
Owen Anderson825b72b2009-08-11 20:47:22 +00007371 if (VT == MVT::i8)
7372 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007373 return Op;
7374}
7375
Dan Gohmand858e902010-04-17 15:26:15 +00007376SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007377 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007378 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007379 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00007380
Mon P Wangaf9b9522008-12-18 21:42:19 +00007381 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
7382 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
7383 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
7384 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
7385 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
7386 //
7387 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
7388 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
7389 // return AloBlo + AloBhi + AhiBlo;
7390
7391 SDValue A = Op.getOperand(0);
7392 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007393
Dale Johannesene4d209d2009-02-03 20:21:25 +00007394 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007395 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7396 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007397 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007398 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7399 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007400 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007401 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007402 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007403 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007404 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007405 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007406 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007407 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007408 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007409 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007410 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7411 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007412 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007413 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7414 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007415 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
7416 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00007417 return Res;
7418}
7419
7420
Dan Gohmand858e902010-04-17 15:26:15 +00007421SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +00007422 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
7423 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00007424 // looks for this combo and may remove the "setcc" instruction if the "setcc"
7425 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00007426 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00007427 SDValue LHS = N->getOperand(0);
7428 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00007429 unsigned BaseOp = 0;
7430 unsigned Cond = 0;
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007431 DebugLoc dl = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00007432
7433 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007434 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00007435 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00007436 // A subtract of one will be selected as a INC. Note that INC doesn't
7437 // set CF, so we can't do this for UADDO.
7438 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
7439 if (C->getAPIntValue() == 1) {
7440 BaseOp = X86ISD::INC;
7441 Cond = X86::COND_O;
7442 break;
7443 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007444 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00007445 Cond = X86::COND_O;
7446 break;
7447 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007448 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00007449 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007450 break;
7451 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00007452 // A subtract of one will be selected as a DEC. Note that DEC doesn't
7453 // set CF, so we can't do this for USUBO.
7454 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
7455 if (C->getAPIntValue() == 1) {
7456 BaseOp = X86ISD::DEC;
7457 Cond = X86::COND_O;
7458 break;
7459 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007460 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00007461 Cond = X86::COND_O;
7462 break;
7463 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007464 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00007465 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007466 break;
7467 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00007468 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00007469 Cond = X86::COND_O;
7470 break;
7471 case ISD::UMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00007472 BaseOp = X86ISD::UMUL;
Dan Gohman653456c2009-01-07 00:15:08 +00007473 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007474 break;
7475 }
Bill Wendling3fafd932008-11-26 22:37:40 +00007476
Bill Wendling61edeb52008-12-02 01:06:39 +00007477 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007478 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007479 SDValue Sum = DAG.getNode(BaseOp, dl, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00007480
Bill Wendling61edeb52008-12-02 01:06:39 +00007481 SDValue SetCC =
Dale Johannesene4d209d2009-02-03 20:21:25 +00007482 DAG.getNode(X86ISD::SETCC, dl, N->getValueType(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00007483 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00007484
Bill Wendling61edeb52008-12-02 01:06:39 +00007485 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
7486 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00007487}
7488
Dan Gohmand858e902010-04-17 15:26:15 +00007489SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007490 EVT T = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007491 DebugLoc dl = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00007492 unsigned Reg = 0;
7493 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00007494 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007495 default:
7496 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007497 case MVT::i8: Reg = X86::AL; size = 1; break;
7498 case MVT::i16: Reg = X86::AX; size = 2; break;
7499 case MVT::i32: Reg = X86::EAX; size = 4; break;
7500 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00007501 assert(Subtarget->is64Bit() && "Node not type legal!");
7502 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00007503 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007504 }
Dale Johannesendd64c412009-02-04 00:33:20 +00007505 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), dl, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00007506 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00007507 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00007508 Op.getOperand(1),
7509 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00007510 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00007511 cpIn.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00007512 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007513 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, dl, Tys, Ops, 5);
Scott Michelfdc40a02009-02-17 22:15:04 +00007514 SDValue cpOut =
Dale Johannesendd64c412009-02-04 00:33:20 +00007515 DAG.getCopyFromReg(Result.getValue(0), dl, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00007516 return cpOut;
7517}
7518
Duncan Sands1607f052008-12-01 11:39:25 +00007519SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007520 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +00007521 assert(Subtarget->is64Bit() && "Result not type legalized?");
Owen Anderson825b72b2009-08-11 20:47:22 +00007522 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00007523 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007524 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007525 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007526 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
7527 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00007528 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00007529 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
7530 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00007531 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00007532 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00007533 rdx.getValue(1)
7534 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007535 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007536}
7537
Dale Johannesen7d07b482010-05-21 00:52:33 +00007538SDValue X86TargetLowering::LowerBIT_CONVERT(SDValue Op,
7539 SelectionDAG &DAG) const {
7540 EVT SrcVT = Op.getOperand(0).getValueType();
7541 EVT DstVT = Op.getValueType();
7542 assert((Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
7543 Subtarget->hasMMX() && !DisableMMX) &&
7544 "Unexpected custom BIT_CONVERT");
7545 assert((DstVT == MVT::i64 ||
7546 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
7547 "Unexpected custom BIT_CONVERT");
7548 // i64 <=> MMX conversions are Legal.
7549 if (SrcVT==MVT::i64 && DstVT.isVector())
7550 return Op;
7551 if (DstVT==MVT::i64 && SrcVT.isVector())
7552 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +00007553 // MMX <=> MMX conversions are Legal.
7554 if (SrcVT.isVector() && DstVT.isVector())
7555 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +00007556 // All other conversions need to be expanded.
7557 return SDValue();
7558}
Dan Gohmand858e902010-04-17 15:26:15 +00007559SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +00007560 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007561 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007562 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007563 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00007564 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007565 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007566 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00007567 Node->getOperand(0),
7568 Node->getOperand(1), negOp,
7569 cast<AtomicSDNode>(Node)->getSrcValue(),
7570 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00007571}
7572
Evan Cheng0db9fe62006-04-25 20:13:52 +00007573/// LowerOperation - Provide custom lowering hooks for some operations.
7574///
Dan Gohmand858e902010-04-17 15:26:15 +00007575SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007576 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007577 default: llvm_unreachable("Should not custom lower this!");
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007578 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
7579 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007580 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00007581 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007582 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
7583 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
7584 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
7585 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
7586 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
7587 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007588 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00007589 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007590 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007591 case ISD::SHL_PARTS:
7592 case ISD::SRA_PARTS:
7593 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
7594 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007595 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007596 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00007597 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007598 case ISD::FABS: return LowerFABS(Op, DAG);
7599 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007600 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00007601 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00007602 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00007603 case ISD::SELECT: return LowerSELECT(Op, DAG);
7604 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007605 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007606 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00007607 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00007608 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007609 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00007610 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
7611 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007612 case ISD::FRAME_TO_ARGS_OFFSET:
7613 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007614 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007615 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007616 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00007617 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00007618 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
7619 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00007620 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00007621 case ISD::SADDO:
7622 case ISD::UADDO:
7623 case ISD::SSUBO:
7624 case ISD::USUBO:
7625 case ISD::SMULO:
7626 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00007627 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Dale Johannesen7d07b482010-05-21 00:52:33 +00007628 case ISD::BIT_CONVERT: return LowerBIT_CONVERT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007629 }
Chris Lattner27a6c732007-11-24 07:07:01 +00007630}
7631
Duncan Sands1607f052008-12-01 11:39:25 +00007632void X86TargetLowering::
7633ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00007634 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007635 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007636 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00007637 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00007638
7639 SDValue Chain = Node->getOperand(0);
7640 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007641 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007642 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007643 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007644 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00007645 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00007646 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00007647 SDValue Result =
7648 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
7649 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00007650 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00007651 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007652 Results.push_back(Result.getValue(2));
7653}
7654
Duncan Sands126d9072008-07-04 11:47:58 +00007655/// ReplaceNodeResults - Replace a node with an illegal result type
7656/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00007657void X86TargetLowering::ReplaceNodeResults(SDNode *N,
7658 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00007659 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007660 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00007661 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00007662 default:
Duncan Sands1607f052008-12-01 11:39:25 +00007663 assert(false && "Do not know how to custom type legalize this operation!");
7664 return;
7665 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00007666 std::pair<SDValue,SDValue> Vals =
7667 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00007668 SDValue FIST = Vals.first, StackSlot = Vals.second;
7669 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00007670 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00007671 // Return a load from the stack slot.
David Greene67c9d422010-02-15 16:53:33 +00007672 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot, NULL, 0,
7673 false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00007674 }
7675 return;
7676 }
7677 case ISD::READCYCLECOUNTER: {
Owen Anderson825b72b2009-08-11 20:47:22 +00007678 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00007679 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007680 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007681 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00007682 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007683 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007684 eax.getValue(2));
7685 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
7686 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00007687 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007688 Results.push_back(edx.getValue(1));
7689 return;
7690 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007691 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00007692 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007693 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00007694 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00007695 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
7696 DAG.getConstant(0, MVT::i32));
7697 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
7698 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00007699 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
7700 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00007701 cpInL.getValue(1));
7702 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00007703 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
7704 DAG.getConstant(0, MVT::i32));
7705 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
7706 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00007707 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00007708 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00007709 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00007710 swapInL.getValue(1));
7711 SDValue Ops[] = { swapInH.getValue(0),
7712 N->getOperand(1),
7713 swapInH.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00007714 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007715 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, dl, Tys, Ops, 3);
Dale Johannesendd64c412009-02-04 00:33:20 +00007716 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00007717 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00007718 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00007719 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00007720 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00007721 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007722 Results.push_back(cpOutH.getValue(1));
7723 return;
7724 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007725 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00007726 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
7727 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007728 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00007729 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
7730 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007731 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00007732 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
7733 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007734 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00007735 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
7736 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007737 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00007738 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
7739 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007740 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00007741 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
7742 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007743 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00007744 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
7745 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00007746 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007747}
7748
Evan Cheng72261582005-12-20 06:22:03 +00007749const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
7750 switch (Opcode) {
7751 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00007752 case X86ISD::BSF: return "X86ISD::BSF";
7753 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00007754 case X86ISD::SHLD: return "X86ISD::SHLD";
7755 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00007756 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00007757 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00007758 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00007759 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00007760 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00007761 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00007762 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
7763 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
7764 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00007765 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00007766 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00007767 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00007768 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00007769 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00007770 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00007771 case X86ISD::COMI: return "X86ISD::COMI";
7772 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00007773 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00007774 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Evan Cheng72261582005-12-20 06:22:03 +00007775 case X86ISD::CMOV: return "X86ISD::CMOV";
7776 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00007777 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00007778 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
7779 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00007780 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00007781 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00007782 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00007783 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00007784 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00007785 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
7786 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00007787 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007788 case X86ISD::MMX_PINSRW: return "X86ISD::MMX_PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00007789 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Evan Cheng8ca29322006-11-10 21:43:37 +00007790 case X86ISD::FMAX: return "X86ISD::FMAX";
7791 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00007792 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
7793 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007794 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +00007795 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Rafael Espindola094fad32009-04-08 21:14:34 +00007796 case X86ISD::SegmentBaseAddress: return "X86ISD::SegmentBaseAddress";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007797 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00007798 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007799 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00007800 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
7801 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007802 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
7803 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
7804 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
7805 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
7806 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
7807 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00007808 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
7809 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00007810 case X86ISD::VSHL: return "X86ISD::VSHL";
7811 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00007812 case X86ISD::CMPPD: return "X86ISD::CMPPD";
7813 case X86ISD::CMPPS: return "X86ISD::CMPPS";
7814 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
7815 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
7816 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
7817 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
7818 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
7819 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
7820 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
7821 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007822 case X86ISD::ADD: return "X86ISD::ADD";
7823 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingd350e022008-12-12 21:15:41 +00007824 case X86ISD::SMUL: return "X86ISD::SMUL";
7825 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00007826 case X86ISD::INC: return "X86ISD::INC";
7827 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00007828 case X86ISD::OR: return "X86ISD::OR";
7829 case X86ISD::XOR: return "X86ISD::XOR";
7830 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00007831 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00007832 case X86ISD::PTEST: return "X86ISD::PTEST";
Dan Gohmand6708ea2009-08-15 01:38:56 +00007833 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00007834 case X86ISD::MINGW_ALLOCA: return "X86ISD::MINGW_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00007835 }
7836}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007837
Chris Lattnerc9addb72007-03-30 23:15:24 +00007838// isLegalAddressingMode - Return true if the addressing mode represented
7839// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00007840bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00007841 const Type *Ty) const {
7842 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007843 CodeModel::Model M = getTargetMachine().getCodeModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00007844
Chris Lattnerc9addb72007-03-30 23:15:24 +00007845 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007846 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00007847 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007848
Chris Lattnerc9addb72007-03-30 23:15:24 +00007849 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00007850 unsigned GVFlags =
7851 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007852
Chris Lattnerdfed4132009-07-10 07:38:24 +00007853 // If a reference to this global requires an extra load, we can't fold it.
7854 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00007855 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007856
Chris Lattnerdfed4132009-07-10 07:38:24 +00007857 // If BaseGV requires a register for the PIC base, we cannot also have a
7858 // BaseReg specified.
7859 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00007860 return false;
Evan Cheng52787842007-08-01 23:46:47 +00007861
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007862 // If lower 4G is not available, then we must use rip-relative addressing.
7863 if (Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
7864 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00007865 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007866
Chris Lattnerc9addb72007-03-30 23:15:24 +00007867 switch (AM.Scale) {
7868 case 0:
7869 case 1:
7870 case 2:
7871 case 4:
7872 case 8:
7873 // These scales always work.
7874 break;
7875 case 3:
7876 case 5:
7877 case 9:
7878 // These scales are formed with basereg+scalereg. Only accept if there is
7879 // no basereg yet.
7880 if (AM.HasBaseReg)
7881 return false;
7882 break;
7883 default: // Other stuff never works.
7884 return false;
7885 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007886
Chris Lattnerc9addb72007-03-30 23:15:24 +00007887 return true;
7888}
7889
7890
Evan Cheng2bd122c2007-10-26 01:56:11 +00007891bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00007892 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00007893 return false;
Evan Chenge127a732007-10-29 07:57:50 +00007894 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
7895 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00007896 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00007897 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00007898 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00007899}
7900
Owen Andersone50ed302009-08-10 22:56:29 +00007901bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007902 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007903 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007904 unsigned NumBits1 = VT1.getSizeInBits();
7905 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00007906 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007907 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00007908 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007909}
Evan Cheng2bd122c2007-10-26 01:56:11 +00007910
Dan Gohman97121ba2009-04-08 00:15:30 +00007911bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00007912 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00007913 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00007914}
7915
Owen Andersone50ed302009-08-10 22:56:29 +00007916bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00007917 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00007918 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00007919}
7920
Owen Andersone50ed302009-08-10 22:56:29 +00007921bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00007922 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00007923 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00007924}
7925
Evan Cheng60c07e12006-07-05 22:17:51 +00007926/// isShuffleMaskLegal - Targets can use this to indicate that they only
7927/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
7928/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
7929/// are assumed to be legal.
7930bool
Eric Christopherfd179292009-08-27 18:07:15 +00007931X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00007932 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +00007933 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +00007934 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +00007935 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +00007936
Nate Begemana09008b2009-10-19 02:17:23 +00007937 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00007938 return (VT.getVectorNumElements() == 2 ||
7939 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
7940 isMOVLMask(M, VT) ||
7941 isSHUFPMask(M, VT) ||
7942 isPSHUFDMask(M, VT) ||
7943 isPSHUFHWMask(M, VT) ||
7944 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00007945 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00007946 isUNPCKLMask(M, VT) ||
7947 isUNPCKHMask(M, VT) ||
7948 isUNPCKL_v_undef_Mask(M, VT) ||
7949 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00007950}
7951
Dan Gohman7d8143f2008-04-09 20:09:42 +00007952bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00007953X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00007954 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00007955 unsigned NumElts = VT.getVectorNumElements();
7956 // FIXME: This collection of masks seems suspect.
7957 if (NumElts == 2)
7958 return true;
7959 if (NumElts == 4 && VT.getSizeInBits() == 128) {
7960 return (isMOVLMask(Mask, VT) ||
7961 isCommutedMOVLMask(Mask, VT, true) ||
7962 isSHUFPMask(Mask, VT) ||
7963 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00007964 }
7965 return false;
7966}
7967
7968//===----------------------------------------------------------------------===//
7969// X86 Scheduler Hooks
7970//===----------------------------------------------------------------------===//
7971
Mon P Wang63307c32008-05-05 19:05:59 +00007972// private utility function
7973MachineBasicBlock *
7974X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
7975 MachineBasicBlock *MBB,
7976 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007977 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007978 unsigned LoadOpc,
7979 unsigned CXchgOpc,
7980 unsigned copyOpc,
7981 unsigned notOpc,
7982 unsigned EAXreg,
7983 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007984 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00007985 // For the atomic bitwise operator, we generate
7986 // thisMBB:
7987 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00007988 // ld t1 = [bitinstr.addr]
7989 // op t2 = t1, [bitinstr.val]
7990 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00007991 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
7992 // bz newMBB
7993 // fallthrough -->nextMBB
7994 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7995 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007996 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00007997 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007998
Mon P Wang63307c32008-05-05 19:05:59 +00007999 /// First build the CFG
8000 MachineFunction *F = MBB->getParent();
8001 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008002 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
8003 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
8004 F->insert(MBBIter, newMBB);
8005 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008006
Mon P Wang63307c32008-05-05 19:05:59 +00008007 // Move all successors to thisMBB to nextMBB
8008 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008009
Mon P Wang63307c32008-05-05 19:05:59 +00008010 // Update thisMBB to fall through to newMBB
8011 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008012
Mon P Wang63307c32008-05-05 19:05:59 +00008013 // newMBB jumps to itself and fall through to nextMBB
8014 newMBB->addSuccessor(nextMBB);
8015 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008016
Mon P Wang63307c32008-05-05 19:05:59 +00008017 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008018 assert(bInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00008019 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00008020 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00008021 MachineOperand& destOper = bInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008022 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00008023 int numArgs = bInstr->getNumOperands() - 1;
8024 for (int i=0; i < numArgs; ++i)
8025 argOpers[i] = &bInstr->getOperand(i+1);
8026
8027 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008028 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
8029 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00008030
Dale Johannesen140be2d2008-08-19 18:47:28 +00008031 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008032 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00008033 for (int i=0; i <= lastAddrIndx; ++i)
8034 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008035
Dale Johannesen140be2d2008-08-19 18:47:28 +00008036 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008037 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00008038 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008039 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008040 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008041 tt = t1;
8042
Dale Johannesen140be2d2008-08-19 18:47:28 +00008043 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00008044 assert((argOpers[valArgIndx]->isReg() ||
8045 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00008046 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00008047 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008048 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00008049 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008050 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008051 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00008052 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008053
Dale Johannesene4d209d2009-02-03 20:21:25 +00008054 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00008055 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008056
Dale Johannesene4d209d2009-02-03 20:21:25 +00008057 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00008058 for (int i=0; i <= lastAddrIndx; ++i)
8059 (*MIB).addOperand(*argOpers[i]);
8060 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00008061 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00008062 (*MIB).setMemRefs(bInstr->memoperands_begin(),
8063 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00008064
Dale Johannesene4d209d2009-02-03 20:21:25 +00008065 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00008066 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00008067
Mon P Wang63307c32008-05-05 19:05:59 +00008068 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008069 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00008070
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008071 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00008072 return nextMBB;
8073}
8074
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00008075// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00008076MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008077X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
8078 MachineBasicBlock *MBB,
8079 unsigned regOpcL,
8080 unsigned regOpcH,
8081 unsigned immOpcL,
8082 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00008083 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008084 // For the atomic bitwise operator, we generate
8085 // thisMBB (instructions are in pairs, except cmpxchg8b)
8086 // ld t1,t2 = [bitinstr.addr]
8087 // newMBB:
8088 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
8089 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00008090 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008091 // mov ECX, EBX <- t5, t6
8092 // mov EAX, EDX <- t1, t2
8093 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
8094 // mov t3, t4 <- EAX, EDX
8095 // bz newMBB
8096 // result in out1, out2
8097 // fallthrough -->nextMBB
8098
8099 const TargetRegisterClass *RC = X86::GR32RegisterClass;
8100 const unsigned LoadOpc = X86::MOV32rm;
8101 const unsigned copyOpc = X86::MOV32rr;
8102 const unsigned NotOpc = X86::NOT32r;
8103 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8104 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
8105 MachineFunction::iterator MBBIter = MBB;
8106 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00008107
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008108 /// First build the CFG
8109 MachineFunction *F = MBB->getParent();
8110 MachineBasicBlock *thisMBB = MBB;
8111 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
8112 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
8113 F->insert(MBBIter, newMBB);
8114 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008115
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008116 // Move all successors to thisMBB to nextMBB
8117 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008118
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008119 // Update thisMBB to fall through to newMBB
8120 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008121
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008122 // newMBB jumps to itself and fall through to nextMBB
8123 newMBB->addSuccessor(nextMBB);
8124 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008125
Dale Johannesene4d209d2009-02-03 20:21:25 +00008126 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008127 // Insert instructions into newMBB based on incoming instruction
8128 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008129 assert(bInstr->getNumOperands() < X86AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00008130 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008131 MachineOperand& dest1Oper = bInstr->getOperand(0);
8132 MachineOperand& dest2Oper = bInstr->getOperand(1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008133 MachineOperand* argOpers[2 + X86AddrNumOperands];
Dan Gohman71ea4e52010-05-14 21:01:44 +00008134 for (int i=0; i < 2 + X86AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008135 argOpers[i] = &bInstr->getOperand(i+2);
8136
Dan Gohman71ea4e52010-05-14 21:01:44 +00008137 // We use some of the operands multiple times, so conservatively just
8138 // clear any kill flags that might be present.
8139 if (argOpers[i]->isReg() && argOpers[i]->isUse())
8140 argOpers[i]->setIsKill(false);
8141 }
8142
Evan Chengad5b52f2010-01-08 19:14:57 +00008143 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008144 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00008145
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008146 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008147 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008148 for (int i=0; i <= lastAddrIndx; ++i)
8149 (*MIB).addOperand(*argOpers[i]);
8150 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008151 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00008152 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00008153 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008154 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00008155 MachineOperand newOp3 = *(argOpers[3]);
8156 if (newOp3.isImm())
8157 newOp3.setImm(newOp3.getImm()+4);
8158 else
8159 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008160 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00008161 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008162
8163 // t3/4 are defined later, at the bottom of the loop
8164 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
8165 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008166 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008167 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008168 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008169 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
8170
Evan Cheng306b4ca2010-01-08 23:41:50 +00008171 // The subsequent operations should be using the destination registers of
8172 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00008173 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00008174 t1 = F->getRegInfo().createVirtualRegister(RC);
8175 t2 = F->getRegInfo().createVirtualRegister(RC);
8176 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
8177 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008178 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00008179 t1 = dest1Oper.getReg();
8180 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008181 }
8182
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008183 int valArgIndx = lastAddrIndx + 1;
8184 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00008185 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008186 "invalid operand");
8187 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
8188 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008189 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008190 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008191 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008192 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00008193 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00008194 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008195 (*MIB).addOperand(*argOpers[valArgIndx]);
8196 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00008197 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008198 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00008199 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008200 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008201 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008202 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008203 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00008204 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00008205 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008206 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008207
Dale Johannesene4d209d2009-02-03 20:21:25 +00008208 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008209 MIB.addReg(t1);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008210 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008211 MIB.addReg(t2);
8212
Dale Johannesene4d209d2009-02-03 20:21:25 +00008213 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008214 MIB.addReg(t5);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008215 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008216 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00008217
Dale Johannesene4d209d2009-02-03 20:21:25 +00008218 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008219 for (int i=0; i <= lastAddrIndx; ++i)
8220 (*MIB).addOperand(*argOpers[i]);
8221
8222 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00008223 (*MIB).setMemRefs(bInstr->memoperands_begin(),
8224 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008225
Dale Johannesene4d209d2009-02-03 20:21:25 +00008226 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008227 MIB.addReg(X86::EAX);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008228 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008229 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00008230
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008231 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008232 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008233
8234 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
8235 return nextMBB;
8236}
8237
8238// private utility function
8239MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00008240X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
8241 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00008242 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00008243 // For the atomic min/max operator, we generate
8244 // thisMBB:
8245 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00008246 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00008247 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00008248 // cmp t1, t2
8249 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00008250 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00008251 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
8252 // bz newMBB
8253 // fallthrough -->nextMBB
8254 //
8255 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8256 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008257 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00008258 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00008259
Mon P Wang63307c32008-05-05 19:05:59 +00008260 /// First build the CFG
8261 MachineFunction *F = MBB->getParent();
8262 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008263 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
8264 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
8265 F->insert(MBBIter, newMBB);
8266 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008267
Dan Gohmand6708ea2009-08-15 01:38:56 +00008268 // Move all successors of thisMBB to nextMBB
Mon P Wang63307c32008-05-05 19:05:59 +00008269 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008270
Mon P Wang63307c32008-05-05 19:05:59 +00008271 // Update thisMBB to fall through to newMBB
8272 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008273
Mon P Wang63307c32008-05-05 19:05:59 +00008274 // newMBB jumps to newMBB and fall through to nextMBB
8275 newMBB->addSuccessor(nextMBB);
8276 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008277
Dale Johannesene4d209d2009-02-03 20:21:25 +00008278 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00008279 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008280 assert(mInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00008281 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00008282 MachineOperand& destOper = mInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008283 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00008284 int numArgs = mInstr->getNumOperands() - 1;
8285 for (int i=0; i < numArgs; ++i)
8286 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008287
Mon P Wang63307c32008-05-05 19:05:59 +00008288 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008289 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
8290 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00008291
Mon P Wangab3e7472008-05-05 22:56:23 +00008292 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008293 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00008294 for (int i=0; i <= lastAddrIndx; ++i)
8295 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00008296
Mon P Wang63307c32008-05-05 19:05:59 +00008297 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00008298 assert((argOpers[valArgIndx]->isReg() ||
8299 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00008300 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00008301
8302 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00008303 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008304 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00008305 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008306 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00008307 (*MIB).addOperand(*argOpers[valArgIndx]);
8308
Dale Johannesene4d209d2009-02-03 20:21:25 +00008309 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00008310 MIB.addReg(t1);
8311
Dale Johannesene4d209d2009-02-03 20:21:25 +00008312 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00008313 MIB.addReg(t1);
8314 MIB.addReg(t2);
8315
8316 // Generate movc
8317 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008318 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00008319 MIB.addReg(t2);
8320 MIB.addReg(t1);
8321
8322 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00008323 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00008324 for (int i=0; i <= lastAddrIndx; ++i)
8325 (*MIB).addOperand(*argOpers[i]);
8326 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00008327 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00008328 (*MIB).setMemRefs(mInstr->memoperands_begin(),
8329 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +00008330
Dale Johannesene4d209d2009-02-03 20:21:25 +00008331 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00008332 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00008333
Mon P Wang63307c32008-05-05 19:05:59 +00008334 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008335 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00008336
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008337 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00008338 return nextMBB;
8339}
8340
Eric Christopherf83a5de2009-08-27 18:08:16 +00008341// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
8342// all of this code can be replaced with that in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +00008343MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +00008344X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +00008345 unsigned numArgs, bool memArg) const {
Eric Christopherb120ab42009-08-18 22:50:32 +00008346
8347 MachineFunction *F = BB->getParent();
8348 DebugLoc dl = MI->getDebugLoc();
8349 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8350
8351 unsigned Opc;
Evan Chengce319102009-09-19 09:51:03 +00008352 if (memArg)
8353 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
8354 else
8355 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
Eric Christopherb120ab42009-08-18 22:50:32 +00008356
8357 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(Opc));
8358
8359 for (unsigned i = 0; i < numArgs; ++i) {
8360 MachineOperand &Op = MI->getOperand(i+1);
8361
8362 if (!(Op.isReg() && Op.isImplicit()))
8363 MIB.addOperand(Op);
8364 }
8365
8366 BuildMI(BB, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
8367 .addReg(X86::XMM0);
8368
8369 F->DeleteMachineInstr(MI);
8370
8371 return BB;
8372}
8373
8374MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +00008375X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
8376 MachineInstr *MI,
8377 MachineBasicBlock *MBB) const {
8378 // Emit code to save XMM registers to the stack. The ABI says that the
8379 // number of registers to save is given in %al, so it's theoretically
8380 // possible to do an indirect jump trick to avoid saving all of them,
8381 // however this code takes a simpler approach and just executes all
8382 // of the stores if %al is non-zero. It's less code, and it's probably
8383 // easier on the hardware branch predictor, and stores aren't all that
8384 // expensive anyway.
8385
8386 // Create the new basic blocks. One block contains all the XMM stores,
8387 // and one block is the final destination regardless of whether any
8388 // stores were performed.
8389 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
8390 MachineFunction *F = MBB->getParent();
8391 MachineFunction::iterator MBBIter = MBB;
8392 ++MBBIter;
8393 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
8394 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
8395 F->insert(MBBIter, XMMSaveMBB);
8396 F->insert(MBBIter, EndMBB);
8397
8398 // Set up the CFG.
8399 // Move any original successors of MBB to the end block.
8400 EndMBB->transferSuccessors(MBB);
8401 // The original block will now fall through to the XMM save block.
8402 MBB->addSuccessor(XMMSaveMBB);
8403 // The XMMSaveMBB will fall through to the end block.
8404 XMMSaveMBB->addSuccessor(EndMBB);
8405
8406 // Now add the instructions.
8407 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8408 DebugLoc DL = MI->getDebugLoc();
8409
8410 unsigned CountReg = MI->getOperand(0).getReg();
8411 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
8412 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
8413
8414 if (!Subtarget->isTargetWin64()) {
8415 // If %al is 0, branch around the XMM save block.
8416 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008417 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008418 MBB->addSuccessor(EndMBB);
8419 }
8420
8421 // In the XMM save block, save all the XMM argument registers.
8422 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
8423 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +00008424 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +00008425 F->getMachineMemOperand(
8426 PseudoSourceValue::getFixedStack(RegSaveFrameIndex),
8427 MachineMemOperand::MOStore, Offset,
8428 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008429 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
8430 .addFrameIndex(RegSaveFrameIndex)
8431 .addImm(/*Scale=*/1)
8432 .addReg(/*IndexReg=*/0)
8433 .addImm(/*Disp=*/Offset)
8434 .addReg(/*Segment=*/0)
8435 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +00008436 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008437 }
8438
8439 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8440
8441 return EndMBB;
8442}
Mon P Wang63307c32008-05-05 19:05:59 +00008443
Evan Cheng60c07e12006-07-05 22:17:51 +00008444MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +00008445X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00008446 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +00008447 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8448 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +00008449
Chris Lattner52600972009-09-02 05:57:00 +00008450 // To "insert" a SELECT_CC instruction, we actually have to insert the
8451 // diamond control-flow pattern. The incoming instruction knows the
8452 // destination vreg to set, the condition code register to branch on, the
8453 // true/false values to select between, and a branch opcode to use.
8454 const BasicBlock *LLVM_BB = BB->getBasicBlock();
8455 MachineFunction::iterator It = BB;
8456 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +00008457
Chris Lattner52600972009-09-02 05:57:00 +00008458 // thisMBB:
8459 // ...
8460 // TrueVal = ...
8461 // cmpTY ccX, r1, r2
8462 // bCC copy1MBB
8463 // fallthrough --> copy0MBB
8464 MachineBasicBlock *thisMBB = BB;
8465 MachineFunction *F = BB->getParent();
8466 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
8467 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
8468 unsigned Opc =
8469 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
Bill Wendling730c07e2010-06-25 20:48:10 +00008470
Chris Lattner52600972009-09-02 05:57:00 +00008471 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
8472 F->insert(It, copy0MBB);
8473 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +00008474
Evan Chengce319102009-09-19 09:51:03 +00008475 // Update machine-CFG edges by first adding all successors of the current
Chris Lattner52600972009-09-02 05:57:00 +00008476 // block to the new block which will contain the Phi node for the select.
Daniel Dunbara279bc32009-09-20 02:20:51 +00008477 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00008478 E = BB->succ_end(); I != E; ++I)
Evan Chengce319102009-09-19 09:51:03 +00008479 sinkMBB->addSuccessor(*I);
Bill Wendling730c07e2010-06-25 20:48:10 +00008480
Evan Chengce319102009-09-19 09:51:03 +00008481 // Next, remove all successors of the current block, and add the true
8482 // and fallthrough blocks as its successors.
8483 while (!BB->succ_empty())
8484 BB->removeSuccessor(BB->succ_begin());
Bill Wendling730c07e2010-06-25 20:48:10 +00008485
Chris Lattner52600972009-09-02 05:57:00 +00008486 // Add the true and fallthrough blocks as its successors.
8487 BB->addSuccessor(copy0MBB);
8488 BB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00008489
Bill Wendling730c07e2010-06-25 20:48:10 +00008490 // If the EFLAGS register isn't dead in the terminator, then claim that it's
8491 // live into the sink and copy blocks.
8492 const MachineFunction *MF = BB->getParent();
8493 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
8494 BitVector ReservedRegs = TRI->getReservedRegs(*MF);
8495 const MachineInstr *Term = BB->getFirstTerminator();
8496
8497 for (unsigned I = 0, E = Term->getNumOperands(); I != E; ++I) {
8498 const MachineOperand &MO = Term->getOperand(I);
8499 if (!MO.isReg() || MO.isKill() || MO.isDead()) continue;
8500 unsigned Reg = MO.getReg();
8501 if (Reg != X86::EFLAGS) continue;
8502 copy0MBB->addLiveIn(Reg);
8503 sinkMBB->addLiveIn(Reg);
8504 }
8505
Chris Lattner52600972009-09-02 05:57:00 +00008506 // copy0MBB:
8507 // %FalseValue = ...
8508 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +00008509 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00008510
Chris Lattner52600972009-09-02 05:57:00 +00008511 // sinkMBB:
8512 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
8513 // ...
Dan Gohman3335a222010-04-30 20:14:26 +00008514 BuildMI(sinkMBB, DL, TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +00008515 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
8516 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
8517
8518 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +00008519 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +00008520}
8521
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008522MachineBasicBlock *
8523X86TargetLowering::EmitLoweredMingwAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00008524 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008525 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8526 DebugLoc DL = MI->getDebugLoc();
8527 MachineFunction *F = BB->getParent();
8528
8529 // The lowering is pretty easy: we're just emitting the call to _alloca. The
8530 // non-trivial part is impdef of ESP.
8531 // FIXME: The code should be tweaked as soon as we'll try to do codegen for
8532 // mingw-w64.
8533
8534 BuildMI(BB, DL, TII->get(X86::CALLpcrel32))
8535 .addExternalSymbol("_alloca")
8536 .addReg(X86::EAX, RegState::Implicit)
8537 .addReg(X86::ESP, RegState::Implicit)
8538 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
8539 .addReg(X86::ESP, RegState::Define | RegState::Implicit);
8540
8541 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8542 return BB;
8543}
Chris Lattner52600972009-09-02 05:57:00 +00008544
8545MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +00008546X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
8547 MachineBasicBlock *BB) const {
8548 // This is pretty easy. We're taking the value that we received from
8549 // our load from the relocation, sticking it in either RDI (x86-64)
8550 // or EAX and doing an indirect call. The return value will then
8551 // be in the normal return register.
Eric Christopher54415362010-06-08 22:04:25 +00008552 const X86InstrInfo *TII
8553 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +00008554 DebugLoc DL = MI->getDebugLoc();
8555 MachineFunction *F = BB->getParent();
8556
Eric Christopher54415362010-06-08 22:04:25 +00008557 assert(MI->getOperand(3).isGlobal() && "This should be a global");
8558
Eric Christopher30ef0e52010-06-03 04:07:48 +00008559 if (Subtarget->is64Bit()) {
Eric Christopher54415362010-06-08 22:04:25 +00008560 MachineInstrBuilder MIB = BuildMI(BB, DL, TII->get(X86::MOV64rm), X86::RDI)
8561 .addReg(X86::RIP)
8562 .addImm(0).addReg(0)
8563 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
8564 MI->getOperand(3).getTargetFlags())
8565 .addReg(0);
Eric Christopher30ef0e52010-06-03 04:07:48 +00008566 MIB = BuildMI(BB, DL, TII->get(X86::CALL64m));
8567 addDirectMem(MIB, X86::RDI).addReg(0);
Eric Christopher61025492010-06-15 23:08:42 +00008568 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
8569 MachineInstrBuilder MIB = BuildMI(BB, DL, TII->get(X86::MOV32rm), X86::EAX)
8570 .addReg(0)
8571 .addImm(0).addReg(0)
8572 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
8573 MI->getOperand(3).getTargetFlags())
8574 .addReg(0);
8575 MIB = BuildMI(BB, DL, TII->get(X86::CALL32m));
8576 addDirectMem(MIB, X86::EAX).addReg(0);
Eric Christopher30ef0e52010-06-03 04:07:48 +00008577 } else {
Eric Christopher54415362010-06-08 22:04:25 +00008578 MachineInstrBuilder MIB = BuildMI(BB, DL, TII->get(X86::MOV32rm), X86::EAX)
8579 .addReg(TII->getGlobalBaseReg(F))
8580 .addImm(0).addReg(0)
8581 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
8582 MI->getOperand(3).getTargetFlags())
8583 .addReg(0);
Eric Christopher30ef0e52010-06-03 04:07:48 +00008584 MIB = BuildMI(BB, DL, TII->get(X86::CALL32m));
8585 addDirectMem(MIB, X86::EAX).addReg(0);
8586 }
8587
8588 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8589 return BB;
8590}
8591
8592MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00008593X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00008594 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00008595 switch (MI->getOpcode()) {
8596 default: assert(false && "Unexpected instr type to insert");
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008597 case X86::MINGW_ALLOCA:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00008598 return EmitLoweredMingwAlloca(MI, BB);
Eric Christopher30ef0e52010-06-03 04:07:48 +00008599 case X86::TLSCall_32:
8600 case X86::TLSCall_64:
8601 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +00008602 case X86::CMOV_GR8:
Mon P Wang9e5ecb82008-12-12 01:25:51 +00008603 case X86::CMOV_V1I64:
Evan Cheng60c07e12006-07-05 22:17:51 +00008604 case X86::CMOV_FR32:
8605 case X86::CMOV_FR64:
8606 case X86::CMOV_V4F32:
8607 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +00008608 case X86::CMOV_V2I64:
Chris Lattner314a1132010-03-14 18:31:44 +00008609 case X86::CMOV_GR16:
8610 case X86::CMOV_GR32:
8611 case X86::CMOV_RFP32:
8612 case X86::CMOV_RFP64:
8613 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00008614 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +00008615
Dale Johannesen849f2142007-07-03 00:53:03 +00008616 case X86::FP32_TO_INT16_IN_MEM:
8617 case X86::FP32_TO_INT32_IN_MEM:
8618 case X86::FP32_TO_INT64_IN_MEM:
8619 case X86::FP64_TO_INT16_IN_MEM:
8620 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00008621 case X86::FP64_TO_INT64_IN_MEM:
8622 case X86::FP80_TO_INT16_IN_MEM:
8623 case X86::FP80_TO_INT32_IN_MEM:
8624 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +00008625 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8626 DebugLoc DL = MI->getDebugLoc();
8627
Evan Cheng60c07e12006-07-05 22:17:51 +00008628 // Change the floating point control register to use "round towards zero"
8629 // mode when truncating to an integer value.
8630 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +00008631 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Chris Lattner52600972009-09-02 05:57:00 +00008632 addFrameReference(BuildMI(BB, DL, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008633
8634 // Load the old value of the high byte of the control word...
8635 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +00008636 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Chris Lattner52600972009-09-02 05:57:00 +00008637 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008638 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008639
8640 // Set the high part to be round to zero...
Chris Lattner52600972009-09-02 05:57:00 +00008641 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00008642 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00008643
8644 // Reload the modified control word now...
Chris Lattner52600972009-09-02 05:57:00 +00008645 addFrameReference(BuildMI(BB, DL, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008646
8647 // Restore the memory image of control word to original value
Chris Lattner52600972009-09-02 05:57:00 +00008648 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00008649 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00008650
8651 // Get the X86 opcode to use.
8652 unsigned Opc;
8653 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008654 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00008655 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
8656 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
8657 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
8658 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
8659 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
8660 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00008661 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
8662 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
8663 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00008664 }
8665
8666 X86AddressMode AM;
8667 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +00008668 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00008669 AM.BaseType = X86AddressMode::RegBase;
8670 AM.Base.Reg = Op.getReg();
8671 } else {
8672 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +00008673 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +00008674 }
8675 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +00008676 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00008677 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008678 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +00008679 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00008680 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008681 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +00008682 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00008683 AM.GV = Op.getGlobal();
8684 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00008685 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008686 }
Chris Lattner52600972009-09-02 05:57:00 +00008687 addFullAddress(BuildMI(BB, DL, TII->get(Opc)), AM)
Rafael Espindola8ef2b892009-04-08 08:09:33 +00008688 .addReg(MI->getOperand(X86AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00008689
8690 // Reload the original control word now.
Chris Lattner52600972009-09-02 05:57:00 +00008691 addFrameReference(BuildMI(BB, DL, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008692
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008693 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00008694 return BB;
8695 }
Eric Christopherb120ab42009-08-18 22:50:32 +00008696 // String/text processing lowering.
8697 case X86::PCMPISTRM128REG:
8698 return EmitPCMP(MI, BB, 3, false /* in-mem */);
8699 case X86::PCMPISTRM128MEM:
8700 return EmitPCMP(MI, BB, 3, true /* in-mem */);
8701 case X86::PCMPESTRM128REG:
8702 return EmitPCMP(MI, BB, 5, false /* in mem */);
8703 case X86::PCMPESTRM128MEM:
8704 return EmitPCMP(MI, BB, 5, true /* in mem */);
8705
8706 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +00008707 case X86::ATOMAND32:
8708 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008709 X86::AND32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008710 X86::LCMPXCHG32, X86::MOV32rr,
8711 X86::NOT32r, X86::EAX,
8712 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00008713 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +00008714 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
8715 X86::OR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008716 X86::LCMPXCHG32, X86::MOV32rr,
8717 X86::NOT32r, X86::EAX,
8718 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00008719 case X86::ATOMXOR32:
8720 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008721 X86::XOR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008722 X86::LCMPXCHG32, X86::MOV32rr,
8723 X86::NOT32r, X86::EAX,
8724 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008725 case X86::ATOMNAND32:
8726 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008727 X86::AND32ri, X86::MOV32rm,
8728 X86::LCMPXCHG32, X86::MOV32rr,
8729 X86::NOT32r, X86::EAX,
8730 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +00008731 case X86::ATOMMIN32:
8732 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
8733 case X86::ATOMMAX32:
8734 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
8735 case X86::ATOMUMIN32:
8736 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
8737 case X86::ATOMUMAX32:
8738 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +00008739
8740 case X86::ATOMAND16:
8741 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
8742 X86::AND16ri, X86::MOV16rm,
8743 X86::LCMPXCHG16, X86::MOV16rr,
8744 X86::NOT16r, X86::AX,
8745 X86::GR16RegisterClass);
8746 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +00008747 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008748 X86::OR16ri, X86::MOV16rm,
8749 X86::LCMPXCHG16, X86::MOV16rr,
8750 X86::NOT16r, X86::AX,
8751 X86::GR16RegisterClass);
8752 case X86::ATOMXOR16:
8753 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
8754 X86::XOR16ri, X86::MOV16rm,
8755 X86::LCMPXCHG16, X86::MOV16rr,
8756 X86::NOT16r, X86::AX,
8757 X86::GR16RegisterClass);
8758 case X86::ATOMNAND16:
8759 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
8760 X86::AND16ri, X86::MOV16rm,
8761 X86::LCMPXCHG16, X86::MOV16rr,
8762 X86::NOT16r, X86::AX,
8763 X86::GR16RegisterClass, true);
8764 case X86::ATOMMIN16:
8765 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
8766 case X86::ATOMMAX16:
8767 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
8768 case X86::ATOMUMIN16:
8769 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
8770 case X86::ATOMUMAX16:
8771 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
8772
8773 case X86::ATOMAND8:
8774 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
8775 X86::AND8ri, X86::MOV8rm,
8776 X86::LCMPXCHG8, X86::MOV8rr,
8777 X86::NOT8r, X86::AL,
8778 X86::GR8RegisterClass);
8779 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +00008780 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008781 X86::OR8ri, X86::MOV8rm,
8782 X86::LCMPXCHG8, X86::MOV8rr,
8783 X86::NOT8r, X86::AL,
8784 X86::GR8RegisterClass);
8785 case X86::ATOMXOR8:
8786 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
8787 X86::XOR8ri, X86::MOV8rm,
8788 X86::LCMPXCHG8, X86::MOV8rr,
8789 X86::NOT8r, X86::AL,
8790 X86::GR8RegisterClass);
8791 case X86::ATOMNAND8:
8792 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
8793 X86::AND8ri, X86::MOV8rm,
8794 X86::LCMPXCHG8, X86::MOV8rr,
8795 X86::NOT8r, X86::AL,
8796 X86::GR8RegisterClass, true);
8797 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008798 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +00008799 case X86::ATOMAND64:
8800 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008801 X86::AND64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008802 X86::LCMPXCHG64, X86::MOV64rr,
8803 X86::NOT64r, X86::RAX,
8804 X86::GR64RegisterClass);
8805 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +00008806 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
8807 X86::OR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008808 X86::LCMPXCHG64, X86::MOV64rr,
8809 X86::NOT64r, X86::RAX,
8810 X86::GR64RegisterClass);
8811 case X86::ATOMXOR64:
8812 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008813 X86::XOR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008814 X86::LCMPXCHG64, X86::MOV64rr,
8815 X86::NOT64r, X86::RAX,
8816 X86::GR64RegisterClass);
8817 case X86::ATOMNAND64:
8818 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
8819 X86::AND64ri32, X86::MOV64rm,
8820 X86::LCMPXCHG64, X86::MOV64rr,
8821 X86::NOT64r, X86::RAX,
8822 X86::GR64RegisterClass, true);
8823 case X86::ATOMMIN64:
8824 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
8825 case X86::ATOMMAX64:
8826 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
8827 case X86::ATOMUMIN64:
8828 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
8829 case X86::ATOMUMAX64:
8830 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008831
8832 // This group does 64-bit operations on a 32-bit host.
8833 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008834 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008835 X86::AND32rr, X86::AND32rr,
8836 X86::AND32ri, X86::AND32ri,
8837 false);
8838 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008839 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008840 X86::OR32rr, X86::OR32rr,
8841 X86::OR32ri, X86::OR32ri,
8842 false);
8843 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008844 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008845 X86::XOR32rr, X86::XOR32rr,
8846 X86::XOR32ri, X86::XOR32ri,
8847 false);
8848 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008849 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008850 X86::AND32rr, X86::AND32rr,
8851 X86::AND32ri, X86::AND32ri,
8852 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008853 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008854 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008855 X86::ADD32rr, X86::ADC32rr,
8856 X86::ADD32ri, X86::ADC32ri,
8857 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008858 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008859 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008860 X86::SUB32rr, X86::SBB32rr,
8861 X86::SUB32ri, X86::SBB32ri,
8862 false);
Dale Johannesen880ae362008-10-03 22:25:52 +00008863 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008864 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +00008865 X86::MOV32rr, X86::MOV32rr,
8866 X86::MOV32ri, X86::MOV32ri,
8867 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008868 case X86::VASTART_SAVE_XMM_REGS:
8869 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +00008870 }
8871}
8872
8873//===----------------------------------------------------------------------===//
8874// X86 Optimization Hooks
8875//===----------------------------------------------------------------------===//
8876
Dan Gohman475871a2008-07-27 21:46:04 +00008877void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00008878 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008879 APInt &KnownZero,
8880 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00008881 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00008882 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008883 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00008884 assert((Opc >= ISD::BUILTIN_OP_END ||
8885 Opc == ISD::INTRINSIC_WO_CHAIN ||
8886 Opc == ISD::INTRINSIC_W_CHAIN ||
8887 Opc == ISD::INTRINSIC_VOID) &&
8888 "Should use MaskedValueIsZero if you don't know whether Op"
8889 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008890
Dan Gohmanf4f92f52008-02-13 23:07:24 +00008891 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008892 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00008893 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +00008894 case X86ISD::ADD:
8895 case X86ISD::SUB:
8896 case X86ISD::SMUL:
8897 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +00008898 case X86ISD::INC:
8899 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +00008900 case X86ISD::OR:
8901 case X86ISD::XOR:
8902 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +00008903 // These nodes' second result is a boolean.
8904 if (Op.getResNo() == 0)
8905 break;
8906 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008907 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008908 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
8909 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +00008910 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008911 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008912}
Chris Lattner259e97c2006-01-31 19:43:35 +00008913
Evan Cheng206ee9d2006-07-07 08:33:52 +00008914/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +00008915/// node is a GlobalAddress + offset.
8916bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +00008917 const GlobalValue* &GA,
8918 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +00008919 if (N->getOpcode() == X86ISD::Wrapper) {
8920 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00008921 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00008922 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +00008923 return true;
8924 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00008925 }
Evan Chengad4196b2008-05-12 19:56:52 +00008926 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +00008927}
8928
Evan Cheng206ee9d2006-07-07 08:33:52 +00008929/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
8930/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
8931/// if the load addresses are consecutive, non-overlapping, and in the right
Nate Begemanfdea31a2010-03-24 20:49:50 +00008932/// order.
Dan Gohman475871a2008-07-27 21:46:04 +00008933static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00008934 const TargetLowering &TLI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00008935 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008936 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00008937 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
Mon P Wang1e955802009-04-03 02:43:30 +00008938
Eli Friedman7a5e5552009-06-07 06:52:44 +00008939 if (VT.getSizeInBits() != 128)
8940 return SDValue();
8941
Nate Begemanfdea31a2010-03-24 20:49:50 +00008942 SmallVector<SDValue, 16> Elts;
8943 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
8944 Elts.push_back(DAG.getShuffleScalarElt(SVN, i));
8945
8946 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00008947}
Evan Chengd880b972008-05-09 21:53:03 +00008948
Dan Gohman1bbf72b2010-03-15 23:23:03 +00008949/// PerformShuffleCombine - Detect vector gather/scatter index generation
8950/// and convert it from being a bunch of shuffles and extracts to a simple
8951/// store and scalar loads to extract the elements.
8952static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
8953 const TargetLowering &TLI) {
8954 SDValue InputVector = N->getOperand(0);
8955
8956 // Only operate on vectors of 4 elements, where the alternative shuffling
8957 // gets to be more expensive.
8958 if (InputVector.getValueType() != MVT::v4i32)
8959 return SDValue();
8960
8961 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
8962 // single use which is a sign-extend or zero-extend, and all elements are
8963 // used.
8964 SmallVector<SDNode *, 4> Uses;
8965 unsigned ExtractedElements = 0;
8966 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
8967 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
8968 if (UI.getUse().getResNo() != InputVector.getResNo())
8969 return SDValue();
8970
8971 SDNode *Extract = *UI;
8972 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8973 return SDValue();
8974
8975 if (Extract->getValueType(0) != MVT::i32)
8976 return SDValue();
8977 if (!Extract->hasOneUse())
8978 return SDValue();
8979 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
8980 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
8981 return SDValue();
8982 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
8983 return SDValue();
8984
8985 // Record which element was extracted.
8986 ExtractedElements |=
8987 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
8988
8989 Uses.push_back(Extract);
8990 }
8991
8992 // If not all the elements were used, this may not be worthwhile.
8993 if (ExtractedElements != 15)
8994 return SDValue();
8995
8996 // Ok, we've now decided to do the transformation.
8997 DebugLoc dl = InputVector.getDebugLoc();
8998
8999 // Store the value to a temporary stack slot.
9000 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
9001 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr, NULL, 0,
9002 false, false, 0);
9003
9004 // Replace each use (extract) with a load of the appropriate element.
9005 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
9006 UE = Uses.end(); UI != UE; ++UI) {
9007 SDNode *Extract = *UI;
9008
9009 // Compute the element's address.
9010 SDValue Idx = Extract->getOperand(1);
9011 unsigned EltSize =
9012 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
9013 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
9014 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
9015
9016 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), OffsetVal, StackPtr);
9017
9018 // Load the scalar.
9019 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch, ScalarAddr,
9020 NULL, 0, false, false, 0);
9021
9022 // Replace the exact with the load.
9023 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
9024 }
9025
9026 // The replacement was made in place; don't return anything.
9027 return SDValue();
9028}
9029
Chris Lattner83e6c992006-10-04 06:57:07 +00009030/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009031static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +00009032 const X86Subtarget *Subtarget) {
9033 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009034 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +00009035 // Get the LHS/RHS of the select.
9036 SDValue LHS = N->getOperand(1);
9037 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +00009038
Dan Gohman670e5392009-09-21 18:03:22 +00009039 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +00009040 // instructions match the semantics of the common C idiom x<y?x:y but not
9041 // x<=y?x:y, because of how they handle negative zero (which can be
9042 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +00009043 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00009044 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +00009045 Cond.getOpcode() == ISD::SETCC) {
9046 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009047
Chris Lattner47b4ce82009-03-11 05:48:52 +00009048 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +00009049 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +00009050 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
9051 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +00009052 switch (CC) {
9053 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +00009054 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +00009055 // Converting this to a min would handle NaNs incorrectly, and swapping
9056 // the operands would cause it to handle comparisons between positive
9057 // and negative zero incorrectly.
9058 if (!FiniteOnlyFPMath() &&
9059 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))) {
9060 if (!UnsafeFPMath &&
9061 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9062 break;
9063 std::swap(LHS, RHS);
9064 }
Dan Gohman670e5392009-09-21 18:03:22 +00009065 Opcode = X86ISD::FMIN;
9066 break;
9067 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +00009068 // Converting this to a min would handle comparisons between positive
9069 // and negative zero incorrectly.
9070 if (!UnsafeFPMath &&
9071 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
9072 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009073 Opcode = X86ISD::FMIN;
9074 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +00009075 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +00009076 // Converting this to a min would handle both negative zeros and NaNs
9077 // incorrectly, but we can swap the operands to fix both.
9078 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00009079 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009080 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +00009081 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009082 Opcode = X86ISD::FMIN;
9083 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009084
Dan Gohman670e5392009-09-21 18:03:22 +00009085 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +00009086 // Converting this to a max would handle comparisons between positive
9087 // and negative zero incorrectly.
9088 if (!UnsafeFPMath &&
9089 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
9090 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009091 Opcode = X86ISD::FMAX;
9092 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +00009093 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +00009094 // Converting this to a max would handle NaNs incorrectly, and swapping
9095 // the operands would cause it to handle comparisons between positive
9096 // and negative zero incorrectly.
9097 if (!FiniteOnlyFPMath() &&
9098 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))) {
9099 if (!UnsafeFPMath &&
9100 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9101 break;
9102 std::swap(LHS, RHS);
9103 }
Dan Gohman670e5392009-09-21 18:03:22 +00009104 Opcode = X86ISD::FMAX;
9105 break;
9106 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +00009107 // Converting this to a max would handle both negative zeros and NaNs
9108 // incorrectly, but we can swap the operands to fix both.
9109 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00009110 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009111 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009112 case ISD::SETGE:
9113 Opcode = X86ISD::FMAX;
9114 break;
Chris Lattner83e6c992006-10-04 06:57:07 +00009115 }
Dan Gohman670e5392009-09-21 18:03:22 +00009116 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +00009117 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
9118 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +00009119 switch (CC) {
9120 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +00009121 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +00009122 // Converting this to a min would handle comparisons between positive
9123 // and negative zero incorrectly, and swapping the operands would
9124 // cause it to handle NaNs incorrectly.
9125 if (!UnsafeFPMath &&
9126 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
9127 if (!FiniteOnlyFPMath() &&
9128 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
9129 break;
9130 std::swap(LHS, RHS);
9131 }
Dan Gohman670e5392009-09-21 18:03:22 +00009132 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +00009133 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009134 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +00009135 // Converting this to a min would handle NaNs incorrectly.
9136 if (!UnsafeFPMath &&
9137 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
9138 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009139 Opcode = X86ISD::FMIN;
9140 break;
9141 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +00009142 // Converting this to a min would handle both negative zeros and NaNs
9143 // incorrectly, but we can swap the operands to fix both.
9144 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00009145 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009146 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009147 case ISD::SETGE:
9148 Opcode = X86ISD::FMIN;
9149 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009150
Dan Gohman670e5392009-09-21 18:03:22 +00009151 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +00009152 // Converting this to a max would handle NaNs incorrectly.
9153 if (!FiniteOnlyFPMath() &&
9154 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
9155 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009156 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +00009157 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009158 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +00009159 // Converting this to a max would handle comparisons between positive
9160 // and negative zero incorrectly, and swapping the operands would
9161 // cause it to handle NaNs incorrectly.
9162 if (!UnsafeFPMath &&
9163 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
9164 if (!FiniteOnlyFPMath() &&
9165 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
9166 break;
9167 std::swap(LHS, RHS);
9168 }
Dan Gohman670e5392009-09-21 18:03:22 +00009169 Opcode = X86ISD::FMAX;
9170 break;
9171 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +00009172 // Converting this to a max would handle both negative zeros and NaNs
9173 // incorrectly, but we can swap the operands to fix both.
9174 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00009175 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009176 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +00009177 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009178 Opcode = X86ISD::FMAX;
9179 break;
9180 }
Chris Lattner83e6c992006-10-04 06:57:07 +00009181 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009182
Chris Lattner47b4ce82009-03-11 05:48:52 +00009183 if (Opcode)
9184 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +00009185 }
Eric Christopherfd179292009-08-27 18:07:15 +00009186
Chris Lattnerd1980a52009-03-12 06:52:53 +00009187 // If this is a select between two integer constants, try to do some
9188 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +00009189 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
9190 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +00009191 // Don't do this for crazy integer types.
9192 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
9193 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +00009194 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +00009195 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +00009196
Chris Lattnercee56e72009-03-13 05:53:31 +00009197 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +00009198 // Efficiently invertible.
9199 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
9200 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
9201 isa<ConstantSDNode>(Cond.getOperand(1))))) {
9202 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +00009203 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +00009204 }
Eric Christopherfd179292009-08-27 18:07:15 +00009205
Chris Lattnerd1980a52009-03-12 06:52:53 +00009206 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00009207 if (FalseC->getAPIntValue() == 0 &&
9208 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00009209 if (NeedsCondInvert) // Invert the condition if needed.
9210 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
9211 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009212
Chris Lattnerd1980a52009-03-12 06:52:53 +00009213 // Zero extend the condition if needed.
9214 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009215
Chris Lattnercee56e72009-03-13 05:53:31 +00009216 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +00009217 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +00009218 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +00009219 }
Eric Christopherfd179292009-08-27 18:07:15 +00009220
Chris Lattner97a29a52009-03-13 05:22:11 +00009221 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +00009222 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +00009223 if (NeedsCondInvert) // Invert the condition if needed.
9224 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
9225 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009226
Chris Lattner97a29a52009-03-13 05:22:11 +00009227 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00009228 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
9229 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00009230 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +00009231 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +00009232 }
Eric Christopherfd179292009-08-27 18:07:15 +00009233
Chris Lattnercee56e72009-03-13 05:53:31 +00009234 // Optimize cases that will turn into an LEA instruction. This requires
9235 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +00009236 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +00009237 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00009238 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +00009239
Chris Lattnercee56e72009-03-13 05:53:31 +00009240 bool isFastMultiplier = false;
9241 if (Diff < 10) {
9242 switch ((unsigned char)Diff) {
9243 default: break;
9244 case 1: // result = add base, cond
9245 case 2: // result = lea base( , cond*2)
9246 case 3: // result = lea base(cond, cond*2)
9247 case 4: // result = lea base( , cond*4)
9248 case 5: // result = lea base(cond, cond*4)
9249 case 8: // result = lea base( , cond*8)
9250 case 9: // result = lea base(cond, cond*8)
9251 isFastMultiplier = true;
9252 break;
9253 }
9254 }
Eric Christopherfd179292009-08-27 18:07:15 +00009255
Chris Lattnercee56e72009-03-13 05:53:31 +00009256 if (isFastMultiplier) {
9257 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
9258 if (NeedsCondInvert) // Invert the condition if needed.
9259 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
9260 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009261
Chris Lattnercee56e72009-03-13 05:53:31 +00009262 // Zero extend the condition if needed.
9263 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
9264 Cond);
9265 // Scale the condition by the difference.
9266 if (Diff != 1)
9267 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
9268 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009269
Chris Lattnercee56e72009-03-13 05:53:31 +00009270 // Add the base if non-zero.
9271 if (FalseC->getAPIntValue() != 0)
9272 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9273 SDValue(FalseC, 0));
9274 return Cond;
9275 }
Eric Christopherfd179292009-08-27 18:07:15 +00009276 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00009277 }
9278 }
Eric Christopherfd179292009-08-27 18:07:15 +00009279
Dan Gohman475871a2008-07-27 21:46:04 +00009280 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +00009281}
9282
Chris Lattnerd1980a52009-03-12 06:52:53 +00009283/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
9284static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
9285 TargetLowering::DAGCombinerInfo &DCI) {
9286 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +00009287
Chris Lattnerd1980a52009-03-12 06:52:53 +00009288 // If the flag operand isn't dead, don't touch this CMOV.
9289 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
9290 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +00009291
Chris Lattnerd1980a52009-03-12 06:52:53 +00009292 // If this is a select between two integer constants, try to do some
9293 // optimizations. Note that the operands are ordered the opposite of SELECT
9294 // operands.
9295 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
9296 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
9297 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
9298 // larger than FalseC (the false value).
9299 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
Eric Christopherfd179292009-08-27 18:07:15 +00009300
Chris Lattnerd1980a52009-03-12 06:52:53 +00009301 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
9302 CC = X86::GetOppositeBranchCondition(CC);
9303 std::swap(TrueC, FalseC);
9304 }
Eric Christopherfd179292009-08-27 18:07:15 +00009305
Chris Lattnerd1980a52009-03-12 06:52:53 +00009306 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00009307 // This is efficient for any integer data type (including i8/i16) and
9308 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +00009309 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
9310 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00009311 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9312 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009313
Chris Lattnerd1980a52009-03-12 06:52:53 +00009314 // Zero extend the condition if needed.
9315 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009316
Chris Lattnerd1980a52009-03-12 06:52:53 +00009317 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
9318 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +00009319 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +00009320 if (N->getNumValues() == 2) // Dead flag value?
9321 return DCI.CombineTo(N, Cond, SDValue());
9322 return Cond;
9323 }
Eric Christopherfd179292009-08-27 18:07:15 +00009324
Chris Lattnercee56e72009-03-13 05:53:31 +00009325 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
9326 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +00009327 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
9328 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00009329 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9330 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009331
Chris Lattner97a29a52009-03-13 05:22:11 +00009332 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00009333 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
9334 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00009335 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9336 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +00009337
Chris Lattner97a29a52009-03-13 05:22:11 +00009338 if (N->getNumValues() == 2) // Dead flag value?
9339 return DCI.CombineTo(N, Cond, SDValue());
9340 return Cond;
9341 }
Eric Christopherfd179292009-08-27 18:07:15 +00009342
Chris Lattnercee56e72009-03-13 05:53:31 +00009343 // Optimize cases that will turn into an LEA instruction. This requires
9344 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +00009345 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +00009346 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00009347 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +00009348
Chris Lattnercee56e72009-03-13 05:53:31 +00009349 bool isFastMultiplier = false;
9350 if (Diff < 10) {
9351 switch ((unsigned char)Diff) {
9352 default: break;
9353 case 1: // result = add base, cond
9354 case 2: // result = lea base( , cond*2)
9355 case 3: // result = lea base(cond, cond*2)
9356 case 4: // result = lea base( , cond*4)
9357 case 5: // result = lea base(cond, cond*4)
9358 case 8: // result = lea base( , cond*8)
9359 case 9: // result = lea base(cond, cond*8)
9360 isFastMultiplier = true;
9361 break;
9362 }
9363 }
Eric Christopherfd179292009-08-27 18:07:15 +00009364
Chris Lattnercee56e72009-03-13 05:53:31 +00009365 if (isFastMultiplier) {
9366 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
9367 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00009368 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9369 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +00009370 // Zero extend the condition if needed.
9371 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
9372 Cond);
9373 // Scale the condition by the difference.
9374 if (Diff != 1)
9375 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
9376 DAG.getConstant(Diff, Cond.getValueType()));
9377
9378 // Add the base if non-zero.
9379 if (FalseC->getAPIntValue() != 0)
9380 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9381 SDValue(FalseC, 0));
9382 if (N->getNumValues() == 2) // Dead flag value?
9383 return DCI.CombineTo(N, Cond, SDValue());
9384 return Cond;
9385 }
Eric Christopherfd179292009-08-27 18:07:15 +00009386 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00009387 }
9388 }
9389 return SDValue();
9390}
9391
9392
Evan Cheng0b0cd912009-03-28 05:57:29 +00009393/// PerformMulCombine - Optimize a single multiply with constant into two
9394/// in order to implement it with two cheaper instructions, e.g.
9395/// LEA + SHL, LEA + LEA.
9396static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
9397 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +00009398 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
9399 return SDValue();
9400
Owen Andersone50ed302009-08-10 22:56:29 +00009401 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009402 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +00009403 return SDValue();
9404
9405 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
9406 if (!C)
9407 return SDValue();
9408 uint64_t MulAmt = C->getZExtValue();
9409 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
9410 return SDValue();
9411
9412 uint64_t MulAmt1 = 0;
9413 uint64_t MulAmt2 = 0;
9414 if ((MulAmt % 9) == 0) {
9415 MulAmt1 = 9;
9416 MulAmt2 = MulAmt / 9;
9417 } else if ((MulAmt % 5) == 0) {
9418 MulAmt1 = 5;
9419 MulAmt2 = MulAmt / 5;
9420 } else if ((MulAmt % 3) == 0) {
9421 MulAmt1 = 3;
9422 MulAmt2 = MulAmt / 3;
9423 }
9424 if (MulAmt2 &&
9425 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
9426 DebugLoc DL = N->getDebugLoc();
9427
9428 if (isPowerOf2_64(MulAmt2) &&
9429 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
9430 // If second multiplifer is pow2, issue it first. We want the multiply by
9431 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
9432 // is an add.
9433 std::swap(MulAmt1, MulAmt2);
9434
9435 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +00009436 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +00009437 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00009438 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +00009439 else
Evan Cheng73f24c92009-03-30 21:36:47 +00009440 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +00009441 DAG.getConstant(MulAmt1, VT));
9442
Eric Christopherfd179292009-08-27 18:07:15 +00009443 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +00009444 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +00009445 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +00009446 else
Evan Cheng73f24c92009-03-30 21:36:47 +00009447 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +00009448 DAG.getConstant(MulAmt2, VT));
9449
9450 // Do not add new nodes to DAG combiner worklist.
9451 DCI.CombineTo(N, NewMul, false);
9452 }
9453 return SDValue();
9454}
9455
Evan Chengad9c0a32009-12-15 00:53:42 +00009456static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
9457 SDValue N0 = N->getOperand(0);
9458 SDValue N1 = N->getOperand(1);
9459 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
9460 EVT VT = N0.getValueType();
9461
9462 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
9463 // since the result of setcc_c is all zero's or all ones.
9464 if (N1C && N0.getOpcode() == ISD::AND &&
9465 N0.getOperand(1).getOpcode() == ISD::Constant) {
9466 SDValue N00 = N0.getOperand(0);
9467 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
9468 ((N00.getOpcode() == ISD::ANY_EXTEND ||
9469 N00.getOpcode() == ISD::ZERO_EXTEND) &&
9470 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
9471 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
9472 APInt ShAmt = N1C->getAPIntValue();
9473 Mask = Mask.shl(ShAmt);
9474 if (Mask != 0)
9475 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
9476 N00, DAG.getConstant(Mask, VT));
9477 }
9478 }
9479
9480 return SDValue();
9481}
Evan Cheng0b0cd912009-03-28 05:57:29 +00009482
Nate Begeman740ab032009-01-26 00:52:55 +00009483/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
9484/// when possible.
9485static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
9486 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +00009487 EVT VT = N->getValueType(0);
9488 if (!VT.isVector() && VT.isInteger() &&
9489 N->getOpcode() == ISD::SHL)
9490 return PerformSHLCombine(N, DAG);
9491
Nate Begeman740ab032009-01-26 00:52:55 +00009492 // On X86 with SSE2 support, we can transform this to a vector shift if
9493 // all elements are shifted by the same amount. We can't do this in legalize
9494 // because the a constant vector is typically transformed to a constant pool
9495 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009496 if (!Subtarget->hasSSE2())
9497 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009498
Owen Anderson825b72b2009-08-11 20:47:22 +00009499 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009500 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009501
Mon P Wang3becd092009-01-28 08:12:05 +00009502 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00009503 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +00009504 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +00009505 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +00009506 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
9507 unsigned NumElts = VT.getVectorNumElements();
9508 unsigned i = 0;
9509 for (; i != NumElts; ++i) {
9510 SDValue Arg = ShAmtOp.getOperand(i);
9511 if (Arg.getOpcode() == ISD::UNDEF) continue;
9512 BaseShAmt = Arg;
9513 break;
9514 }
9515 for (; i != NumElts; ++i) {
9516 SDValue Arg = ShAmtOp.getOperand(i);
9517 if (Arg.getOpcode() == ISD::UNDEF) continue;
9518 if (Arg != BaseShAmt) {
9519 return SDValue();
9520 }
9521 }
9522 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +00009523 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +00009524 SDValue InVec = ShAmtOp.getOperand(0);
9525 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
9526 unsigned NumElts = InVec.getValueType().getVectorNumElements();
9527 unsigned i = 0;
9528 for (; i != NumElts; ++i) {
9529 SDValue Arg = InVec.getOperand(i);
9530 if (Arg.getOpcode() == ISD::UNDEF) continue;
9531 BaseShAmt = Arg;
9532 break;
9533 }
9534 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
9535 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +00009536 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +00009537 if (C->getZExtValue() == SplatIdx)
9538 BaseShAmt = InVec.getOperand(1);
9539 }
9540 }
9541 if (BaseShAmt.getNode() == 0)
9542 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
9543 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +00009544 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009545 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +00009546
Mon P Wangefa42202009-09-03 19:56:25 +00009547 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +00009548 if (EltVT.bitsGT(MVT::i32))
9549 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
9550 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +00009551 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +00009552
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009553 // The shift amount is identical so we can do a vector shift.
9554 SDValue ValOp = N->getOperand(0);
9555 switch (N->getOpcode()) {
9556 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009557 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009558 break;
9559 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +00009560 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009561 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009562 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009563 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009564 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009565 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009566 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009567 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009568 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009569 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009570 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009571 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009572 break;
9573 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +00009574 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009575 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009576 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009577 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009578 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009579 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009580 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009581 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009582 break;
9583 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +00009584 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009585 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009586 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009587 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009588 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009589 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009590 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009591 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009592 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009593 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009594 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009595 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009596 break;
Nate Begeman740ab032009-01-26 00:52:55 +00009597 }
9598 return SDValue();
9599}
9600
Evan Cheng760d1942010-01-04 21:22:48 +00009601static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +00009602 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +00009603 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +00009604 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +00009605 return SDValue();
9606
Evan Cheng760d1942010-01-04 21:22:48 +00009607 EVT VT = N->getValueType(0);
Evan Cheng8b1190a2010-04-28 01:18:01 +00009608 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
Evan Cheng760d1942010-01-04 21:22:48 +00009609 return SDValue();
9610
9611 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
9612 SDValue N0 = N->getOperand(0);
9613 SDValue N1 = N->getOperand(1);
9614 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
9615 std::swap(N0, N1);
9616 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
9617 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +00009618 if (!N0.hasOneUse() || !N1.hasOneUse())
9619 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +00009620
9621 SDValue ShAmt0 = N0.getOperand(1);
9622 if (ShAmt0.getValueType() != MVT::i8)
9623 return SDValue();
9624 SDValue ShAmt1 = N1.getOperand(1);
9625 if (ShAmt1.getValueType() != MVT::i8)
9626 return SDValue();
9627 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
9628 ShAmt0 = ShAmt0.getOperand(0);
9629 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
9630 ShAmt1 = ShAmt1.getOperand(0);
9631
9632 DebugLoc DL = N->getDebugLoc();
9633 unsigned Opc = X86ISD::SHLD;
9634 SDValue Op0 = N0.getOperand(0);
9635 SDValue Op1 = N1.getOperand(0);
9636 if (ShAmt0.getOpcode() == ISD::SUB) {
9637 Opc = X86ISD::SHRD;
9638 std::swap(Op0, Op1);
9639 std::swap(ShAmt0, ShAmt1);
9640 }
9641
Evan Cheng8b1190a2010-04-28 01:18:01 +00009642 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +00009643 if (ShAmt1.getOpcode() == ISD::SUB) {
9644 SDValue Sum = ShAmt1.getOperand(0);
9645 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +00009646 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
9647 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
9648 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
9649 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +00009650 return DAG.getNode(Opc, DL, VT,
9651 Op0, Op1,
9652 DAG.getNode(ISD::TRUNCATE, DL,
9653 MVT::i8, ShAmt0));
9654 }
9655 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
9656 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
9657 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +00009658 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +00009659 return DAG.getNode(Opc, DL, VT,
9660 N0.getOperand(0), N1.getOperand(0),
9661 DAG.getNode(ISD::TRUNCATE, DL,
9662 MVT::i8, ShAmt0));
9663 }
9664
9665 return SDValue();
9666}
9667
Chris Lattner149a4e52008-02-22 02:09:43 +00009668/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009669static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +00009670 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +00009671 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
9672 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +00009673 // A preferable solution to the general problem is to figure out the right
9674 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +00009675
9676 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +00009677 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +00009678 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +00009679 if (VT.getSizeInBits() != 64)
9680 return SDValue();
9681
Devang Patel578efa92009-06-05 21:57:13 +00009682 const Function *F = DAG.getMachineFunction().getFunction();
9683 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +00009684 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +00009685 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +00009686 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +00009687 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +00009688 isa<LoadSDNode>(St->getValue()) &&
9689 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
9690 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00009691 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009692 LoadSDNode *Ld = 0;
9693 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +00009694 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +00009695 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009696 // Must be a store of a load. We currently handle two cases: the load
9697 // is a direct child, and it's under an intervening TokenFactor. It is
9698 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +00009699 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +00009700 Ld = cast<LoadSDNode>(St->getChain());
9701 else if (St->getValue().hasOneUse() &&
9702 ChainVal->getOpcode() == ISD::TokenFactor) {
9703 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +00009704 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +00009705 TokenFactorIndex = i;
9706 Ld = cast<LoadSDNode>(St->getValue());
9707 } else
9708 Ops.push_back(ChainVal->getOperand(i));
9709 }
9710 }
Dale Johannesen079f2a62008-02-25 19:20:14 +00009711
Evan Cheng536e6672009-03-12 05:59:15 +00009712 if (!Ld || !ISD::isNormalLoad(Ld))
9713 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009714
Evan Cheng536e6672009-03-12 05:59:15 +00009715 // If this is not the MMX case, i.e. we are just turning i64 load/store
9716 // into f64 load/store, avoid the transformation if there are multiple
9717 // uses of the loaded value.
9718 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
9719 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009720
Evan Cheng536e6672009-03-12 05:59:15 +00009721 DebugLoc LdDL = Ld->getDebugLoc();
9722 DebugLoc StDL = N->getDebugLoc();
9723 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
9724 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
9725 // pair instead.
9726 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009727 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Evan Cheng536e6672009-03-12 05:59:15 +00009728 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(),
9729 Ld->getBasePtr(), Ld->getSrcValue(),
9730 Ld->getSrcValueOffset(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +00009731 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +00009732 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +00009733 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +00009734 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +00009735 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +00009736 Ops.size());
9737 }
Evan Cheng536e6672009-03-12 05:59:15 +00009738 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner149a4e52008-02-22 02:09:43 +00009739 St->getSrcValue(), St->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00009740 St->isVolatile(), St->isNonTemporal(),
9741 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +00009742 }
Evan Cheng536e6672009-03-12 05:59:15 +00009743
9744 // Otherwise, lower to two pairs of 32-bit loads / stores.
9745 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +00009746 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
9747 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +00009748
Owen Anderson825b72b2009-08-11 20:47:22 +00009749 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Evan Cheng536e6672009-03-12 05:59:15 +00009750 Ld->getSrcValue(), Ld->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00009751 Ld->isVolatile(), Ld->isNonTemporal(),
9752 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00009753 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Evan Cheng536e6672009-03-12 05:59:15 +00009754 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
David Greene67c9d422010-02-15 16:53:33 +00009755 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +00009756 MinAlign(Ld->getAlignment(), 4));
9757
9758 SDValue NewChain = LoLd.getValue(1);
9759 if (TokenFactorIndex != -1) {
9760 Ops.push_back(LoLd);
9761 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +00009762 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +00009763 Ops.size());
9764 }
9765
9766 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +00009767 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
9768 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +00009769
9770 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
9771 St->getSrcValue(), St->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00009772 St->isVolatile(), St->isNonTemporal(),
9773 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +00009774 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
9775 St->getSrcValue(),
9776 St->getSrcValueOffset() + 4,
9777 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +00009778 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +00009779 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +00009780 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +00009781 }
Dan Gohman475871a2008-07-27 21:46:04 +00009782 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +00009783}
9784
Chris Lattner6cf73262008-01-25 06:14:17 +00009785/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
9786/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009787static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +00009788 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
9789 // F[X]OR(0.0, x) -> x
9790 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +00009791 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
9792 if (C->getValueAPF().isPosZero())
9793 return N->getOperand(1);
9794 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
9795 if (C->getValueAPF().isPosZero())
9796 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +00009797 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00009798}
9799
9800/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009801static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +00009802 // FAND(0.0, x) -> 0.0
9803 // FAND(x, 0.0) -> 0.0
9804 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
9805 if (C->getValueAPF().isPosZero())
9806 return N->getOperand(0);
9807 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
9808 if (C->getValueAPF().isPosZero())
9809 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +00009810 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00009811}
9812
Dan Gohmane5af2d32009-01-29 01:59:02 +00009813static SDValue PerformBTCombine(SDNode *N,
9814 SelectionDAG &DAG,
9815 TargetLowering::DAGCombinerInfo &DCI) {
9816 // BT ignores high bits in the bit index operand.
9817 SDValue Op1 = N->getOperand(1);
9818 if (Op1.hasOneUse()) {
9819 unsigned BitWidth = Op1.getValueSizeInBits();
9820 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
9821 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +00009822 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
9823 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +00009824 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +00009825 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
9826 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
9827 DCI.CommitTargetLoweringOpt(TLO);
9828 }
9829 return SDValue();
9830}
Chris Lattner83e6c992006-10-04 06:57:07 +00009831
Eli Friedman7a5e5552009-06-07 06:52:44 +00009832static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
9833 SDValue Op = N->getOperand(0);
9834 if (Op.getOpcode() == ISD::BIT_CONVERT)
9835 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00009836 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +00009837 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +00009838 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +00009839 OpVT.getVectorElementType().getSizeInBits()) {
9840 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT, Op);
9841 }
9842 return SDValue();
9843}
9844
Evan Cheng2e489c42009-12-16 00:53:11 +00009845static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
9846 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
9847 // (and (i32 x86isd::setcc_carry), 1)
9848 // This eliminates the zext. This transformation is necessary because
9849 // ISD::SETCC is always legalized to i8.
9850 DebugLoc dl = N->getDebugLoc();
9851 SDValue N0 = N->getOperand(0);
9852 EVT VT = N->getValueType(0);
9853 if (N0.getOpcode() == ISD::AND &&
9854 N0.hasOneUse() &&
9855 N0.getOperand(0).hasOneUse()) {
9856 SDValue N00 = N0.getOperand(0);
9857 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
9858 return SDValue();
9859 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
9860 if (!C || C->getZExtValue() != 1)
9861 return SDValue();
9862 return DAG.getNode(ISD::AND, dl, VT,
9863 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
9864 N00.getOperand(0), N00.getOperand(1)),
9865 DAG.getConstant(1, VT));
9866 }
9867
9868 return SDValue();
9869}
9870
Dan Gohman475871a2008-07-27 21:46:04 +00009871SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +00009872 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +00009873 SelectionDAG &DAG = DCI.DAG;
9874 switch (N->getOpcode()) {
9875 default: break;
Evan Chengad4196b2008-05-12 19:56:52 +00009876 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00009877 case ISD::EXTRACT_VECTOR_ELT:
9878 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +00009879 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +00009880 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +00009881 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +00009882 case ISD::SHL:
9883 case ISD::SRA:
9884 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +00009885 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +00009886 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +00009887 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +00009888 case X86ISD::FOR: return PerformFORCombine(N, DAG);
9889 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +00009890 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +00009891 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +00009892 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Evan Cheng206ee9d2006-07-07 08:33:52 +00009893 }
9894
Dan Gohman475871a2008-07-27 21:46:04 +00009895 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00009896}
9897
Evan Chenge5b51ac2010-04-17 06:13:15 +00009898/// isTypeDesirableForOp - Return true if the target has native support for
9899/// the specified value type and it is 'desirable' to use the type for the
9900/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
9901/// instruction encodings are longer and some i16 instructions are slow.
9902bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
9903 if (!isTypeLegal(VT))
9904 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00009905 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +00009906 return true;
9907
9908 switch (Opc) {
9909 default:
9910 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +00009911 case ISD::LOAD:
9912 case ISD::SIGN_EXTEND:
9913 case ISD::ZERO_EXTEND:
9914 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +00009915 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +00009916 case ISD::SRL:
9917 case ISD::SUB:
9918 case ISD::ADD:
9919 case ISD::MUL:
9920 case ISD::AND:
9921 case ISD::OR:
9922 case ISD::XOR:
9923 return false;
9924 }
9925}
9926
Evan Chengc82c20b2010-04-24 04:44:57 +00009927static bool MayFoldLoad(SDValue Op) {
9928 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
9929}
9930
9931static bool MayFoldIntoStore(SDValue Op) {
9932 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
9933}
9934
Evan Chenge5b51ac2010-04-17 06:13:15 +00009935/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +00009936/// beneficial for dag combiner to promote the specified node. If true, it
9937/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +00009938bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +00009939 EVT VT = Op.getValueType();
9940 if (VT != MVT::i16)
9941 return false;
9942
Evan Cheng4c26e932010-04-19 19:29:22 +00009943 bool Promote = false;
9944 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +00009945 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +00009946 default: break;
9947 case ISD::LOAD: {
9948 LoadSDNode *LD = cast<LoadSDNode>(Op);
9949 // If the non-extending load has a single use and it's not live out, then it
9950 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00009951 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
9952 Op.hasOneUse()*/) {
9953 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
9954 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
9955 // The only case where we'd want to promote LOAD (rather then it being
9956 // promoted as an operand is when it's only use is liveout.
9957 if (UI->getOpcode() != ISD::CopyToReg)
9958 return false;
9959 }
9960 }
Evan Cheng4c26e932010-04-19 19:29:22 +00009961 Promote = true;
9962 break;
9963 }
9964 case ISD::SIGN_EXTEND:
9965 case ISD::ZERO_EXTEND:
9966 case ISD::ANY_EXTEND:
9967 Promote = true;
9968 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +00009969 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00009970 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +00009971 SDValue N0 = Op.getOperand(0);
9972 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +00009973 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +00009974 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +00009975 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +00009976 break;
9977 }
Evan Cheng64b7bf72010-04-16 06:14:10 +00009978 case ISD::ADD:
9979 case ISD::MUL:
9980 case ISD::AND:
9981 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +00009982 case ISD::XOR:
9983 Commute = true;
9984 // fallthrough
9985 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +00009986 SDValue N0 = Op.getOperand(0);
9987 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +00009988 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +00009989 return false;
9990 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +00009991 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +00009992 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +00009993 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +00009994 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +00009995 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +00009996 }
9997 }
9998
9999 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000010000 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000010001}
10002
Evan Cheng60c07e12006-07-05 22:17:51 +000010003//===----------------------------------------------------------------------===//
10004// X86 Inline Assembly Support
10005//===----------------------------------------------------------------------===//
10006
Chris Lattnerb8105652009-07-20 17:51:36 +000010007static bool LowerToBSwap(CallInst *CI) {
10008 // FIXME: this should verify that we are targetting a 486 or better. If not,
10009 // we will turn this bswap into something that will be lowered to logical ops
10010 // instead of emitting the bswap asm. For now, we don't support 486 or lower
10011 // so don't worry about this.
Eric Christopherfd179292009-08-27 18:07:15 +000010012
Chris Lattnerb8105652009-07-20 17:51:36 +000010013 // Verify this is a simple bswap.
Gabor Greife1c2b9c2010-06-30 13:03:37 +000010014 if (CI->getNumArgOperands() != 1 ||
Gabor Greif1cfe44a2010-06-26 11:51:52 +000010015 CI->getType() != CI->getArgOperand(0)->getType() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000010016 !CI->getType()->isIntegerTy())
Chris Lattnerb8105652009-07-20 17:51:36 +000010017 return false;
Eric Christopherfd179292009-08-27 18:07:15 +000010018
Chris Lattnerb8105652009-07-20 17:51:36 +000010019 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
10020 if (!Ty || Ty->getBitWidth() % 16 != 0)
10021 return false;
Eric Christopherfd179292009-08-27 18:07:15 +000010022
Chris Lattnerb8105652009-07-20 17:51:36 +000010023 // Okay, we can do this xform, do so now.
10024 const Type *Tys[] = { Ty };
10025 Module *M = CI->getParent()->getParent()->getParent();
10026 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
Eric Christopherfd179292009-08-27 18:07:15 +000010027
Gabor Greif1cfe44a2010-06-26 11:51:52 +000010028 Value *Op = CI->getArgOperand(0);
Chris Lattnerb8105652009-07-20 17:51:36 +000010029 Op = CallInst::Create(Int, Op, CI->getName(), CI);
Eric Christopherfd179292009-08-27 18:07:15 +000010030
Chris Lattnerb8105652009-07-20 17:51:36 +000010031 CI->replaceAllUsesWith(Op);
10032 CI->eraseFromParent();
10033 return true;
10034}
10035
10036bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
10037 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
10038 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
10039
10040 std::string AsmStr = IA->getAsmString();
10041
10042 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000010043 SmallVector<StringRef, 4> AsmPieces;
Chris Lattnerb8105652009-07-20 17:51:36 +000010044 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
10045
10046 switch (AsmPieces.size()) {
10047 default: return false;
10048 case 1:
10049 AsmStr = AsmPieces[0];
10050 AsmPieces.clear();
10051 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
10052
10053 // bswap $0
10054 if (AsmPieces.size() == 2 &&
10055 (AsmPieces[0] == "bswap" ||
10056 AsmPieces[0] == "bswapq" ||
10057 AsmPieces[0] == "bswapl") &&
10058 (AsmPieces[1] == "$0" ||
10059 AsmPieces[1] == "${0:q}")) {
10060 // No need to check constraints, nothing other than the equivalent of
10061 // "=r,0" would be valid here.
10062 return LowerToBSwap(CI);
10063 }
10064 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000010065 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000010066 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000010067 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000010068 AsmPieces[1] == "$$8," &&
10069 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000010070 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
10071 AsmPieces.clear();
Benjamin Kramer018cbd52010-03-12 13:54:59 +000010072 const std::string &Constraints = IA->getConstraintString();
10073 SplitString(StringRef(Constraints).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000010074 std::sort(AsmPieces.begin(), AsmPieces.end());
10075 if (AsmPieces.size() == 4 &&
10076 AsmPieces[0] == "~{cc}" &&
10077 AsmPieces[1] == "~{dirflag}" &&
10078 AsmPieces[2] == "~{flags}" &&
10079 AsmPieces[3] == "~{fpsr}") {
10080 return LowerToBSwap(CI);
10081 }
Chris Lattnerb8105652009-07-20 17:51:36 +000010082 }
10083 break;
10084 case 3:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000010085 if (CI->getType()->isIntegerTy(64) &&
Owen Anderson1d0be152009-08-13 21:58:54 +000010086 Constraints.size() >= 2 &&
Chris Lattnerb8105652009-07-20 17:51:36 +000010087 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
10088 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
10089 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramerd4f19592010-01-11 18:03:24 +000010090 SmallVector<StringRef, 4> Words;
Chris Lattnerb8105652009-07-20 17:51:36 +000010091 SplitString(AsmPieces[0], Words, " \t");
10092 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
10093 Words.clear();
10094 SplitString(AsmPieces[1], Words, " \t");
10095 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
10096 Words.clear();
10097 SplitString(AsmPieces[2], Words, " \t,");
10098 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
10099 Words[2] == "%edx") {
10100 return LowerToBSwap(CI);
10101 }
10102 }
10103 }
10104 }
10105 break;
10106 }
10107 return false;
10108}
10109
10110
10111
Chris Lattnerf4dff842006-07-11 02:54:03 +000010112/// getConstraintType - Given a constraint letter, return the type of
10113/// constraint it is for this target.
10114X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000010115X86TargetLowering::getConstraintType(const std::string &Constraint) const {
10116 if (Constraint.size() == 1) {
10117 switch (Constraint[0]) {
10118 case 'A':
Dale Johannesen330169f2008-11-13 21:52:36 +000010119 return C_Register;
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010120 case 'f':
Chris Lattner4234f572007-03-25 02:14:49 +000010121 case 'r':
10122 case 'R':
10123 case 'l':
10124 case 'q':
10125 case 'Q':
10126 case 'x':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000010127 case 'y':
Chris Lattner4234f572007-03-25 02:14:49 +000010128 case 'Y':
10129 return C_RegisterClass;
Dale Johannesen78e3e522009-02-12 20:58:09 +000010130 case 'e':
10131 case 'Z':
10132 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000010133 default:
10134 break;
10135 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000010136 }
Chris Lattner4234f572007-03-25 02:14:49 +000010137 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000010138}
10139
Dale Johannesenba2a0b92008-01-29 02:21:21 +000010140/// LowerXConstraint - try to replace an X constraint, which matches anything,
10141/// with another that has more specific requirements based on the type of the
10142/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000010143const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000010144LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000010145 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
10146 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000010147 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesenba2a0b92008-01-29 02:21:21 +000010148 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +000010149 return "Y";
10150 if (Subtarget->hasSSE1())
10151 return "x";
10152 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010153
Chris Lattner5e764232008-04-26 23:02:14 +000010154 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000010155}
10156
Chris Lattner48884cd2007-08-25 00:47:38 +000010157/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
10158/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000010159void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +000010160 char Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000010161 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000010162 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010163 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000010164
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010165 switch (Constraint) {
10166 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000010167 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000010168 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000010169 if (C->getZExtValue() <= 31) {
10170 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000010171 break;
10172 }
Devang Patel84f7fd22007-03-17 00:13:28 +000010173 }
Chris Lattner48884cd2007-08-25 00:47:38 +000010174 return;
Evan Cheng364091e2008-09-22 23:57:37 +000010175 case 'J':
10176 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000010177 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000010178 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
10179 break;
10180 }
10181 }
10182 return;
10183 case 'K':
10184 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000010185 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000010186 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
10187 break;
10188 }
10189 }
10190 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000010191 case 'N':
10192 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000010193 if (C->getZExtValue() <= 255) {
10194 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000010195 break;
10196 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000010197 }
Chris Lattner48884cd2007-08-25 00:47:38 +000010198 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000010199 case 'e': {
10200 // 32-bit signed value
10201 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000010202 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
10203 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000010204 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000010205 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000010206 break;
10207 }
10208 // FIXME gcc accepts some relocatable values here too, but only in certain
10209 // memory models; it's complicated.
10210 }
10211 return;
10212 }
10213 case 'Z': {
10214 // 32-bit unsigned value
10215 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000010216 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
10217 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000010218 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
10219 break;
10220 }
10221 }
10222 // FIXME gcc accepts some relocatable values here too, but only in certain
10223 // memory models; it's complicated.
10224 return;
10225 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000010226 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010227 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000010228 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000010229 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000010230 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000010231 break;
10232 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010233
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000010234 // In any sort of PIC mode addresses need to be computed at runtime by
10235 // adding in a register or some sort of table lookup. These can't
10236 // be used as immediates.
10237 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC() ||
10238 Subtarget->isPICStyleRIPRel())
10239 return;
10240
Chris Lattnerdc43a882007-05-03 16:52:29 +000010241 // If we are in non-pic codegen mode, we allow the address of a global (with
10242 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000010243 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000010244 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000010245
Chris Lattner49921962009-05-08 18:23:14 +000010246 // Match either (GA), (GA+C), (GA+C1+C2), etc.
10247 while (1) {
10248 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
10249 Offset += GA->getOffset();
10250 break;
10251 } else if (Op.getOpcode() == ISD::ADD) {
10252 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
10253 Offset += C->getZExtValue();
10254 Op = Op.getOperand(0);
10255 continue;
10256 }
10257 } else if (Op.getOpcode() == ISD::SUB) {
10258 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
10259 Offset += -C->getZExtValue();
10260 Op = Op.getOperand(0);
10261 continue;
10262 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000010263 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000010264
Chris Lattner49921962009-05-08 18:23:14 +000010265 // Otherwise, this isn't something we can handle, reject it.
10266 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000010267 }
Eric Christopherfd179292009-08-27 18:07:15 +000010268
Dan Gohman46510a72010-04-15 01:51:59 +000010269 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000010270 // If we require an extra load to get this address, as in PIC mode, we
10271 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000010272 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
10273 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000010274 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000010275
Dale Johannesen1784d162010-06-25 21:55:36 +000010276 Result = DAG.getTargetGlobalAddress(GV, GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000010277 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010278 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000010279 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010280
Gabor Greifba36cb52008-08-28 21:40:38 +000010281 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000010282 Ops.push_back(Result);
10283 return;
10284 }
Dale Johannesen1784d162010-06-25 21:55:36 +000010285 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010286}
10287
Chris Lattner259e97c2006-01-31 19:43:35 +000010288std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +000010289getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000010290 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +000010291 if (Constraint.size() == 1) {
10292 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +000010293 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +000010294 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +000010295 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
10296 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000010297 if (VT == MVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010298 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
10299 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
10300 X86::R10D,X86::R11D,X86::R12D,
10301 X86::R13D,X86::R14D,X86::R15D,
10302 X86::EBP, X86::ESP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010303 else if (VT == MVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010304 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
10305 X86::SI, X86::DI, X86::R8W,X86::R9W,
10306 X86::R10W,X86::R11W,X86::R12W,
10307 X86::R13W,X86::R14W,X86::R15W,
10308 X86::BP, X86::SP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010309 else if (VT == MVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010310 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
10311 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
10312 X86::R10B,X86::R11B,X86::R12B,
10313 X86::R13B,X86::R14B,X86::R15B,
10314 X86::BPL, X86::SPL, 0);
10315
Owen Anderson825b72b2009-08-11 20:47:22 +000010316 else if (VT == MVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010317 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
10318 X86::RSI, X86::RDI, X86::R8, X86::R9,
10319 X86::R10, X86::R11, X86::R12,
10320 X86::R13, X86::R14, X86::R15,
10321 X86::RBP, X86::RSP, 0);
10322
10323 break;
10324 }
Eric Christopherfd179292009-08-27 18:07:15 +000010325 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +000010326 case 'Q': // Q_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000010327 if (VT == MVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000010328 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010329 else if (VT == MVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000010330 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010331 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +000010332 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010333 else if (VT == MVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +000010334 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
10335 break;
Chris Lattner259e97c2006-01-31 19:43:35 +000010336 }
10337 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010338
Chris Lattner1efa40f2006-02-22 00:56:39 +000010339 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +000010340}
Chris Lattnerf76d1802006-07-31 23:26:50 +000010341
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010342std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000010343X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000010344 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000010345 // First, see if this is a constraint that directly corresponds to an LLVM
10346 // register class.
10347 if (Constraint.size() == 1) {
10348 // GCC Constraint Letters
10349 switch (Constraint[0]) {
10350 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000010351 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000010352 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000010353 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +000010354 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010355 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000010356 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010357 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000010358 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000010359 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000010360 case 'R': // LEGACY_REGS
10361 if (VT == MVT::i8)
10362 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
10363 if (VT == MVT::i16)
10364 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
10365 if (VT == MVT::i32 || !Subtarget->is64Bit())
10366 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
10367 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010368 case 'f': // FP Stack registers.
10369 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
10370 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000010371 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010372 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010373 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010374 return std::make_pair(0U, X86::RFP64RegisterClass);
10375 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000010376 case 'y': // MMX_REGS if MMX allowed.
10377 if (!Subtarget->hasMMX()) break;
10378 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000010379 case 'Y': // SSE_REGS if SSE2 allowed
10380 if (!Subtarget->hasSSE2()) break;
10381 // FALL THROUGH.
10382 case 'x': // SSE_REGS if SSE1 allowed
10383 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010384
Owen Anderson825b72b2009-08-11 20:47:22 +000010385 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000010386 default: break;
10387 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000010388 case MVT::f32:
10389 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000010390 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010391 case MVT::f64:
10392 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000010393 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000010394 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000010395 case MVT::v16i8:
10396 case MVT::v8i16:
10397 case MVT::v4i32:
10398 case MVT::v2i64:
10399 case MVT::v4f32:
10400 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000010401 return std::make_pair(0U, X86::VR128RegisterClass);
10402 }
Chris Lattnerad043e82007-04-09 05:11:28 +000010403 break;
10404 }
10405 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010406
Chris Lattnerf76d1802006-07-31 23:26:50 +000010407 // Use the default implementation in TargetLowering to convert the register
10408 // constraint into a member of a register class.
10409 std::pair<unsigned, const TargetRegisterClass*> Res;
10410 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000010411
10412 // Not found as a standard register?
10413 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000010414 // Map st(0) -> st(7) -> ST0
10415 if (Constraint.size() == 7 && Constraint[0] == '{' &&
10416 tolower(Constraint[1]) == 's' &&
10417 tolower(Constraint[2]) == 't' &&
10418 Constraint[3] == '(' &&
10419 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
10420 Constraint[5] == ')' &&
10421 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000010422
Chris Lattner56d77c72009-09-13 22:41:48 +000010423 Res.first = X86::ST0+Constraint[4]-'0';
10424 Res.second = X86::RFP80RegisterClass;
10425 return Res;
10426 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000010427
Chris Lattner56d77c72009-09-13 22:41:48 +000010428 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000010429 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000010430 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000010431 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000010432 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000010433 }
Chris Lattner56d77c72009-09-13 22:41:48 +000010434
10435 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000010436 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000010437 Res.first = X86::EFLAGS;
10438 Res.second = X86::CCRRegisterClass;
10439 return Res;
10440 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000010441
Dale Johannesen330169f2008-11-13 21:52:36 +000010442 // 'A' means EAX + EDX.
10443 if (Constraint == "A") {
10444 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000010445 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000010446 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000010447 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000010448 return Res;
10449 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010450
Chris Lattnerf76d1802006-07-31 23:26:50 +000010451 // Otherwise, check to see if this is a register class of the wrong value
10452 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
10453 // turn into {ax},{dx}.
10454 if (Res.second->hasType(VT))
10455 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010456
Chris Lattnerf76d1802006-07-31 23:26:50 +000010457 // All of the single-register GCC register classes map their values onto
10458 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
10459 // really want an 8-bit or 32-bit register, map to the appropriate register
10460 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000010461 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000010462 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010463 unsigned DestReg = 0;
10464 switch (Res.first) {
10465 default: break;
10466 case X86::AX: DestReg = X86::AL; break;
10467 case X86::DX: DestReg = X86::DL; break;
10468 case X86::CX: DestReg = X86::CL; break;
10469 case X86::BX: DestReg = X86::BL; break;
10470 }
10471 if (DestReg) {
10472 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010473 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010474 }
Owen Anderson825b72b2009-08-11 20:47:22 +000010475 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010476 unsigned DestReg = 0;
10477 switch (Res.first) {
10478 default: break;
10479 case X86::AX: DestReg = X86::EAX; break;
10480 case X86::DX: DestReg = X86::EDX; break;
10481 case X86::CX: DestReg = X86::ECX; break;
10482 case X86::BX: DestReg = X86::EBX; break;
10483 case X86::SI: DestReg = X86::ESI; break;
10484 case X86::DI: DestReg = X86::EDI; break;
10485 case X86::BP: DestReg = X86::EBP; break;
10486 case X86::SP: DestReg = X86::ESP; break;
10487 }
10488 if (DestReg) {
10489 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010490 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010491 }
Owen Anderson825b72b2009-08-11 20:47:22 +000010492 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010493 unsigned DestReg = 0;
10494 switch (Res.first) {
10495 default: break;
10496 case X86::AX: DestReg = X86::RAX; break;
10497 case X86::DX: DestReg = X86::RDX; break;
10498 case X86::CX: DestReg = X86::RCX; break;
10499 case X86::BX: DestReg = X86::RBX; break;
10500 case X86::SI: DestReg = X86::RSI; break;
10501 case X86::DI: DestReg = X86::RDI; break;
10502 case X86::BP: DestReg = X86::RBP; break;
10503 case X86::SP: DestReg = X86::RSP; break;
10504 }
10505 if (DestReg) {
10506 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010507 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010508 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000010509 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000010510 } else if (Res.second == X86::FR32RegisterClass ||
10511 Res.second == X86::FR64RegisterClass ||
10512 Res.second == X86::VR128RegisterClass) {
10513 // Handle references to XMM physical registers that got mapped into the
10514 // wrong class. This can happen with constraints like {xmm0} where the
10515 // target independent register mapper will just pick the first match it can
10516 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000010517 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000010518 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000010519 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000010520 Res.second = X86::FR64RegisterClass;
10521 else if (X86::VR128RegisterClass->hasType(VT))
10522 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000010523 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010524
Chris Lattnerf76d1802006-07-31 23:26:50 +000010525 return Res;
10526}