blob: 78144abe807c4aa63e4c437e1be3e2a8e709bcf1 [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 Johannesenf1fc3a82007-09-23 14:52:20 +0000217 if (!X86ScalarSSEf64) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000218 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
219 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000220 if (Subtarget->is64Bit() && Subtarget->hasMMX() && !DisableMMX) {
221 // Without SSE, i64->f64 goes through memory; i64->MMX is legal.
222 setOperationAction(ISD::BIT_CONVERT , MVT::i64 , Custom);
223 setOperationAction(ISD::BIT_CONVERT , MVT::f64 , Expand);
224 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000225 }
Chris Lattner21f66852005-12-23 05:15:23 +0000226
Dan Gohmanb00ee212008-02-18 19:34:53 +0000227 // Scalar integer divide and remainder are lowered to use operations that
228 // produce two results, to match the available instructions. This exposes
229 // the two-result form to trivial CSE, which is able to combine x/y and x%y
230 // into a single instruction.
231 //
232 // Scalar integer multiply-high is also lowered to use two-result
233 // operations, to match the available instructions. However, plain multiply
234 // (low) operations are left as Legal, as there are single-result
235 // instructions for this in x86. Using the two-result multiply instructions
236 // when both high and low results are needed must be arranged by dagcombine.
Owen Anderson825b72b2009-08-11 20:47:22 +0000237 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
238 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
239 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
240 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
241 setOperationAction(ISD::SREM , MVT::i8 , Expand);
242 setOperationAction(ISD::UREM , MVT::i8 , Expand);
243 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
244 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
245 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
246 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
247 setOperationAction(ISD::SREM , MVT::i16 , Expand);
248 setOperationAction(ISD::UREM , MVT::i16 , Expand);
249 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
250 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
251 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
252 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
253 setOperationAction(ISD::SREM , MVT::i32 , Expand);
254 setOperationAction(ISD::UREM , MVT::i32 , Expand);
255 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
256 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
257 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
258 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
259 setOperationAction(ISD::SREM , MVT::i64 , Expand);
260 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohmana37c9f72007-09-25 18:23:27 +0000261
Owen Anderson825b72b2009-08-11 20:47:22 +0000262 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
263 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
264 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
265 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000266 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000267 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
268 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
269 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
270 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
271 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
272 setOperationAction(ISD::FREM , MVT::f32 , Expand);
273 setOperationAction(ISD::FREM , MVT::f64 , Expand);
274 setOperationAction(ISD::FREM , MVT::f80 , Expand);
275 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000276
Owen Anderson825b72b2009-08-11 20:47:22 +0000277 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
278 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
279 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
280 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000281 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
282 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000283 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
284 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
285 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000286 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000287 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
288 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
289 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000290 }
291
Owen Anderson825b72b2009-08-11 20:47:22 +0000292 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
293 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000294
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000295 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000296 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000297 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000298 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000299 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000300 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
301 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
302 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
303 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
304 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000305 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000306 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
307 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
308 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
309 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000310 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000311 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
312 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000313 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000314 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000315
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000316 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000317 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
318 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
319 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
320 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000321 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000322 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
323 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000324 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000325 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000326 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
327 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
328 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
329 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000330 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000331 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000332 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000333 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
334 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
335 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000336 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000337 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
338 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
339 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000340 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000341
Evan Chengd2cde682008-03-10 19:38:10 +0000342 if (Subtarget->hasSSE1())
Owen Anderson825b72b2009-08-11 20:47:22 +0000343 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000344
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000345 if (!Subtarget->hasSSE2())
Owen Anderson825b72b2009-08-11 20:47:22 +0000346 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000347
Mon P Wang63307c32008-05-05 19:05:59 +0000348 // Expand certain atomics
Owen Anderson825b72b2009-08-11 20:47:22 +0000349 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Custom);
350 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Custom);
351 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
352 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Bill Wendling5bf1b4e2008-08-20 00:28:16 +0000353
Owen Anderson825b72b2009-08-11 20:47:22 +0000354 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Custom);
355 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Custom);
356 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
357 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000358
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000359 if (!Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000360 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
361 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
362 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
363 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
364 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
365 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
366 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000367 }
368
Evan Cheng3c992d22006-03-07 02:02:57 +0000369 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000370 if (!Subtarget->isTargetDarwin() &&
371 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000372 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000373 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000374 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000375
Owen Anderson825b72b2009-08-11 20:47:22 +0000376 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
377 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
378 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
379 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000380 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000381 setExceptionPointerRegister(X86::RAX);
382 setExceptionSelectorRegister(X86::RDX);
383 } else {
384 setExceptionPointerRegister(X86::EAX);
385 setExceptionSelectorRegister(X86::EDX);
386 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000387 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
388 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000389
Owen Anderson825b72b2009-08-11 20:47:22 +0000390 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000391
Owen Anderson825b72b2009-08-11 20:47:22 +0000392 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000393
Nate Begemanacc398c2006-01-25 18:21:52 +0000394 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000395 setOperationAction(ISD::VASTART , MVT::Other, Custom);
396 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000397 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000398 setOperationAction(ISD::VAARG , MVT::Other, Custom);
399 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000400 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000401 setOperationAction(ISD::VAARG , MVT::Other, Expand);
402 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000403 }
Evan Chengae642192007-03-02 23:16:35 +0000404
Owen Anderson825b72b2009-08-11 20:47:22 +0000405 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
406 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000407 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000408 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000409 if (Subtarget->isTargetCygMing())
Owen Anderson825b72b2009-08-11 20:47:22 +0000410 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000411 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000412 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000413
Evan Chengc7ce29b2009-02-13 22:36:38 +0000414 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000415 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000416 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000417 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
418 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000419
Evan Cheng223547a2006-01-31 22:28:30 +0000420 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000421 setOperationAction(ISD::FABS , MVT::f64, Custom);
422 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000423
424 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000425 setOperationAction(ISD::FNEG , MVT::f64, Custom);
426 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000427
Evan Cheng68c47cb2007-01-05 07:55:56 +0000428 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000429 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
430 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000431
Evan Chengd25e9e82006-02-02 00:28:23 +0000432 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000433 setOperationAction(ISD::FSIN , MVT::f64, Expand);
434 setOperationAction(ISD::FCOS , MVT::f64, Expand);
435 setOperationAction(ISD::FSIN , MVT::f32, Expand);
436 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000437
Chris Lattnera54aa942006-01-29 06:26:08 +0000438 // Expand FP immediates into loads from the stack, except for the special
439 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000440 addLegalFPImmediate(APFloat(+0.0)); // xorpd
441 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000442 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000443 // Use SSE for f32, x87 for f64.
444 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000445 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
446 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000447
448 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000449 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000450
451 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000452 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000453
Owen Anderson825b72b2009-08-11 20:47:22 +0000454 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000455
456 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000457 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
458 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000459
460 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000461 setOperationAction(ISD::FSIN , MVT::f32, Expand);
462 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000463
Nate Begemane1795842008-02-14 08:57:00 +0000464 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000465 addLegalFPImmediate(APFloat(+0.0f)); // xorps
466 addLegalFPImmediate(APFloat(+0.0)); // FLD0
467 addLegalFPImmediate(APFloat(+1.0)); // FLD1
468 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
469 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
470
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000471 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000472 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
473 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000474 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000475 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000476 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000477 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000478 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
479 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000480
Owen Anderson825b72b2009-08-11 20:47:22 +0000481 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
482 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
483 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
484 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000485
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000486 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000487 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
488 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000489 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000490 addLegalFPImmediate(APFloat(+0.0)); // FLD0
491 addLegalFPImmediate(APFloat(+1.0)); // FLD1
492 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
493 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000494 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
495 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
496 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
497 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000498 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000499
Dale Johannesen59a58732007-08-05 18:49:15 +0000500 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000501 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000502 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
503 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
504 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000505 {
506 bool ignored;
507 APFloat TmpFlt(+0.0);
508 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
509 &ignored);
510 addLegalFPImmediate(TmpFlt); // FLD0
511 TmpFlt.changeSign();
512 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
513 APFloat TmpFlt2(+1.0);
514 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
515 &ignored);
516 addLegalFPImmediate(TmpFlt2); // FLD1
517 TmpFlt2.changeSign();
518 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
519 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000520
Evan Chengc7ce29b2009-02-13 22:36:38 +0000521 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000522 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
523 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000524 }
Dale Johannesen2f429012007-09-26 21:10:55 +0000525 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000526
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000527 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000528 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
529 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
530 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000531
Owen Anderson825b72b2009-08-11 20:47:22 +0000532 setOperationAction(ISD::FLOG, MVT::f80, Expand);
533 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
534 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
535 setOperationAction(ISD::FEXP, MVT::f80, Expand);
536 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000537
Mon P Wangf007a8b2008-11-06 05:31:54 +0000538 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000539 // (for widening) or expand (for scalarization). Then we will selectively
540 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000541 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
542 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
543 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
544 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
545 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
546 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
547 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
548 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
549 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
550 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
551 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
552 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
553 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
554 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
555 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
556 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
557 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
558 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
559 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
560 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
561 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
562 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
563 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
564 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
565 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
566 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
567 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
568 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
569 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
570 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
571 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
572 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
573 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
574 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
575 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
576 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
577 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
578 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
579 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
580 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
581 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
582 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
583 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
584 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
585 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
586 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
587 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
588 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
589 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
590 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000591 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000592 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
593 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
594 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
595 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
596 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
597 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
598 setTruncStoreAction((MVT::SimpleValueType)VT,
599 (MVT::SimpleValueType)InnerVT, Expand);
600 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
601 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
602 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000603 }
604
Evan Chengc7ce29b2009-02-13 22:36:38 +0000605 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
606 // with -msoft-float, disable use of MMX as well.
Evan Cheng92722532009-03-26 23:06:32 +0000607 if (!UseSoftFloat && !DisableMMX && Subtarget->hasMMX()) {
Dale Johannesen76090172010-04-20 22:34:09 +0000608 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass, false);
609 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass, false);
610 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass, false);
611 addRegisterClass(MVT::v2f32, X86::VR64RegisterClass, false);
612 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass, false);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000613
Owen Anderson825b72b2009-08-11 20:47:22 +0000614 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
615 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
616 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
617 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000618
Owen Anderson825b72b2009-08-11 20:47:22 +0000619 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
620 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
621 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
622 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000623
Owen Anderson825b72b2009-08-11 20:47:22 +0000624 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
625 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
Bill Wendling74027e92007-03-15 21:24:36 +0000626
Owen Anderson825b72b2009-08-11 20:47:22 +0000627 setOperationAction(ISD::AND, MVT::v8i8, Promote);
628 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
629 setOperationAction(ISD::AND, MVT::v4i16, Promote);
630 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
631 setOperationAction(ISD::AND, MVT::v2i32, Promote);
632 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
633 setOperationAction(ISD::AND, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000634
Owen Anderson825b72b2009-08-11 20:47:22 +0000635 setOperationAction(ISD::OR, MVT::v8i8, Promote);
636 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
637 setOperationAction(ISD::OR, MVT::v4i16, Promote);
638 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
639 setOperationAction(ISD::OR, MVT::v2i32, Promote);
640 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
641 setOperationAction(ISD::OR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000642
Owen Anderson825b72b2009-08-11 20:47:22 +0000643 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
644 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
645 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
646 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
647 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
648 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
649 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000650
Owen Anderson825b72b2009-08-11 20:47:22 +0000651 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
652 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
653 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
654 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
655 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
656 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
657 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
658 AddPromotedToType (ISD::LOAD, MVT::v2f32, MVT::v1i64);
659 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000660
Owen Anderson825b72b2009-08-11 20:47:22 +0000661 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
662 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
663 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
664 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom);
665 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
Bill Wendlinga348c562007-03-22 18:42:45 +0000666
Owen Anderson825b72b2009-08-11 20:47:22 +0000667 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
668 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
669 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
670 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000671
Owen Anderson825b72b2009-08-11 20:47:22 +0000672 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f32, Custom);
673 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
674 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
675 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Bill Wendling3180e202008-07-20 02:32:23 +0000676
Owen Anderson825b72b2009-08-11 20:47:22 +0000677 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
Mon P Wang9e5ecb82008-12-12 01:25:51 +0000678
Owen Anderson825b72b2009-08-11 20:47:22 +0000679 setOperationAction(ISD::SELECT, MVT::v8i8, Promote);
680 setOperationAction(ISD::SELECT, MVT::v4i16, Promote);
681 setOperationAction(ISD::SELECT, MVT::v2i32, Promote);
682 setOperationAction(ISD::SELECT, MVT::v1i64, Custom);
683 setOperationAction(ISD::VSETCC, MVT::v8i8, Custom);
684 setOperationAction(ISD::VSETCC, MVT::v4i16, Custom);
685 setOperationAction(ISD::VSETCC, MVT::v2i32, Custom);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000686
687 if (!X86ScalarSSEf64 && Subtarget->is64Bit()) {
688 setOperationAction(ISD::BIT_CONVERT, MVT::v8i8, Custom);
689 setOperationAction(ISD::BIT_CONVERT, MVT::v4i16, Custom);
690 setOperationAction(ISD::BIT_CONVERT, MVT::v2i32, Custom);
691 setOperationAction(ISD::BIT_CONVERT, MVT::v2f32, Custom);
692 setOperationAction(ISD::BIT_CONVERT, MVT::v1i64, Custom);
693 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000694 }
695
Evan Cheng92722532009-03-26 23:06:32 +0000696 if (!UseSoftFloat && Subtarget->hasSSE1()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000697 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000698
Owen Anderson825b72b2009-08-11 20:47:22 +0000699 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
700 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
701 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
702 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
703 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
704 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
705 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
706 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
707 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
708 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
709 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
710 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000711 }
712
Evan Cheng92722532009-03-26 23:06:32 +0000713 if (!UseSoftFloat && Subtarget->hasSSE2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000714 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000715
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000716 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
717 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000718 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
719 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
720 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
721 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000722
Owen Anderson825b72b2009-08-11 20:47:22 +0000723 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
724 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
725 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
726 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
727 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
728 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
729 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
730 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
731 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
732 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
733 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
734 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
735 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
736 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
737 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
738 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000739
Owen Anderson825b72b2009-08-11 20:47:22 +0000740 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
741 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
742 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
743 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000744
Owen Anderson825b72b2009-08-11 20:47:22 +0000745 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
746 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
747 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
748 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
749 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000750
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000751 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
752 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
753 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
754 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
755 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
756
Evan Cheng2c3ae372006-04-12 21:21:57 +0000757 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000758 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
759 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000760 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000761 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000762 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000763 // Do not attempt to custom lower non-128-bit vectors
764 if (!VT.is128BitVector())
765 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000766 setOperationAction(ISD::BUILD_VECTOR,
767 VT.getSimpleVT().SimpleTy, Custom);
768 setOperationAction(ISD::VECTOR_SHUFFLE,
769 VT.getSimpleVT().SimpleTy, Custom);
770 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
771 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000772 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000773
Owen Anderson825b72b2009-08-11 20:47:22 +0000774 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
775 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
776 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
777 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
778 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
779 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000780
Nate Begemancdd1eec2008-02-12 22:51:28 +0000781 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000782 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
783 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000784 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000785
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000786 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000787 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
788 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000789 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000790
791 // Do not attempt to promote non-128-bit vectors
792 if (!VT.is128BitVector()) {
793 continue;
794 }
Eric Christopher4bd24c22010-03-30 01:04:59 +0000795
Owen Andersond6662ad2009-08-10 20:46:15 +0000796 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000797 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000798 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000799 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000800 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000801 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000802 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000803 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000804 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000805 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000806 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000807
Owen Anderson825b72b2009-08-11 20:47:22 +0000808 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000809
Evan Cheng2c3ae372006-04-12 21:21:57 +0000810 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000811 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
812 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
813 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
814 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000815
Owen Anderson825b72b2009-08-11 20:47:22 +0000816 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
817 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Eli Friedman23ef1052009-06-06 03:57:58 +0000818 if (!DisableMMX && Subtarget->hasMMX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000819 setOperationAction(ISD::FP_TO_SINT, MVT::v2i32, Custom);
820 setOperationAction(ISD::SINT_TO_FP, MVT::v2i32, Custom);
Eli Friedman23ef1052009-06-06 03:57:58 +0000821 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000822 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000823
Nate Begeman14d12ca2008-02-11 04:19:36 +0000824 if (Subtarget->hasSSE41()) {
825 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000826 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000827
828 // i8 and i16 vectors are custom , because the source register and source
829 // source memory operand types are not the same width. f32 vectors are
830 // custom since the immediate controlling the insert encodes additional
831 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000832 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
833 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
834 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
835 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000836
Owen Anderson825b72b2009-08-11 20:47:22 +0000837 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
838 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
839 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
840 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000841
842 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000843 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
844 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000845 }
846 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000847
Nate Begeman30a0de92008-07-17 16:51:19 +0000848 if (Subtarget->hasSSE42()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000849 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
Nate Begeman30a0de92008-07-17 16:51:19 +0000850 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000851
David Greene9b9838d2009-06-29 16:47:10 +0000852 if (!UseSoftFloat && Subtarget->hasAVX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000853 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
854 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
855 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
856 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000857
Owen Anderson825b72b2009-08-11 20:47:22 +0000858 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
859 setOperationAction(ISD::LOAD, MVT::v8i32, Legal);
860 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
861 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
862 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
863 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
864 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
865 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
866 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
867 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
868 //setOperationAction(ISD::BUILD_VECTOR, MVT::v8f32, Custom);
869 //setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Custom);
870 //setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8f32, Custom);
871 //setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
872 //setOperationAction(ISD::VSETCC, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000873
874 // Operations to consider commented out -v16i16 v32i8
Owen Anderson825b72b2009-08-11 20:47:22 +0000875 //setOperationAction(ISD::ADD, MVT::v16i16, Legal);
876 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
877 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
878 //setOperationAction(ISD::SUB, MVT::v32i8, Legal);
879 //setOperationAction(ISD::SUB, MVT::v16i16, Legal);
880 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
881 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
882 //setOperationAction(ISD::MUL, MVT::v16i16, Legal);
883 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
884 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
885 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
886 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
887 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
888 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000889
Owen Anderson825b72b2009-08-11 20:47:22 +0000890 setOperationAction(ISD::VSETCC, MVT::v4f64, Custom);
891 // setOperationAction(ISD::VSETCC, MVT::v32i8, Custom);
892 // setOperationAction(ISD::VSETCC, MVT::v16i16, Custom);
893 setOperationAction(ISD::VSETCC, MVT::v8i32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000894
Owen Anderson825b72b2009-08-11 20:47:22 +0000895 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v32i8, Custom);
896 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i16, Custom);
897 // setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i16, Custom);
898 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i32, Custom);
899 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000900
Owen Anderson825b72b2009-08-11 20:47:22 +0000901 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f64, Custom);
902 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i64, Custom);
903 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f64, Custom);
904 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i64, Custom);
905 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f64, Custom);
906 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000907
908#if 0
909 // Not sure we want to do this since there are no 256-bit integer
910 // operations in AVX
911
912 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
913 // This includes 256-bit vectors
Owen Anderson825b72b2009-08-11 20:47:22 +0000914 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; ++i) {
915 EVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000916
917 // Do not attempt to custom lower non-power-of-2 vectors
918 if (!isPowerOf2_32(VT.getVectorNumElements()))
919 continue;
920
921 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
922 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
923 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
924 }
925
926 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000927 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i64, Custom);
928 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i64, Custom);
Eric Christopherfd179292009-08-27 18:07:15 +0000929 }
David Greene9b9838d2009-06-29 16:47:10 +0000930#endif
931
932#if 0
933 // Not sure we want to do this since there are no 256-bit integer
934 // operations in AVX
935
936 // Promote v32i8, v16i16, v8i32 load, select, and, or, xor to v4i64.
937 // Including 256-bit vectors
Owen Anderson825b72b2009-08-11 20:47:22 +0000938 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; i++) {
939 EVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000940
941 if (!VT.is256BitVector()) {
942 continue;
943 }
944 setOperationAction(ISD::AND, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000945 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000946 setOperationAction(ISD::OR, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000947 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000948 setOperationAction(ISD::XOR, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000949 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000950 setOperationAction(ISD::LOAD, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000951 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000952 setOperationAction(ISD::SELECT, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000953 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000954 }
955
Owen Anderson825b72b2009-08-11 20:47:22 +0000956 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
David Greene9b9838d2009-06-29 16:47:10 +0000957#endif
958 }
959
Evan Cheng6be2c582006-04-05 23:38:46 +0000960 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000961 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +0000962
Bill Wendling74c37652008-12-09 22:08:41 +0000963 // Add/Sub/Mul with overflow operations are custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000964 setOperationAction(ISD::SADDO, MVT::i32, Custom);
965 setOperationAction(ISD::SADDO, MVT::i64, Custom);
966 setOperationAction(ISD::UADDO, MVT::i32, Custom);
967 setOperationAction(ISD::UADDO, MVT::i64, Custom);
968 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
969 setOperationAction(ISD::SSUBO, MVT::i64, Custom);
970 setOperationAction(ISD::USUBO, MVT::i32, Custom);
971 setOperationAction(ISD::USUBO, MVT::i64, Custom);
972 setOperationAction(ISD::SMULO, MVT::i32, Custom);
973 setOperationAction(ISD::SMULO, MVT::i64, Custom);
Bill Wendling41ea7e72008-11-24 19:21:46 +0000974
Evan Chengd54f2d52009-03-31 19:38:51 +0000975 if (!Subtarget->is64Bit()) {
976 // These libcalls are not available in 32-bit.
977 setLibcallName(RTLIB::SHL_I128, 0);
978 setLibcallName(RTLIB::SRL_I128, 0);
979 setLibcallName(RTLIB::SRA_I128, 0);
980 }
981
Evan Cheng206ee9d2006-07-07 08:33:52 +0000982 // We have target-specific dag combine patterns for the following nodes:
983 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +0000984 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +0000985 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +0000986 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +0000987 setTargetDAGCombine(ISD::SHL);
988 setTargetDAGCombine(ISD::SRA);
989 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +0000990 setTargetDAGCombine(ISD::OR);
Chris Lattner149a4e52008-02-22 02:09:43 +0000991 setTargetDAGCombine(ISD::STORE);
Owen Anderson99177002009-06-29 18:04:45 +0000992 setTargetDAGCombine(ISD::MEMBARRIER);
Evan Cheng2e489c42009-12-16 00:53:11 +0000993 setTargetDAGCombine(ISD::ZERO_EXTEND);
Evan Cheng0b0cd912009-03-28 05:57:29 +0000994 if (Subtarget->is64Bit())
995 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +0000996
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000997 computeRegisterProperties();
998
Evan Cheng87ed7162006-02-14 08:25:08 +0000999 // FIXME: These should be based on subtarget info. Plus, the values should
1000 // be smaller when we are in optimizing for size mode.
Dan Gohman87060f52008-06-30 21:00:56 +00001001 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng255f20f2010-04-01 06:04:33 +00001002 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Dan Gohman87060f52008-06-30 21:00:56 +00001003 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Evan Chengfb8075d2008-02-28 00:43:03 +00001004 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001005 benefitFromCodePlacementOpt = true;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001006}
1007
Scott Michel5b8f82e2008-03-10 15:42:14 +00001008
Owen Anderson825b72b2009-08-11 20:47:22 +00001009MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
1010 return MVT::i8;
Scott Michel5b8f82e2008-03-10 15:42:14 +00001011}
1012
1013
Evan Cheng29286502008-01-23 23:17:41 +00001014/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1015/// the desired ByVal argument alignment.
1016static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
1017 if (MaxAlign == 16)
1018 return;
1019 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
1020 if (VTy->getBitWidth() == 128)
1021 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +00001022 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1023 unsigned EltAlign = 0;
1024 getMaxByValAlign(ATy->getElementType(), EltAlign);
1025 if (EltAlign > MaxAlign)
1026 MaxAlign = EltAlign;
1027 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1028 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1029 unsigned EltAlign = 0;
1030 getMaxByValAlign(STy->getElementType(i), EltAlign);
1031 if (EltAlign > MaxAlign)
1032 MaxAlign = EltAlign;
1033 if (MaxAlign == 16)
1034 break;
1035 }
1036 }
1037 return;
1038}
1039
1040/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1041/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001042/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1043/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +00001044unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001045 if (Subtarget->is64Bit()) {
1046 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001047 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001048 if (TyAlign > 8)
1049 return TyAlign;
1050 return 8;
1051 }
1052
Evan Cheng29286502008-01-23 23:17:41 +00001053 unsigned Align = 4;
Dale Johannesen0c191872008-02-08 19:48:20 +00001054 if (Subtarget->hasSSE1())
1055 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001056 return Align;
1057}
Chris Lattner2b02a442007-02-25 08:29:00 +00001058
Evan Chengf0df0312008-05-15 08:39:06 +00001059/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001060/// and store operations as a result of memset, memcpy, and memmove
1061/// lowering. If DstAlign is zero that means it's safe to destination
1062/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1063/// means there isn't a need to check it against alignment requirement,
1064/// probably because the source does not need to be loaded. If
1065/// 'NonScalarIntSafe' is true, that means it's safe to return a
1066/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1067/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1068/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001069/// It returns EVT::Other if the type should be determined using generic
1070/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001071EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001072X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1073 unsigned DstAlign, unsigned SrcAlign,
Evan Chengf28f8bc2010-04-02 19:36:14 +00001074 bool NonScalarIntSafe,
Evan Chengc3b0c342010-04-08 07:37:57 +00001075 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001076 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001077 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1078 // linux. This is because the stack realignment code can't handle certain
1079 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001080 const Function *F = MF.getFunction();
Evan Chengf28f8bc2010-04-02 19:36:14 +00001081 if (NonScalarIntSafe &&
1082 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001083 if (Size >= 16 &&
1084 (Subtarget->isUnalignedMemAccessFast() ||
Chandler Carruthae1d41c2010-04-02 01:31:24 +00001085 ((DstAlign == 0 || DstAlign >= 16) &&
1086 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001087 Subtarget->getStackAlignment() >= 16) {
1088 if (Subtarget->hasSSE2())
1089 return MVT::v4i32;
Evan Chengf28f8bc2010-04-02 19:36:14 +00001090 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001091 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001092 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001093 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001094 Subtarget->getStackAlignment() >= 8 &&
Evan Chengc3b0c342010-04-08 07:37:57 +00001095 Subtarget->hasSSE2()) {
1096 // Do not use f64 to lower memcpy if source is string constant. It's
1097 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001098 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001099 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001100 }
Evan Chengf0df0312008-05-15 08:39:06 +00001101 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001102 return MVT::i64;
1103 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001104}
1105
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001106/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1107/// current function. The returned value is a member of the
1108/// MachineJumpTableInfo::JTEntryKind enum.
1109unsigned X86TargetLowering::getJumpTableEncoding() const {
1110 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1111 // symbol.
1112 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1113 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001114 return MachineJumpTableInfo::EK_Custom32;
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001115
1116 // Otherwise, use the normal jump table encoding heuristics.
1117 return TargetLowering::getJumpTableEncoding();
1118}
1119
Chris Lattner589c6f62010-01-26 06:28:43 +00001120/// getPICBaseSymbol - Return the X86-32 PIC base.
1121MCSymbol *
1122X86TargetLowering::getPICBaseSymbol(const MachineFunction *MF,
1123 MCContext &Ctx) const {
1124 const MCAsmInfo &MAI = *getTargetMachine().getMCAsmInfo();
Chris Lattner9b97a732010-03-30 18:10:53 +00001125 return Ctx.GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix())+
1126 Twine(MF->getFunctionNumber())+"$pb");
Chris Lattner589c6f62010-01-26 06:28:43 +00001127}
1128
1129
Chris Lattnerc64daab2010-01-26 05:02:42 +00001130const MCExpr *
1131X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1132 const MachineBasicBlock *MBB,
1133 unsigned uid,MCContext &Ctx) const{
1134 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1135 Subtarget->isPICStyleGOT());
1136 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1137 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001138 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1139 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001140}
1141
Evan Chengcc415862007-11-09 01:32:10 +00001142/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1143/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001144SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001145 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001146 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001147 // This doesn't have DebugLoc associated with it, but is not really the
1148 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001149 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001150 return Table;
1151}
1152
Chris Lattner589c6f62010-01-26 06:28:43 +00001153/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1154/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1155/// MCExpr.
1156const MCExpr *X86TargetLowering::
1157getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1158 MCContext &Ctx) const {
1159 // X86-64 uses RIP relative addressing based on the jump table label.
1160 if (Subtarget->isPICStyleRIPRel())
1161 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1162
1163 // Otherwise, the reference is relative to the PIC base.
1164 return MCSymbolRefExpr::Create(getPICBaseSymbol(MF, Ctx), Ctx);
1165}
1166
Bill Wendlingb4202b82009-07-01 18:50:55 +00001167/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +00001168unsigned X86TargetLowering::getFunctionAlignment(const Function *F) const {
Dan Gohman25103a22009-08-18 00:20:06 +00001169 return F->hasFnAttr(Attribute::OptimizeForSize) ? 0 : 4;
Bill Wendling20c568f2009-06-30 22:38:32 +00001170}
1171
Chris Lattner2b02a442007-02-25 08:29:00 +00001172//===----------------------------------------------------------------------===//
1173// Return Value Calling Convention Implementation
1174//===----------------------------------------------------------------------===//
1175
Chris Lattner59ed56b2007-02-28 04:55:35 +00001176#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001177
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001178bool
1179X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv, bool isVarArg,
1180 const SmallVectorImpl<EVT> &OutTys,
1181 const SmallVectorImpl<ISD::ArgFlagsTy> &ArgsFlags,
Dan Gohmand858e902010-04-17 15:26:15 +00001182 SelectionDAG &DAG) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001183 SmallVector<CCValAssign, 16> RVLocs;
1184 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1185 RVLocs, *DAG.getContext());
1186 return CCInfo.CheckReturn(OutTys, ArgsFlags, RetCC_X86);
1187}
1188
Dan Gohman98ca4f22009-08-05 01:29:28 +00001189SDValue
1190X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001191 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001192 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmand858e902010-04-17 15:26:15 +00001193 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001194 MachineFunction &MF = DAG.getMachineFunction();
1195 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001196
Chris Lattner9774c912007-02-27 05:28:59 +00001197 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001198 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1199 RVLocs, *DAG.getContext());
1200 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001201
Evan Chengdcea1632010-02-04 02:40:39 +00001202 // Add the regs to the liveout set for the function.
1203 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1204 for (unsigned i = 0; i != RVLocs.size(); ++i)
1205 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1206 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001207
Dan Gohman475871a2008-07-27 21:46:04 +00001208 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001209
Dan Gohman475871a2008-07-27 21:46:04 +00001210 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001211 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1212 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001213 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1214 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001215
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001216 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001217 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1218 CCValAssign &VA = RVLocs[i];
1219 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman98ca4f22009-08-05 01:29:28 +00001220 SDValue ValToCopy = Outs[i].Val;
Scott Michelfdc40a02009-02-17 22:15:04 +00001221
Chris Lattner447ff682008-03-11 03:23:40 +00001222 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1223 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001224 if (VA.getLocReg() == X86::ST0 ||
1225 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001226 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1227 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001228 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001229 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001230 RetOps.push_back(ValToCopy);
1231 // Don't emit a copytoreg.
1232 continue;
1233 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001234
Evan Cheng242b38b2009-02-23 09:03:22 +00001235 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1236 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001237 if (Subtarget->is64Bit()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001238 EVT ValVT = ValToCopy.getValueType();
Evan Cheng242b38b2009-02-23 09:03:22 +00001239 if (ValVT.isVector() && ValVT.getSizeInBits() == 64) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001240 ValToCopy = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, ValToCopy);
Evan Cheng242b38b2009-02-23 09:03:22 +00001241 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1)
Owen Anderson825b72b2009-08-11 20:47:22 +00001242 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, ValToCopy);
Evan Cheng242b38b2009-02-23 09:03:22 +00001243 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001244 }
1245
Dale Johannesendd64c412009-02-04 00:33:20 +00001246 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001247 Flag = Chain.getValue(1);
1248 }
Dan Gohman61a92132008-04-21 23:59:07 +00001249
1250 // The x86-64 ABI for returning structs by value requires that we copy
1251 // the sret argument into %rax for the return. We saved the argument into
1252 // a virtual register in the entry block, so now we copy the value out
1253 // and into %rax.
1254 if (Subtarget->is64Bit() &&
1255 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1256 MachineFunction &MF = DAG.getMachineFunction();
1257 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1258 unsigned Reg = FuncInfo->getSRetReturnReg();
1259 if (!Reg) {
Evan Chengdcea1632010-02-04 02:40:39 +00001260 Reg = MRI.createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001261 FuncInfo->setSRetReturnReg(Reg);
1262 }
Dale Johannesendd64c412009-02-04 00:33:20 +00001263 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001264
Dale Johannesendd64c412009-02-04 00:33:20 +00001265 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001266 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001267
1268 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001269 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001270 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001271
Chris Lattner447ff682008-03-11 03:23:40 +00001272 RetOps[0] = Chain; // Update chain.
1273
1274 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001275 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001276 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001277
1278 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001279 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001280}
1281
Dan Gohman98ca4f22009-08-05 01:29:28 +00001282/// LowerCallResult - Lower the result values of a call into the
1283/// appropriate copies out of appropriate physical registers.
1284///
1285SDValue
1286X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001287 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001288 const SmallVectorImpl<ISD::InputArg> &Ins,
1289 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001290 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001291
Chris Lattnere32bbf62007-02-28 07:09:55 +00001292 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001293 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001294 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001295 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001296 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001297 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001298
Chris Lattner3085e152007-02-25 08:59:22 +00001299 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001300 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001301 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001302 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001303
Torok Edwin3f142c32009-02-01 18:15:56 +00001304 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001305 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Dan Gohman98ca4f22009-08-05 01:29:28 +00001306 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001307 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001308 }
1309
Chris Lattner8e6da152008-03-10 21:08:41 +00001310 // If this is a call to a function that returns an fp value on the floating
1311 // point stack, but where we prefer to use the value in xmm registers, copy
1312 // 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 +00001313 if ((VA.getLocReg() == X86::ST0 ||
1314 VA.getLocReg() == X86::ST1) &&
1315 isScalarFPTypeInSSEReg(VA.getValVT())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001316 CopyVT = MVT::f80;
Chris Lattner3085e152007-02-25 08:59:22 +00001317 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001318
Evan Cheng79fb3b42009-02-20 20:43:02 +00001319 SDValue Val;
1320 if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001321 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1322 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1323 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001324 MVT::v2i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001325 Val = Chain.getValue(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001326 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1327 Val, DAG.getConstant(0, MVT::i64));
Evan Cheng242b38b2009-02-23 09:03:22 +00001328 } else {
1329 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001330 MVT::i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001331 Val = Chain.getValue(0);
1332 }
Evan Cheng79fb3b42009-02-20 20:43:02 +00001333 Val = DAG.getNode(ISD::BIT_CONVERT, dl, CopyVT, Val);
1334 } else {
1335 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1336 CopyVT, InFlag).getValue(1);
1337 Val = Chain.getValue(0);
1338 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001339 InFlag = Chain.getValue(2);
Chris Lattner112dedc2007-12-29 06:41:28 +00001340
Dan Gohman37eed792009-02-04 17:28:58 +00001341 if (CopyVT != VA.getValVT()) {
Chris Lattner8e6da152008-03-10 21:08:41 +00001342 // Round the F80 the right size, which also moves to the appropriate xmm
1343 // register.
Dan Gohman37eed792009-02-04 17:28:58 +00001344 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
Chris Lattner8e6da152008-03-10 21:08:41 +00001345 // This truncation won't change the value.
1346 DAG.getIntPtrConstant(1));
1347 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001348
Dan Gohman98ca4f22009-08-05 01:29:28 +00001349 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001350 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001351
Dan Gohman98ca4f22009-08-05 01:29:28 +00001352 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001353}
1354
1355
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001356//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001357// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001358//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001359// StdCall calling convention seems to be standard for many Windows' API
1360// routines and around. It differs from C calling convention just a little:
1361// callee should clean up the stack, not caller. Symbols should be also
1362// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001363// For info on fast calling convention see Fast Calling Convention (tail call)
1364// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001365
Dan Gohman98ca4f22009-08-05 01:29:28 +00001366/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001367/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001368static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1369 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001370 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001371
Dan Gohman98ca4f22009-08-05 01:29:28 +00001372 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001373}
1374
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001375/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001376/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001377static bool
1378ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1379 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001380 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001381
Dan Gohman98ca4f22009-08-05 01:29:28 +00001382 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001383}
1384
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001385/// IsCalleePop - Determines whether the callee is required to pop its
1386/// own arguments. Callee pop is necessary to support tail calls.
Dan Gohmand858e902010-04-17 15:26:15 +00001387bool X86TargetLowering::IsCalleePop(bool IsVarArg,
1388 CallingConv::ID CallingConv) const {
Gordon Henriksen86737662008-01-05 16:56:59 +00001389 if (IsVarArg)
1390 return false;
1391
Dan Gohman095cc292008-09-13 01:54:27 +00001392 switch (CallingConv) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001393 default:
1394 return false;
1395 case CallingConv::X86_StdCall:
1396 return !Subtarget->is64Bit();
1397 case CallingConv::X86_FastCall:
1398 return !Subtarget->is64Bit();
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001399 case CallingConv::X86_ThisCall:
1400 return !Subtarget->is64Bit();
Gordon Henriksen86737662008-01-05 16:56:59 +00001401 case CallingConv::Fast:
Dan Gohman1797ed52010-02-08 20:27:50 +00001402 return GuaranteedTailCallOpt;
Chris Lattner29689432010-03-11 00:22:57 +00001403 case CallingConv::GHC:
1404 return GuaranteedTailCallOpt;
Gordon Henriksen86737662008-01-05 16:56:59 +00001405 }
1406}
1407
Dan Gohman095cc292008-09-13 01:54:27 +00001408/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1409/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001410CCAssignFn *X86TargetLowering::CCAssignFnForNode(CallingConv::ID CC) const {
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001411 if (Subtarget->is64Bit()) {
Chris Lattner29689432010-03-11 00:22:57 +00001412 if (CC == CallingConv::GHC)
1413 return CC_X86_64_GHC;
1414 else if (Subtarget->isTargetWin64())
Anton Korobeynikov8f88cb02008-03-22 20:37:30 +00001415 return CC_X86_Win64_C;
Evan Chenge9ac9e62008-09-07 09:07:23 +00001416 else
1417 return CC_X86_64_C;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001418 }
1419
Gordon Henriksen86737662008-01-05 16:56:59 +00001420 if (CC == CallingConv::X86_FastCall)
1421 return CC_X86_32_FastCall;
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001422 else if (CC == CallingConv::X86_ThisCall)
1423 return CC_X86_32_ThisCall;
Evan Chengb188dd92008-09-10 18:25:29 +00001424 else if (CC == CallingConv::Fast)
1425 return CC_X86_32_FastCC;
Chris Lattner29689432010-03-11 00:22:57 +00001426 else if (CC == CallingConv::GHC)
1427 return CC_X86_32_GHC;
Gordon Henriksen86737662008-01-05 16:56:59 +00001428 else
1429 return CC_X86_32_C;
1430}
1431
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001432/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1433/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001434/// the specific parameter attribute. The copy will be passed as a byval
1435/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001436static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001437CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001438 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1439 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001440 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesendd64c412009-02-04 00:33:20 +00001441 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001442 /*isVolatile*/false, /*AlwaysInline=*/true,
1443 NULL, 0, NULL, 0);
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001444}
1445
Chris Lattner29689432010-03-11 00:22:57 +00001446/// IsTailCallConvention - Return true if the calling convention is one that
1447/// supports tail call optimization.
1448static bool IsTailCallConvention(CallingConv::ID CC) {
1449 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1450}
1451
Evan Cheng0c439eb2010-01-27 00:07:07 +00001452/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1453/// a tailcall target by changing its ABI.
1454static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001455 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001456}
1457
Dan Gohman98ca4f22009-08-05 01:29:28 +00001458SDValue
1459X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001460 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001461 const SmallVectorImpl<ISD::InputArg> &Ins,
1462 DebugLoc dl, SelectionDAG &DAG,
1463 const CCValAssign &VA,
1464 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001465 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001466 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001467 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001468 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001469 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001470 EVT ValVT;
1471
1472 // If value is passed by pointer we have address passed instead of the value
1473 // itself.
1474 if (VA.getLocInfo() == CCValAssign::Indirect)
1475 ValVT = VA.getLocVT();
1476 else
1477 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001478
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001479 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001480 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001481 // In case of tail call optimization mark all arguments mutable. Since they
1482 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001483 if (Flags.isByVal()) {
1484 int FI = MFI->CreateFixedObject(Flags.getByValSize(),
1485 VA.getLocMemOffset(), isImmutable, false);
1486 return DAG.getFrameIndex(FI, getPointerTy());
1487 } else {
1488 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
1489 VA.getLocMemOffset(), isImmutable, false);
1490 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1491 return DAG.getLoad(ValVT, dl, Chain, FIN,
David Greene67c9d422010-02-15 16:53:33 +00001492 PseudoSourceValue::getFixedStack(FI), 0,
1493 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001494 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001495}
1496
Dan Gohman475871a2008-07-27 21:46:04 +00001497SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001498X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001499 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001500 bool isVarArg,
1501 const SmallVectorImpl<ISD::InputArg> &Ins,
1502 DebugLoc dl,
1503 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001504 SmallVectorImpl<SDValue> &InVals)
1505 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001506 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001507 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001508
Gordon Henriksen86737662008-01-05 16:56:59 +00001509 const Function* Fn = MF.getFunction();
1510 if (Fn->hasExternalLinkage() &&
1511 Subtarget->isTargetCygMing() &&
1512 Fn->getName() == "main")
1513 FuncInfo->setForceFramePointer(true);
1514
Evan Cheng1bc78042006-04-26 01:20:17 +00001515 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001516 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001517 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001518
Chris Lattner29689432010-03-11 00:22:57 +00001519 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1520 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001521
Chris Lattner638402b2007-02-28 07:00:42 +00001522 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001523 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001524 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1525 ArgLocs, *DAG.getContext());
1526 CCInfo.AnalyzeFormalArguments(Ins, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001527
Chris Lattnerf39f7712007-02-28 05:46:49 +00001528 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001529 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001530 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1531 CCValAssign &VA = ArgLocs[i];
1532 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1533 // places.
1534 assert(VA.getValNo() != LastVal &&
1535 "Don't support value assigned to multiple locs yet");
1536 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001537
Chris Lattnerf39f7712007-02-28 05:46:49 +00001538 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001539 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001540 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001541 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001542 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001543 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001544 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001545 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001546 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001547 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001548 RC = X86::FR64RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001549 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001550 RC = X86::VR128RegisterClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001551 else if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
1552 RC = X86::VR64RegisterClass;
1553 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001554 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001555
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001556 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001557 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001558
Chris Lattnerf39f7712007-02-28 05:46:49 +00001559 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1560 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1561 // right size.
1562 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001563 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001564 DAG.getValueType(VA.getValVT()));
1565 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001566 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001567 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001568 else if (VA.getLocInfo() == CCValAssign::BCvt)
Anton Korobeynikov6dde14b2009-08-03 08:14:14 +00001569 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001570
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001571 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001572 // Handle MMX values passed in XMM regs.
1573 if (RegVT.isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001574 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1575 ArgValue, DAG.getConstant(0, MVT::i64));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001576 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1577 } else
1578 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001579 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001580 } else {
1581 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001582 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001583 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001584
1585 // If value is passed via pointer - do a load.
1586 if (VA.getLocInfo() == CCValAssign::Indirect)
David Greene67c9d422010-02-15 16:53:33 +00001587 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue, NULL, 0,
1588 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001589
Dan Gohman98ca4f22009-08-05 01:29:28 +00001590 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001591 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001592
Dan Gohman61a92132008-04-21 23:59:07 +00001593 // The x86-64 ABI for returning structs by value requires that we copy
1594 // the sret argument into %rax for the return. Save the argument into
1595 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001596 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001597 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1598 unsigned Reg = FuncInfo->getSRetReturnReg();
1599 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001600 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001601 FuncInfo->setSRetReturnReg(Reg);
1602 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001603 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001604 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001605 }
1606
Chris Lattnerf39f7712007-02-28 05:46:49 +00001607 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001608 // Align stack specially for tail calls.
1609 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001610 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001611
Evan Cheng1bc78042006-04-26 01:20:17 +00001612 // If the function takes variable number of arguments, make a frame index for
1613 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001614 if (isVarArg) {
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001615 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1616 CallConv != CallingConv::X86_ThisCall)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001617 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,
1618 true, false));
Gordon Henriksen86737662008-01-05 16:56:59 +00001619 }
1620 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001621 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1622
1623 // FIXME: We should really autogenerate these arrays
1624 static const unsigned GPR64ArgRegsWin64[] = {
1625 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001626 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001627 static const unsigned XMMArgRegsWin64[] = {
1628 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1629 };
1630 static const unsigned GPR64ArgRegs64Bit[] = {
1631 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1632 };
1633 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001634 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1635 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1636 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001637 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1638
1639 if (IsWin64) {
1640 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1641 GPR64ArgRegs = GPR64ArgRegsWin64;
1642 XMMArgRegs = XMMArgRegsWin64;
1643 } else {
1644 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1645 GPR64ArgRegs = GPR64ArgRegs64Bit;
1646 XMMArgRegs = XMMArgRegs64Bit;
1647 }
1648 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1649 TotalNumIntRegs);
1650 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1651 TotalNumXMMRegs);
1652
Devang Patel578efa92009-06-05 21:57:13 +00001653 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Evan Chengc7ce29b2009-02-13 22:36:38 +00001654 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001655 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001656 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001657 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001658 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00001659 // Kernel mode asks for SSE to be disabled, so don't push them
1660 // on the stack.
1661 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001662
Gordon Henriksen86737662008-01-05 16:56:59 +00001663 // For X86-64, if there are vararg parameters that are passed via
1664 // registers, then we must store them to their spots on the stack so they
1665 // may be loaded by deferencing the result of va_next.
Dan Gohman1e93df62010-04-17 14:41:14 +00001666 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1667 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1668 FuncInfo->setRegSaveFrameIndex(
1669 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
1670 false));
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001671
Gordon Henriksen86737662008-01-05 16:56:59 +00001672 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001673 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001674 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1675 getPointerTy());
1676 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001677 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001678 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1679 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001680 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
1681 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001682 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001683 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001684 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Dan Gohman1e93df62010-04-17 14:41:14 +00001685 PseudoSourceValue::getFixedStack(
1686 FuncInfo->getRegSaveFrameIndex()),
David Greene67c9d422010-02-15 16:53:33 +00001687 Offset, false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001688 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001689 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001690 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001691
Dan Gohmanface41a2009-08-16 21:24:25 +00001692 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1693 // Now store the XMM (fp + vector) parameter registers.
1694 SmallVector<SDValue, 11> SaveXMMOps;
1695 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001696
Dan Gohmanface41a2009-08-16 21:24:25 +00001697 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
1698 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1699 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001700
Dan Gohman1e93df62010-04-17 14:41:14 +00001701 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1702 FuncInfo->getRegSaveFrameIndex()));
1703 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1704 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001705
Dan Gohmanface41a2009-08-16 21:24:25 +00001706 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
1707 unsigned VReg = MF.addLiveIn(XMMArgRegs[NumXMMRegs],
1708 X86::VR128RegisterClass);
1709 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1710 SaveXMMOps.push_back(Val);
1711 }
1712 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1713 MVT::Other,
1714 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001715 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001716
1717 if (!MemOps.empty())
1718 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1719 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001720 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001721 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001722
Gordon Henriksen86737662008-01-05 16:56:59 +00001723 // Some CCs need callee pop.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001724 if (IsCalleePop(isVarArg, CallConv)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001725 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001726 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001727 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001728 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001729 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001730 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001731 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001732
Gordon Henriksen86737662008-01-05 16:56:59 +00001733 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001734 // RegSaveFrameIndex is X86-64 only.
1735 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001736 if (CallConv == CallingConv::X86_FastCall ||
1737 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001738 // fastcc functions can't have varargs.
1739 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001740 }
Evan Cheng25caf632006-05-23 21:06:34 +00001741
Dan Gohman98ca4f22009-08-05 01:29:28 +00001742 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001743}
1744
Dan Gohman475871a2008-07-27 21:46:04 +00001745SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001746X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1747 SDValue StackPtr, SDValue Arg,
1748 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001749 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001750 ISD::ArgFlagsTy Flags) const {
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +00001751 const unsigned FirstStackArgOffset = (Subtarget->isTargetWin64() ? 32 : 0);
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +00001752 unsigned LocMemOffset = FirstStackArgOffset + VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001753 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001754 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001755 if (Flags.isByVal()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001756 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Evan Chengdffbd832008-01-10 00:09:10 +00001757 }
Dale Johannesenace16102009-02-03 19:33:06 +00001758 return DAG.getStore(Chain, dl, Arg, PtrOff,
David Greene67c9d422010-02-15 16:53:33 +00001759 PseudoSourceValue::getStack(), LocMemOffset,
1760 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001761}
1762
Bill Wendling64e87322009-01-16 19:25:27 +00001763/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001764/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001765SDValue
1766X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001767 SDValue &OutRetAddr, SDValue Chain,
1768 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001769 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001770 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001771 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001772 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001773
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001774 // Load the "old" Return address.
David Greene67c9d422010-02-15 16:53:33 +00001775 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, NULL, 0, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001776 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001777}
1778
1779/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1780/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001781static SDValue
1782EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001783 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001784 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001785 // Store the return address to the appropriate stack slot.
1786 if (!FPDiff) return Chain;
1787 // Calculate the new stack slot for the return address.
1788 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001789 int NewReturnAddrFI =
Arnold Schwaighofer92652752010-02-22 16:18:09 +00001790 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001791 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001792 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001793 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
David Greene67c9d422010-02-15 16:53:33 +00001794 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0,
1795 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001796 return Chain;
1797}
1798
Dan Gohman98ca4f22009-08-05 01:29:28 +00001799SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001800X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001801 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001802 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001803 const SmallVectorImpl<ISD::OutputArg> &Outs,
1804 const SmallVectorImpl<ISD::InputArg> &Ins,
1805 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001806 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001807 MachineFunction &MF = DAG.getMachineFunction();
1808 bool Is64Bit = Subtarget->is64Bit();
1809 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00001810 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001811
Evan Cheng5f941932010-02-05 02:21:12 +00001812 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001813 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00001814 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1815 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Evan Cheng022d9e12010-02-02 23:55:14 +00001816 Outs, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00001817
1818 // Sibcalls are automatically detected tailcalls which do not require
1819 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00001820 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00001821 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00001822
1823 if (isTailCall)
1824 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00001825 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00001826
Chris Lattner29689432010-03-11 00:22:57 +00001827 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1828 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001829
Chris Lattner638402b2007-02-28 07:00:42 +00001830 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001831 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001832 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1833 ArgLocs, *DAG.getContext());
1834 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001835
Chris Lattner423c5f42007-02-28 05:31:48 +00001836 // Get a count of how many bytes are to be pushed on the stack.
1837 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00001838 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00001839 // This is a sibcall. The memory operands are available in caller's
1840 // own caller's stack.
1841 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00001842 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00001843 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001844
Gordon Henriksen86737662008-01-05 16:56:59 +00001845 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00001846 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001847 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00001848 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00001849 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1850 FPDiff = NumBytesCallerPushed - NumBytes;
1851
1852 // Set the delta of movement of the returnaddr stackslot.
1853 // But only set if delta is greater than previous delta.
1854 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1855 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1856 }
1857
Evan Chengf22f9b32010-02-06 03:28:46 +00001858 if (!IsSibcall)
1859 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001860
Dan Gohman475871a2008-07-27 21:46:04 +00001861 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001862 // Load return adress for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00001863 if (isTailCall && FPDiff)
1864 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
1865 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001866
Dan Gohman475871a2008-07-27 21:46:04 +00001867 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1868 SmallVector<SDValue, 8> MemOpChains;
1869 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001870
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001871 // Walk the register/memloc assignments, inserting copies/loads. In the case
1872 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00001873 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1874 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001875 EVT RegVT = VA.getLocVT();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001876 SDValue Arg = Outs[i].Val;
1877 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00001878 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00001879
Chris Lattner423c5f42007-02-28 05:31:48 +00001880 // Promote the value if needed.
1881 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001882 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00001883 case CCValAssign::Full: break;
1884 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001885 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001886 break;
1887 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001888 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001889 break;
1890 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001891 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
1892 // Special case: passing MMX values in XMM registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001893 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
1894 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
1895 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001896 } else
1897 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
1898 break;
1899 case CCValAssign::BCvt:
1900 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001901 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001902 case CCValAssign::Indirect: {
1903 // Store the argument.
1904 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00001905 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001906 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
David Greene67c9d422010-02-15 16:53:33 +00001907 PseudoSourceValue::getFixedStack(FI), 0,
1908 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001909 Arg = SpillSlot;
1910 break;
1911 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00001912 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001913
Chris Lattner423c5f42007-02-28 05:31:48 +00001914 if (VA.isRegLoc()) {
1915 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Evan Chengf22f9b32010-02-06 03:28:46 +00001916 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00001917 assert(VA.isMemLoc());
1918 if (StackPtr.getNode() == 0)
1919 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
1920 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1921 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001922 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001923 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001924
Evan Cheng32fe1032006-05-25 00:59:30 +00001925 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001926 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001927 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001928
Evan Cheng347d5f72006-04-28 21:29:37 +00001929 // Build a sequence of copy-to-reg nodes chained together with token chain
1930 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001931 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001932 // Tail call byval lowering might overwrite argument registers so in case of
1933 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001934 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001935 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001936 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001937 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001938 InFlag = Chain.getValue(1);
1939 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001940
Chris Lattner88e1fd52009-07-09 04:24:46 +00001941 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001942 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1943 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001944 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001945 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
1946 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001947 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001948 InFlag);
1949 InFlag = Chain.getValue(1);
1950 } else {
1951 // If we are tail calling and generating PIC/GOT style code load the
1952 // address of the callee into ECX. The value in ecx is used as target of
1953 // the tail jump. This is done to circumvent the ebx/callee-saved problem
1954 // for tail calls on PIC/GOT architectures. Normally we would just put the
1955 // address of GOT into ebx and then call target@PLT. But for tail calls
1956 // ebx would be restored (since ebx is callee saved) before jumping to the
1957 // target@PLT.
1958
1959 // Note: The actual moving to ECX is done further down.
1960 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1961 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1962 !G->getGlobal()->hasProtectedVisibility())
1963 Callee = LowerGlobalAddress(Callee, DAG);
1964 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00001965 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001966 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001967 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001968
Gordon Henriksen86737662008-01-05 16:56:59 +00001969 if (Is64Bit && isVarArg) {
1970 // From AMD64 ABI document:
1971 // For calls that may call functions that use varargs or stdargs
1972 // (prototype-less calls or calls to functions containing ellipsis (...) in
1973 // the declaration) %al is used as hidden argument to specify the number
1974 // of SSE registers used. The contents of %al do not need to match exactly
1975 // the number of registers, but must be an ubound on the number of SSE
1976 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001977
1978 // FIXME: Verify this on Win64
Gordon Henriksen86737662008-01-05 16:56:59 +00001979 // Count the number of XMM registers allocated.
1980 static const unsigned XMMArgRegs[] = {
1981 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1982 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1983 };
1984 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Scott Michelfdc40a02009-02-17 22:15:04 +00001985 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00001986 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001987
Dale Johannesendd64c412009-02-04 00:33:20 +00001988 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00001989 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00001990 InFlag = Chain.getValue(1);
1991 }
1992
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001993
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001994 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001995 if (isTailCall) {
1996 // Force all the incoming stack arguments to be loaded from the stack
1997 // before any new outgoing arguments are stored to the stack, because the
1998 // outgoing stack slots may alias the incoming argument stack slots, and
1999 // the alias isn't otherwise explicit. This is slightly more conservative
2000 // than necessary, because it means that each store effectively depends
2001 // on every argument instead of just those arguments it would clobber.
2002 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2003
Dan Gohman475871a2008-07-27 21:46:04 +00002004 SmallVector<SDValue, 8> MemOpChains2;
2005 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002006 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002007 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002008 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002009 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002010 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2011 CCValAssign &VA = ArgLocs[i];
2012 if (VA.isRegLoc())
2013 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002014 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00002015 SDValue Arg = Outs[i].Val;
2016 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002017 // Create frame index.
2018 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002019 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
David Greene3f2bf852009-11-12 20:49:22 +00002020 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true, false);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002021 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002022
Duncan Sands276dcbd2008-03-21 09:14:45 +00002023 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002024 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002025 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002026 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002027 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002028 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002029 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002030
Dan Gohman98ca4f22009-08-05 01:29:28 +00002031 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2032 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002033 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002034 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002035 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002036 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002037 DAG.getStore(ArgChain, dl, Arg, FIN,
David Greene67c9d422010-02-15 16:53:33 +00002038 PseudoSourceValue::getFixedStack(FI), 0,
2039 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002040 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002041 }
2042 }
2043
2044 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002045 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002046 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002047
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002048 // Copy arguments to their registers.
2049 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002050 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002051 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002052 InFlag = Chain.getValue(1);
2053 }
Dan Gohman475871a2008-07-27 21:46:04 +00002054 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002055
Gordon Henriksen86737662008-01-05 16:56:59 +00002056 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002057 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002058 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002059 }
2060
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002061 bool WasGlobalOrExternal = false;
2062 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2063 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2064 // In the 64-bit large code model, we have to make all calls
2065 // through a register, since the call instruction's 32-bit
2066 // pc-relative offset may not be large enough to hold the whole
2067 // address.
2068 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
2069 WasGlobalOrExternal = true;
2070 // If the callee is a GlobalAddress node (quite common, every direct call
2071 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2072 // it.
2073
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002074 // We should use extra load for direct calls to dllimported functions in
2075 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002076 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002077 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002078 unsigned char OpFlags = 0;
Eric Christopherfd179292009-08-27 18:07:15 +00002079
Chris Lattner48a7d022009-07-09 05:02:21 +00002080 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2081 // external symbols most go through the PLT in PIC mode. If the symbol
2082 // has hidden or protected visibility, or if it is static or local, then
2083 // we don't need to use the PLT - we can directly call it.
2084 if (Subtarget->isTargetELF() &&
2085 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002086 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002087 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002088 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002089 (GV->isDeclaration() || GV->isWeakForLinker()) &&
2090 Subtarget->getDarwinVers() < 9) {
2091 // PC-relative references to external symbols should go through $stub,
2092 // unless we're building with the leopard linker or later, which
2093 // automatically synthesizes these stubs.
2094 OpFlags = X86II::MO_DARWIN_STUB;
2095 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002096
Chris Lattner74e726e2009-07-09 05:27:35 +00002097 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002098 G->getOffset(), OpFlags);
2099 }
Bill Wendling056292f2008-09-16 21:48:12 +00002100 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002101 WasGlobalOrExternal = true;
Chris Lattner48a7d022009-07-09 05:02:21 +00002102 unsigned char OpFlags = 0;
2103
2104 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to external
2105 // symbols should go through the PLT.
2106 if (Subtarget->isTargetELF() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002107 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002108 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002109 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002110 Subtarget->getDarwinVers() < 9) {
2111 // PC-relative references to external symbols should go through $stub,
2112 // unless we're building with the leopard linker or later, which
2113 // automatically synthesizes these stubs.
2114 OpFlags = X86II::MO_DARWIN_STUB;
2115 }
Eric Christopherfd179292009-08-27 18:07:15 +00002116
Chris Lattner48a7d022009-07-09 05:02:21 +00002117 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2118 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002119 }
2120
Chris Lattnerd96d0722007-02-25 06:40:16 +00002121 // Returns a chain & a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +00002122 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00002123 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002124
Evan Chengf22f9b32010-02-06 03:28:46 +00002125 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002126 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2127 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002128 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002129 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002130
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002131 Ops.push_back(Chain);
2132 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002133
Dan Gohman98ca4f22009-08-05 01:29:28 +00002134 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002135 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002136
Gordon Henriksen86737662008-01-05 16:56:59 +00002137 // Add argument registers to the end of the list so that they are known live
2138 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002139 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2140 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2141 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002142
Evan Cheng586ccac2008-03-18 23:36:35 +00002143 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002144 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002145 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2146
2147 // Add an implicit use of AL for x86 vararg functions.
2148 if (Is64Bit && isVarArg)
Owen Anderson825b72b2009-08-11 20:47:22 +00002149 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002150
Gabor Greifba36cb52008-08-28 21:40:38 +00002151 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002152 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002153
Dan Gohman98ca4f22009-08-05 01:29:28 +00002154 if (isTailCall) {
2155 // If this is the first return lowered for this function, add the regs
2156 // to the liveout set for the function.
2157 if (MF.getRegInfo().liveout_empty()) {
2158 SmallVector<CCValAssign, 16> RVLocs;
2159 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
2160 *DAG.getContext());
2161 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
2162 for (unsigned i = 0; i != RVLocs.size(); ++i)
2163 if (RVLocs[i].isRegLoc())
2164 MF.getRegInfo().addLiveOut(RVLocs[i].getLocReg());
2165 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002166 return DAG.getNode(X86ISD::TC_RETURN, dl,
2167 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002168 }
2169
Dale Johannesenace16102009-02-03 19:33:06 +00002170 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002171 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002172
Chris Lattner2d297092006-05-23 18:50:38 +00002173 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002174 unsigned NumBytesForCalleeToPush;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002175 if (IsCalleePop(isVarArg, CallConv))
Gordon Henriksen86737662008-01-05 16:56:59 +00002176 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002177 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002178 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002179 // pops the hidden struct pointer, so we have to push it back.
2180 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002181 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002182 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002183 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002184
Gordon Henriksenae636f82008-01-03 16:47:34 +00002185 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002186 if (!IsSibcall) {
2187 Chain = DAG.getCALLSEQ_END(Chain,
2188 DAG.getIntPtrConstant(NumBytes, true),
2189 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2190 true),
2191 InFlag);
2192 InFlag = Chain.getValue(1);
2193 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002194
Chris Lattner3085e152007-02-25 08:59:22 +00002195 // Handle result values, copying them out of physregs into vregs that we
2196 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002197 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2198 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002199}
2200
Evan Cheng25ab6902006-09-08 06:48:29 +00002201
2202//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002203// Fast Calling Convention (tail call) implementation
2204//===----------------------------------------------------------------------===//
2205
2206// Like std call, callee cleans arguments, convention except that ECX is
2207// reserved for storing the tail called function address. Only 2 registers are
2208// free for argument passing (inreg). Tail call optimization is performed
2209// provided:
2210// * tailcallopt is enabled
2211// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002212// On X86_64 architecture with GOT-style position independent code only local
2213// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002214// To keep the stack aligned according to platform abi the function
2215// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2216// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002217// If a tail called function callee has more arguments than the caller the
2218// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002219// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002220// original REtADDR, but before the saved framepointer or the spilled registers
2221// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2222// stack layout:
2223// arg1
2224// arg2
2225// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002226// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002227// move area ]
2228// (possible EBP)
2229// ESI
2230// EDI
2231// local1 ..
2232
2233/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2234/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002235unsigned
2236X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2237 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002238 MachineFunction &MF = DAG.getMachineFunction();
2239 const TargetMachine &TM = MF.getTarget();
2240 const TargetFrameInfo &TFI = *TM.getFrameInfo();
2241 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002242 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002243 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002244 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002245 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2246 // Number smaller than 12 so just add the difference.
2247 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2248 } else {
2249 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002250 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002251 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002252 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002253 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002254}
2255
Evan Cheng5f941932010-02-05 02:21:12 +00002256/// MatchingStackOffset - Return true if the given stack call argument is
2257/// already available in the same position (relatively) of the caller's
2258/// incoming argument stack.
2259static
2260bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2261 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2262 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002263 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2264 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002265 if (Arg.getOpcode() == ISD::CopyFromReg) {
2266 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
2267 if (!VR || TargetRegisterInfo::isPhysicalRegister(VR))
2268 return false;
2269 MachineInstr *Def = MRI->getVRegDef(VR);
2270 if (!Def)
2271 return false;
2272 if (!Flags.isByVal()) {
2273 if (!TII->isLoadFromStackSlot(Def, FI))
2274 return false;
2275 } else {
2276 unsigned Opcode = Def->getOpcode();
2277 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2278 Def->getOperand(1).isFI()) {
2279 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002280 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002281 } else
2282 return false;
2283 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002284 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2285 if (Flags.isByVal())
2286 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002287 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002288 // define @foo(%struct.X* %A) {
2289 // tail call @bar(%struct.X* byval %A)
2290 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002291 return false;
2292 SDValue Ptr = Ld->getBasePtr();
2293 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2294 if (!FINode)
2295 return false;
2296 FI = FINode->getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002297 } else
2298 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002299
Evan Cheng4cae1332010-03-05 08:38:04 +00002300 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002301 if (!MFI->isFixedObjectIndex(FI))
2302 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002303 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002304}
2305
Dan Gohman98ca4f22009-08-05 01:29:28 +00002306/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2307/// for tail call optimization. Targets which want to do tail call
2308/// optimization should implement this function.
2309bool
2310X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002311 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002312 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002313 bool isCalleeStructRet,
2314 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002315 const SmallVectorImpl<ISD::OutputArg> &Outs,
2316 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002317 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002318 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002319 CalleeCC != CallingConv::C)
2320 return false;
2321
Evan Cheng7096ae42010-01-29 06:45:59 +00002322 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002323 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002324 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002325 CallingConv::ID CallerCC = CallerF->getCallingConv();
2326 bool CCMatch = CallerCC == CalleeCC;
2327
Dan Gohman1797ed52010-02-08 20:27:50 +00002328 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002329 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002330 return true;
2331 return false;
2332 }
2333
Evan Chengb2c92902010-02-02 02:22:50 +00002334 // Look for obvious safe cases to perform tail call optimization that does not
2335 // requite ABI changes. This is what gcc calls sibcall.
2336
Evan Cheng2c12cb42010-03-26 16:26:03 +00002337 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2338 // emit a special epilogue.
2339 if (RegInfo->needsStackRealignment(MF))
2340 return false;
2341
Evan Cheng3c262ee2010-03-26 02:13:13 +00002342 // Do not sibcall optimize vararg calls unless the call site is not passing any
2343 // arguments.
2344 if (isVarArg && !Outs.empty())
Evan Cheng843bd692010-01-31 06:44:49 +00002345 return false;
2346
Evan Chenga375d472010-03-15 18:54:48 +00002347 // Also avoid sibcall optimization if either caller or callee uses struct
2348 // return semantics.
2349 if (isCalleeStructRet || isCallerStructRet)
2350 return false;
2351
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002352 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2353 // Therefore if it's not used by the call it is not safe to optimize this into
2354 // a sibcall.
2355 bool Unused = false;
2356 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2357 if (!Ins[i].Used) {
2358 Unused = true;
2359 break;
2360 }
2361 }
2362 if (Unused) {
2363 SmallVector<CCValAssign, 16> RVLocs;
2364 CCState CCInfo(CalleeCC, false, getTargetMachine(),
2365 RVLocs, *DAG.getContext());
2366 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002367 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002368 CCValAssign &VA = RVLocs[i];
2369 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2370 return false;
2371 }
2372 }
2373
Evan Cheng13617962010-04-30 01:12:32 +00002374 // If the calling conventions do not match, then we'd better make sure the
2375 // results are returned in the same way as what the caller expects.
2376 if (!CCMatch) {
2377 SmallVector<CCValAssign, 16> RVLocs1;
2378 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
2379 RVLocs1, *DAG.getContext());
2380 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2381
2382 SmallVector<CCValAssign, 16> RVLocs2;
2383 CCState CCInfo2(CallerCC, false, getTargetMachine(),
2384 RVLocs2, *DAG.getContext());
2385 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2386
2387 if (RVLocs1.size() != RVLocs2.size())
2388 return false;
2389 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2390 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2391 return false;
2392 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2393 return false;
2394 if (RVLocs1[i].isRegLoc()) {
2395 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2396 return false;
2397 } else {
2398 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2399 return false;
2400 }
2401 }
2402 }
2403
Evan Chenga6bff982010-01-30 01:22:00 +00002404 // If the callee takes no arguments then go on to check the results of the
2405 // call.
2406 if (!Outs.empty()) {
2407 // Check if stack adjustment is needed. For now, do not do this if any
2408 // argument is passed on the stack.
2409 SmallVector<CCValAssign, 16> ArgLocs;
2410 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
2411 ArgLocs, *DAG.getContext());
2412 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CalleeCC));
Evan Chengb2c92902010-02-02 02:22:50 +00002413 if (CCInfo.getNextStackOffset()) {
2414 MachineFunction &MF = DAG.getMachineFunction();
2415 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2416 return false;
2417 if (Subtarget->isTargetWin64())
2418 // Win64 ABI has additional complications.
2419 return false;
2420
2421 // Check if the arguments are already laid out in the right way as
2422 // the caller's fixed stack objects.
2423 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002424 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2425 const X86InstrInfo *TII =
2426 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002427 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2428 CCValAssign &VA = ArgLocs[i];
2429 EVT RegVT = VA.getLocVT();
2430 SDValue Arg = Outs[i].Val;
2431 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002432 if (VA.getLocInfo() == CCValAssign::Indirect)
2433 return false;
2434 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002435 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2436 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002437 return false;
2438 }
2439 }
2440 }
Evan Chenga6bff982010-01-30 01:22:00 +00002441 }
Evan Chengb1712452010-01-27 06:25:16 +00002442
Evan Cheng86809cc2010-02-03 03:28:02 +00002443 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002444}
2445
Dan Gohman3df24e62008-09-03 23:12:08 +00002446FastISel *
Chris Lattnered3a8062010-04-05 06:05:26 +00002447X86TargetLowering::createFastISel(MachineFunction &mf,
Evan Chengddc419c2010-01-26 19:04:47 +00002448 DenseMap<const Value *, unsigned> &vm,
2449 DenseMap<const BasicBlock*, MachineBasicBlock*> &bm,
Dan Gohmanf81eca02010-04-22 20:46:50 +00002450 DenseMap<const AllocaInst *, int> &am,
2451 std::vector<std::pair<MachineInstr*, unsigned> > &pn
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002452#ifndef NDEBUG
Dan Gohman25208642010-04-14 19:53:31 +00002453 , SmallSet<const Instruction *, 8> &cil
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002454#endif
Dan Gohmand858e902010-04-17 15:26:15 +00002455 ) const {
Dan Gohmanf81eca02010-04-22 20:46:50 +00002456 return X86::createFastISel(mf, vm, bm, am, pn
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002457#ifndef NDEBUG
2458 , cil
2459#endif
2460 );
Dan Gohmand9f3c482008-08-19 21:32:53 +00002461}
2462
2463
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002464//===----------------------------------------------------------------------===//
2465// Other Lowering Hooks
2466//===----------------------------------------------------------------------===//
2467
2468
Dan Gohmand858e902010-04-17 15:26:15 +00002469SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002470 MachineFunction &MF = DAG.getMachineFunction();
2471 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2472 int ReturnAddrIndex = FuncInfo->getRAIndex();
2473
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002474 if (ReturnAddrIndex == 0) {
2475 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002476 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002477 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Arnold Schwaighofer92652752010-02-22 16:18:09 +00002478 false, false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002479 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002480 }
2481
Evan Cheng25ab6902006-09-08 06:48:29 +00002482 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002483}
2484
2485
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002486bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2487 bool hasSymbolicDisplacement) {
2488 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002489 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002490 return false;
2491
2492 // If we don't have a symbolic displacement - we don't have any extra
2493 // restrictions.
2494 if (!hasSymbolicDisplacement)
2495 return true;
2496
2497 // FIXME: Some tweaks might be needed for medium code model.
2498 if (M != CodeModel::Small && M != CodeModel::Kernel)
2499 return false;
2500
2501 // For small code model we assume that latest object is 16MB before end of 31
2502 // bits boundary. We may also accept pretty large negative constants knowing
2503 // that all objects are in the positive half of address space.
2504 if (M == CodeModel::Small && Offset < 16*1024*1024)
2505 return true;
2506
2507 // For kernel code model we know that all object resist in the negative half
2508 // of 32bits address space. We may not accept negative offsets, since they may
2509 // be just off and we may accept pretty large positive ones.
2510 if (M == CodeModel::Kernel && Offset > 0)
2511 return true;
2512
2513 return false;
2514}
2515
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002516/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2517/// specific condition code, returning the condition code and the LHS/RHS of the
2518/// comparison to make.
2519static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2520 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002521 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002522 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2523 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2524 // X > -1 -> X == 0, jump !sign.
2525 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002526 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002527 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2528 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002529 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002530 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002531 // X < 1 -> X <= 0
2532 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002533 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002534 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002535 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002536
Evan Chengd9558e02006-01-06 00:43:03 +00002537 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002538 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002539 case ISD::SETEQ: return X86::COND_E;
2540 case ISD::SETGT: return X86::COND_G;
2541 case ISD::SETGE: return X86::COND_GE;
2542 case ISD::SETLT: return X86::COND_L;
2543 case ISD::SETLE: return X86::COND_LE;
2544 case ISD::SETNE: return X86::COND_NE;
2545 case ISD::SETULT: return X86::COND_B;
2546 case ISD::SETUGT: return X86::COND_A;
2547 case ISD::SETULE: return X86::COND_BE;
2548 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002549 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002550 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002551
Chris Lattner4c78e022008-12-23 23:42:27 +00002552 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002553
Chris Lattner4c78e022008-12-23 23:42:27 +00002554 // If LHS is a foldable load, but RHS is not, flip the condition.
2555 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2556 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2557 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2558 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002559 }
2560
Chris Lattner4c78e022008-12-23 23:42:27 +00002561 switch (SetCCOpcode) {
2562 default: break;
2563 case ISD::SETOLT:
2564 case ISD::SETOLE:
2565 case ISD::SETUGT:
2566 case ISD::SETUGE:
2567 std::swap(LHS, RHS);
2568 break;
2569 }
2570
2571 // On a floating point condition, the flags are set as follows:
2572 // ZF PF CF op
2573 // 0 | 0 | 0 | X > Y
2574 // 0 | 0 | 1 | X < Y
2575 // 1 | 0 | 0 | X == Y
2576 // 1 | 1 | 1 | unordered
2577 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002578 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002579 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002580 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002581 case ISD::SETOLT: // flipped
2582 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002583 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002584 case ISD::SETOLE: // flipped
2585 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002586 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002587 case ISD::SETUGT: // flipped
2588 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002589 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002590 case ISD::SETUGE: // flipped
2591 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002592 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002593 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002594 case ISD::SETNE: return X86::COND_NE;
2595 case ISD::SETUO: return X86::COND_P;
2596 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002597 case ISD::SETOEQ:
2598 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002599 }
Evan Chengd9558e02006-01-06 00:43:03 +00002600}
2601
Evan Cheng4a460802006-01-11 00:33:36 +00002602/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2603/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002604/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002605static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002606 switch (X86CC) {
2607 default:
2608 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002609 case X86::COND_B:
2610 case X86::COND_BE:
2611 case X86::COND_E:
2612 case X86::COND_P:
2613 case X86::COND_A:
2614 case X86::COND_AE:
2615 case X86::COND_NE:
2616 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002617 return true;
2618 }
2619}
2620
Evan Chengeb2f9692009-10-27 19:56:55 +00002621/// isFPImmLegal - Returns true if the target can instruction select the
2622/// specified FP immediate natively. If false, the legalizer will
2623/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002624bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00002625 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
2626 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
2627 return true;
2628 }
2629 return false;
2630}
2631
Nate Begeman9008ca62009-04-27 18:41:29 +00002632/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2633/// the specified range (L, H].
2634static bool isUndefOrInRange(int Val, int Low, int Hi) {
2635 return (Val < 0) || (Val >= Low && Val < Hi);
2636}
2637
2638/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2639/// specified value.
2640static bool isUndefOrEqual(int Val, int CmpVal) {
2641 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002642 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002643 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002644}
2645
Nate Begeman9008ca62009-04-27 18:41:29 +00002646/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2647/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2648/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00002649static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002650 if (VT == MVT::v4f32 || VT == MVT::v4i32 || VT == MVT::v4i16)
Nate Begeman9008ca62009-04-27 18:41:29 +00002651 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002652 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00002653 return (Mask[0] < 2 && Mask[1] < 2);
2654 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002655}
2656
Nate Begeman9008ca62009-04-27 18:41:29 +00002657bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002658 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002659 N->getMask(M);
2660 return ::isPSHUFDMask(M, N->getValueType(0));
2661}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002662
Nate Begeman9008ca62009-04-27 18:41:29 +00002663/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2664/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00002665static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002666 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002667 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002668
Nate Begeman9008ca62009-04-27 18:41:29 +00002669 // Lower quadword copied in order or undef.
2670 for (int i = 0; i != 4; ++i)
2671 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002672 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002673
Evan Cheng506d3df2006-03-29 23:07:14 +00002674 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002675 for (int i = 4; i != 8; ++i)
2676 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
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 return true;
2680}
2681
Nate Begeman9008ca62009-04-27 18:41:29 +00002682bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002683 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002684 N->getMask(M);
2685 return ::isPSHUFHWMask(M, N->getValueType(0));
2686}
Evan Cheng506d3df2006-03-29 23:07:14 +00002687
Nate Begeman9008ca62009-04-27 18:41:29 +00002688/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2689/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00002690static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002691 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002692 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002693
Rafael Espindola15684b22009-04-24 12:40:33 +00002694 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00002695 for (int i = 4; i != 8; ++i)
2696 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00002697 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002698
Rafael Espindola15684b22009-04-24 12:40:33 +00002699 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002700 for (int i = 0; i != 4; ++i)
2701 if (Mask[i] >= 4)
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 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002705}
2706
Nate Begeman9008ca62009-04-27 18:41:29 +00002707bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002708 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002709 N->getMask(M);
2710 return ::isPSHUFLWMask(M, N->getValueType(0));
2711}
2712
Nate Begemana09008b2009-10-19 02:17:23 +00002713/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
2714/// is suitable for input to PALIGNR.
2715static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
2716 bool hasSSSE3) {
2717 int i, e = VT.getVectorNumElements();
2718
2719 // Do not handle v2i64 / v2f64 shuffles with palignr.
2720 if (e < 4 || !hasSSSE3)
2721 return false;
2722
2723 for (i = 0; i != e; ++i)
2724 if (Mask[i] >= 0)
2725 break;
2726
2727 // All undef, not a palignr.
2728 if (i == e)
2729 return false;
2730
2731 // Determine if it's ok to perform a palignr with only the LHS, since we
2732 // don't have access to the actual shuffle elements to see if RHS is undef.
2733 bool Unary = Mask[i] < (int)e;
2734 bool NeedsUnary = false;
2735
2736 int s = Mask[i] - i;
2737
2738 // Check the rest of the elements to see if they are consecutive.
2739 for (++i; i != e; ++i) {
2740 int m = Mask[i];
2741 if (m < 0)
2742 continue;
2743
2744 Unary = Unary && (m < (int)e);
2745 NeedsUnary = NeedsUnary || (m < s);
2746
2747 if (NeedsUnary && !Unary)
2748 return false;
2749 if (Unary && m != ((s+i) & (e-1)))
2750 return false;
2751 if (!Unary && m != (s+i))
2752 return false;
2753 }
2754 return true;
2755}
2756
2757bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
2758 SmallVector<int, 8> M;
2759 N->getMask(M);
2760 return ::isPALIGNRMask(M, N->getValueType(0), true);
2761}
2762
Evan Cheng14aed5e2006-03-24 01:18:28 +00002763/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2764/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00002765static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002766 int NumElems = VT.getVectorNumElements();
2767 if (NumElems != 2 && NumElems != 4)
2768 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002769
Nate Begeman9008ca62009-04-27 18:41:29 +00002770 int Half = NumElems / 2;
2771 for (int i = 0; i < Half; ++i)
2772 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002773 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002774 for (int i = Half; i < NumElems; ++i)
2775 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002776 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002777
Evan Cheng14aed5e2006-03-24 01:18:28 +00002778 return true;
2779}
2780
Nate Begeman9008ca62009-04-27 18:41:29 +00002781bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
2782 SmallVector<int, 8> M;
2783 N->getMask(M);
2784 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002785}
2786
Evan Cheng213d2cf2007-05-17 18:45:50 +00002787/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002788/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2789/// half elements to come from vector 1 (which would equal the dest.) and
2790/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00002791static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002792 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002793
2794 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00002795 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002796
Nate Begeman9008ca62009-04-27 18:41:29 +00002797 int Half = NumElems / 2;
2798 for (int i = 0; i < Half; ++i)
2799 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002800 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002801 for (int i = Half; i < NumElems; ++i)
2802 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002803 return false;
2804 return true;
2805}
2806
Nate Begeman9008ca62009-04-27 18:41:29 +00002807static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
2808 SmallVector<int, 8> M;
2809 N->getMask(M);
2810 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002811}
2812
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002813/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2814/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002815bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
2816 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002817 return false;
2818
Evan Cheng2064a2b2006-03-28 06:50:32 +00002819 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00002820 return isUndefOrEqual(N->getMaskElt(0), 6) &&
2821 isUndefOrEqual(N->getMaskElt(1), 7) &&
2822 isUndefOrEqual(N->getMaskElt(2), 2) &&
2823 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002824}
2825
Nate Begeman0b10b912009-11-07 23:17:15 +00002826/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2827/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2828/// <2, 3, 2, 3>
2829bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
2830 unsigned NumElems = N->getValueType(0).getVectorNumElements();
2831
2832 if (NumElems != 4)
2833 return false;
2834
2835 return isUndefOrEqual(N->getMaskElt(0), 2) &&
2836 isUndefOrEqual(N->getMaskElt(1), 3) &&
2837 isUndefOrEqual(N->getMaskElt(2), 2) &&
2838 isUndefOrEqual(N->getMaskElt(3), 3);
2839}
2840
Evan Cheng5ced1d82006-04-06 23:23:56 +00002841/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2842/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00002843bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
2844 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002845
Evan Cheng5ced1d82006-04-06 23:23:56 +00002846 if (NumElems != 2 && NumElems != 4)
2847 return false;
2848
Evan Chengc5cdff22006-04-07 21:53:05 +00002849 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002850 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002851 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002852
Evan Chengc5cdff22006-04-07 21:53:05 +00002853 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002854 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002855 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002856
2857 return true;
2858}
2859
Nate Begeman0b10b912009-11-07 23:17:15 +00002860/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
2861/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
2862bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002863 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002864
Evan Cheng5ced1d82006-04-06 23:23:56 +00002865 if (NumElems != 2 && NumElems != 4)
2866 return false;
2867
Evan Chengc5cdff22006-04-07 21:53:05 +00002868 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002869 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002870 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002871
Nate Begeman9008ca62009-04-27 18:41:29 +00002872 for (unsigned i = 0; i < NumElems/2; ++i)
2873 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002874 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002875
2876 return true;
2877}
2878
Evan Cheng0038e592006-03-28 00:39:58 +00002879/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2880/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00002881static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002882 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002883 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002884 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00002885 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002886
Nate Begeman9008ca62009-04-27 18:41:29 +00002887 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2888 int BitI = Mask[i];
2889 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002890 if (!isUndefOrEqual(BitI, j))
2891 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002892 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002893 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002894 return false;
2895 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002896 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002897 return false;
2898 }
Evan Cheng0038e592006-03-28 00:39:58 +00002899 }
Evan Cheng0038e592006-03-28 00:39:58 +00002900 return true;
2901}
2902
Nate Begeman9008ca62009-04-27 18:41:29 +00002903bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2904 SmallVector<int, 8> M;
2905 N->getMask(M);
2906 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002907}
2908
Evan Cheng4fcb9222006-03-28 02:43:26 +00002909/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2910/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00002911static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002912 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002913 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002914 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00002915 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002916
Nate Begeman9008ca62009-04-27 18:41:29 +00002917 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2918 int BitI = Mask[i];
2919 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00002920 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00002921 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002922 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002923 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002924 return false;
2925 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002926 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002927 return false;
2928 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002929 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002930 return true;
2931}
2932
Nate Begeman9008ca62009-04-27 18:41:29 +00002933bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2934 SmallVector<int, 8> M;
2935 N->getMask(M);
2936 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002937}
2938
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002939/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2940/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2941/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00002942static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002943 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002944 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002945 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002946
Nate Begeman9008ca62009-04-27 18:41:29 +00002947 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
2948 int BitI = Mask[i];
2949 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002950 if (!isUndefOrEqual(BitI, j))
2951 return false;
2952 if (!isUndefOrEqual(BitI1, j))
2953 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002954 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002955 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002956}
2957
Nate Begeman9008ca62009-04-27 18:41:29 +00002958bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
2959 SmallVector<int, 8> M;
2960 N->getMask(M);
2961 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
2962}
2963
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002964/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2965/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2966/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00002967static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002968 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002969 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2970 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002971
Nate Begeman9008ca62009-04-27 18:41:29 +00002972 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2973 int BitI = Mask[i];
2974 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002975 if (!isUndefOrEqual(BitI, j))
2976 return false;
2977 if (!isUndefOrEqual(BitI1, j))
2978 return false;
2979 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002980 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002981}
2982
Nate Begeman9008ca62009-04-27 18:41:29 +00002983bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
2984 SmallVector<int, 8> M;
2985 N->getMask(M);
2986 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
2987}
2988
Evan Cheng017dcc62006-04-21 01:05:10 +00002989/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2990/// specifies a shuffle of elements that is suitable for input to MOVSS,
2991/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00002992static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00002993 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002994 return false;
Eli Friedman10415532009-06-06 06:05:10 +00002995
2996 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002997
Nate Begeman9008ca62009-04-27 18:41:29 +00002998 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002999 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003000
Nate Begeman9008ca62009-04-27 18:41:29 +00003001 for (int i = 1; i < NumElts; ++i)
3002 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003003 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003004
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003005 return true;
3006}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003007
Nate Begeman9008ca62009-04-27 18:41:29 +00003008bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3009 SmallVector<int, 8> M;
3010 N->getMask(M);
3011 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003012}
3013
Evan Cheng017dcc62006-04-21 01:05:10 +00003014/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3015/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003016/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003017static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003018 bool V2IsSplat = false, bool V2IsUndef = false) {
3019 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003020 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003021 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003022
Nate Begeman9008ca62009-04-27 18:41:29 +00003023 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003024 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003025
Nate Begeman9008ca62009-04-27 18:41:29 +00003026 for (int i = 1; i < NumOps; ++i)
3027 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3028 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3029 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003030 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003031
Evan Cheng39623da2006-04-20 08:58:49 +00003032 return true;
3033}
3034
Nate Begeman9008ca62009-04-27 18:41:29 +00003035static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003036 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003037 SmallVector<int, 8> M;
3038 N->getMask(M);
3039 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003040}
3041
Evan Chengd9539472006-04-14 21:59:03 +00003042/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3043/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003044bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
3045 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003046 return false;
3047
3048 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00003049 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003050 int Elt = N->getMaskElt(i);
3051 if (Elt >= 0 && Elt != 1)
3052 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00003053 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003054
3055 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003056 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003057 int Elt = N->getMaskElt(i);
3058 if (Elt >= 0 && Elt != 3)
3059 return false;
3060 if (Elt == 3)
3061 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003062 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003063 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00003064 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003065 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003066}
3067
3068/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3069/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003070bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
3071 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003072 return false;
3073
3074 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00003075 for (unsigned i = 0; i < 2; ++i)
3076 if (N->getMaskElt(i) > 0)
3077 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003078
3079 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003080 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003081 int Elt = N->getMaskElt(i);
3082 if (Elt >= 0 && Elt != 2)
3083 return false;
3084 if (Elt == 2)
3085 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003086 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003087 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003088 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003089}
3090
Evan Cheng0b457f02008-09-25 20:50:48 +00003091/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3092/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003093bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3094 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003095
Nate Begeman9008ca62009-04-27 18:41:29 +00003096 for (int i = 0; i < e; ++i)
3097 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003098 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003099 for (int i = 0; i < e; ++i)
3100 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003101 return false;
3102 return true;
3103}
3104
Evan Cheng63d33002006-03-22 08:01:21 +00003105/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003106/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003107unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003108 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3109 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3110
Evan Chengb9df0ca2006-03-22 02:53:00 +00003111 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3112 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003113 for (int i = 0; i < NumOperands; ++i) {
3114 int Val = SVOp->getMaskElt(NumOperands-i-1);
3115 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003116 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003117 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003118 if (i != NumOperands - 1)
3119 Mask <<= Shift;
3120 }
Evan Cheng63d33002006-03-22 08:01:21 +00003121 return Mask;
3122}
3123
Evan Cheng506d3df2006-03-29 23:07:14 +00003124/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003125/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003126unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003127 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003128 unsigned Mask = 0;
3129 // 8 nodes, but we only care about the last 4.
3130 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003131 int Val = SVOp->getMaskElt(i);
3132 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003133 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003134 if (i != 4)
3135 Mask <<= 2;
3136 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003137 return Mask;
3138}
3139
3140/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003141/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003142unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003143 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003144 unsigned Mask = 0;
3145 // 8 nodes, but we only care about the first 4.
3146 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003147 int Val = SVOp->getMaskElt(i);
3148 if (Val >= 0)
3149 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003150 if (i != 0)
3151 Mask <<= 2;
3152 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003153 return Mask;
3154}
3155
Nate Begemana09008b2009-10-19 02:17:23 +00003156/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3157/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3158unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3159 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3160 EVT VVT = N->getValueType(0);
3161 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3162 int Val = 0;
3163
3164 unsigned i, e;
3165 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3166 Val = SVOp->getMaskElt(i);
3167 if (Val >= 0)
3168 break;
3169 }
3170 return (Val - i) * EltSize;
3171}
3172
Evan Cheng37b73872009-07-30 08:33:02 +00003173/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3174/// constant +0.0.
3175bool X86::isZeroNode(SDValue Elt) {
3176 return ((isa<ConstantSDNode>(Elt) &&
3177 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
3178 (isa<ConstantFPSDNode>(Elt) &&
3179 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3180}
3181
Nate Begeman9008ca62009-04-27 18:41:29 +00003182/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3183/// their permute mask.
3184static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3185 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003186 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003187 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003188 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003189
Nate Begeman5a5ca152009-04-29 05:20:52 +00003190 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003191 int idx = SVOp->getMaskElt(i);
3192 if (idx < 0)
3193 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003194 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003195 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003196 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003197 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003198 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003199 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3200 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003201}
3202
Evan Cheng779ccea2007-12-07 21:30:01 +00003203/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3204/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003205static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003206 unsigned NumElems = VT.getVectorNumElements();
3207 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003208 int idx = Mask[i];
3209 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003210 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003211 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003212 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003213 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003214 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003215 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003216}
3217
Evan Cheng533a0aa2006-04-19 20:35:22 +00003218/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3219/// match movhlps. The lower half elements should come from upper half of
3220/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003221/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003222static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3223 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003224 return false;
3225 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003226 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003227 return false;
3228 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003229 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003230 return false;
3231 return true;
3232}
3233
Evan Cheng5ced1d82006-04-06 23:23:56 +00003234/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003235/// is promoted to a vector. It also returns the LoadSDNode by reference if
3236/// required.
3237static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003238 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3239 return false;
3240 N = N->getOperand(0).getNode();
3241 if (!ISD::isNON_EXTLoad(N))
3242 return false;
3243 if (LD)
3244 *LD = cast<LoadSDNode>(N);
3245 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003246}
3247
Evan Cheng533a0aa2006-04-19 20:35:22 +00003248/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3249/// match movlp{s|d}. The lower half elements should come from lower half of
3250/// V1 (and in order), and the upper half elements should come from the upper
3251/// half of V2 (and in order). And since V1 will become the source of the
3252/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003253static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3254 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003255 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003256 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003257 // Is V2 is a vector load, don't do this transformation. We will try to use
3258 // load folding shufps op.
3259 if (ISD::isNON_EXTLoad(V2))
3260 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003261
Nate Begeman5a5ca152009-04-29 05:20:52 +00003262 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003263
Evan Cheng533a0aa2006-04-19 20:35:22 +00003264 if (NumElems != 2 && NumElems != 4)
3265 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003266 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003267 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003268 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003269 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003270 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003271 return false;
3272 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003273}
3274
Evan Cheng39623da2006-04-20 08:58:49 +00003275/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3276/// all the same.
3277static bool isSplatVector(SDNode *N) {
3278 if (N->getOpcode() != ISD::BUILD_VECTOR)
3279 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003280
Dan Gohman475871a2008-07-27 21:46:04 +00003281 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003282 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3283 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003284 return false;
3285 return true;
3286}
3287
Evan Cheng213d2cf2007-05-17 18:45:50 +00003288/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003289/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003290/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003291static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003292 SDValue V1 = N->getOperand(0);
3293 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003294 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3295 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003296 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003297 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003298 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003299 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3300 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003301 if (Opc != ISD::BUILD_VECTOR ||
3302 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003303 return false;
3304 } else if (Idx >= 0) {
3305 unsigned Opc = V1.getOpcode();
3306 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3307 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003308 if (Opc != ISD::BUILD_VECTOR ||
3309 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003310 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003311 }
3312 }
3313 return true;
3314}
3315
3316/// getZeroVector - Returns a vector of specified type with all zero elements.
3317///
Owen Andersone50ed302009-08-10 22:56:29 +00003318static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003319 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003320 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003321
Chris Lattner8a594482007-11-25 00:24:49 +00003322 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3323 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003324 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003325 if (VT.getSizeInBits() == 64) { // MMX
Owen Anderson825b72b2009-08-11 20:47:22 +00003326 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3327 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003328 } else if (HasSSE2) { // SSE2
Owen Anderson825b72b2009-08-11 20:47:22 +00003329 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3330 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003331 } else { // SSE1
Owen Anderson825b72b2009-08-11 20:47:22 +00003332 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3333 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003334 }
Dale Johannesenace16102009-02-03 19:33:06 +00003335 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003336}
3337
Chris Lattner8a594482007-11-25 00:24:49 +00003338/// getOnesVector - Returns a vector of specified type with all bits set.
3339///
Owen Andersone50ed302009-08-10 22:56:29 +00003340static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003341 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003342
Chris Lattner8a594482007-11-25 00:24:49 +00003343 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3344 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003345 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003346 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003347 if (VT.getSizeInBits() == 64) // MMX
Owen Anderson825b72b2009-08-11 20:47:22 +00003348 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Chris Lattner8a594482007-11-25 00:24:49 +00003349 else // SSE
Owen Anderson825b72b2009-08-11 20:47:22 +00003350 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Dale Johannesenace16102009-02-03 19:33:06 +00003351 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003352}
3353
3354
Evan Cheng39623da2006-04-20 08:58:49 +00003355/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3356/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003357static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003358 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003359 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003360
Evan Cheng39623da2006-04-20 08:58:49 +00003361 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003362 SmallVector<int, 8> MaskVec;
3363 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003364
Nate Begeman5a5ca152009-04-29 05:20:52 +00003365 for (unsigned i = 0; i != NumElems; ++i) {
3366 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003367 MaskVec[i] = NumElems;
3368 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003369 }
Evan Cheng39623da2006-04-20 08:58:49 +00003370 }
Evan Cheng39623da2006-04-20 08:58:49 +00003371 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003372 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3373 SVOp->getOperand(1), &MaskVec[0]);
3374 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003375}
3376
Evan Cheng017dcc62006-04-21 01:05:10 +00003377/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3378/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003379static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003380 SDValue V2) {
3381 unsigned NumElems = VT.getVectorNumElements();
3382 SmallVector<int, 8> Mask;
3383 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003384 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003385 Mask.push_back(i);
3386 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003387}
3388
Nate Begeman9008ca62009-04-27 18:41:29 +00003389/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003390static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003391 SDValue V2) {
3392 unsigned NumElems = VT.getVectorNumElements();
3393 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003394 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003395 Mask.push_back(i);
3396 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003397 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003398 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003399}
3400
Nate Begeman9008ca62009-04-27 18:41:29 +00003401/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003402static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003403 SDValue V2) {
3404 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003405 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003406 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003407 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003408 Mask.push_back(i + Half);
3409 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003410 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003411 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003412}
3413
Evan Cheng0c0f83f2008-04-05 00:30:36 +00003414/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Eric Christopherfd179292009-08-27 18:07:15 +00003415static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00003416 bool HasSSE2) {
3417 if (SV->getValueType(0).getVectorNumElements() <= 4)
3418 return SDValue(SV, 0);
Eric Christopherfd179292009-08-27 18:07:15 +00003419
Owen Anderson825b72b2009-08-11 20:47:22 +00003420 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003421 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003422 DebugLoc dl = SV->getDebugLoc();
3423 SDValue V1 = SV->getOperand(0);
3424 int NumElems = VT.getVectorNumElements();
3425 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003426
Nate Begeman9008ca62009-04-27 18:41:29 +00003427 // unpack elements to the correct location
3428 while (NumElems > 4) {
3429 if (EltNo < NumElems/2) {
3430 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3431 } else {
3432 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3433 EltNo -= NumElems/2;
3434 }
3435 NumElems >>= 1;
3436 }
Eric Christopherfd179292009-08-27 18:07:15 +00003437
Nate Begeman9008ca62009-04-27 18:41:29 +00003438 // Perform the splat.
3439 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Dale Johannesenace16102009-02-03 19:33:06 +00003440 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003441 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
3442 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003443}
3444
Evan Chengba05f722006-04-21 23:03:30 +00003445/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003446/// vector of zero or undef vector. This produces a shuffle where the low
3447/// element of V2 is swizzled into the zero/undef vector, landing at element
3448/// 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 +00003449static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003450 bool isZero, bool HasSSE2,
3451 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003452 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003453 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003454 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3455 unsigned NumElems = VT.getVectorNumElements();
3456 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003457 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003458 // If this is the insertion idx, put the low elt of V2 here.
3459 MaskVec.push_back(i == Idx ? NumElems : i);
3460 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003461}
3462
Evan Chengf26ffe92008-05-29 08:22:04 +00003463/// getNumOfConsecutiveZeros - Return the number of elements in a result of
3464/// a shuffle that is zero.
3465static
Nate Begeman9008ca62009-04-27 18:41:29 +00003466unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, int NumElems,
3467 bool Low, SelectionDAG &DAG) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003468 unsigned NumZeros = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003469 for (int i = 0; i < NumElems; ++i) {
Evan Chengab262272008-06-25 20:52:59 +00003470 unsigned Index = Low ? i : NumElems-i-1;
Nate Begeman9008ca62009-04-27 18:41:29 +00003471 int Idx = SVOp->getMaskElt(Index);
3472 if (Idx < 0) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003473 ++NumZeros;
3474 continue;
3475 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003476 SDValue Elt = DAG.getShuffleScalarElt(SVOp, Index);
Evan Cheng37b73872009-07-30 08:33:02 +00003477 if (Elt.getNode() && X86::isZeroNode(Elt))
Evan Chengf26ffe92008-05-29 08:22:04 +00003478 ++NumZeros;
3479 else
3480 break;
3481 }
3482 return NumZeros;
3483}
3484
3485/// isVectorShift - Returns true if the shuffle can be implemented as a
3486/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003487/// FIXME: split into pslldqi, psrldqi, palignr variants.
3488static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00003489 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
John McCallb1fb4492010-04-07 01:49:15 +00003490 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
Evan Chengf26ffe92008-05-29 08:22:04 +00003491
3492 isLeft = true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003493 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, true, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003494 if (!NumZeros) {
3495 isLeft = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003496 NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, false, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003497 if (!NumZeros)
3498 return false;
3499 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003500 bool SeenV1 = false;
3501 bool SeenV2 = false;
John McCallb1fb4492010-04-07 01:49:15 +00003502 for (unsigned i = NumZeros; i < NumElems; ++i) {
3503 unsigned Val = isLeft ? (i - NumZeros) : i;
3504 int Idx_ = SVOp->getMaskElt(isLeft ? i : (i - NumZeros));
3505 if (Idx_ < 0)
Evan Chengf26ffe92008-05-29 08:22:04 +00003506 continue;
John McCallb1fb4492010-04-07 01:49:15 +00003507 unsigned Idx = (unsigned) Idx_;
Nate Begeman9008ca62009-04-27 18:41:29 +00003508 if (Idx < NumElems)
Evan Chengf26ffe92008-05-29 08:22:04 +00003509 SeenV1 = true;
3510 else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003511 Idx -= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00003512 SeenV2 = true;
3513 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003514 if (Idx != Val)
Evan Chengf26ffe92008-05-29 08:22:04 +00003515 return false;
3516 }
3517 if (SeenV1 && SeenV2)
3518 return false;
3519
Nate Begeman9008ca62009-04-27 18:41:29 +00003520 ShVal = SeenV1 ? SVOp->getOperand(0) : SVOp->getOperand(1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003521 ShAmt = NumZeros;
3522 return true;
3523}
3524
3525
Evan Chengc78d3b42006-04-24 18:01:45 +00003526/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3527///
Dan Gohman475871a2008-07-27 21:46:04 +00003528static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003529 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00003530 SelectionDAG &DAG,
3531 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003532 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00003533 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003534
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003535 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003536 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003537 bool First = true;
3538 for (unsigned i = 0; i < 16; ++i) {
3539 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3540 if (ThisIsNonZero && First) {
3541 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003542 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003543 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003544 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003545 First = false;
3546 }
3547
3548 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00003549 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003550 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3551 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003552 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003553 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00003554 }
3555 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003556 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
3557 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
3558 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00003559 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003560 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00003561 } else
3562 ThisElt = LastElt;
3563
Gabor Greifba36cb52008-08-28 21:40:38 +00003564 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00003565 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00003566 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00003567 }
3568 }
3569
Owen Anderson825b72b2009-08-11 20:47:22 +00003570 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00003571}
3572
Bill Wendlinga348c562007-03-22 18:42:45 +00003573/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003574///
Dan Gohman475871a2008-07-27 21:46:04 +00003575static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00003576 unsigned NumNonZero, unsigned NumZero,
3577 SelectionDAG &DAG,
3578 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003579 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00003580 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003581
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003582 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003583 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003584 bool First = true;
3585 for (unsigned i = 0; i < 8; ++i) {
3586 bool isNonZero = (NonZeros & (1 << i)) != 0;
3587 if (isNonZero) {
3588 if (First) {
3589 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003590 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003591 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003592 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003593 First = false;
3594 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003595 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003596 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00003597 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00003598 }
3599 }
3600
3601 return V;
3602}
3603
Evan Chengf26ffe92008-05-29 08:22:04 +00003604/// getVShift - Return a vector logical shift node.
3605///
Owen Andersone50ed302009-08-10 22:56:29 +00003606static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00003607 unsigned NumBits, SelectionDAG &DAG,
3608 const TargetLowering &TLI, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003609 bool isMMX = VT.getSizeInBits() == 64;
Owen Anderson825b72b2009-08-11 20:47:22 +00003610 EVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00003611 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Dale Johannesenace16102009-02-03 19:33:06 +00003612 SrcOp = DAG.getNode(ISD::BIT_CONVERT, dl, ShVT, SrcOp);
3613 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3614 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00003615 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00003616}
3617
Dan Gohman475871a2008-07-27 21:46:04 +00003618SDValue
Evan Chengc3630942009-12-09 21:00:30 +00003619X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00003620 SelectionDAG &DAG) const {
Evan Chengc3630942009-12-09 21:00:30 +00003621
3622 // Check if the scalar load can be widened into a vector load. And if
3623 // the address is "base + cst" see if the cst can be "absorbed" into
3624 // the shuffle mask.
3625 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
3626 SDValue Ptr = LD->getBasePtr();
3627 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
3628 return SDValue();
3629 EVT PVT = LD->getValueType(0);
3630 if (PVT != MVT::i32 && PVT != MVT::f32)
3631 return SDValue();
3632
3633 int FI = -1;
3634 int64_t Offset = 0;
3635 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
3636 FI = FINode->getIndex();
3637 Offset = 0;
3638 } else if (Ptr.getOpcode() == ISD::ADD &&
3639 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
3640 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
3641 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
3642 Offset = Ptr.getConstantOperandVal(1);
3643 Ptr = Ptr.getOperand(0);
3644 } else {
3645 return SDValue();
3646 }
3647
3648 SDValue Chain = LD->getChain();
3649 // Make sure the stack object alignment is at least 16.
3650 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3651 if (DAG.InferPtrAlignment(Ptr) < 16) {
3652 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00003653 // Can't change the alignment. FIXME: It's possible to compute
3654 // the exact stack offset and reference FI + adjust offset instead.
3655 // If someone *really* cares about this. That's the way to implement it.
3656 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00003657 } else {
3658 MFI->setObjectAlignment(FI, 16);
3659 }
3660 }
3661
3662 // (Offset % 16) must be multiple of 4. Then address is then
3663 // Ptr + (Offset & ~15).
3664 if (Offset < 0)
3665 return SDValue();
3666 if ((Offset % 16) & 3)
3667 return SDValue();
3668 int64_t StartOffset = Offset & ~15;
3669 if (StartOffset)
3670 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
3671 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
3672
3673 int EltNo = (Offset - StartOffset) >> 2;
3674 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
3675 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
David Greene67c9d422010-02-15 16:53:33 +00003676 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,LD->getSrcValue(),0,
3677 false, false, 0);
Evan Chengc3630942009-12-09 21:00:30 +00003678 // Canonicalize it to a v4i32 shuffle.
3679 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32, V1);
3680 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3681 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
3682 DAG.getUNDEF(MVT::v4i32), &Mask[0]));
3683 }
3684
3685 return SDValue();
3686}
3687
Nate Begeman1449f292010-03-24 22:19:06 +00003688/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
3689/// vector of type 'VT', see if the elements can be replaced by a single large
3690/// load which has the same value as a build_vector whose operands are 'elts'.
3691///
3692/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
3693///
3694/// FIXME: we'd also like to handle the case where the last elements are zero
3695/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
3696/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00003697static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
3698 DebugLoc &dl, SelectionDAG &DAG) {
3699 EVT EltVT = VT.getVectorElementType();
3700 unsigned NumElems = Elts.size();
3701
Nate Begemanfdea31a2010-03-24 20:49:50 +00003702 LoadSDNode *LDBase = NULL;
3703 unsigned LastLoadedElt = -1U;
Nate Begeman1449f292010-03-24 22:19:06 +00003704
3705 // For each element in the initializer, see if we've found a load or an undef.
3706 // If we don't find an initial load element, or later load elements are
3707 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00003708 for (unsigned i = 0; i < NumElems; ++i) {
3709 SDValue Elt = Elts[i];
3710
3711 if (!Elt.getNode() ||
3712 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
3713 return SDValue();
3714 if (!LDBase) {
3715 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
3716 return SDValue();
3717 LDBase = cast<LoadSDNode>(Elt.getNode());
3718 LastLoadedElt = i;
3719 continue;
3720 }
3721 if (Elt.getOpcode() == ISD::UNDEF)
3722 continue;
3723
3724 LoadSDNode *LD = cast<LoadSDNode>(Elt);
3725 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
3726 return SDValue();
3727 LastLoadedElt = i;
3728 }
Nate Begeman1449f292010-03-24 22:19:06 +00003729
3730 // If we have found an entire vector of loads and undefs, then return a large
3731 // load of the entire vector width starting at the base pointer. If we found
3732 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00003733 if (LastLoadedElt == NumElems - 1) {
3734 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
3735 return DAG.getLoad(VT, dl, LDBase->getChain(), LDBase->getBasePtr(),
3736 LDBase->getSrcValue(), LDBase->getSrcValueOffset(),
3737 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
3738 return DAG.getLoad(VT, dl, LDBase->getChain(), LDBase->getBasePtr(),
3739 LDBase->getSrcValue(), LDBase->getSrcValueOffset(),
3740 LDBase->isVolatile(), LDBase->isNonTemporal(),
3741 LDBase->getAlignment());
3742 } else if (NumElems == 4 && LastLoadedElt == 1) {
3743 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
3744 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
3745 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2);
3746 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, ResNode);
3747 }
3748 return SDValue();
3749}
3750
Evan Chengc3630942009-12-09 21:00:30 +00003751SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00003752X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003753 DebugLoc dl = Op.getDebugLoc();
Chris Lattner8a594482007-11-25 00:24:49 +00003754 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif327ef032008-08-28 23:19:51 +00003755 if (ISD::isBuildVectorAllZeros(Op.getNode())
3756 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattner8a594482007-11-25 00:24:49 +00003757 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3758 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3759 // eliminated on x86-32 hosts.
Owen Anderson825b72b2009-08-11 20:47:22 +00003760 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
Chris Lattner8a594482007-11-25 00:24:49 +00003761 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003762
Gabor Greifba36cb52008-08-28 21:40:38 +00003763 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00003764 return getOnesVector(Op.getValueType(), DAG, dl);
3765 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00003766 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003767
Owen Andersone50ed302009-08-10 22:56:29 +00003768 EVT VT = Op.getValueType();
3769 EVT ExtVT = VT.getVectorElementType();
3770 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003771
3772 unsigned NumElems = Op.getNumOperands();
3773 unsigned NumZero = 0;
3774 unsigned NumNonZero = 0;
3775 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003776 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00003777 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003778 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003779 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00003780 if (Elt.getOpcode() == ISD::UNDEF)
3781 continue;
3782 Values.insert(Elt);
3783 if (Elt.getOpcode() != ISD::Constant &&
3784 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003785 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00003786 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00003787 NumZero++;
3788 else {
3789 NonZeros |= (1 << i);
3790 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003791 }
3792 }
3793
Dan Gohman7f321562007-06-25 16:23:39 +00003794 if (NumNonZero == 0) {
Chris Lattner8a594482007-11-25 00:24:49 +00003795 // All undef vector. Return an UNDEF. All zero vectors were handled above.
Dale Johannesene8d72302009-02-06 23:05:02 +00003796 return DAG.getUNDEF(VT);
Dan Gohman7f321562007-06-25 16:23:39 +00003797 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003798
Chris Lattner67f453a2008-03-09 05:42:06 +00003799 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00003800 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003801 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00003802 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00003803
Chris Lattner62098042008-03-09 01:05:04 +00003804 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3805 // the value are obviously zero, truncate the value to i32 and do the
3806 // insertion that way. Only do this if the value is non-constant or if the
3807 // value is a constant being inserted into element 0. It is cheaper to do
3808 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00003809 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00003810 (!IsAllConstants || Idx == 0)) {
3811 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3812 // Handle MMX and SSE both.
Owen Anderson825b72b2009-08-11 20:47:22 +00003813 EVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3814 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Scott Michelfdc40a02009-02-17 22:15:04 +00003815
Chris Lattner62098042008-03-09 01:05:04 +00003816 // Truncate the value (which may itself be a constant) to i32, and
3817 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00003818 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00003819 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00003820 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3821 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00003822
Chris Lattner62098042008-03-09 01:05:04 +00003823 // Now we have our 32-bit value zero extended in the low element of
3824 // a vector. If Idx != 0, swizzle it into place.
3825 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003826 SmallVector<int, 4> Mask;
3827 Mask.push_back(Idx);
3828 for (unsigned i = 1; i != VecElts; ++i)
3829 Mask.push_back(i);
3830 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00003831 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00003832 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003833 }
Dale Johannesenace16102009-02-03 19:33:06 +00003834 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00003835 }
3836 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003837
Chris Lattner19f79692008-03-08 22:59:52 +00003838 // If we have a constant or non-constant insertion into the low element of
3839 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3840 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00003841 // depending on what the source datatype is.
3842 if (Idx == 0) {
3843 if (NumZero == 0) {
3844 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00003845 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
3846 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00003847 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
3848 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
3849 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
3850 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00003851 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
3852 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
3853 EVT MiddleVT = VT.getSizeInBits() == 64 ? MVT::v2i32 : MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00003854 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
3855 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3856 Subtarget->hasSSE2(), DAG);
3857 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Item);
3858 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003859 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003860
3861 // Is it a vector logical left shift?
3862 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00003863 X86::isZeroNode(Op.getOperand(0)) &&
3864 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003865 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00003866 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00003867 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00003868 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00003869 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00003870 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003871
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003872 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00003873 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003874
Chris Lattner19f79692008-03-08 22:59:52 +00003875 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3876 // is a non-constant being inserted into an element other than the low one,
3877 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3878 // movd/movss) to move this into the low element, then shuffle it into
3879 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003880 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00003881 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00003882
Evan Cheng0db9fe62006-04-25 20:13:52 +00003883 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003884 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3885 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00003886 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003887 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00003888 MaskVec.push_back(i == Idx ? 0 : 1);
3889 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003890 }
3891 }
3892
Chris Lattner67f453a2008-03-09 05:42:06 +00003893 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00003894 if (Values.size() == 1) {
3895 if (EVTBits == 32) {
3896 // Instead of a shuffle like this:
3897 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
3898 // Check if it's possible to issue this instead.
3899 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
3900 unsigned Idx = CountTrailingZeros_32(NonZeros);
3901 SDValue Item = Op.getOperand(Idx);
3902 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
3903 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
3904 }
Dan Gohman475871a2008-07-27 21:46:04 +00003905 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00003906 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003907
Dan Gohmana3941172007-07-24 22:55:08 +00003908 // A vector full of immediates; various special cases are already
3909 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003910 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00003911 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00003912
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003913 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003914 if (EVTBits == 64) {
3915 if (NumNonZero == 1) {
3916 // One half is zero or undef.
3917 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00003918 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003919 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00003920 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3921 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00003922 }
Dan Gohman475871a2008-07-27 21:46:04 +00003923 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00003924 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003925
3926 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00003927 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00003928 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003929 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003930 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003931 }
3932
Bill Wendling826f36f2007-03-28 00:57:11 +00003933 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00003934 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003935 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003936 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003937 }
3938
3939 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003940 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00003941 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003942 if (NumElems == 4 && NumZero > 0) {
3943 for (unsigned i = 0; i < 4; ++i) {
3944 bool isZero = !(NonZeros & (1 << i));
3945 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003946 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003947 else
Dale Johannesenace16102009-02-03 19:33:06 +00003948 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003949 }
3950
3951 for (unsigned i = 0; i < 2; ++i) {
3952 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3953 default: break;
3954 case 0:
3955 V[i] = V[i*2]; // Must be a zero vector.
3956 break;
3957 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00003958 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003959 break;
3960 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00003961 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003962 break;
3963 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00003964 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003965 break;
3966 }
3967 }
3968
Nate Begeman9008ca62009-04-27 18:41:29 +00003969 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003970 bool Reverse = (NonZeros & 0x3) == 2;
3971 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003972 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003973 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3974 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003975 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
3976 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003977 }
3978
Nate Begemanfdea31a2010-03-24 20:49:50 +00003979 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
3980 // Check for a build vector of consecutive loads.
3981 for (unsigned i = 0; i < NumElems; ++i)
3982 V[i] = Op.getOperand(i);
3983
3984 // Check for elements which are consecutive loads.
3985 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
3986 if (LD.getNode())
3987 return LD;
3988
3989 // For SSE 4.1, use inserts into undef.
3990 if (getSubtarget()->hasSSE41()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003991 V[0] = DAG.getUNDEF(VT);
3992 for (unsigned i = 0; i < NumElems; ++i)
3993 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
3994 V[0] = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, V[0],
3995 Op.getOperand(i), DAG.getIntPtrConstant(i));
3996 return V[0];
3997 }
Nate Begemanfdea31a2010-03-24 20:49:50 +00003998
3999 // Otherwise, expand into a number of unpckl*
Evan Cheng0db9fe62006-04-25 20:13:52 +00004000 // e.g. for v4f32
4001 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
4002 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
4003 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Evan Cheng0db9fe62006-04-25 20:13:52 +00004004 for (unsigned i = 0; i < NumElems; ++i)
Dale Johannesenace16102009-02-03 19:33:06 +00004005 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004006 NumElems >>= 1;
4007 while (NumElems != 0) {
4008 for (unsigned i = 0; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004009 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + NumElems]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004010 NumElems >>= 1;
4011 }
4012 return V[0];
4013 }
Dan Gohman475871a2008-07-27 21:46:04 +00004014 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004015}
4016
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004017SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004018X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004019 // We support concatenate two MMX registers and place them in a MMX
4020 // register. This is better than doing a stack convert.
4021 DebugLoc dl = Op.getDebugLoc();
4022 EVT ResVT = Op.getValueType();
4023 assert(Op.getNumOperands() == 2);
4024 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
4025 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
4026 int Mask[2];
4027 SDValue InVec = DAG.getNode(ISD::BIT_CONVERT,dl, MVT::v1i64, Op.getOperand(0));
4028 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4029 InVec = Op.getOperand(1);
4030 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4031 unsigned NumElts = ResVT.getVectorNumElements();
4032 VecOp = DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
4033 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
4034 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
4035 } else {
4036 InVec = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v1i64, InVec);
4037 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4038 Mask[0] = 0; Mask[1] = 2;
4039 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
4040 }
4041 return DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
4042}
4043
Nate Begemanb9a47b82009-02-23 08:49:38 +00004044// v8i16 shuffles - Prefer shuffles in the following order:
4045// 1. [all] pshuflw, pshufhw, optional move
4046// 2. [ssse3] 1 x pshufb
4047// 3. [ssse3] 2 x pshufb + 1 x por
4048// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004049static
Nate Begeman9008ca62009-04-27 18:41:29 +00004050SDValue LowerVECTOR_SHUFFLEv8i16(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00004051 SelectionDAG &DAG,
4052 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004053 SDValue V1 = SVOp->getOperand(0);
4054 SDValue V2 = SVOp->getOperand(1);
4055 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004056 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00004057
Nate Begemanb9a47b82009-02-23 08:49:38 +00004058 // Determine if more than 1 of the words in each of the low and high quadwords
4059 // of the result come from the same quadword of one of the two inputs. Undef
4060 // mask values count as coming from any quadword, for better codegen.
4061 SmallVector<unsigned, 4> LoQuad(4);
4062 SmallVector<unsigned, 4> HiQuad(4);
4063 BitVector InputQuads(4);
4064 for (unsigned i = 0; i < 8; ++i) {
4065 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00004066 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004067 MaskVals.push_back(EltIdx);
4068 if (EltIdx < 0) {
4069 ++Quad[0];
4070 ++Quad[1];
4071 ++Quad[2];
4072 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00004073 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004074 }
4075 ++Quad[EltIdx / 4];
4076 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00004077 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004078
Nate Begemanb9a47b82009-02-23 08:49:38 +00004079 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004080 unsigned MaxQuad = 1;
4081 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004082 if (LoQuad[i] > MaxQuad) {
4083 BestLoQuad = i;
4084 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004085 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004086 }
4087
Nate Begemanb9a47b82009-02-23 08:49:38 +00004088 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004089 MaxQuad = 1;
4090 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004091 if (HiQuad[i] > MaxQuad) {
4092 BestHiQuad = i;
4093 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004094 }
4095 }
4096
Nate Begemanb9a47b82009-02-23 08:49:38 +00004097 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00004098 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00004099 // single pshufb instruction is necessary. If There are more than 2 input
4100 // quads, disable the next transformation since it does not help SSSE3.
4101 bool V1Used = InputQuads[0] || InputQuads[1];
4102 bool V2Used = InputQuads[2] || InputQuads[3];
4103 if (TLI.getSubtarget()->hasSSSE3()) {
4104 if (InputQuads.count() == 2 && V1Used && V2Used) {
4105 BestLoQuad = InputQuads.find_first();
4106 BestHiQuad = InputQuads.find_next(BestLoQuad);
4107 }
4108 if (InputQuads.count() > 2) {
4109 BestLoQuad = -1;
4110 BestHiQuad = -1;
4111 }
4112 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004113
Nate Begemanb9a47b82009-02-23 08:49:38 +00004114 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
4115 // the shuffle mask. If a quad is scored as -1, that means that it contains
4116 // words from all 4 input quadwords.
4117 SDValue NewV;
4118 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004119 SmallVector<int, 8> MaskV;
4120 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
4121 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00004122 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004123 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V1),
4124 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V2), &MaskV[0]);
4125 NewV = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004126
Nate Begemanb9a47b82009-02-23 08:49:38 +00004127 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
4128 // source words for the shuffle, to aid later transformations.
4129 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00004130 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00004131 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004132 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00004133 if (idx != (int)i)
4134 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004135 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00004136 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004137 AllWordsInNewV = false;
4138 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00004139 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004140
Nate Begemanb9a47b82009-02-23 08:49:38 +00004141 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
4142 if (AllWordsInNewV) {
4143 for (int i = 0; i != 8; ++i) {
4144 int idx = MaskVals[i];
4145 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004146 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004147 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004148 if ((idx != i) && idx < 4)
4149 pshufhw = false;
4150 if ((idx != i) && idx > 3)
4151 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00004152 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00004153 V1 = NewV;
4154 V2Used = false;
4155 BestLoQuad = 0;
4156 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004157 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004158
Nate Begemanb9a47b82009-02-23 08:49:38 +00004159 // If we've eliminated the use of V2, and the new mask is a pshuflw or
4160 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00004161 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Eric Christopherfd179292009-08-27 18:07:15 +00004162 return DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00004163 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Evan Cheng14b32e12007-12-11 01:46:18 +00004164 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004165 }
Eric Christopherfd179292009-08-27 18:07:15 +00004166
Nate Begemanb9a47b82009-02-23 08:49:38 +00004167 // If we have SSSE3, and all words of the result are from 1 input vector,
4168 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
4169 // is present, fall back to case 4.
4170 if (TLI.getSubtarget()->hasSSSE3()) {
4171 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004172
Nate Begemanb9a47b82009-02-23 08:49:38 +00004173 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00004174 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00004175 // mask, and elements that come from V1 in the V2 mask, so that the two
4176 // results can be OR'd together.
4177 bool TwoInputs = V1Used && V2Used;
4178 for (unsigned i = 0; i != 8; ++i) {
4179 int EltIdx = MaskVals[i] * 2;
4180 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004181 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4182 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004183 continue;
4184 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004185 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
4186 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004187 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004188 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004189 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004190 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004191 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004192 if (!TwoInputs)
Owen Anderson825b72b2009-08-11 20:47:22 +00004193 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004194
Nate Begemanb9a47b82009-02-23 08:49:38 +00004195 // Calculate the shuffle mask for the second input, shuffle it, and
4196 // OR it with the first shuffled input.
4197 pshufbMask.clear();
4198 for (unsigned i = 0; i != 8; ++i) {
4199 int EltIdx = MaskVals[i] * 2;
4200 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004201 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4202 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004203 continue;
4204 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004205 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
4206 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004207 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004208 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00004209 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004210 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004211 MVT::v16i8, &pshufbMask[0], 16));
4212 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
4213 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004214 }
4215
4216 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
4217 // and update MaskVals with new element order.
4218 BitVector InOrder(8);
4219 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004220 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004221 for (int i = 0; i != 4; ++i) {
4222 int idx = MaskVals[i];
4223 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004224 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004225 InOrder.set(i);
4226 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004227 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004228 InOrder.set(i);
4229 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004230 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004231 }
4232 }
4233 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004234 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00004235 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004236 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004237 }
Eric Christopherfd179292009-08-27 18:07:15 +00004238
Nate Begemanb9a47b82009-02-23 08:49:38 +00004239 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
4240 // and update MaskVals with the new element order.
4241 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004242 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004243 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004244 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004245 for (unsigned i = 4; i != 8; ++i) {
4246 int idx = MaskVals[i];
4247 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004248 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004249 InOrder.set(i);
4250 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004251 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004252 InOrder.set(i);
4253 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004254 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004255 }
4256 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004257 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004258 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004259 }
Eric Christopherfd179292009-08-27 18:07:15 +00004260
Nate Begemanb9a47b82009-02-23 08:49:38 +00004261 // In case BestHi & BestLo were both -1, which means each quadword has a word
4262 // from each of the four input quadwords, calculate the InOrder bitvector now
4263 // before falling through to the insert/extract cleanup.
4264 if (BestLoQuad == -1 && BestHiQuad == -1) {
4265 NewV = V1;
4266 for (int i = 0; i != 8; ++i)
4267 if (MaskVals[i] < 0 || MaskVals[i] == i)
4268 InOrder.set(i);
4269 }
Eric Christopherfd179292009-08-27 18:07:15 +00004270
Nate Begemanb9a47b82009-02-23 08:49:38 +00004271 // The other elements are put in the right place using pextrw and pinsrw.
4272 for (unsigned i = 0; i != 8; ++i) {
4273 if (InOrder[i])
4274 continue;
4275 int EltIdx = MaskVals[i];
4276 if (EltIdx < 0)
4277 continue;
4278 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004279 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004280 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00004281 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004282 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004283 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004284 DAG.getIntPtrConstant(i));
4285 }
4286 return NewV;
4287}
4288
4289// v16i8 shuffles - Prefer shuffles in the following order:
4290// 1. [ssse3] 1 x pshufb
4291// 2. [ssse3] 2 x pshufb + 1 x por
4292// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
4293static
Nate Begeman9008ca62009-04-27 18:41:29 +00004294SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00004295 SelectionDAG &DAG,
4296 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004297 SDValue V1 = SVOp->getOperand(0);
4298 SDValue V2 = SVOp->getOperand(1);
4299 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004300 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00004301 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00004302
Nate Begemanb9a47b82009-02-23 08:49:38 +00004303 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00004304 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00004305 // present, fall back to case 3.
4306 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
4307 bool V1Only = true;
4308 bool V2Only = true;
4309 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004310 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00004311 if (EltIdx < 0)
4312 continue;
4313 if (EltIdx < 16)
4314 V2Only = false;
4315 else
4316 V1Only = false;
4317 }
Eric Christopherfd179292009-08-27 18:07:15 +00004318
Nate Begemanb9a47b82009-02-23 08:49:38 +00004319 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
4320 if (TLI.getSubtarget()->hasSSSE3()) {
4321 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004322
Nate Begemanb9a47b82009-02-23 08:49:38 +00004323 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00004324 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004325 //
4326 // Otherwise, we have elements from both input vectors, and must zero out
4327 // elements that come from V2 in the first mask, and V1 in the second mask
4328 // so that we can OR them together.
4329 bool TwoInputs = !(V1Only || V2Only);
4330 for (unsigned i = 0; i != 16; ++i) {
4331 int EltIdx = MaskVals[i];
4332 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004333 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004334 continue;
4335 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004336 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004337 }
4338 // If all the elements are from V2, assign it to V1 and return after
4339 // building the first pshufb.
4340 if (V2Only)
4341 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00004342 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004343 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004344 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004345 if (!TwoInputs)
4346 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00004347
Nate Begemanb9a47b82009-02-23 08:49:38 +00004348 // Calculate the shuffle mask for the second input, shuffle it, and
4349 // OR it with the first shuffled input.
4350 pshufbMask.clear();
4351 for (unsigned i = 0; i != 16; ++i) {
4352 int EltIdx = MaskVals[i];
4353 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004354 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004355 continue;
4356 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004357 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004358 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004359 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004360 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004361 MVT::v16i8, &pshufbMask[0], 16));
4362 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004363 }
Eric Christopherfd179292009-08-27 18:07:15 +00004364
Nate Begemanb9a47b82009-02-23 08:49:38 +00004365 // No SSSE3 - Calculate in place words and then fix all out of place words
4366 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
4367 // the 16 different words that comprise the two doublequadword input vectors.
Owen Anderson825b72b2009-08-11 20:47:22 +00004368 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
4369 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004370 SDValue NewV = V2Only ? V2 : V1;
4371 for (int i = 0; i != 8; ++i) {
4372 int Elt0 = MaskVals[i*2];
4373 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00004374
Nate Begemanb9a47b82009-02-23 08:49:38 +00004375 // This word of the result is all undef, skip it.
4376 if (Elt0 < 0 && Elt1 < 0)
4377 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004378
Nate Begemanb9a47b82009-02-23 08:49:38 +00004379 // This word of the result is already in the correct place, skip it.
4380 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
4381 continue;
4382 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
4383 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004384
Nate Begemanb9a47b82009-02-23 08:49:38 +00004385 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
4386 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
4387 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00004388
4389 // If Elt0 and Elt1 are defined, are consecutive, and can be load
4390 // using a single extract together, load it and store it.
4391 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004392 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004393 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00004394 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004395 DAG.getIntPtrConstant(i));
4396 continue;
4397 }
4398
Nate Begemanb9a47b82009-02-23 08:49:38 +00004399 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00004400 // source byte is not also odd, shift the extracted word left 8 bits
4401 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004402 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004403 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004404 DAG.getIntPtrConstant(Elt1 / 2));
4405 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004406 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004407 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004408 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004409 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
4410 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004411 }
4412 // If Elt0 is defined, extract it from the appropriate source. If the
4413 // source byte is not also even, shift the extracted word right 8 bits. If
4414 // Elt1 was also defined, OR the extracted values together before
4415 // inserting them in the result.
4416 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004417 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004418 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
4419 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004420 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004421 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004422 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004423 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
4424 DAG.getConstant(0x00FF, MVT::i16));
4425 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00004426 : InsElt0;
4427 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004428 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004429 DAG.getIntPtrConstant(i));
4430 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004431 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004432}
4433
Evan Cheng7a831ce2007-12-15 03:00:47 +00004434/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
4435/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
4436/// done when every pair / quad of shuffle mask elements point to elements in
4437/// the right sequence. e.g.
Evan Cheng14b32e12007-12-11 01:46:18 +00004438/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
4439static
Nate Begeman9008ca62009-04-27 18:41:29 +00004440SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
4441 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00004442 const TargetLowering &TLI, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00004443 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00004444 SDValue V1 = SVOp->getOperand(0);
4445 SDValue V2 = SVOp->getOperand(1);
4446 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00004447 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Owen Anderson825b72b2009-08-11 20:47:22 +00004448 EVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Owen Andersone50ed302009-08-10 22:56:29 +00004449 EVT MaskEltVT = MaskVT.getVectorElementType();
4450 EVT NewVT = MaskVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00004451 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004452 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004453 case MVT::v4f32: NewVT = MVT::v2f64; break;
4454 case MVT::v4i32: NewVT = MVT::v2i64; break;
4455 case MVT::v8i16: NewVT = MVT::v4i32; break;
4456 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004457 }
4458
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004459 if (NewWidth == 2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004460 if (VT.isInteger())
Owen Anderson825b72b2009-08-11 20:47:22 +00004461 NewVT = MVT::v2i64;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004462 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004463 NewVT = MVT::v2f64;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004464 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004465 int Scale = NumElems / NewWidth;
4466 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00004467 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004468 int StartIdx = -1;
4469 for (int j = 0; j < Scale; ++j) {
4470 int EltIdx = SVOp->getMaskElt(i+j);
4471 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004472 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00004473 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00004474 StartIdx = EltIdx - (EltIdx % Scale);
4475 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00004476 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004477 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004478 if (StartIdx == -1)
4479 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00004480 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004481 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004482 }
4483
Dale Johannesenace16102009-02-03 19:33:06 +00004484 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V1);
4485 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00004486 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004487}
4488
Evan Chengd880b972008-05-09 21:53:03 +00004489/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004490///
Owen Andersone50ed302009-08-10 22:56:29 +00004491static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00004492 SDValue SrcOp, SelectionDAG &DAG,
4493 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004494 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004495 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00004496 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00004497 LD = dyn_cast<LoadSDNode>(SrcOp);
4498 if (!LD) {
4499 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
4500 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00004501 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
4502 if ((ExtVT.SimpleTy != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00004503 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4504 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00004505 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004506 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00004507 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Dale Johannesenace16102009-02-03 19:33:06 +00004508 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4509 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
4510 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
4511 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00004512 SrcOp.getOperand(0)
4513 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004514 }
4515 }
4516 }
4517
Dale Johannesenace16102009-02-03 19:33:06 +00004518 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4519 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004520 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00004521 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004522}
4523
Evan Chengace3c172008-07-22 21:13:36 +00004524/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
4525/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004526static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00004527LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
4528 SDValue V1 = SVOp->getOperand(0);
4529 SDValue V2 = SVOp->getOperand(1);
4530 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004531 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00004532
Evan Chengace3c172008-07-22 21:13:36 +00004533 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00004534 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00004535 SmallVector<int, 8> Mask1(4U, -1);
4536 SmallVector<int, 8> PermMask;
4537 SVOp->getMask(PermMask);
4538
Evan Chengace3c172008-07-22 21:13:36 +00004539 unsigned NumHi = 0;
4540 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00004541 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004542 int Idx = PermMask[i];
4543 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004544 Locs[i] = std::make_pair(-1, -1);
4545 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004546 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
4547 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004548 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00004549 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004550 NumLo++;
4551 } else {
4552 Locs[i] = std::make_pair(1, NumHi);
4553 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004554 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004555 NumHi++;
4556 }
4557 }
4558 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004559
Evan Chengace3c172008-07-22 21:13:36 +00004560 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004561 // If no more than two elements come from either vector. This can be
4562 // implemented with two shuffles. First shuffle gather the elements.
4563 // The second shuffle, which takes the first shuffle as both of its
4564 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00004565 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004566
Nate Begeman9008ca62009-04-27 18:41:29 +00004567 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00004568
Evan Chengace3c172008-07-22 21:13:36 +00004569 for (unsigned i = 0; i != 4; ++i) {
4570 if (Locs[i].first == -1)
4571 continue;
4572 else {
4573 unsigned Idx = (i < 2) ? 0 : 4;
4574 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004575 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004576 }
4577 }
4578
Nate Begeman9008ca62009-04-27 18:41:29 +00004579 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004580 } else if (NumLo == 3 || NumHi == 3) {
4581 // Otherwise, we must have three elements from one vector, call it X, and
4582 // one element from the other, call it Y. First, use a shufps to build an
4583 // intermediate vector with the one element from Y and the element from X
4584 // that will be in the same half in the final destination (the indexes don't
4585 // matter). Then, use a shufps to build the final vector, taking the half
4586 // containing the element from Y from the intermediate, and the other half
4587 // from X.
4588 if (NumHi == 3) {
4589 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00004590 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004591 std::swap(V1, V2);
4592 }
4593
4594 // Find the element from V2.
4595 unsigned HiIndex;
4596 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004597 int Val = PermMask[HiIndex];
4598 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004599 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004600 if (Val >= 4)
4601 break;
4602 }
4603
Nate Begeman9008ca62009-04-27 18:41:29 +00004604 Mask1[0] = PermMask[HiIndex];
4605 Mask1[1] = -1;
4606 Mask1[2] = PermMask[HiIndex^1];
4607 Mask1[3] = -1;
4608 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004609
4610 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004611 Mask1[0] = PermMask[0];
4612 Mask1[1] = PermMask[1];
4613 Mask1[2] = HiIndex & 1 ? 6 : 4;
4614 Mask1[3] = HiIndex & 1 ? 4 : 6;
4615 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004616 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004617 Mask1[0] = HiIndex & 1 ? 2 : 0;
4618 Mask1[1] = HiIndex & 1 ? 0 : 2;
4619 Mask1[2] = PermMask[2];
4620 Mask1[3] = PermMask[3];
4621 if (Mask1[2] >= 0)
4622 Mask1[2] += 4;
4623 if (Mask1[3] >= 0)
4624 Mask1[3] += 4;
4625 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004626 }
Evan Chengace3c172008-07-22 21:13:36 +00004627 }
4628
4629 // Break it into (shuffle shuffle_hi, shuffle_lo).
4630 Locs.clear();
Nate Begeman9008ca62009-04-27 18:41:29 +00004631 SmallVector<int,8> LoMask(4U, -1);
4632 SmallVector<int,8> HiMask(4U, -1);
4633
4634 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00004635 unsigned MaskIdx = 0;
4636 unsigned LoIdx = 0;
4637 unsigned HiIdx = 2;
4638 for (unsigned i = 0; i != 4; ++i) {
4639 if (i == 2) {
4640 MaskPtr = &HiMask;
4641 MaskIdx = 1;
4642 LoIdx = 0;
4643 HiIdx = 2;
4644 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004645 int Idx = PermMask[i];
4646 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004647 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00004648 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004649 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004650 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004651 LoIdx++;
4652 } else {
4653 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004654 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004655 HiIdx++;
4656 }
4657 }
4658
Nate Begeman9008ca62009-04-27 18:41:29 +00004659 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
4660 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
4661 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00004662 for (unsigned i = 0; i != 4; ++i) {
4663 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004664 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00004665 } else {
4666 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004667 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00004668 }
4669 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004670 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00004671}
4672
Dan Gohman475871a2008-07-27 21:46:04 +00004673SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004674X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00004675 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00004676 SDValue V1 = Op.getOperand(0);
4677 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00004678 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004679 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00004680 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004681 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004682 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
4683 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00004684 bool V1IsSplat = false;
4685 bool V2IsSplat = false;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004686
Nate Begeman9008ca62009-04-27 18:41:29 +00004687 if (isZeroShuffle(SVOp))
Dale Johannesenace16102009-02-03 19:33:06 +00004688 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004689
Nate Begeman9008ca62009-04-27 18:41:29 +00004690 // Promote splats to v4f32.
4691 if (SVOp->isSplat()) {
Eric Christopherfd179292009-08-27 18:07:15 +00004692 if (isMMX || NumElems < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004693 return Op;
4694 return PromoteSplat(SVOp, DAG, Subtarget->hasSSE2());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004695 }
4696
Evan Cheng7a831ce2007-12-15 03:00:47 +00004697 // If the shuffle can be profitably rewritten as a narrower shuffle, then
4698 // do it!
Owen Anderson825b72b2009-08-11 20:47:22 +00004699 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004700 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004701 if (NewOp.getNode())
Scott Michelfdc40a02009-02-17 22:15:04 +00004702 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004703 LowerVECTOR_SHUFFLE(NewOp, DAG));
Owen Anderson825b72b2009-08-11 20:47:22 +00004704 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Evan Cheng7a831ce2007-12-15 03:00:47 +00004705 // FIXME: Figure out a cleaner way to do this.
4706 // Try to make use of movq to zero out the top part.
Gabor Greifba36cb52008-08-28 21:40:38 +00004707 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004708 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004709 if (NewOp.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004710 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
4711 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
4712 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004713 }
Gabor Greifba36cb52008-08-28 21:40:38 +00004714 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004715 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
4716 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
Evan Chengd880b972008-05-09 21:53:03 +00004717 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Nate Begeman9008ca62009-04-27 18:41:29 +00004718 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004719 }
4720 }
Eric Christopherfd179292009-08-27 18:07:15 +00004721
Nate Begeman9008ca62009-04-27 18:41:29 +00004722 if (X86::isPSHUFDMask(SVOp))
4723 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004724
Evan Chengf26ffe92008-05-29 08:22:04 +00004725 // Check if this can be converted into a logical shift.
4726 bool isLeft = false;
4727 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00004728 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00004729 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00004730 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00004731 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004732 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00004733 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00004734 EVT EltVT = VT.getVectorElementType();
4735 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004736 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004737 }
Eric Christopherfd179292009-08-27 18:07:15 +00004738
Nate Begeman9008ca62009-04-27 18:41:29 +00004739 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004740 if (V1IsUndef)
4741 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00004742 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004743 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Nate Begemanfb8ead02008-07-25 19:05:58 +00004744 if (!isMMX)
4745 return Op;
Evan Cheng7e2ff772008-05-08 00:57:18 +00004746 }
Eric Christopherfd179292009-08-27 18:07:15 +00004747
Nate Begeman9008ca62009-04-27 18:41:29 +00004748 // FIXME: fold these into legal mask.
4749 if (!isMMX && (X86::isMOVSHDUPMask(SVOp) ||
4750 X86::isMOVSLDUPMask(SVOp) ||
4751 X86::isMOVHLPSMask(SVOp) ||
Nate Begeman0b10b912009-11-07 23:17:15 +00004752 X86::isMOVLHPSMask(SVOp) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00004753 X86::isMOVLPMask(SVOp)))
Evan Cheng9bbbb982006-10-25 20:48:19 +00004754 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004755
Nate Begeman9008ca62009-04-27 18:41:29 +00004756 if (ShouldXformToMOVHLPS(SVOp) ||
4757 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
4758 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004759
Evan Chengf26ffe92008-05-29 08:22:04 +00004760 if (isShift) {
4761 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00004762 EVT EltVT = VT.getVectorElementType();
4763 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004764 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004765 }
Eric Christopherfd179292009-08-27 18:07:15 +00004766
Evan Cheng9eca5e82006-10-25 21:49:50 +00004767 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00004768 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4769 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00004770 V1IsSplat = isSplatVector(V1.getNode());
4771 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00004772
Chris Lattner8a594482007-11-25 00:24:49 +00004773 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00004774 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004775 Op = CommuteVectorShuffle(SVOp, DAG);
4776 SVOp = cast<ShuffleVectorSDNode>(Op);
4777 V1 = SVOp->getOperand(0);
4778 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00004779 std::swap(V1IsSplat, V2IsSplat);
4780 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00004781 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00004782 }
4783
Nate Begeman9008ca62009-04-27 18:41:29 +00004784 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
4785 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00004786 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00004787 return V1;
4788 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
4789 // the instruction selector will not match, so get a canonical MOVL with
4790 // swapped operands to undo the commute.
4791 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00004792 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004793
Nate Begeman9008ca62009-04-27 18:41:29 +00004794 if (X86::isUNPCKL_v_undef_Mask(SVOp) ||
4795 X86::isUNPCKH_v_undef_Mask(SVOp) ||
4796 X86::isUNPCKLMask(SVOp) ||
4797 X86::isUNPCKHMask(SVOp))
Evan Chengd9b8e402006-10-16 06:36:00 +00004798 return Op;
Evan Chenge1113032006-10-04 18:33:38 +00004799
Evan Cheng9bbbb982006-10-25 20:48:19 +00004800 if (V2IsSplat) {
4801 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004802 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00004803 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00004804 SDValue NewMask = NormalizeMask(SVOp, DAG);
4805 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
4806 if (NSVOp != SVOp) {
4807 if (X86::isUNPCKLMask(NSVOp, true)) {
4808 return NewMask;
4809 } else if (X86::isUNPCKHMask(NSVOp, true)) {
4810 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004811 }
4812 }
4813 }
4814
Evan Cheng9eca5e82006-10-25 21:49:50 +00004815 if (Commuted) {
4816 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00004817 // FIXME: this seems wrong.
4818 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
4819 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
4820 if (X86::isUNPCKL_v_undef_Mask(NewSVOp) ||
4821 X86::isUNPCKH_v_undef_Mask(NewSVOp) ||
4822 X86::isUNPCKLMask(NewSVOp) ||
4823 X86::isUNPCKHMask(NewSVOp))
4824 return NewOp;
Evan Cheng9eca5e82006-10-25 21:49:50 +00004825 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004826
Nate Begemanb9a47b82009-02-23 08:49:38 +00004827 // FIXME: for mmx, bitcast v2i32 to v4i16 for shuffle.
Nate Begeman9008ca62009-04-27 18:41:29 +00004828
4829 // Normalize the node to match x86 shuffle ops if needed
4830 if (!isMMX && V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
4831 return CommuteVectorShuffle(SVOp, DAG);
4832
4833 // Check for legal shuffle and return?
4834 SmallVector<int, 16> PermMask;
4835 SVOp->getMask(PermMask);
4836 if (isShuffleMaskLegal(PermMask, VT))
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004837 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004838
Evan Cheng14b32e12007-12-11 01:46:18 +00004839 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00004840 if (VT == MVT::v8i16) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004841 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(SVOp, DAG, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004842 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00004843 return NewOp;
4844 }
4845
Owen Anderson825b72b2009-08-11 20:47:22 +00004846 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004847 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004848 if (NewOp.getNode())
4849 return NewOp;
4850 }
Eric Christopherfd179292009-08-27 18:07:15 +00004851
Evan Chengace3c172008-07-22 21:13:36 +00004852 // Handle all 4 wide cases with a number of shuffles except for MMX.
4853 if (NumElems == 4 && !isMMX)
Nate Begeman9008ca62009-04-27 18:41:29 +00004854 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004855
Dan Gohman475871a2008-07-27 21:46:04 +00004856 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004857}
4858
Dan Gohman475871a2008-07-27 21:46:04 +00004859SDValue
4860X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00004861 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00004862 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004863 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004864 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004865 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004866 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004867 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004868 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004869 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004870 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00004871 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
4872 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
4873 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004874 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4875 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004876 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004877 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00004878 Op.getOperand(0)),
4879 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00004880 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004881 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004882 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004883 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004884 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00004885 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00004886 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4887 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004888 // result has a single use which is a store or a bitcast to i32. And in
4889 // the case of a store, it's not worth it if the index is a constant 0,
4890 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00004891 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00004892 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004893 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004894 if ((User->getOpcode() != ISD::STORE ||
4895 (isa<ConstantSDNode>(Op.getOperand(1)) &&
4896 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman171c11e2008-04-16 02:32:24 +00004897 (User->getOpcode() != ISD::BIT_CONVERT ||
Owen Anderson825b72b2009-08-11 20:47:22 +00004898 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00004899 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00004900 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
4901 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004902 Op.getOperand(0)),
4903 Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004904 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Extract);
4905 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00004906 // ExtractPS works with constant index.
4907 if (isa<ConstantSDNode>(Op.getOperand(1)))
4908 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004909 }
Dan Gohman475871a2008-07-27 21:46:04 +00004910 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004911}
4912
4913
Dan Gohman475871a2008-07-27 21:46:04 +00004914SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004915X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
4916 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004917 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00004918 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004919
Evan Cheng62a3f152008-03-24 21:52:23 +00004920 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004921 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004922 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00004923 return Res;
4924 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00004925
Owen Andersone50ed302009-08-10 22:56:29 +00004926 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004927 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004928 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004929 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004930 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004931 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004932 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004933 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4934 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Scott Michelfdc40a02009-02-17 22:15:04 +00004935 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004936 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00004937 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004938 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00004939 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00004940 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004941 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00004942 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004943 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004944 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004945 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004946 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004947 if (Idx == 0)
4948 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004949
Evan Cheng0db9fe62006-04-25 20:13:52 +00004950 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00004951 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00004952 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00004953 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00004954 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004955 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004956 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004957 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004958 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4959 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4960 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004961 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004962 if (Idx == 0)
4963 return Op;
4964
4965 // UNPCKHPD the element to the lowest double word, then movsd.
4966 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4967 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00004968 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00004969 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00004970 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00004971 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004972 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004973 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004974 }
4975
Dan Gohman475871a2008-07-27 21:46:04 +00004976 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004977}
4978
Dan Gohman475871a2008-07-27 21:46:04 +00004979SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004980X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
4981 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00004982 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00004983 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004984 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004985
Dan Gohman475871a2008-07-27 21:46:04 +00004986 SDValue N0 = Op.getOperand(0);
4987 SDValue N1 = Op.getOperand(1);
4988 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004989
Dan Gohman8a55ce42009-09-23 21:02:20 +00004990 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00004991 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00004992 unsigned Opc;
4993 if (VT == MVT::v8i16)
4994 Opc = X86ISD::PINSRW;
4995 else if (VT == MVT::v4i16)
4996 Opc = X86ISD::MMX_PINSRW;
4997 else if (VT == MVT::v16i8)
4998 Opc = X86ISD::PINSRB;
4999 else
5000 Opc = X86ISD::PINSRB;
5001
Nate Begeman14d12ca2008-02-11 04:19:36 +00005002 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
5003 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00005004 if (N1.getValueType() != MVT::i32)
5005 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
5006 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005007 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00005008 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00005009 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00005010 // Bits [7:6] of the constant are the source select. This will always be
5011 // zero here. The DAG Combiner may combine an extract_elt index into these
5012 // bits. For example (insert (extract, 3), 2) could be matched by putting
5013 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00005014 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00005015 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00005016 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00005017 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005018 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00005019 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00005020 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00005021 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00005022 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00005023 // PINSR* works with constant index.
5024 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00005025 }
Dan Gohman475871a2008-07-27 21:46:04 +00005026 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005027}
5028
Dan Gohman475871a2008-07-27 21:46:04 +00005029SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005030X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005031 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00005032 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005033
5034 if (Subtarget->hasSSE41())
5035 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
5036
Dan Gohman8a55ce42009-09-23 21:02:20 +00005037 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00005038 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00005039
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005040 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005041 SDValue N0 = Op.getOperand(0);
5042 SDValue N1 = Op.getOperand(1);
5043 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00005044
Dan Gohman8a55ce42009-09-23 21:02:20 +00005045 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00005046 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
5047 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00005048 if (N1.getValueType() != MVT::i32)
5049 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
5050 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005051 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00005052 return DAG.getNode(VT == MVT::v8i16 ? X86ISD::PINSRW : X86ISD::MMX_PINSRW,
5053 dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005054 }
Dan Gohman475871a2008-07-27 21:46:04 +00005055 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005056}
5057
Dan Gohman475871a2008-07-27 21:46:04 +00005058SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005059X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005060 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00005061 if (Op.getValueType() == MVT::v2f32)
5062 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f32,
5063 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i32,
5064 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32,
Evan Cheng52672b82008-07-22 18:39:19 +00005065 Op.getOperand(0))));
5066
Owen Anderson825b72b2009-08-11 20:47:22 +00005067 if (Op.getValueType() == MVT::v1i64 && Op.getOperand(0).getValueType() == MVT::i64)
5068 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 {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005368 // TODO: implement the "local dynamic" model
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00005369 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005370 assert(Subtarget->isTargetELF() &&
5371 "TLS not implemented for non-ELF targets");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005372 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00005373 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00005374
Chris Lattnerb903bed2009-06-26 21:20:29 +00005375 // If GV is an alias then use the aliasee for determining
5376 // thread-localness.
5377 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
5378 GV = GA->resolveAliasedGlobal(false);
Eric Christopherfd179292009-08-27 18:07:15 +00005379
Chris Lattnerb903bed2009-06-26 21:20:29 +00005380 TLSModel::Model model = getTLSModel(GV,
5381 getTargetMachine().getRelocationModel());
Eric Christopherfd179292009-08-27 18:07:15 +00005382
Chris Lattnerb903bed2009-06-26 21:20:29 +00005383 switch (model) {
5384 case TLSModel::GeneralDynamic:
5385 case TLSModel::LocalDynamic: // not implemented
5386 if (Subtarget->is64Bit())
Rafael Espindola9a580232009-02-27 13:37:18 +00005387 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
Chris Lattnerb903bed2009-06-26 21:20:29 +00005388 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00005389
Chris Lattnerb903bed2009-06-26 21:20:29 +00005390 case TLSModel::InitialExec:
5391 case TLSModel::LocalExec:
5392 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
5393 Subtarget->is64Bit());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005394 }
Eric Christopherfd179292009-08-27 18:07:15 +00005395
Torok Edwinc23197a2009-07-14 16:55:14 +00005396 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00005397 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005398}
5399
Evan Cheng0db9fe62006-04-25 20:13:52 +00005400
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005401/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00005402/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00005403SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00005404 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00005405 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005406 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005407 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005408 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00005409 SDValue ShOpLo = Op.getOperand(0);
5410 SDValue ShOpHi = Op.getOperand(1);
5411 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00005412 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00005413 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00005414 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00005415
Dan Gohman475871a2008-07-27 21:46:04 +00005416 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005417 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00005418 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
5419 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005420 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005421 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
5422 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005423 }
Evan Chenge3413162006-01-09 18:33:28 +00005424
Owen Anderson825b72b2009-08-11 20:47:22 +00005425 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
5426 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00005427 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00005428 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00005429
Dan Gohman475871a2008-07-27 21:46:04 +00005430 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00005431 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00005432 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
5433 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00005434
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005435 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00005436 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
5437 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005438 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005439 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
5440 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005441 }
5442
Dan Gohman475871a2008-07-27 21:46:04 +00005443 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00005444 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005445}
Evan Chenga3195e82006-01-12 22:54:21 +00005446
Dan Gohmand858e902010-04-17 15:26:15 +00005447SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
5448 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005449 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00005450
5451 if (SrcVT.isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005452 if (SrcVT == MVT::v2i32 && Op.getValueType() == MVT::v2f64) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005453 return Op;
5454 }
5455 return SDValue();
5456 }
5457
Owen Anderson825b72b2009-08-11 20:47:22 +00005458 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00005459 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00005460
Eli Friedman36df4992009-05-27 00:47:34 +00005461 // These are really Legal; return the operand so the caller accepts it as
5462 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00005463 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00005464 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00005465 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00005466 Subtarget->is64Bit()) {
5467 return Op;
5468 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005469
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005470 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005471 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005472 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005473 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005474 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00005475 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00005476 StackSlot,
David Greene67c9d422010-02-15 16:53:33 +00005477 PseudoSourceValue::getFixedStack(SSFI), 0,
5478 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00005479 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
5480}
Evan Cheng0db9fe62006-04-25 20:13:52 +00005481
Owen Andersone50ed302009-08-10 22:56:29 +00005482SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Dale Johannesen8d908eb2010-05-15 18:51:12 +00005483 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00005484 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005485 // Build the FILD
Eli Friedman948e95a2009-05-23 09:59:16 +00005486 DebugLoc dl = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00005487 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00005488 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005489 if (useSSE)
Owen Anderson825b72b2009-08-11 20:47:22 +00005490 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00005491 else
Owen Anderson825b72b2009-08-11 20:47:22 +00005492 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005493 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Dale Johannesenace16102009-02-03 19:33:06 +00005494 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD, dl,
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005495 Tys, Ops, array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005496
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005497 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005498 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00005499 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005500
5501 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
5502 // shouldn't be necessary except that RFP cannot be live across
5503 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005504 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005505 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005506 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00005507 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005508 SDValue Ops[] = {
5509 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
5510 };
5511 Chain = DAG.getNode(X86ISD::FST, dl, Tys, Ops, array_lengthof(Ops));
Dale Johannesenace16102009-02-03 19:33:06 +00005512 Result = DAG.getLoad(Op.getValueType(), dl, Chain, StackSlot,
David Greene67c9d422010-02-15 16:53:33 +00005513 PseudoSourceValue::getFixedStack(SSFI), 0,
5514 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005515 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005516
Evan Cheng0db9fe62006-04-25 20:13:52 +00005517 return Result;
5518}
5519
Bill Wendling8b8a6362009-01-17 03:56:04 +00005520// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00005521SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
5522 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00005523 // This algorithm is not obvious. Here it is in C code, more or less:
5524 /*
5525 double uint64_to_double( uint32_t hi, uint32_t lo ) {
5526 static const __m128i exp = { 0x4330000045300000ULL, 0 };
5527 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00005528
Bill Wendling8b8a6362009-01-17 03:56:04 +00005529 // Copy ints to xmm registers.
5530 __m128i xh = _mm_cvtsi32_si128( hi );
5531 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00005532
Bill Wendling8b8a6362009-01-17 03:56:04 +00005533 // Combine into low half of a single xmm register.
5534 __m128i x = _mm_unpacklo_epi32( xh, xl );
5535 __m128d d;
5536 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00005537
Bill Wendling8b8a6362009-01-17 03:56:04 +00005538 // Merge in appropriate exponents to give the integer bits the right
5539 // magnitude.
5540 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00005541
Bill Wendling8b8a6362009-01-17 03:56:04 +00005542 // Subtract away the biases to deal with the IEEE-754 double precision
5543 // implicit 1.
5544 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00005545
Bill Wendling8b8a6362009-01-17 03:56:04 +00005546 // All conversions up to here are exact. The correctly rounded result is
5547 // calculated using the current rounding mode using the following
5548 // horizontal add.
5549 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
5550 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
5551 // store doesn't really need to be here (except
5552 // maybe to zero the other double)
5553 return sd;
5554 }
5555 */
Dale Johannesen040225f2008-10-21 23:07:49 +00005556
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005557 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00005558 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00005559
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005560 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00005561 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00005562 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
5563 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
5564 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
5565 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00005566 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005567 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005568
Bill Wendling8b8a6362009-01-17 03:56:04 +00005569 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00005570 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005571 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00005572 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005573 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00005574 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005575 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005576
Owen Anderson825b72b2009-08-11 20:47:22 +00005577 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5578 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00005579 Op.getOperand(0),
5580 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005581 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5582 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00005583 Op.getOperand(0),
5584 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005585 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
5586 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005587 PseudoSourceValue::getConstantPool(), 0,
David Greene67c9d422010-02-15 16:53:33 +00005588 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00005589 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
5590 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Unpck2);
5591 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005592 PseudoSourceValue::getConstantPool(), 0,
David Greene67c9d422010-02-15 16:53:33 +00005593 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00005594 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005595
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005596 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00005597 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00005598 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
5599 DAG.getUNDEF(MVT::v2f64), ShufMask);
5600 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
5601 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005602 DAG.getIntPtrConstant(0));
5603}
5604
Bill Wendling8b8a6362009-01-17 03:56:04 +00005605// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00005606SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
5607 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005608 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00005609 // FP constant to bias correct the final result.
5610 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00005611 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005612
5613 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00005614 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5615 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005616 Op.getOperand(0),
5617 DAG.getIntPtrConstant(0)));
5618
Owen Anderson825b72b2009-08-11 20:47:22 +00005619 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5620 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00005621 DAG.getIntPtrConstant(0));
5622
5623 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00005624 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
5625 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005626 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005627 MVT::v2f64, Load)),
5628 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005629 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005630 MVT::v2f64, Bias)));
5631 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5632 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00005633 DAG.getIntPtrConstant(0));
5634
5635 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00005636 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005637
5638 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00005639 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00005640
Owen Anderson825b72b2009-08-11 20:47:22 +00005641 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005642 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00005643 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00005644 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005645 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00005646 }
5647
5648 // Handle final rounding.
5649 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00005650}
5651
Dan Gohmand858e902010-04-17 15:26:15 +00005652SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
5653 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00005654 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005655 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00005656
Dale Johannesen8d908eb2010-05-15 18:51:12 +00005657 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00005658 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
5659 // the optimization here.
5660 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00005661 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00005662
Owen Andersone50ed302009-08-10 22:56:29 +00005663 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00005664 EVT DstVT = Op.getValueType();
5665 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00005666 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00005667 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00005668 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00005669
5670 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00005671 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00005672 if (SrcVT == MVT::i32) {
5673 SDValue WordOff = DAG.getConstant(4, getPointerTy());
5674 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
5675 getPointerTy(), StackSlot, WordOff);
5676 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
5677 StackSlot, NULL, 0, false, false, 0);
5678 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
5679 OffsetSlot, NULL, 0, false, false, 0);
5680 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
5681 return Fild;
5682 }
5683
5684 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
5685 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
David Greene67c9d422010-02-15 16:53:33 +00005686 StackSlot, NULL, 0, false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00005687 // For i64 source, we need to add the appropriate power of 2 if the input
5688 // was negative. This is the same as the optimization in
5689 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
5690 // we must be careful to do the computation in x87 extended precision, not
5691 // in SSE. (The generic code can't know it's OK to do this, or how to.)
5692 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
5693 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
5694 SDValue Fild = DAG.getNode(X86ISD::FILD, dl, Tys, Ops, 3);
5695
5696 APInt FF(32, 0x5F800000ULL);
5697
5698 // Check whether the sign bit is set.
5699 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
5700 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
5701 ISD::SETLT);
5702
5703 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
5704 SDValue FudgePtr = DAG.getConstantPool(
5705 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
5706 getPointerTy());
5707
5708 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
5709 SDValue Zero = DAG.getIntPtrConstant(0);
5710 SDValue Four = DAG.getIntPtrConstant(4);
5711 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
5712 Zero, Four);
5713 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
5714
5715 // Load the value out, extending it from f32 to f80.
5716 // FIXME: Avoid the extend by constructing the right constant pool?
5717 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
5718 FudgePtr, PseudoSourceValue::getConstantPool(),
5719 0, MVT::f32, false, false, 4);
5720 // Extend everything to 80 bits to force it to be done on x87.
5721 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
5722 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00005723}
5724
Dan Gohman475871a2008-07-27 21:46:04 +00005725std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00005726FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005727 DebugLoc dl = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00005728
Owen Andersone50ed302009-08-10 22:56:29 +00005729 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00005730
5731 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005732 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
5733 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00005734 }
5735
Owen Anderson825b72b2009-08-11 20:47:22 +00005736 assert(DstTy.getSimpleVT() <= MVT::i64 &&
5737 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00005738 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00005739
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005740 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00005741 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00005742 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005743 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00005744 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00005745 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00005746 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005747 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005748
Evan Cheng87c89352007-10-15 20:11:21 +00005749 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
5750 // stack slot.
5751 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00005752 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00005753 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005754 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00005755
Evan Cheng0db9fe62006-04-25 20:13:52 +00005756 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00005757 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005758 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005759 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
5760 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
5761 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005762 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005763
Dan Gohman475871a2008-07-27 21:46:04 +00005764 SDValue Chain = DAG.getEntryNode();
5765 SDValue Value = Op.getOperand(0);
Chris Lattner78631162008-01-16 06:24:21 +00005766 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005767 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dale Johannesenace16102009-02-03 19:33:06 +00005768 Chain = DAG.getStore(Chain, dl, Value, StackSlot,
David Greene67c9d422010-02-15 16:53:33 +00005769 PseudoSourceValue::getFixedStack(SSFI), 0,
5770 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005771 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00005772 SDValue Ops[] = {
Chris Lattner5a88b832007-02-25 07:10:00 +00005773 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
5774 };
Dale Johannesenace16102009-02-03 19:33:06 +00005775 Value = DAG.getNode(X86ISD::FLD, dl, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005776 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00005777 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005778 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5779 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005780
Evan Cheng0db9fe62006-04-25 20:13:52 +00005781 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00005782 SDValue Ops[] = { Chain, Value, StackSlot };
Owen Anderson825b72b2009-08-11 20:47:22 +00005783 SDValue FIST = DAG.getNode(Opc, dl, MVT::Other, Ops, 3);
Evan Chengd9558e02006-01-06 00:43:03 +00005784
Chris Lattner27a6c732007-11-24 07:07:01 +00005785 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005786}
5787
Dan Gohmand858e902010-04-17 15:26:15 +00005788SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
5789 SelectionDAG &DAG) const {
Eli Friedman23ef1052009-06-06 03:57:58 +00005790 if (Op.getValueType().isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005791 if (Op.getValueType() == MVT::v2i32 &&
5792 Op.getOperand(0).getValueType() == MVT::v2f64) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005793 return Op;
5794 }
5795 return SDValue();
5796 }
5797
Eli Friedman948e95a2009-05-23 09:59:16 +00005798 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00005799 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00005800 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
5801 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00005802
Chris Lattner27a6c732007-11-24 07:07:01 +00005803 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005804 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
David Greene67c9d422010-02-15 16:53:33 +00005805 FIST, StackSlot, NULL, 0, false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00005806}
5807
Dan Gohmand858e902010-04-17 15:26:15 +00005808SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
5809 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00005810 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
5811 SDValue FIST = Vals.first, StackSlot = Vals.second;
5812 assert(FIST.getNode() && "Unexpected failure");
5813
5814 // Load the result.
5815 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
David Greene67c9d422010-02-15 16:53:33 +00005816 FIST, StackSlot, NULL, 0, false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00005817}
5818
Dan Gohmand858e902010-04-17 15:26:15 +00005819SDValue X86TargetLowering::LowerFABS(SDValue Op,
5820 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005821 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005822 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005823 EVT VT = Op.getValueType();
5824 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005825 if (VT.isVector())
5826 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005827 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005828 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005829 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00005830 CV.push_back(C);
5831 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005832 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005833 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00005834 CV.push_back(C);
5835 CV.push_back(C);
5836 CV.push_back(C);
5837 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005838 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005839 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005840 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005841 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005842 PseudoSourceValue::getConstantPool(), 0,
5843 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005844 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005845}
5846
Dan Gohmand858e902010-04-17 15:26:15 +00005847SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005848 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005849 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005850 EVT VT = Op.getValueType();
5851 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00005852 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00005853 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005854 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005855 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005856 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00005857 CV.push_back(C);
5858 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005859 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005860 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00005861 CV.push_back(C);
5862 CV.push_back(C);
5863 CV.push_back(C);
5864 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005865 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005866 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005867 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005868 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005869 PseudoSourceValue::getConstantPool(), 0,
5870 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005871 if (VT.isVector()) {
Dale Johannesenace16102009-02-03 19:33:06 +00005872 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00005873 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
5874 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005875 Op.getOperand(0)),
Owen Anderson825b72b2009-08-11 20:47:22 +00005876 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00005877 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005878 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00005879 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005880}
5881
Dan Gohmand858e902010-04-17 15:26:15 +00005882SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005883 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00005884 SDValue Op0 = Op.getOperand(0);
5885 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005886 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005887 EVT VT = Op.getValueType();
5888 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00005889
5890 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005891 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005892 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005893 SrcVT = VT;
5894 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005895 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005896 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005897 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005898 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005899 }
5900
5901 // At this point the operands and the result should have the same
5902 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00005903
Evan Cheng68c47cb2007-01-05 07:55:56 +00005904 // First get the sign bit of second operand.
5905 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005906 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005907 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
5908 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005909 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005910 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
5911 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5912 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5913 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005914 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005915 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005916 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005917 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005918 PseudoSourceValue::getConstantPool(), 0,
5919 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005920 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005921
5922 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005923 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005924 // Op0 is MVT::f32, Op1 is MVT::f64.
5925 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
5926 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
5927 DAG.getConstant(32, MVT::i32));
5928 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, SignBit);
5929 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00005930 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005931 }
5932
Evan Cheng73d6cf12007-01-05 21:37:56 +00005933 // Clear first operand sign bit.
5934 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00005935 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005936 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
5937 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005938 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005939 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
5940 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5941 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5942 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005943 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005944 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005945 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005946 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005947 PseudoSourceValue::getConstantPool(), 0,
5948 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005949 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005950
5951 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00005952 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005953}
5954
Dan Gohman076aee32009-03-04 19:44:21 +00005955/// Emit nodes that will be selected as "test Op0,Op0", or something
5956/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005957SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00005958 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00005959 DebugLoc dl = Op.getDebugLoc();
5960
Dan Gohman31125812009-03-07 01:58:32 +00005961 // CF and OF aren't always set the way we want. Determine which
5962 // of these we need.
5963 bool NeedCF = false;
5964 bool NeedOF = false;
5965 switch (X86CC) {
5966 case X86::COND_A: case X86::COND_AE:
5967 case X86::COND_B: case X86::COND_BE:
5968 NeedCF = true;
5969 break;
5970 case X86::COND_G: case X86::COND_GE:
5971 case X86::COND_L: case X86::COND_LE:
5972 case X86::COND_O: case X86::COND_NO:
5973 NeedOF = true;
5974 break;
5975 default: break;
5976 }
5977
Dan Gohman076aee32009-03-04 19:44:21 +00005978 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00005979 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
5980 // we prove that the arithmetic won't overflow, we can't use OF or CF.
5981 if (Op.getResNo() == 0 && !NeedOF && !NeedCF) {
Dan Gohman076aee32009-03-04 19:44:21 +00005982 unsigned Opcode = 0;
Dan Gohman51bb4742009-03-05 21:29:28 +00005983 unsigned NumOperands = 0;
Dan Gohman076aee32009-03-04 19:44:21 +00005984 switch (Op.getNode()->getOpcode()) {
5985 case ISD::ADD:
Stuart Hastings5a6a65b2010-04-28 00:35:10 +00005986 // Due to an isel shortcoming, be conservative if this add is
5987 // likely to be selected as part of a load-modify-store
5988 // instruction. When the root node in a match is a store, isel
5989 // doesn't know how to remap non-chain non-flag uses of other
5990 // nodes in the match, such as the ADD in this case. This leads
5991 // to the ADD being left around and reselected, with the result
5992 // being two adds in the output. Alas, even if none our users
5993 // are stores, that doesn't prove we're O.K. Ergo, if we have
5994 // any parents that aren't CopyToReg or SETCC, eschew INC/DEC.
5995 // A better fix seems to require climbing the DAG back to the
5996 // root, and it doesn't seem to be worth the effort.
Dan Gohman076aee32009-03-04 19:44:21 +00005997 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Stuart Hastings5a6a65b2010-04-28 00:35:10 +00005998 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5999 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
Dan Gohman076aee32009-03-04 19:44:21 +00006000 goto default_case;
Dan Gohman076aee32009-03-04 19:44:21 +00006001 if (ConstantSDNode *C =
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00006002 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
6003 // An add of one will be selected as an INC.
Dan Gohman076aee32009-03-04 19:44:21 +00006004 if (C->getAPIntValue() == 1) {
6005 Opcode = X86ISD::INC;
Dan Gohman51bb4742009-03-05 21:29:28 +00006006 NumOperands = 1;
Dan Gohman076aee32009-03-04 19:44:21 +00006007 break;
6008 }
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00006009 // An add of negative one (subtract of one) will be selected as a DEC.
6010 if (C->getAPIntValue().isAllOnesValue()) {
6011 Opcode = X86ISD::DEC;
Dan Gohman51bb4742009-03-05 21:29:28 +00006012 NumOperands = 1;
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00006013 break;
6014 }
6015 }
Dan Gohman076aee32009-03-04 19:44:21 +00006016 // Otherwise use a regular EFLAGS-setting add.
6017 Opcode = X86ISD::ADD;
Dan Gohman51bb4742009-03-05 21:29:28 +00006018 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00006019 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00006020 case ISD::AND: {
6021 // If the primary and result isn't used, don't bother using X86ISD::AND,
6022 // because a TEST instruction will be better.
6023 bool NonFlagUse = false;
6024 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Evan Cheng17751da2010-01-07 00:54:06 +00006025 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
6026 SDNode *User = *UI;
6027 unsigned UOpNo = UI.getOperandNo();
6028 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
6029 // Look pass truncate.
6030 UOpNo = User->use_begin().getOperandNo();
6031 User = *User->use_begin();
6032 }
6033 if (User->getOpcode() != ISD::BRCOND &&
6034 User->getOpcode() != ISD::SETCC &&
6035 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
Dan Gohmane220c4b2009-09-18 19:59:53 +00006036 NonFlagUse = true;
6037 break;
6038 }
Evan Cheng17751da2010-01-07 00:54:06 +00006039 }
Dan Gohmane220c4b2009-09-18 19:59:53 +00006040 if (!NonFlagUse)
6041 break;
6042 }
6043 // FALL THROUGH
Dan Gohman076aee32009-03-04 19:44:21 +00006044 case ISD::SUB:
Dan Gohmane220c4b2009-09-18 19:59:53 +00006045 case ISD::OR:
6046 case ISD::XOR:
6047 // Due to the ISEL shortcoming noted above, be conservative if this op is
Dan Gohman076aee32009-03-04 19:44:21 +00006048 // likely to be selected as part of a load-modify-store instruction.
6049 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
6050 UE = Op.getNode()->use_end(); UI != UE; ++UI)
6051 if (UI->getOpcode() == ISD::STORE)
6052 goto default_case;
Dan Gohmane220c4b2009-09-18 19:59:53 +00006053 // Otherwise use a regular EFLAGS-setting instruction.
6054 switch (Op.getNode()->getOpcode()) {
6055 case ISD::SUB: Opcode = X86ISD::SUB; break;
6056 case ISD::OR: Opcode = X86ISD::OR; break;
6057 case ISD::XOR: Opcode = X86ISD::XOR; break;
6058 case ISD::AND: Opcode = X86ISD::AND; break;
6059 default: llvm_unreachable("unexpected operator!");
6060 }
Dan Gohman51bb4742009-03-05 21:29:28 +00006061 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00006062 break;
6063 case X86ISD::ADD:
6064 case X86ISD::SUB:
6065 case X86ISD::INC:
6066 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +00006067 case X86ISD::OR:
6068 case X86ISD::XOR:
6069 case X86ISD::AND:
Dan Gohman076aee32009-03-04 19:44:21 +00006070 return SDValue(Op.getNode(), 1);
6071 default:
6072 default_case:
6073 break;
6074 }
6075 if (Opcode != 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006076 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
Dan Gohman076aee32009-03-04 19:44:21 +00006077 SmallVector<SDValue, 4> Ops;
Dan Gohman31125812009-03-07 01:58:32 +00006078 for (unsigned i = 0; i != NumOperands; ++i)
Dan Gohman076aee32009-03-04 19:44:21 +00006079 Ops.push_back(Op.getOperand(i));
Dan Gohmanfc166572009-04-09 23:54:40 +00006080 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
Dan Gohman076aee32009-03-04 19:44:21 +00006081 DAG.ReplaceAllUsesWith(Op, New);
6082 return SDValue(New.getNode(), 1);
6083 }
6084 }
6085
6086 // Otherwise just emit a CMP with 0, which is the TEST pattern.
Owen Anderson825b72b2009-08-11 20:47:22 +00006087 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
Dan Gohman076aee32009-03-04 19:44:21 +00006088 DAG.getConstant(0, Op.getValueType()));
6089}
6090
6091/// Emit nodes that will be selected as "cmp Op0,Op1", or something
6092/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00006093SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00006094 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00006095 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
6096 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00006097 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00006098
6099 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00006100 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00006101}
6102
Evan Chengd40d03e2010-01-06 19:38:29 +00006103/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
6104/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00006105SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
6106 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00006107 SDValue Op0 = And.getOperand(0);
6108 SDValue Op1 = And.getOperand(1);
6109 if (Op0.getOpcode() == ISD::TRUNCATE)
6110 Op0 = Op0.getOperand(0);
6111 if (Op1.getOpcode() == ISD::TRUNCATE)
6112 Op1 = Op1.getOperand(0);
6113
Evan Chengd40d03e2010-01-06 19:38:29 +00006114 SDValue LHS, RHS;
Evan Cheng2c755ba2010-02-27 07:36:59 +00006115 if (Op1.getOpcode() == ISD::SHL) {
6116 if (ConstantSDNode *And10C = dyn_cast<ConstantSDNode>(Op1.getOperand(0)))
6117 if (And10C->getZExtValue() == 1) {
6118 LHS = Op0;
6119 RHS = Op1.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00006120 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00006121 } else if (Op0.getOpcode() == ISD::SHL) {
6122 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
6123 if (And00C->getZExtValue() == 1) {
6124 LHS = Op1;
6125 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00006126 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00006127 } else if (Op1.getOpcode() == ISD::Constant) {
6128 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
6129 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00006130 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
6131 LHS = AndLHS.getOperand(0);
6132 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00006133 }
Evan Chengd40d03e2010-01-06 19:38:29 +00006134 }
Evan Cheng0488db92007-09-25 01:57:46 +00006135
Evan Chengd40d03e2010-01-06 19:38:29 +00006136 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00006137 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00006138 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00006139 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00006140 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00006141 // Also promote i16 to i32 for performance / code size reason.
6142 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00006143 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00006144 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00006145
Evan Chengd40d03e2010-01-06 19:38:29 +00006146 // If the operand types disagree, extend the shift amount to match. Since
6147 // BT ignores high bits (like shifts) we can use anyextend.
6148 if (LHS.getValueType() != RHS.getValueType())
6149 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00006150
Evan Chengd40d03e2010-01-06 19:38:29 +00006151 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
6152 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
6153 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6154 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00006155 }
6156
Evan Cheng54de3ea2010-01-05 06:52:31 +00006157 return SDValue();
6158}
6159
Dan Gohmand858e902010-04-17 15:26:15 +00006160SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng54de3ea2010-01-05 06:52:31 +00006161 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
6162 SDValue Op0 = Op.getOperand(0);
6163 SDValue Op1 = Op.getOperand(1);
6164 DebugLoc dl = Op.getDebugLoc();
6165 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
6166
6167 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00006168 // Lower (X & (1 << N)) == 0 to BT(X, N).
6169 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
6170 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
6171 if (Op0.getOpcode() == ISD::AND &&
6172 Op0.hasOneUse() &&
6173 Op1.getOpcode() == ISD::Constant &&
6174 cast<ConstantSDNode>(Op1)->getZExtValue() == 0 &&
6175 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
6176 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
6177 if (NewSetCC.getNode())
6178 return NewSetCC;
6179 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00006180
Evan Cheng2c755ba2010-02-27 07:36:59 +00006181 // Look for "(setcc) == / != 1" to avoid unncessary setcc.
6182 if (Op0.getOpcode() == X86ISD::SETCC &&
6183 Op1.getOpcode() == ISD::Constant &&
6184 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
6185 cast<ConstantSDNode>(Op1)->isNullValue()) &&
6186 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
6187 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
6188 bool Invert = (CC == ISD::SETNE) ^
6189 cast<ConstantSDNode>(Op1)->isNullValue();
6190 if (Invert)
6191 CCode = X86::GetOppositeBranchCondition(CCode);
6192 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6193 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
6194 }
6195
Evan Chenge5b51ac2010-04-17 06:13:15 +00006196 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00006197 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00006198 if (X86CC == X86::COND_INVALID)
6199 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00006200
Evan Cheng552f09a2010-04-26 19:06:11 +00006201 SDValue Cond = EmitCmp(Op0, Op1, X86CC, DAG);
Evan Chengad9c0a32009-12-15 00:53:42 +00006202
6203 // Use sbb x, x to materialize carry bit into a GPR.
Evan Cheng2e489c42009-12-16 00:53:11 +00006204 if (X86CC == X86::COND_B)
Evan Chengad9c0a32009-12-15 00:53:42 +00006205 return DAG.getNode(ISD::AND, dl, MVT::i8,
6206 DAG.getNode(X86ISD::SETCC_CARRY, dl, MVT::i8,
6207 DAG.getConstant(X86CC, MVT::i8), Cond),
6208 DAG.getConstant(1, MVT::i8));
Evan Chengad9c0a32009-12-15 00:53:42 +00006209
Owen Anderson825b72b2009-08-11 20:47:22 +00006210 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6211 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00006212}
6213
Dan Gohmand858e902010-04-17 15:26:15 +00006214SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00006215 SDValue Cond;
6216 SDValue Op0 = Op.getOperand(0);
6217 SDValue Op1 = Op.getOperand(1);
6218 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00006219 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00006220 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
6221 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006222 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00006223
6224 if (isFP) {
6225 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00006226 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00006227 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
6228 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00006229 bool Swap = false;
6230
6231 switch (SetCCOpcode) {
6232 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00006233 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00006234 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00006235 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00006236 case ISD::SETGT: Swap = true; // Fallthrough
6237 case ISD::SETLT:
6238 case ISD::SETOLT: SSECC = 1; break;
6239 case ISD::SETOGE:
6240 case ISD::SETGE: Swap = true; // Fallthrough
6241 case ISD::SETLE:
6242 case ISD::SETOLE: SSECC = 2; break;
6243 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00006244 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00006245 case ISD::SETNE: SSECC = 4; break;
6246 case ISD::SETULE: Swap = true;
6247 case ISD::SETUGE: SSECC = 5; break;
6248 case ISD::SETULT: Swap = true;
6249 case ISD::SETUGT: SSECC = 6; break;
6250 case ISD::SETO: SSECC = 7; break;
6251 }
6252 if (Swap)
6253 std::swap(Op0, Op1);
6254
Nate Begemanfb8ead02008-07-25 19:05:58 +00006255 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00006256 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00006257 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00006258 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00006259 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
6260 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00006261 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00006262 }
6263 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00006264 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00006265 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
6266 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00006267 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00006268 }
Torok Edwinc23197a2009-07-14 16:55:14 +00006269 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00006270 }
6271 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00006272 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00006273 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006274
Nate Begeman30a0de92008-07-17 16:51:19 +00006275 // We are handling one of the integer comparisons here. Since SSE only has
6276 // GT and EQ comparisons for integer, swapping operands and multiple
6277 // operations may be required for some comparisons.
6278 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
6279 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00006280
Owen Anderson825b72b2009-08-11 20:47:22 +00006281 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00006282 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00006283 case MVT::v8i8:
6284 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
6285 case MVT::v4i16:
6286 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
6287 case MVT::v2i32:
6288 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
6289 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00006290 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006291
Nate Begeman30a0de92008-07-17 16:51:19 +00006292 switch (SetCCOpcode) {
6293 default: break;
6294 case ISD::SETNE: Invert = true;
6295 case ISD::SETEQ: Opc = EQOpc; break;
6296 case ISD::SETLT: Swap = true;
6297 case ISD::SETGT: Opc = GTOpc; break;
6298 case ISD::SETGE: Swap = true;
6299 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
6300 case ISD::SETULT: Swap = true;
6301 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
6302 case ISD::SETUGE: Swap = true;
6303 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
6304 }
6305 if (Swap)
6306 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006307
Nate Begeman30a0de92008-07-17 16:51:19 +00006308 // Since SSE has no unsigned integer comparisons, we need to flip the sign
6309 // bits of the inputs before performing those operations.
6310 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00006311 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00006312 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
6313 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00006314 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00006315 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
6316 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00006317 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
6318 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00006319 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006320
Dale Johannesenace16102009-02-03 19:33:06 +00006321 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00006322
6323 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00006324 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00006325 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00006326
Nate Begeman30a0de92008-07-17 16:51:19 +00006327 return Result;
6328}
Evan Cheng0488db92007-09-25 01:57:46 +00006329
Evan Cheng370e5342008-12-03 08:38:43 +00006330// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00006331static bool isX86LogicalCmp(SDValue Op) {
6332 unsigned Opc = Op.getNode()->getOpcode();
6333 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
6334 return true;
6335 if (Op.getResNo() == 1 &&
6336 (Opc == X86ISD::ADD ||
6337 Opc == X86ISD::SUB ||
6338 Opc == X86ISD::SMUL ||
6339 Opc == X86ISD::UMUL ||
6340 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00006341 Opc == X86ISD::DEC ||
6342 Opc == X86ISD::OR ||
6343 Opc == X86ISD::XOR ||
6344 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00006345 return true;
6346
6347 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00006348}
6349
Dan Gohmand858e902010-04-17 15:26:15 +00006350SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00006351 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00006352 SDValue Cond = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006353 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00006354 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00006355
Dan Gohman1a492952009-10-20 16:22:37 +00006356 if (Cond.getOpcode() == ISD::SETCC) {
6357 SDValue NewCond = LowerSETCC(Cond, DAG);
6358 if (NewCond.getNode())
6359 Cond = NewCond;
6360 }
Evan Cheng734503b2006-09-11 02:19:56 +00006361
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006362 // (select (x == 0), -1, 0) -> (sign_bit (x - 1))
6363 SDValue Op1 = Op.getOperand(1);
6364 SDValue Op2 = Op.getOperand(2);
6365 if (Cond.getOpcode() == X86ISD::SETCC &&
6366 cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue() == X86::COND_E) {
6367 SDValue Cmp = Cond.getOperand(1);
6368 if (Cmp.getOpcode() == X86ISD::CMP) {
6369 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op1);
6370 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
6371 ConstantSDNode *RHSC =
6372 dyn_cast<ConstantSDNode>(Cmp.getOperand(1).getNode());
6373 if (N1C && N1C->isAllOnesValue() &&
6374 N2C && N2C->isNullValue() &&
6375 RHSC && RHSC->isNullValue()) {
6376 SDValue CmpOp0 = Cmp.getOperand(0);
Chris Lattnerda0688e2010-03-14 18:44:35 +00006377 Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006378 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
6379 return DAG.getNode(X86ISD::SETCC_CARRY, dl, Op.getValueType(),
6380 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
6381 }
6382 }
6383 }
6384
Evan Chengad9c0a32009-12-15 00:53:42 +00006385 // Look pass (and (setcc_carry (cmp ...)), 1).
6386 if (Cond.getOpcode() == ISD::AND &&
6387 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
6388 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
6389 if (C && C->getAPIntValue() == 1)
6390 Cond = Cond.getOperand(0);
6391 }
6392
Evan Cheng3f41d662007-10-08 22:16:29 +00006393 // If condition flag is set by a X86ISD::CMP, then use it as the condition
6394 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00006395 if (Cond.getOpcode() == X86ISD::SETCC ||
6396 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00006397 CC = Cond.getOperand(0);
6398
Dan Gohman475871a2008-07-27 21:46:04 +00006399 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00006400 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00006401 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00006402
Evan Cheng3f41d662007-10-08 22:16:29 +00006403 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006404 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00006405 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00006406 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00006407
Chris Lattnerd1980a52009-03-12 06:52:53 +00006408 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
6409 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00006410 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00006411 addTest = false;
6412 }
6413 }
6414
6415 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00006416 // Look pass the truncate.
6417 if (Cond.getOpcode() == ISD::TRUNCATE)
6418 Cond = Cond.getOperand(0);
6419
6420 // We know the result of AND is compared against zero. Try to match
6421 // it to BT.
6422 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
6423 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
6424 if (NewSetCC.getNode()) {
6425 CC = NewSetCC.getOperand(0);
6426 Cond = NewSetCC.getOperand(1);
6427 addTest = false;
6428 }
6429 }
6430 }
6431
6432 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006433 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00006434 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00006435 }
6436
Evan Cheng0488db92007-09-25 01:57:46 +00006437 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
6438 // condition is true.
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006439 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
6440 SDValue Ops[] = { Op2, Op1, CC, Cond };
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006441 return DAG.getNode(X86ISD::CMOV, dl, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00006442}
6443
Evan Cheng370e5342008-12-03 08:38:43 +00006444// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
6445// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
6446// from the AND / OR.
6447static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
6448 Opc = Op.getOpcode();
6449 if (Opc != ISD::OR && Opc != ISD::AND)
6450 return false;
6451 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
6452 Op.getOperand(0).hasOneUse() &&
6453 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
6454 Op.getOperand(1).hasOneUse());
6455}
6456
Evan Cheng961d6d42009-02-02 08:19:07 +00006457// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
6458// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00006459static bool isXor1OfSetCC(SDValue Op) {
6460 if (Op.getOpcode() != ISD::XOR)
6461 return false;
6462 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6463 if (N1C && N1C->getAPIntValue() == 1) {
6464 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
6465 Op.getOperand(0).hasOneUse();
6466 }
6467 return false;
6468}
6469
Dan Gohmand858e902010-04-17 15:26:15 +00006470SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00006471 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00006472 SDValue Chain = Op.getOperand(0);
6473 SDValue Cond = Op.getOperand(1);
6474 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006475 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00006476 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00006477
Dan Gohman1a492952009-10-20 16:22:37 +00006478 if (Cond.getOpcode() == ISD::SETCC) {
6479 SDValue NewCond = LowerSETCC(Cond, DAG);
6480 if (NewCond.getNode())
6481 Cond = NewCond;
6482 }
Chris Lattnere55484e2008-12-25 05:34:37 +00006483#if 0
6484 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00006485 else if (Cond.getOpcode() == X86ISD::ADD ||
6486 Cond.getOpcode() == X86ISD::SUB ||
6487 Cond.getOpcode() == X86ISD::SMUL ||
6488 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00006489 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00006490#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00006491
Evan Chengad9c0a32009-12-15 00:53:42 +00006492 // Look pass (and (setcc_carry (cmp ...)), 1).
6493 if (Cond.getOpcode() == ISD::AND &&
6494 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
6495 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
6496 if (C && C->getAPIntValue() == 1)
6497 Cond = Cond.getOperand(0);
6498 }
6499
Evan Cheng3f41d662007-10-08 22:16:29 +00006500 // If condition flag is set by a X86ISD::CMP, then use it as the condition
6501 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00006502 if (Cond.getOpcode() == X86ISD::SETCC ||
6503 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00006504 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006505
Dan Gohman475871a2008-07-27 21:46:04 +00006506 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00006507 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00006508 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00006509 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00006510 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00006511 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00006512 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00006513 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00006514 default: break;
6515 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00006516 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00006517 // These can only come from an arithmetic instruction with overflow,
6518 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00006519 Cond = Cond.getNode()->getOperand(1);
6520 addTest = false;
6521 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00006522 }
Evan Cheng0488db92007-09-25 01:57:46 +00006523 }
Evan Cheng370e5342008-12-03 08:38:43 +00006524 } else {
6525 unsigned CondOpc;
6526 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
6527 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00006528 if (CondOpc == ISD::OR) {
6529 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
6530 // two branches instead of an explicit OR instruction with a
6531 // separate test.
6532 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00006533 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00006534 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006535 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00006536 Chain, Dest, CC, Cmp);
6537 CC = Cond.getOperand(1).getOperand(0);
6538 Cond = Cmp;
6539 addTest = false;
6540 }
6541 } else { // ISD::AND
6542 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
6543 // two branches instead of an explicit AND instruction with a
6544 // separate test. However, we only do this if this block doesn't
6545 // have a fall-through edge, because this requires an explicit
6546 // jmp when the condition is false.
6547 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00006548 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00006549 Op.getNode()->hasOneUse()) {
6550 X86::CondCode CCode =
6551 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
6552 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006553 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00006554 SDValue User = SDValue(*Op.getNode()->use_begin(), 0);
6555 // Look for an unconditional branch following this conditional branch.
6556 // We need this because we need to reverse the successors in order
6557 // to implement FCMP_OEQ.
6558 if (User.getOpcode() == ISD::BR) {
6559 SDValue FalseBB = User.getOperand(1);
6560 SDValue NewBR =
6561 DAG.UpdateNodeOperands(User, User.getOperand(0), Dest);
6562 assert(NewBR == User);
6563 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00006564
Dale Johannesene4d209d2009-02-03 20:21:25 +00006565 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00006566 Chain, Dest, CC, Cmp);
6567 X86::CondCode CCode =
6568 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
6569 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006570 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00006571 Cond = Cmp;
6572 addTest = false;
6573 }
6574 }
Dan Gohman279c22e2008-10-21 03:29:32 +00006575 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00006576 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
6577 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
6578 // It should be transformed during dag combiner except when the condition
6579 // is set by a arithmetics with overflow node.
6580 X86::CondCode CCode =
6581 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
6582 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006583 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00006584 Cond = Cond.getOperand(0).getOperand(1);
6585 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00006586 }
Evan Cheng0488db92007-09-25 01:57:46 +00006587 }
6588
6589 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00006590 // Look pass the truncate.
6591 if (Cond.getOpcode() == ISD::TRUNCATE)
6592 Cond = Cond.getOperand(0);
6593
6594 // We know the result of AND is compared against zero. Try to match
6595 // it to BT.
6596 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
6597 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
6598 if (NewSetCC.getNode()) {
6599 CC = NewSetCC.getOperand(0);
6600 Cond = NewSetCC.getOperand(1);
6601 addTest = false;
6602 }
6603 }
6604 }
6605
6606 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006607 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00006608 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00006609 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00006610 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00006611 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00006612}
6613
Anton Korobeynikove060b532007-04-17 19:34:00 +00006614
6615// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
6616// Calls to _alloca is needed to probe the stack when allocating more than 4k
6617// bytes in one go. Touching the stack at 4K increments is necessary to ensure
6618// that the guard pages used by the OS virtual memory manager are allocated in
6619// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00006620SDValue
6621X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006622 SelectionDAG &DAG) const {
Anton Korobeynikove060b532007-04-17 19:34:00 +00006623 assert(Subtarget->isTargetCygMing() &&
6624 "This should be used only on Cygwin/Mingw targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006625 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006626
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006627 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00006628 SDValue Chain = Op.getOperand(0);
6629 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006630 // FIXME: Ensure alignment here
6631
Dan Gohman475871a2008-07-27 21:46:04 +00006632 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006633
Owen Andersone50ed302009-08-10 22:56:29 +00006634 EVT IntPtr = getPointerTy();
Owen Anderson825b72b2009-08-11 20:47:22 +00006635 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006636
Dale Johannesendd64c412009-02-04 00:33:20 +00006637 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006638 Flag = Chain.getValue(1);
6639
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00006640 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006641
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00006642 Chain = DAG.getNode(X86ISD::MINGW_ALLOCA, dl, NodeTys, Chain, Flag);
6643 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006644
Dale Johannesendd64c412009-02-04 00:33:20 +00006645 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006646
Dan Gohman475871a2008-07-27 21:46:04 +00006647 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006648 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006649}
6650
Dan Gohmand858e902010-04-17 15:26:15 +00006651SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00006652 MachineFunction &MF = DAG.getMachineFunction();
6653 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
6654
Dan Gohman69de1932008-02-06 22:27:42 +00006655 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006656 DebugLoc dl = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00006657
Evan Cheng25ab6902006-09-08 06:48:29 +00006658 if (!Subtarget->is64Bit()) {
6659 // vastart just stores the address of the VarArgsFrameIndex slot into the
6660 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00006661 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
6662 getPointerTy());
David Greene67c9d422010-02-15 16:53:33 +00006663 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0,
6664 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006665 }
6666
6667 // __va_list_tag:
6668 // gp_offset (0 - 6 * 8)
6669 // fp_offset (48 - 48 + 8 * 16)
6670 // overflow_arg_area (point to parameters coming in memory).
6671 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00006672 SmallVector<SDValue, 8> MemOps;
6673 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00006674 // Store gp_offset
Dale Johannesene4d209d2009-02-03 20:21:25 +00006675 SDValue Store = DAG.getStore(Op.getOperand(0), dl,
Dan Gohman1e93df62010-04-17 14:41:14 +00006676 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
6677 MVT::i32),
David Greene67c9d422010-02-15 16:53:33 +00006678 FIN, SV, 0, false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006679 MemOps.push_back(Store);
6680
6681 // Store fp_offset
Scott Michelfdc40a02009-02-17 22:15:04 +00006682 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006683 FIN, DAG.getIntPtrConstant(4));
6684 Store = DAG.getStore(Op.getOperand(0), dl,
Dan Gohman1e93df62010-04-17 14:41:14 +00006685 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
6686 MVT::i32),
David Greene67c9d422010-02-15 16:53:33 +00006687 FIN, SV, 0, false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006688 MemOps.push_back(Store);
6689
6690 // Store ptr to overflow_arg_area
Scott Michelfdc40a02009-02-17 22:15:04 +00006691 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006692 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00006693 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
6694 getPointerTy());
David Greene67c9d422010-02-15 16:53:33 +00006695 Store = DAG.getStore(Op.getOperand(0), dl, OVFIN, FIN, SV, 0,
6696 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006697 MemOps.push_back(Store);
6698
6699 // Store ptr to reg_save_area.
Scott Michelfdc40a02009-02-17 22:15:04 +00006700 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006701 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00006702 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
6703 getPointerTy());
David Greene67c9d422010-02-15 16:53:33 +00006704 Store = DAG.getStore(Op.getOperand(0), dl, RSFIN, FIN, SV, 0,
6705 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006706 MemOps.push_back(Store);
Owen Anderson825b72b2009-08-11 20:47:22 +00006707 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006708 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00006709}
6710
Dan Gohmand858e902010-04-17 15:26:15 +00006711SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman9018e832008-05-10 01:26:14 +00006712 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
6713 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman475871a2008-07-27 21:46:04 +00006714 SDValue Chain = Op.getOperand(0);
6715 SDValue SrcPtr = Op.getOperand(1);
6716 SDValue SrcSV = Op.getOperand(2);
Dan Gohman9018e832008-05-10 01:26:14 +00006717
Chris Lattner75361b62010-04-07 22:58:41 +00006718 report_fatal_error("VAArgInst is not yet implemented for x86-64!");
Dan Gohman475871a2008-07-27 21:46:04 +00006719 return SDValue();
Dan Gohman9018e832008-05-10 01:26:14 +00006720}
6721
Dan Gohmand858e902010-04-17 15:26:15 +00006722SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00006723 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00006724 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00006725 SDValue Chain = Op.getOperand(0);
6726 SDValue DstPtr = Op.getOperand(1);
6727 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00006728 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
6729 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006730 DebugLoc dl = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00006731
Dale Johannesendd64c412009-02-04 00:33:20 +00006732 return DAG.getMemcpy(Chain, dl, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00006733 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
6734 false, DstSV, 0, SrcSV, 0);
Evan Chengae642192007-03-02 23:16:35 +00006735}
6736
Dan Gohman475871a2008-07-27 21:46:04 +00006737SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006738X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006739 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006740 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006741 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00006742 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00006743 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00006744 case Intrinsic::x86_sse_comieq_ss:
6745 case Intrinsic::x86_sse_comilt_ss:
6746 case Intrinsic::x86_sse_comile_ss:
6747 case Intrinsic::x86_sse_comigt_ss:
6748 case Intrinsic::x86_sse_comige_ss:
6749 case Intrinsic::x86_sse_comineq_ss:
6750 case Intrinsic::x86_sse_ucomieq_ss:
6751 case Intrinsic::x86_sse_ucomilt_ss:
6752 case Intrinsic::x86_sse_ucomile_ss:
6753 case Intrinsic::x86_sse_ucomigt_ss:
6754 case Intrinsic::x86_sse_ucomige_ss:
6755 case Intrinsic::x86_sse_ucomineq_ss:
6756 case Intrinsic::x86_sse2_comieq_sd:
6757 case Intrinsic::x86_sse2_comilt_sd:
6758 case Intrinsic::x86_sse2_comile_sd:
6759 case Intrinsic::x86_sse2_comigt_sd:
6760 case Intrinsic::x86_sse2_comige_sd:
6761 case Intrinsic::x86_sse2_comineq_sd:
6762 case Intrinsic::x86_sse2_ucomieq_sd:
6763 case Intrinsic::x86_sse2_ucomilt_sd:
6764 case Intrinsic::x86_sse2_ucomile_sd:
6765 case Intrinsic::x86_sse2_ucomigt_sd:
6766 case Intrinsic::x86_sse2_ucomige_sd:
6767 case Intrinsic::x86_sse2_ucomineq_sd: {
6768 unsigned Opc = 0;
6769 ISD::CondCode CC = ISD::SETCC_INVALID;
6770 switch (IntNo) {
6771 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006772 case Intrinsic::x86_sse_comieq_ss:
6773 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006774 Opc = X86ISD::COMI;
6775 CC = ISD::SETEQ;
6776 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006777 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006778 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006779 Opc = X86ISD::COMI;
6780 CC = ISD::SETLT;
6781 break;
6782 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006783 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006784 Opc = X86ISD::COMI;
6785 CC = ISD::SETLE;
6786 break;
6787 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006788 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006789 Opc = X86ISD::COMI;
6790 CC = ISD::SETGT;
6791 break;
6792 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006793 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006794 Opc = X86ISD::COMI;
6795 CC = ISD::SETGE;
6796 break;
6797 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006798 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006799 Opc = X86ISD::COMI;
6800 CC = ISD::SETNE;
6801 break;
6802 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006803 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006804 Opc = X86ISD::UCOMI;
6805 CC = ISD::SETEQ;
6806 break;
6807 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006808 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006809 Opc = X86ISD::UCOMI;
6810 CC = ISD::SETLT;
6811 break;
6812 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006813 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006814 Opc = X86ISD::UCOMI;
6815 CC = ISD::SETLE;
6816 break;
6817 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006818 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006819 Opc = X86ISD::UCOMI;
6820 CC = ISD::SETGT;
6821 break;
6822 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006823 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006824 Opc = X86ISD::UCOMI;
6825 CC = ISD::SETGE;
6826 break;
6827 case Intrinsic::x86_sse_ucomineq_ss:
6828 case Intrinsic::x86_sse2_ucomineq_sd:
6829 Opc = X86ISD::UCOMI;
6830 CC = ISD::SETNE;
6831 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006832 }
Evan Cheng734503b2006-09-11 02:19:56 +00006833
Dan Gohman475871a2008-07-27 21:46:04 +00006834 SDValue LHS = Op.getOperand(1);
6835 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00006836 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00006837 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00006838 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
6839 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6840 DAG.getConstant(X86CC, MVT::i8), Cond);
6841 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00006842 }
Eric Christopher71c67532009-07-29 00:28:05 +00006843 // ptest intrinsics. The intrinsic these come from are designed to return
Eric Christopher794bfed2009-07-29 01:01:19 +00006844 // an integer value, not just an instruction so lower it to the ptest
6845 // pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00006846 case Intrinsic::x86_sse41_ptestz:
6847 case Intrinsic::x86_sse41_ptestc:
6848 case Intrinsic::x86_sse41_ptestnzc:{
6849 unsigned X86CC = 0;
6850 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00006851 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Eric Christopher71c67532009-07-29 00:28:05 +00006852 case Intrinsic::x86_sse41_ptestz:
6853 // ZF = 1
6854 X86CC = X86::COND_E;
6855 break;
6856 case Intrinsic::x86_sse41_ptestc:
6857 // CF = 1
6858 X86CC = X86::COND_B;
6859 break;
Eric Christopherfd179292009-08-27 18:07:15 +00006860 case Intrinsic::x86_sse41_ptestnzc:
Eric Christopher71c67532009-07-29 00:28:05 +00006861 // ZF and CF = 0
6862 X86CC = X86::COND_A;
6863 break;
6864 }
Eric Christopherfd179292009-08-27 18:07:15 +00006865
Eric Christopher71c67532009-07-29 00:28:05 +00006866 SDValue LHS = Op.getOperand(1);
6867 SDValue RHS = Op.getOperand(2);
Owen Anderson825b72b2009-08-11 20:47:22 +00006868 SDValue Test = DAG.getNode(X86ISD::PTEST, dl, MVT::i32, LHS, RHS);
6869 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
6870 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
6871 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00006872 }
Evan Cheng5759f972008-05-04 09:15:50 +00006873
6874 // Fix vector shift instructions where the last operand is a non-immediate
6875 // i32 value.
6876 case Intrinsic::x86_sse2_pslli_w:
6877 case Intrinsic::x86_sse2_pslli_d:
6878 case Intrinsic::x86_sse2_pslli_q:
6879 case Intrinsic::x86_sse2_psrli_w:
6880 case Intrinsic::x86_sse2_psrli_d:
6881 case Intrinsic::x86_sse2_psrli_q:
6882 case Intrinsic::x86_sse2_psrai_w:
6883 case Intrinsic::x86_sse2_psrai_d:
6884 case Intrinsic::x86_mmx_pslli_w:
6885 case Intrinsic::x86_mmx_pslli_d:
6886 case Intrinsic::x86_mmx_pslli_q:
6887 case Intrinsic::x86_mmx_psrli_w:
6888 case Intrinsic::x86_mmx_psrli_d:
6889 case Intrinsic::x86_mmx_psrli_q:
6890 case Intrinsic::x86_mmx_psrai_w:
6891 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00006892 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00006893 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00006894 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00006895
6896 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00006897 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00006898 switch (IntNo) {
6899 case Intrinsic::x86_sse2_pslli_w:
6900 NewIntNo = Intrinsic::x86_sse2_psll_w;
6901 break;
6902 case Intrinsic::x86_sse2_pslli_d:
6903 NewIntNo = Intrinsic::x86_sse2_psll_d;
6904 break;
6905 case Intrinsic::x86_sse2_pslli_q:
6906 NewIntNo = Intrinsic::x86_sse2_psll_q;
6907 break;
6908 case Intrinsic::x86_sse2_psrli_w:
6909 NewIntNo = Intrinsic::x86_sse2_psrl_w;
6910 break;
6911 case Intrinsic::x86_sse2_psrli_d:
6912 NewIntNo = Intrinsic::x86_sse2_psrl_d;
6913 break;
6914 case Intrinsic::x86_sse2_psrli_q:
6915 NewIntNo = Intrinsic::x86_sse2_psrl_q;
6916 break;
6917 case Intrinsic::x86_sse2_psrai_w:
6918 NewIntNo = Intrinsic::x86_sse2_psra_w;
6919 break;
6920 case Intrinsic::x86_sse2_psrai_d:
6921 NewIntNo = Intrinsic::x86_sse2_psra_d;
6922 break;
6923 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00006924 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00006925 switch (IntNo) {
6926 case Intrinsic::x86_mmx_pslli_w:
6927 NewIntNo = Intrinsic::x86_mmx_psll_w;
6928 break;
6929 case Intrinsic::x86_mmx_pslli_d:
6930 NewIntNo = Intrinsic::x86_mmx_psll_d;
6931 break;
6932 case Intrinsic::x86_mmx_pslli_q:
6933 NewIntNo = Intrinsic::x86_mmx_psll_q;
6934 break;
6935 case Intrinsic::x86_mmx_psrli_w:
6936 NewIntNo = Intrinsic::x86_mmx_psrl_w;
6937 break;
6938 case Intrinsic::x86_mmx_psrli_d:
6939 NewIntNo = Intrinsic::x86_mmx_psrl_d;
6940 break;
6941 case Intrinsic::x86_mmx_psrli_q:
6942 NewIntNo = Intrinsic::x86_mmx_psrl_q;
6943 break;
6944 case Intrinsic::x86_mmx_psrai_w:
6945 NewIntNo = Intrinsic::x86_mmx_psra_w;
6946 break;
6947 case Intrinsic::x86_mmx_psrai_d:
6948 NewIntNo = Intrinsic::x86_mmx_psra_d;
6949 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006950 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00006951 }
6952 break;
6953 }
6954 }
Mon P Wangefa42202009-09-03 19:56:25 +00006955
6956 // The vector shift intrinsics with scalars uses 32b shift amounts but
6957 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
6958 // to be zero.
6959 SDValue ShOps[4];
6960 ShOps[0] = ShAmt;
6961 ShOps[1] = DAG.getConstant(0, MVT::i32);
6962 if (ShAmtVT == MVT::v4i32) {
6963 ShOps[2] = DAG.getUNDEF(MVT::i32);
6964 ShOps[3] = DAG.getUNDEF(MVT::i32);
6965 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
6966 } else {
6967 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
6968 }
6969
Owen Andersone50ed302009-08-10 22:56:29 +00006970 EVT VT = Op.getValueType();
Mon P Wangefa42202009-09-03 19:56:25 +00006971 ShAmt = DAG.getNode(ISD::BIT_CONVERT, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006972 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00006973 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00006974 Op.getOperand(1), ShAmt);
6975 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00006976 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006977}
Evan Cheng72261582005-12-20 06:22:03 +00006978
Dan Gohmand858e902010-04-17 15:26:15 +00006979SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
6980 SelectionDAG &DAG) const {
Bill Wendling64e87322009-01-16 19:25:27 +00006981 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006982 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00006983
6984 if (Depth > 0) {
6985 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
6986 SDValue Offset =
6987 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00006988 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006989 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00006990 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006991 FrameAddr, Offset),
David Greene67c9d422010-02-15 16:53:33 +00006992 NULL, 0, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00006993 }
6994
6995 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00006996 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00006997 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
David Greene67c9d422010-02-15 16:53:33 +00006998 RetAddrFI, NULL, 0, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00006999}
7000
Dan Gohmand858e902010-04-17 15:26:15 +00007001SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00007002 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7003 MFI->setFrameAddressIsTaken(true);
Owen Andersone50ed302009-08-10 22:56:29 +00007004 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007005 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00007006 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
7007 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00007008 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00007009 while (Depth--)
David Greene67c9d422010-02-15 16:53:33 +00007010 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0,
7011 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00007012 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00007013}
7014
Dan Gohman475871a2008-07-27 21:46:04 +00007015SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007016 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00007017 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007018}
7019
Dan Gohmand858e902010-04-17 15:26:15 +00007020SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007021 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00007022 SDValue Chain = Op.getOperand(0);
7023 SDValue Offset = Op.getOperand(1);
7024 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007025 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007026
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007027 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
7028 getPointerTy());
7029 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007030
Dale Johannesene4d209d2009-02-03 20:21:25 +00007031 SDValue StoreAddr = DAG.getNode(ISD::SUB, dl, getPointerTy(), Frame,
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00007032 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007033 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
David Greene67c9d422010-02-15 16:53:33 +00007034 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, NULL, 0, false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00007035 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007036 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007037
Dale Johannesene4d209d2009-02-03 20:21:25 +00007038 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007039 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007040 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007041}
7042
Dan Gohman475871a2008-07-27 21:46:04 +00007043SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007044 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007045 SDValue Root = Op.getOperand(0);
7046 SDValue Trmp = Op.getOperand(1); // trampoline
7047 SDValue FPtr = Op.getOperand(2); // nested function
7048 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007049 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00007050
Dan Gohman69de1932008-02-06 22:27:42 +00007051 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00007052
7053 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00007054 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00007055
7056 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00007057 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
7058 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00007059
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00007060 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
7061 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00007062
7063 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
7064
7065 // Load the pointer to the nested function into R11.
7066 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00007067 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00007068 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
David Greene67c9d422010-02-15 16:53:33 +00007069 Addr, TrmpAddr, 0, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007070
Owen Anderson825b72b2009-08-11 20:47:22 +00007071 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7072 DAG.getConstant(2, MVT::i64));
David Greene67c9d422010-02-15 16:53:33 +00007073 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr, TrmpAddr, 2,
7074 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00007075
7076 // Load the 'nest' parameter value into R10.
7077 // R10 is specified in X86CallingConv.td
7078 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00007079 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7080 DAG.getConstant(10, MVT::i64));
7081 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
David Greene67c9d422010-02-15 16:53:33 +00007082 Addr, TrmpAddr, 10, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007083
Owen Anderson825b72b2009-08-11 20:47:22 +00007084 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7085 DAG.getConstant(12, MVT::i64));
David Greene67c9d422010-02-15 16:53:33 +00007086 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 12,
7087 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00007088
7089 // Jump to the nested function.
7090 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00007091 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7092 DAG.getConstant(20, MVT::i64));
7093 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
David Greene67c9d422010-02-15 16:53:33 +00007094 Addr, TrmpAddr, 20, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007095
7096 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00007097 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7098 DAG.getConstant(22, MVT::i64));
7099 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
David Greene67c9d422010-02-15 16:53:33 +00007100 TrmpAddr, 22, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007101
Dan Gohman475871a2008-07-27 21:46:04 +00007102 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00007103 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007104 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007105 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00007106 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00007107 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00007108 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00007109 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007110
7111 switch (CC) {
7112 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00007113 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00007114 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00007115 case CallingConv::X86_StdCall: {
7116 // Pass 'nest' parameter in ECX.
7117 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00007118 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007119
7120 // Check that ECX wasn't needed by an 'inreg' parameter.
7121 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00007122 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00007123
Chris Lattner58d74912008-03-12 17:45:29 +00007124 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00007125 unsigned InRegCount = 0;
7126 unsigned Idx = 1;
7127
7128 for (FunctionType::param_iterator I = FTy->param_begin(),
7129 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00007130 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00007131 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00007132 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007133
7134 if (InRegCount > 2) {
Chris Lattner75361b62010-04-07 22:58:41 +00007135 report_fatal_error("Nest register in use - reduce number of inreg parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00007136 }
7137 }
7138 break;
7139 }
7140 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00007141 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00007142 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00007143 // Pass 'nest' parameter in EAX.
7144 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00007145 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007146 break;
7147 }
7148
Dan Gohman475871a2008-07-27 21:46:04 +00007149 SDValue OutChains[4];
7150 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007151
Owen Anderson825b72b2009-08-11 20:47:22 +00007152 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7153 DAG.getConstant(10, MVT::i32));
7154 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007155
Chris Lattnera62fe662010-02-05 19:20:30 +00007156 // This is storing the opcode for MOV32ri.
7157 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00007158 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00007159 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007160 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
David Greene67c9d422010-02-15 16:53:33 +00007161 Trmp, TrmpAddr, 0, false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007162
Owen Anderson825b72b2009-08-11 20:47:22 +00007163 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7164 DAG.getConstant(1, MVT::i32));
David Greene67c9d422010-02-15 16:53:33 +00007165 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 1,
7166 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007167
Chris Lattnera62fe662010-02-05 19:20:30 +00007168 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00007169 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7170 DAG.getConstant(5, MVT::i32));
7171 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
David Greene67c9d422010-02-15 16:53:33 +00007172 TrmpAddr, 5, false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007173
Owen Anderson825b72b2009-08-11 20:47:22 +00007174 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7175 DAG.getConstant(6, MVT::i32));
David Greene67c9d422010-02-15 16:53:33 +00007176 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr, TrmpAddr, 6,
7177 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007178
Dan Gohman475871a2008-07-27 21:46:04 +00007179 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00007180 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007181 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007182 }
7183}
7184
Dan Gohmand858e902010-04-17 15:26:15 +00007185SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
7186 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007187 /*
7188 The rounding mode is in bits 11:10 of FPSR, and has the following
7189 settings:
7190 00 Round to nearest
7191 01 Round to -inf
7192 10 Round to +inf
7193 11 Round to 0
7194
7195 FLT_ROUNDS, on the other hand, expects the following:
7196 -1 Undefined
7197 0 Round to 0
7198 1 Round to nearest
7199 2 Round to +inf
7200 3 Round to -inf
7201
7202 To perform the conversion, we do:
7203 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
7204 */
7205
7206 MachineFunction &MF = DAG.getMachineFunction();
7207 const TargetMachine &TM = MF.getTarget();
7208 const TargetFrameInfo &TFI = *TM.getFrameInfo();
7209 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00007210 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007211 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007212
7213 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00007214 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007215 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007216
Owen Anderson825b72b2009-08-11 20:47:22 +00007217 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, dl, MVT::Other,
Evan Cheng8a186ae2008-09-24 23:26:36 +00007218 DAG.getEntryNode(), StackSlot);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007219
7220 // Load FP Control Word from stack slot
David Greene67c9d422010-02-15 16:53:33 +00007221 SDValue CWD = DAG.getLoad(MVT::i16, dl, Chain, StackSlot, NULL, 0,
7222 false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007223
7224 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00007225 SDValue CWD1 =
Owen Anderson825b72b2009-08-11 20:47:22 +00007226 DAG.getNode(ISD::SRL, dl, MVT::i16,
7227 DAG.getNode(ISD::AND, dl, MVT::i16,
7228 CWD, DAG.getConstant(0x800, MVT::i16)),
7229 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00007230 SDValue CWD2 =
Owen Anderson825b72b2009-08-11 20:47:22 +00007231 DAG.getNode(ISD::SRL, dl, MVT::i16,
7232 DAG.getNode(ISD::AND, dl, MVT::i16,
7233 CWD, DAG.getConstant(0x400, MVT::i16)),
7234 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007235
Dan Gohman475871a2008-07-27 21:46:04 +00007236 SDValue RetVal =
Owen Anderson825b72b2009-08-11 20:47:22 +00007237 DAG.getNode(ISD::AND, dl, MVT::i16,
7238 DAG.getNode(ISD::ADD, dl, MVT::i16,
7239 DAG.getNode(ISD::OR, dl, MVT::i16, CWD1, CWD2),
7240 DAG.getConstant(1, MVT::i16)),
7241 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007242
7243
Duncan Sands83ec4b62008-06-06 12:08:01 +00007244 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007245 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007246}
7247
Dan Gohmand858e902010-04-17 15:26:15 +00007248SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007249 EVT VT = Op.getValueType();
7250 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007251 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007252 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00007253
7254 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007255 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00007256 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00007257 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007258 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007259 }
Evan Cheng18efe262007-12-14 02:13:44 +00007260
Evan Cheng152804e2007-12-14 08:30:15 +00007261 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007262 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007263 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00007264
7265 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007266 SDValue Ops[] = {
7267 Op,
7268 DAG.getConstant(NumBits+NumBits-1, OpVT),
7269 DAG.getConstant(X86::COND_E, MVT::i8),
7270 Op.getValue(1)
7271 };
7272 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00007273
7274 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00007275 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00007276
Owen Anderson825b72b2009-08-11 20:47:22 +00007277 if (VT == MVT::i8)
7278 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007279 return Op;
7280}
7281
Dan Gohmand858e902010-04-17 15:26:15 +00007282SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007283 EVT VT = Op.getValueType();
7284 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007285 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007286 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00007287
7288 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007289 if (VT == MVT::i8) {
7290 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007291 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007292 }
Evan Cheng152804e2007-12-14 08:30:15 +00007293
7294 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007295 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007296 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00007297
7298 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007299 SDValue Ops[] = {
7300 Op,
7301 DAG.getConstant(NumBits, OpVT),
7302 DAG.getConstant(X86::COND_E, MVT::i8),
7303 Op.getValue(1)
7304 };
7305 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00007306
Owen Anderson825b72b2009-08-11 20:47:22 +00007307 if (VT == MVT::i8)
7308 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007309 return Op;
7310}
7311
Dan Gohmand858e902010-04-17 15:26:15 +00007312SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007313 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007314 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007315 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00007316
Mon P Wangaf9b9522008-12-18 21:42:19 +00007317 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
7318 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
7319 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
7320 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
7321 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
7322 //
7323 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
7324 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
7325 // return AloBlo + AloBhi + AhiBlo;
7326
7327 SDValue A = Op.getOperand(0);
7328 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007329
Dale Johannesene4d209d2009-02-03 20:21:25 +00007330 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007331 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7332 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007333 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007334 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7335 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007336 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007337 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007338 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007339 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007340 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007341 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007342 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007343 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007344 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007345 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007346 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7347 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007348 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007349 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7350 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007351 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
7352 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00007353 return Res;
7354}
7355
7356
Dan Gohmand858e902010-04-17 15:26:15 +00007357SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +00007358 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
7359 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00007360 // looks for this combo and may remove the "setcc" instruction if the "setcc"
7361 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00007362 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00007363 SDValue LHS = N->getOperand(0);
7364 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00007365 unsigned BaseOp = 0;
7366 unsigned Cond = 0;
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007367 DebugLoc dl = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00007368
7369 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007370 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00007371 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00007372 // A subtract of one will be selected as a INC. Note that INC doesn't
7373 // set CF, so we can't do this for UADDO.
7374 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
7375 if (C->getAPIntValue() == 1) {
7376 BaseOp = X86ISD::INC;
7377 Cond = X86::COND_O;
7378 break;
7379 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007380 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00007381 Cond = X86::COND_O;
7382 break;
7383 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007384 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00007385 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007386 break;
7387 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00007388 // A subtract of one will be selected as a DEC. Note that DEC doesn't
7389 // set CF, so we can't do this for USUBO.
7390 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
7391 if (C->getAPIntValue() == 1) {
7392 BaseOp = X86ISD::DEC;
7393 Cond = X86::COND_O;
7394 break;
7395 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007396 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00007397 Cond = X86::COND_O;
7398 break;
7399 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007400 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00007401 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007402 break;
7403 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00007404 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00007405 Cond = X86::COND_O;
7406 break;
7407 case ISD::UMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00007408 BaseOp = X86ISD::UMUL;
Dan Gohman653456c2009-01-07 00:15:08 +00007409 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007410 break;
7411 }
Bill Wendling3fafd932008-11-26 22:37:40 +00007412
Bill Wendling61edeb52008-12-02 01:06:39 +00007413 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007414 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007415 SDValue Sum = DAG.getNode(BaseOp, dl, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00007416
Bill Wendling61edeb52008-12-02 01:06:39 +00007417 SDValue SetCC =
Dale Johannesene4d209d2009-02-03 20:21:25 +00007418 DAG.getNode(X86ISD::SETCC, dl, N->getValueType(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00007419 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00007420
Bill Wendling61edeb52008-12-02 01:06:39 +00007421 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
7422 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00007423}
7424
Dan Gohmand858e902010-04-17 15:26:15 +00007425SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007426 EVT T = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007427 DebugLoc dl = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00007428 unsigned Reg = 0;
7429 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00007430 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007431 default:
7432 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007433 case MVT::i8: Reg = X86::AL; size = 1; break;
7434 case MVT::i16: Reg = X86::AX; size = 2; break;
7435 case MVT::i32: Reg = X86::EAX; size = 4; break;
7436 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00007437 assert(Subtarget->is64Bit() && "Node not type legal!");
7438 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00007439 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007440 }
Dale Johannesendd64c412009-02-04 00:33:20 +00007441 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), dl, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00007442 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00007443 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00007444 Op.getOperand(1),
7445 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00007446 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00007447 cpIn.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00007448 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007449 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, dl, Tys, Ops, 5);
Scott Michelfdc40a02009-02-17 22:15:04 +00007450 SDValue cpOut =
Dale Johannesendd64c412009-02-04 00:33:20 +00007451 DAG.getCopyFromReg(Result.getValue(0), dl, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00007452 return cpOut;
7453}
7454
Duncan Sands1607f052008-12-01 11:39:25 +00007455SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007456 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +00007457 assert(Subtarget->is64Bit() && "Result not type legalized?");
Owen Anderson825b72b2009-08-11 20:47:22 +00007458 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00007459 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007460 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007461 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007462 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
7463 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00007464 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00007465 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
7466 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00007467 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00007468 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00007469 rdx.getValue(1)
7470 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007471 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007472}
7473
Dale Johannesen7d07b482010-05-21 00:52:33 +00007474SDValue X86TargetLowering::LowerBIT_CONVERT(SDValue Op,
7475 SelectionDAG &DAG) const {
7476 EVT SrcVT = Op.getOperand(0).getValueType();
7477 EVT DstVT = Op.getValueType();
7478 assert((Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
7479 Subtarget->hasMMX() && !DisableMMX) &&
7480 "Unexpected custom BIT_CONVERT");
7481 assert((DstVT == MVT::i64 ||
7482 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
7483 "Unexpected custom BIT_CONVERT");
7484 // i64 <=> MMX conversions are Legal.
7485 if (SrcVT==MVT::i64 && DstVT.isVector())
7486 return Op;
7487 if (DstVT==MVT::i64 && SrcVT.isVector())
7488 return Op;
7489 // All other conversions need to be expanded.
7490 return SDValue();
7491}
Dan Gohmand858e902010-04-17 15:26:15 +00007492SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +00007493 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007494 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007495 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007496 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00007497 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007498 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007499 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00007500 Node->getOperand(0),
7501 Node->getOperand(1), negOp,
7502 cast<AtomicSDNode>(Node)->getSrcValue(),
7503 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00007504}
7505
Evan Cheng0db9fe62006-04-25 20:13:52 +00007506/// LowerOperation - Provide custom lowering hooks for some operations.
7507///
Dan Gohmand858e902010-04-17 15:26:15 +00007508SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007509 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007510 default: llvm_unreachable("Should not custom lower this!");
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007511 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
7512 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007513 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00007514 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007515 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
7516 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
7517 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
7518 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
7519 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
7520 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007521 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00007522 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007523 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007524 case ISD::SHL_PARTS:
7525 case ISD::SRA_PARTS:
7526 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
7527 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007528 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007529 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00007530 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007531 case ISD::FABS: return LowerFABS(Op, DAG);
7532 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007533 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00007534 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00007535 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00007536 case ISD::SELECT: return LowerSELECT(Op, DAG);
7537 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007538 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007539 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00007540 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00007541 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007542 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00007543 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
7544 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007545 case ISD::FRAME_TO_ARGS_OFFSET:
7546 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007547 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007548 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007549 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00007550 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00007551 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
7552 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00007553 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00007554 case ISD::SADDO:
7555 case ISD::UADDO:
7556 case ISD::SSUBO:
7557 case ISD::USUBO:
7558 case ISD::SMULO:
7559 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00007560 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Dale Johannesen7d07b482010-05-21 00:52:33 +00007561 case ISD::BIT_CONVERT: return LowerBIT_CONVERT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007562 }
Chris Lattner27a6c732007-11-24 07:07:01 +00007563}
7564
Duncan Sands1607f052008-12-01 11:39:25 +00007565void X86TargetLowering::
7566ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00007567 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007568 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007569 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00007570 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00007571
7572 SDValue Chain = Node->getOperand(0);
7573 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007574 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007575 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007576 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007577 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00007578 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00007579 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00007580 SDValue Result =
7581 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
7582 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00007583 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00007584 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007585 Results.push_back(Result.getValue(2));
7586}
7587
Duncan Sands126d9072008-07-04 11:47:58 +00007588/// ReplaceNodeResults - Replace a node with an illegal result type
7589/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00007590void X86TargetLowering::ReplaceNodeResults(SDNode *N,
7591 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00007592 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007593 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00007594 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00007595 default:
Duncan Sands1607f052008-12-01 11:39:25 +00007596 assert(false && "Do not know how to custom type legalize this operation!");
7597 return;
7598 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00007599 std::pair<SDValue,SDValue> Vals =
7600 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00007601 SDValue FIST = Vals.first, StackSlot = Vals.second;
7602 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00007603 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00007604 // Return a load from the stack slot.
David Greene67c9d422010-02-15 16:53:33 +00007605 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot, NULL, 0,
7606 false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00007607 }
7608 return;
7609 }
7610 case ISD::READCYCLECOUNTER: {
Owen Anderson825b72b2009-08-11 20:47:22 +00007611 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00007612 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007613 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007614 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00007615 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007616 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007617 eax.getValue(2));
7618 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
7619 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00007620 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007621 Results.push_back(edx.getValue(1));
7622 return;
7623 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007624 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00007625 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007626 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00007627 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00007628 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
7629 DAG.getConstant(0, MVT::i32));
7630 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
7631 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00007632 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
7633 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00007634 cpInL.getValue(1));
7635 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00007636 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
7637 DAG.getConstant(0, MVT::i32));
7638 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
7639 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00007640 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00007641 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00007642 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00007643 swapInL.getValue(1));
7644 SDValue Ops[] = { swapInH.getValue(0),
7645 N->getOperand(1),
7646 swapInH.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00007647 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007648 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, dl, Tys, Ops, 3);
Dale Johannesendd64c412009-02-04 00:33:20 +00007649 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00007650 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00007651 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00007652 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00007653 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00007654 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007655 Results.push_back(cpOutH.getValue(1));
7656 return;
7657 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007658 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00007659 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
7660 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007661 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00007662 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
7663 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007664 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00007665 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
7666 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007667 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00007668 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
7669 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007670 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00007671 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
7672 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007673 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00007674 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
7675 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007676 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00007677 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
7678 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00007679 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007680}
7681
Evan Cheng72261582005-12-20 06:22:03 +00007682const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
7683 switch (Opcode) {
7684 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00007685 case X86ISD::BSF: return "X86ISD::BSF";
7686 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00007687 case X86ISD::SHLD: return "X86ISD::SHLD";
7688 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00007689 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00007690 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00007691 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00007692 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00007693 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00007694 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00007695 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
7696 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
7697 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00007698 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00007699 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00007700 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00007701 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00007702 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00007703 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00007704 case X86ISD::COMI: return "X86ISD::COMI";
7705 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00007706 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00007707 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Evan Cheng72261582005-12-20 06:22:03 +00007708 case X86ISD::CMOV: return "X86ISD::CMOV";
7709 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00007710 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00007711 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
7712 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00007713 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00007714 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00007715 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00007716 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00007717 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00007718 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
7719 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00007720 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007721 case X86ISD::MMX_PINSRW: return "X86ISD::MMX_PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00007722 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Evan Cheng8ca29322006-11-10 21:43:37 +00007723 case X86ISD::FMAX: return "X86ISD::FMAX";
7724 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00007725 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
7726 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007727 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Rafael Espindola094fad32009-04-08 21:14:34 +00007728 case X86ISD::SegmentBaseAddress: return "X86ISD::SegmentBaseAddress";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007729 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00007730 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007731 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00007732 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
7733 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007734 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
7735 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
7736 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
7737 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
7738 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
7739 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00007740 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
7741 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00007742 case X86ISD::VSHL: return "X86ISD::VSHL";
7743 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00007744 case X86ISD::CMPPD: return "X86ISD::CMPPD";
7745 case X86ISD::CMPPS: return "X86ISD::CMPPS";
7746 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
7747 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
7748 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
7749 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
7750 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
7751 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
7752 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
7753 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007754 case X86ISD::ADD: return "X86ISD::ADD";
7755 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingd350e022008-12-12 21:15:41 +00007756 case X86ISD::SMUL: return "X86ISD::SMUL";
7757 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00007758 case X86ISD::INC: return "X86ISD::INC";
7759 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00007760 case X86ISD::OR: return "X86ISD::OR";
7761 case X86ISD::XOR: return "X86ISD::XOR";
7762 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00007763 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00007764 case X86ISD::PTEST: return "X86ISD::PTEST";
Dan Gohmand6708ea2009-08-15 01:38:56 +00007765 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00007766 case X86ISD::MINGW_ALLOCA: return "X86ISD::MINGW_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00007767 }
7768}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007769
Chris Lattnerc9addb72007-03-30 23:15:24 +00007770// isLegalAddressingMode - Return true if the addressing mode represented
7771// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00007772bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00007773 const Type *Ty) const {
7774 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007775 CodeModel::Model M = getTargetMachine().getCodeModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00007776
Chris Lattnerc9addb72007-03-30 23:15:24 +00007777 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007778 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00007779 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007780
Chris Lattnerc9addb72007-03-30 23:15:24 +00007781 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00007782 unsigned GVFlags =
7783 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007784
Chris Lattnerdfed4132009-07-10 07:38:24 +00007785 // If a reference to this global requires an extra load, we can't fold it.
7786 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00007787 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007788
Chris Lattnerdfed4132009-07-10 07:38:24 +00007789 // If BaseGV requires a register for the PIC base, we cannot also have a
7790 // BaseReg specified.
7791 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00007792 return false;
Evan Cheng52787842007-08-01 23:46:47 +00007793
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007794 // If lower 4G is not available, then we must use rip-relative addressing.
7795 if (Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
7796 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00007797 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007798
Chris Lattnerc9addb72007-03-30 23:15:24 +00007799 switch (AM.Scale) {
7800 case 0:
7801 case 1:
7802 case 2:
7803 case 4:
7804 case 8:
7805 // These scales always work.
7806 break;
7807 case 3:
7808 case 5:
7809 case 9:
7810 // These scales are formed with basereg+scalereg. Only accept if there is
7811 // no basereg yet.
7812 if (AM.HasBaseReg)
7813 return false;
7814 break;
7815 default: // Other stuff never works.
7816 return false;
7817 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007818
Chris Lattnerc9addb72007-03-30 23:15:24 +00007819 return true;
7820}
7821
7822
Evan Cheng2bd122c2007-10-26 01:56:11 +00007823bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00007824 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00007825 return false;
Evan Chenge127a732007-10-29 07:57:50 +00007826 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
7827 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00007828 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00007829 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00007830 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00007831}
7832
Owen Andersone50ed302009-08-10 22:56:29 +00007833bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007834 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007835 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007836 unsigned NumBits1 = VT1.getSizeInBits();
7837 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00007838 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007839 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00007840 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007841}
Evan Cheng2bd122c2007-10-26 01:56:11 +00007842
Dan Gohman97121ba2009-04-08 00:15:30 +00007843bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00007844 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00007845 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00007846}
7847
Owen Andersone50ed302009-08-10 22:56:29 +00007848bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00007849 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00007850 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00007851}
7852
Owen Andersone50ed302009-08-10 22:56:29 +00007853bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00007854 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00007855 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00007856}
7857
Evan Cheng60c07e12006-07-05 22:17:51 +00007858/// isShuffleMaskLegal - Targets can use this to indicate that they only
7859/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
7860/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
7861/// are assumed to be legal.
7862bool
Eric Christopherfd179292009-08-27 18:07:15 +00007863X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00007864 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +00007865 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +00007866 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +00007867 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +00007868
Nate Begemana09008b2009-10-19 02:17:23 +00007869 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00007870 return (VT.getVectorNumElements() == 2 ||
7871 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
7872 isMOVLMask(M, VT) ||
7873 isSHUFPMask(M, VT) ||
7874 isPSHUFDMask(M, VT) ||
7875 isPSHUFHWMask(M, VT) ||
7876 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00007877 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00007878 isUNPCKLMask(M, VT) ||
7879 isUNPCKHMask(M, VT) ||
7880 isUNPCKL_v_undef_Mask(M, VT) ||
7881 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00007882}
7883
Dan Gohman7d8143f2008-04-09 20:09:42 +00007884bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00007885X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00007886 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00007887 unsigned NumElts = VT.getVectorNumElements();
7888 // FIXME: This collection of masks seems suspect.
7889 if (NumElts == 2)
7890 return true;
7891 if (NumElts == 4 && VT.getSizeInBits() == 128) {
7892 return (isMOVLMask(Mask, VT) ||
7893 isCommutedMOVLMask(Mask, VT, true) ||
7894 isSHUFPMask(Mask, VT) ||
7895 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00007896 }
7897 return false;
7898}
7899
7900//===----------------------------------------------------------------------===//
7901// X86 Scheduler Hooks
7902//===----------------------------------------------------------------------===//
7903
Mon P Wang63307c32008-05-05 19:05:59 +00007904// private utility function
7905MachineBasicBlock *
7906X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
7907 MachineBasicBlock *MBB,
7908 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007909 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007910 unsigned LoadOpc,
7911 unsigned CXchgOpc,
7912 unsigned copyOpc,
7913 unsigned notOpc,
7914 unsigned EAXreg,
7915 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007916 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00007917 // For the atomic bitwise operator, we generate
7918 // thisMBB:
7919 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00007920 // ld t1 = [bitinstr.addr]
7921 // op t2 = t1, [bitinstr.val]
7922 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00007923 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
7924 // bz newMBB
7925 // fallthrough -->nextMBB
7926 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7927 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007928 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00007929 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007930
Mon P Wang63307c32008-05-05 19:05:59 +00007931 /// First build the CFG
7932 MachineFunction *F = MBB->getParent();
7933 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007934 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7935 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7936 F->insert(MBBIter, newMBB);
7937 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007938
Mon P Wang63307c32008-05-05 19:05:59 +00007939 // Move all successors to thisMBB to nextMBB
7940 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007941
Mon P Wang63307c32008-05-05 19:05:59 +00007942 // Update thisMBB to fall through to newMBB
7943 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007944
Mon P Wang63307c32008-05-05 19:05:59 +00007945 // newMBB jumps to itself and fall through to nextMBB
7946 newMBB->addSuccessor(nextMBB);
7947 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007948
Mon P Wang63307c32008-05-05 19:05:59 +00007949 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007950 assert(bInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00007951 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00007952 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00007953 MachineOperand& destOper = bInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007954 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00007955 int numArgs = bInstr->getNumOperands() - 1;
7956 for (int i=0; i < numArgs; ++i)
7957 argOpers[i] = &bInstr->getOperand(i+1);
7958
7959 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007960 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
7961 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00007962
Dale Johannesen140be2d2008-08-19 18:47:28 +00007963 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007964 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00007965 for (int i=0; i <= lastAddrIndx; ++i)
7966 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007967
Dale Johannesen140be2d2008-08-19 18:47:28 +00007968 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007969 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007970 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007971 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007972 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007973 tt = t1;
7974
Dale Johannesen140be2d2008-08-19 18:47:28 +00007975 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00007976 assert((argOpers[valArgIndx]->isReg() ||
7977 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00007978 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00007979 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007980 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00007981 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007982 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007983 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00007984 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007985
Dale Johannesene4d209d2009-02-03 20:21:25 +00007986 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00007987 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007988
Dale Johannesene4d209d2009-02-03 20:21:25 +00007989 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00007990 for (int i=0; i <= lastAddrIndx; ++i)
7991 (*MIB).addOperand(*argOpers[i]);
7992 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00007993 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00007994 (*MIB).setMemRefs(bInstr->memoperands_begin(),
7995 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00007996
Dale Johannesene4d209d2009-02-03 20:21:25 +00007997 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00007998 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00007999
Mon P Wang63307c32008-05-05 19:05:59 +00008000 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008001 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00008002
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008003 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00008004 return nextMBB;
8005}
8006
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00008007// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00008008MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008009X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
8010 MachineBasicBlock *MBB,
8011 unsigned regOpcL,
8012 unsigned regOpcH,
8013 unsigned immOpcL,
8014 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00008015 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008016 // For the atomic bitwise operator, we generate
8017 // thisMBB (instructions are in pairs, except cmpxchg8b)
8018 // ld t1,t2 = [bitinstr.addr]
8019 // newMBB:
8020 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
8021 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00008022 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008023 // mov ECX, EBX <- t5, t6
8024 // mov EAX, EDX <- t1, t2
8025 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
8026 // mov t3, t4 <- EAX, EDX
8027 // bz newMBB
8028 // result in out1, out2
8029 // fallthrough -->nextMBB
8030
8031 const TargetRegisterClass *RC = X86::GR32RegisterClass;
8032 const unsigned LoadOpc = X86::MOV32rm;
8033 const unsigned copyOpc = X86::MOV32rr;
8034 const unsigned NotOpc = X86::NOT32r;
8035 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8036 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
8037 MachineFunction::iterator MBBIter = MBB;
8038 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00008039
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008040 /// First build the CFG
8041 MachineFunction *F = MBB->getParent();
8042 MachineBasicBlock *thisMBB = MBB;
8043 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
8044 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
8045 F->insert(MBBIter, newMBB);
8046 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008047
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008048 // Move all successors to thisMBB to nextMBB
8049 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008050
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008051 // Update thisMBB to fall through to newMBB
8052 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008053
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008054 // newMBB jumps to itself and fall through to nextMBB
8055 newMBB->addSuccessor(nextMBB);
8056 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008057
Dale Johannesene4d209d2009-02-03 20:21:25 +00008058 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008059 // Insert instructions into newMBB based on incoming instruction
8060 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008061 assert(bInstr->getNumOperands() < X86AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00008062 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008063 MachineOperand& dest1Oper = bInstr->getOperand(0);
8064 MachineOperand& dest2Oper = bInstr->getOperand(1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008065 MachineOperand* argOpers[2 + X86AddrNumOperands];
Dan Gohman71ea4e52010-05-14 21:01:44 +00008066 for (int i=0; i < 2 + X86AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008067 argOpers[i] = &bInstr->getOperand(i+2);
8068
Dan Gohman71ea4e52010-05-14 21:01:44 +00008069 // We use some of the operands multiple times, so conservatively just
8070 // clear any kill flags that might be present.
8071 if (argOpers[i]->isReg() && argOpers[i]->isUse())
8072 argOpers[i]->setIsKill(false);
8073 }
8074
Evan Chengad5b52f2010-01-08 19:14:57 +00008075 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008076 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00008077
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008078 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008079 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008080 for (int i=0; i <= lastAddrIndx; ++i)
8081 (*MIB).addOperand(*argOpers[i]);
8082 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008083 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00008084 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00008085 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008086 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00008087 MachineOperand newOp3 = *(argOpers[3]);
8088 if (newOp3.isImm())
8089 newOp3.setImm(newOp3.getImm()+4);
8090 else
8091 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008092 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00008093 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008094
8095 // t3/4 are defined later, at the bottom of the loop
8096 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
8097 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008098 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008099 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008100 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008101 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
8102
Evan Cheng306b4ca2010-01-08 23:41:50 +00008103 // The subsequent operations should be using the destination registers of
8104 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00008105 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00008106 t1 = F->getRegInfo().createVirtualRegister(RC);
8107 t2 = F->getRegInfo().createVirtualRegister(RC);
8108 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
8109 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008110 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00008111 t1 = dest1Oper.getReg();
8112 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008113 }
8114
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008115 int valArgIndx = lastAddrIndx + 1;
8116 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00008117 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008118 "invalid operand");
8119 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
8120 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008121 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008122 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008123 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008124 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00008125 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00008126 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008127 (*MIB).addOperand(*argOpers[valArgIndx]);
8128 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00008129 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008130 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00008131 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008132 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008133 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008134 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008135 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00008136 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00008137 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008138 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008139
Dale Johannesene4d209d2009-02-03 20:21:25 +00008140 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008141 MIB.addReg(t1);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008142 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008143 MIB.addReg(t2);
8144
Dale Johannesene4d209d2009-02-03 20:21:25 +00008145 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008146 MIB.addReg(t5);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008147 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008148 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00008149
Dale Johannesene4d209d2009-02-03 20:21:25 +00008150 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008151 for (int i=0; i <= lastAddrIndx; ++i)
8152 (*MIB).addOperand(*argOpers[i]);
8153
8154 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00008155 (*MIB).setMemRefs(bInstr->memoperands_begin(),
8156 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008157
Dale Johannesene4d209d2009-02-03 20:21:25 +00008158 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008159 MIB.addReg(X86::EAX);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008160 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008161 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00008162
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008163 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008164 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008165
8166 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
8167 return nextMBB;
8168}
8169
8170// private utility function
8171MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00008172X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
8173 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00008174 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00008175 // For the atomic min/max operator, we generate
8176 // thisMBB:
8177 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00008178 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00008179 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00008180 // cmp t1, t2
8181 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00008182 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00008183 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
8184 // bz newMBB
8185 // fallthrough -->nextMBB
8186 //
8187 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8188 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008189 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00008190 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00008191
Mon P Wang63307c32008-05-05 19:05:59 +00008192 /// First build the CFG
8193 MachineFunction *F = MBB->getParent();
8194 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008195 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
8196 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
8197 F->insert(MBBIter, newMBB);
8198 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008199
Dan Gohmand6708ea2009-08-15 01:38:56 +00008200 // Move all successors of thisMBB to nextMBB
Mon P Wang63307c32008-05-05 19:05:59 +00008201 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008202
Mon P Wang63307c32008-05-05 19:05:59 +00008203 // Update thisMBB to fall through to newMBB
8204 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008205
Mon P Wang63307c32008-05-05 19:05:59 +00008206 // newMBB jumps to newMBB and fall through to nextMBB
8207 newMBB->addSuccessor(nextMBB);
8208 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008209
Dale Johannesene4d209d2009-02-03 20:21:25 +00008210 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00008211 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008212 assert(mInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00008213 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00008214 MachineOperand& destOper = mInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008215 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00008216 int numArgs = mInstr->getNumOperands() - 1;
8217 for (int i=0; i < numArgs; ++i)
8218 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008219
Mon P Wang63307c32008-05-05 19:05:59 +00008220 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008221 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
8222 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00008223
Mon P Wangab3e7472008-05-05 22:56:23 +00008224 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008225 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00008226 for (int i=0; i <= lastAddrIndx; ++i)
8227 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00008228
Mon P Wang63307c32008-05-05 19:05:59 +00008229 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00008230 assert((argOpers[valArgIndx]->isReg() ||
8231 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00008232 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00008233
8234 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00008235 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008236 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00008237 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008238 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00008239 (*MIB).addOperand(*argOpers[valArgIndx]);
8240
Dale Johannesene4d209d2009-02-03 20:21:25 +00008241 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00008242 MIB.addReg(t1);
8243
Dale Johannesene4d209d2009-02-03 20:21:25 +00008244 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00008245 MIB.addReg(t1);
8246 MIB.addReg(t2);
8247
8248 // Generate movc
8249 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008250 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00008251 MIB.addReg(t2);
8252 MIB.addReg(t1);
8253
8254 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00008255 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00008256 for (int i=0; i <= lastAddrIndx; ++i)
8257 (*MIB).addOperand(*argOpers[i]);
8258 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00008259 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00008260 (*MIB).setMemRefs(mInstr->memoperands_begin(),
8261 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +00008262
Dale Johannesene4d209d2009-02-03 20:21:25 +00008263 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00008264 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00008265
Mon P Wang63307c32008-05-05 19:05:59 +00008266 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008267 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00008268
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008269 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00008270 return nextMBB;
8271}
8272
Eric Christopherf83a5de2009-08-27 18:08:16 +00008273// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
8274// all of this code can be replaced with that in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +00008275MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +00008276X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +00008277 unsigned numArgs, bool memArg) const {
Eric Christopherb120ab42009-08-18 22:50:32 +00008278
8279 MachineFunction *F = BB->getParent();
8280 DebugLoc dl = MI->getDebugLoc();
8281 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8282
8283 unsigned Opc;
Evan Chengce319102009-09-19 09:51:03 +00008284 if (memArg)
8285 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
8286 else
8287 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
Eric Christopherb120ab42009-08-18 22:50:32 +00008288
8289 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(Opc));
8290
8291 for (unsigned i = 0; i < numArgs; ++i) {
8292 MachineOperand &Op = MI->getOperand(i+1);
8293
8294 if (!(Op.isReg() && Op.isImplicit()))
8295 MIB.addOperand(Op);
8296 }
8297
8298 BuildMI(BB, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
8299 .addReg(X86::XMM0);
8300
8301 F->DeleteMachineInstr(MI);
8302
8303 return BB;
8304}
8305
8306MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +00008307X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
8308 MachineInstr *MI,
8309 MachineBasicBlock *MBB) const {
8310 // Emit code to save XMM registers to the stack. The ABI says that the
8311 // number of registers to save is given in %al, so it's theoretically
8312 // possible to do an indirect jump trick to avoid saving all of them,
8313 // however this code takes a simpler approach and just executes all
8314 // of the stores if %al is non-zero. It's less code, and it's probably
8315 // easier on the hardware branch predictor, and stores aren't all that
8316 // expensive anyway.
8317
8318 // Create the new basic blocks. One block contains all the XMM stores,
8319 // and one block is the final destination regardless of whether any
8320 // stores were performed.
8321 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
8322 MachineFunction *F = MBB->getParent();
8323 MachineFunction::iterator MBBIter = MBB;
8324 ++MBBIter;
8325 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
8326 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
8327 F->insert(MBBIter, XMMSaveMBB);
8328 F->insert(MBBIter, EndMBB);
8329
8330 // Set up the CFG.
8331 // Move any original successors of MBB to the end block.
8332 EndMBB->transferSuccessors(MBB);
8333 // The original block will now fall through to the XMM save block.
8334 MBB->addSuccessor(XMMSaveMBB);
8335 // The XMMSaveMBB will fall through to the end block.
8336 XMMSaveMBB->addSuccessor(EndMBB);
8337
8338 // Now add the instructions.
8339 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8340 DebugLoc DL = MI->getDebugLoc();
8341
8342 unsigned CountReg = MI->getOperand(0).getReg();
8343 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
8344 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
8345
8346 if (!Subtarget->isTargetWin64()) {
8347 // If %al is 0, branch around the XMM save block.
8348 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008349 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008350 MBB->addSuccessor(EndMBB);
8351 }
8352
8353 // In the XMM save block, save all the XMM argument registers.
8354 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
8355 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +00008356 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +00008357 F->getMachineMemOperand(
8358 PseudoSourceValue::getFixedStack(RegSaveFrameIndex),
8359 MachineMemOperand::MOStore, Offset,
8360 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008361 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
8362 .addFrameIndex(RegSaveFrameIndex)
8363 .addImm(/*Scale=*/1)
8364 .addReg(/*IndexReg=*/0)
8365 .addImm(/*Disp=*/Offset)
8366 .addReg(/*Segment=*/0)
8367 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +00008368 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008369 }
8370
8371 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8372
8373 return EndMBB;
8374}
Mon P Wang63307c32008-05-05 19:05:59 +00008375
Evan Cheng60c07e12006-07-05 22:17:51 +00008376MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +00008377X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00008378 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +00008379 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8380 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +00008381
Chris Lattner52600972009-09-02 05:57:00 +00008382 // To "insert" a SELECT_CC instruction, we actually have to insert the
8383 // diamond control-flow pattern. The incoming instruction knows the
8384 // destination vreg to set, the condition code register to branch on, the
8385 // true/false values to select between, and a branch opcode to use.
8386 const BasicBlock *LLVM_BB = BB->getBasicBlock();
8387 MachineFunction::iterator It = BB;
8388 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +00008389
Chris Lattner52600972009-09-02 05:57:00 +00008390 // thisMBB:
8391 // ...
8392 // TrueVal = ...
8393 // cmpTY ccX, r1, r2
8394 // bCC copy1MBB
8395 // fallthrough --> copy0MBB
8396 MachineBasicBlock *thisMBB = BB;
8397 MachineFunction *F = BB->getParent();
8398 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
8399 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
8400 unsigned Opc =
8401 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
8402 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
8403 F->insert(It, copy0MBB);
8404 F->insert(It, sinkMBB);
Evan Chengce319102009-09-19 09:51:03 +00008405 // Update machine-CFG edges by first adding all successors of the current
Chris Lattner52600972009-09-02 05:57:00 +00008406 // block to the new block which will contain the Phi node for the select.
Daniel Dunbara279bc32009-09-20 02:20:51 +00008407 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00008408 E = BB->succ_end(); I != E; ++I)
Evan Chengce319102009-09-19 09:51:03 +00008409 sinkMBB->addSuccessor(*I);
Evan Chengce319102009-09-19 09:51:03 +00008410 // Next, remove all successors of the current block, and add the true
8411 // and fallthrough blocks as its successors.
8412 while (!BB->succ_empty())
8413 BB->removeSuccessor(BB->succ_begin());
Chris Lattner52600972009-09-02 05:57:00 +00008414 // Add the true and fallthrough blocks as its successors.
8415 BB->addSuccessor(copy0MBB);
8416 BB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00008417
Chris Lattner52600972009-09-02 05:57:00 +00008418 // copy0MBB:
8419 // %FalseValue = ...
8420 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +00008421 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00008422
Chris Lattner52600972009-09-02 05:57:00 +00008423 // sinkMBB:
8424 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
8425 // ...
Dan Gohman3335a222010-04-30 20:14:26 +00008426 BuildMI(sinkMBB, DL, TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +00008427 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
8428 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
8429
8430 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +00008431 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +00008432}
8433
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008434MachineBasicBlock *
8435X86TargetLowering::EmitLoweredMingwAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00008436 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008437 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8438 DebugLoc DL = MI->getDebugLoc();
8439 MachineFunction *F = BB->getParent();
8440
8441 // The lowering is pretty easy: we're just emitting the call to _alloca. The
8442 // non-trivial part is impdef of ESP.
8443 // FIXME: The code should be tweaked as soon as we'll try to do codegen for
8444 // mingw-w64.
8445
8446 BuildMI(BB, DL, TII->get(X86::CALLpcrel32))
8447 .addExternalSymbol("_alloca")
8448 .addReg(X86::EAX, RegState::Implicit)
8449 .addReg(X86::ESP, RegState::Implicit)
8450 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
8451 .addReg(X86::ESP, RegState::Define | RegState::Implicit);
8452
8453 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8454 return BB;
8455}
Chris Lattner52600972009-09-02 05:57:00 +00008456
8457MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00008458X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00008459 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00008460 switch (MI->getOpcode()) {
8461 default: assert(false && "Unexpected instr type to insert");
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008462 case X86::MINGW_ALLOCA:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00008463 return EmitLoweredMingwAlloca(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +00008464 case X86::CMOV_GR8:
Mon P Wang9e5ecb82008-12-12 01:25:51 +00008465 case X86::CMOV_V1I64:
Evan Cheng60c07e12006-07-05 22:17:51 +00008466 case X86::CMOV_FR32:
8467 case X86::CMOV_FR64:
8468 case X86::CMOV_V4F32:
8469 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +00008470 case X86::CMOV_V2I64:
Chris Lattner314a1132010-03-14 18:31:44 +00008471 case X86::CMOV_GR16:
8472 case X86::CMOV_GR32:
8473 case X86::CMOV_RFP32:
8474 case X86::CMOV_RFP64:
8475 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00008476 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +00008477
Dale Johannesen849f2142007-07-03 00:53:03 +00008478 case X86::FP32_TO_INT16_IN_MEM:
8479 case X86::FP32_TO_INT32_IN_MEM:
8480 case X86::FP32_TO_INT64_IN_MEM:
8481 case X86::FP64_TO_INT16_IN_MEM:
8482 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00008483 case X86::FP64_TO_INT64_IN_MEM:
8484 case X86::FP80_TO_INT16_IN_MEM:
8485 case X86::FP80_TO_INT32_IN_MEM:
8486 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +00008487 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8488 DebugLoc DL = MI->getDebugLoc();
8489
Evan Cheng60c07e12006-07-05 22:17:51 +00008490 // Change the floating point control register to use "round towards zero"
8491 // mode when truncating to an integer value.
8492 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +00008493 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Chris Lattner52600972009-09-02 05:57:00 +00008494 addFrameReference(BuildMI(BB, DL, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008495
8496 // Load the old value of the high byte of the control word...
8497 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +00008498 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Chris Lattner52600972009-09-02 05:57:00 +00008499 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008500 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008501
8502 // Set the high part to be round to zero...
Chris Lattner52600972009-09-02 05:57:00 +00008503 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00008504 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00008505
8506 // Reload the modified control word now...
Chris Lattner52600972009-09-02 05:57:00 +00008507 addFrameReference(BuildMI(BB, DL, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008508
8509 // Restore the memory image of control word to original value
Chris Lattner52600972009-09-02 05:57:00 +00008510 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00008511 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00008512
8513 // Get the X86 opcode to use.
8514 unsigned Opc;
8515 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008516 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00008517 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
8518 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
8519 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
8520 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
8521 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
8522 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00008523 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
8524 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
8525 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00008526 }
8527
8528 X86AddressMode AM;
8529 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +00008530 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00008531 AM.BaseType = X86AddressMode::RegBase;
8532 AM.Base.Reg = Op.getReg();
8533 } else {
8534 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +00008535 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +00008536 }
8537 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +00008538 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00008539 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008540 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +00008541 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00008542 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008543 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +00008544 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00008545 AM.GV = Op.getGlobal();
8546 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00008547 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008548 }
Chris Lattner52600972009-09-02 05:57:00 +00008549 addFullAddress(BuildMI(BB, DL, TII->get(Opc)), AM)
Rafael Espindola8ef2b892009-04-08 08:09:33 +00008550 .addReg(MI->getOperand(X86AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00008551
8552 // Reload the original control word now.
Chris Lattner52600972009-09-02 05:57:00 +00008553 addFrameReference(BuildMI(BB, DL, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008554
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008555 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00008556 return BB;
8557 }
Eric Christopherb120ab42009-08-18 22:50:32 +00008558 // String/text processing lowering.
8559 case X86::PCMPISTRM128REG:
8560 return EmitPCMP(MI, BB, 3, false /* in-mem */);
8561 case X86::PCMPISTRM128MEM:
8562 return EmitPCMP(MI, BB, 3, true /* in-mem */);
8563 case X86::PCMPESTRM128REG:
8564 return EmitPCMP(MI, BB, 5, false /* in mem */);
8565 case X86::PCMPESTRM128MEM:
8566 return EmitPCMP(MI, BB, 5, true /* in mem */);
8567
8568 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +00008569 case X86::ATOMAND32:
8570 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008571 X86::AND32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008572 X86::LCMPXCHG32, X86::MOV32rr,
8573 X86::NOT32r, X86::EAX,
8574 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00008575 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +00008576 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
8577 X86::OR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008578 X86::LCMPXCHG32, X86::MOV32rr,
8579 X86::NOT32r, X86::EAX,
8580 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00008581 case X86::ATOMXOR32:
8582 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008583 X86::XOR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008584 X86::LCMPXCHG32, X86::MOV32rr,
8585 X86::NOT32r, X86::EAX,
8586 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008587 case X86::ATOMNAND32:
8588 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008589 X86::AND32ri, X86::MOV32rm,
8590 X86::LCMPXCHG32, X86::MOV32rr,
8591 X86::NOT32r, X86::EAX,
8592 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +00008593 case X86::ATOMMIN32:
8594 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
8595 case X86::ATOMMAX32:
8596 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
8597 case X86::ATOMUMIN32:
8598 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
8599 case X86::ATOMUMAX32:
8600 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +00008601
8602 case X86::ATOMAND16:
8603 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
8604 X86::AND16ri, X86::MOV16rm,
8605 X86::LCMPXCHG16, X86::MOV16rr,
8606 X86::NOT16r, X86::AX,
8607 X86::GR16RegisterClass);
8608 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +00008609 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008610 X86::OR16ri, X86::MOV16rm,
8611 X86::LCMPXCHG16, X86::MOV16rr,
8612 X86::NOT16r, X86::AX,
8613 X86::GR16RegisterClass);
8614 case X86::ATOMXOR16:
8615 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
8616 X86::XOR16ri, X86::MOV16rm,
8617 X86::LCMPXCHG16, X86::MOV16rr,
8618 X86::NOT16r, X86::AX,
8619 X86::GR16RegisterClass);
8620 case X86::ATOMNAND16:
8621 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
8622 X86::AND16ri, X86::MOV16rm,
8623 X86::LCMPXCHG16, X86::MOV16rr,
8624 X86::NOT16r, X86::AX,
8625 X86::GR16RegisterClass, true);
8626 case X86::ATOMMIN16:
8627 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
8628 case X86::ATOMMAX16:
8629 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
8630 case X86::ATOMUMIN16:
8631 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
8632 case X86::ATOMUMAX16:
8633 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
8634
8635 case X86::ATOMAND8:
8636 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
8637 X86::AND8ri, X86::MOV8rm,
8638 X86::LCMPXCHG8, X86::MOV8rr,
8639 X86::NOT8r, X86::AL,
8640 X86::GR8RegisterClass);
8641 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +00008642 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008643 X86::OR8ri, X86::MOV8rm,
8644 X86::LCMPXCHG8, X86::MOV8rr,
8645 X86::NOT8r, X86::AL,
8646 X86::GR8RegisterClass);
8647 case X86::ATOMXOR8:
8648 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
8649 X86::XOR8ri, X86::MOV8rm,
8650 X86::LCMPXCHG8, X86::MOV8rr,
8651 X86::NOT8r, X86::AL,
8652 X86::GR8RegisterClass);
8653 case X86::ATOMNAND8:
8654 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
8655 X86::AND8ri, X86::MOV8rm,
8656 X86::LCMPXCHG8, X86::MOV8rr,
8657 X86::NOT8r, X86::AL,
8658 X86::GR8RegisterClass, true);
8659 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008660 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +00008661 case X86::ATOMAND64:
8662 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008663 X86::AND64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008664 X86::LCMPXCHG64, X86::MOV64rr,
8665 X86::NOT64r, X86::RAX,
8666 X86::GR64RegisterClass);
8667 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +00008668 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
8669 X86::OR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008670 X86::LCMPXCHG64, X86::MOV64rr,
8671 X86::NOT64r, X86::RAX,
8672 X86::GR64RegisterClass);
8673 case X86::ATOMXOR64:
8674 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008675 X86::XOR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008676 X86::LCMPXCHG64, X86::MOV64rr,
8677 X86::NOT64r, X86::RAX,
8678 X86::GR64RegisterClass);
8679 case X86::ATOMNAND64:
8680 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
8681 X86::AND64ri32, X86::MOV64rm,
8682 X86::LCMPXCHG64, X86::MOV64rr,
8683 X86::NOT64r, X86::RAX,
8684 X86::GR64RegisterClass, true);
8685 case X86::ATOMMIN64:
8686 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
8687 case X86::ATOMMAX64:
8688 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
8689 case X86::ATOMUMIN64:
8690 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
8691 case X86::ATOMUMAX64:
8692 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008693
8694 // This group does 64-bit operations on a 32-bit host.
8695 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008696 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008697 X86::AND32rr, X86::AND32rr,
8698 X86::AND32ri, X86::AND32ri,
8699 false);
8700 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008701 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008702 X86::OR32rr, X86::OR32rr,
8703 X86::OR32ri, X86::OR32ri,
8704 false);
8705 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008706 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008707 X86::XOR32rr, X86::XOR32rr,
8708 X86::XOR32ri, X86::XOR32ri,
8709 false);
8710 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008711 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008712 X86::AND32rr, X86::AND32rr,
8713 X86::AND32ri, X86::AND32ri,
8714 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008715 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008716 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008717 X86::ADD32rr, X86::ADC32rr,
8718 X86::ADD32ri, X86::ADC32ri,
8719 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008720 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008721 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008722 X86::SUB32rr, X86::SBB32rr,
8723 X86::SUB32ri, X86::SBB32ri,
8724 false);
Dale Johannesen880ae362008-10-03 22:25:52 +00008725 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008726 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +00008727 X86::MOV32rr, X86::MOV32rr,
8728 X86::MOV32ri, X86::MOV32ri,
8729 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008730 case X86::VASTART_SAVE_XMM_REGS:
8731 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +00008732 }
8733}
8734
8735//===----------------------------------------------------------------------===//
8736// X86 Optimization Hooks
8737//===----------------------------------------------------------------------===//
8738
Dan Gohman475871a2008-07-27 21:46:04 +00008739void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00008740 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008741 APInt &KnownZero,
8742 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00008743 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00008744 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008745 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00008746 assert((Opc >= ISD::BUILTIN_OP_END ||
8747 Opc == ISD::INTRINSIC_WO_CHAIN ||
8748 Opc == ISD::INTRINSIC_W_CHAIN ||
8749 Opc == ISD::INTRINSIC_VOID) &&
8750 "Should use MaskedValueIsZero if you don't know whether Op"
8751 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008752
Dan Gohmanf4f92f52008-02-13 23:07:24 +00008753 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008754 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00008755 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +00008756 case X86ISD::ADD:
8757 case X86ISD::SUB:
8758 case X86ISD::SMUL:
8759 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +00008760 case X86ISD::INC:
8761 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +00008762 case X86ISD::OR:
8763 case X86ISD::XOR:
8764 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +00008765 // These nodes' second result is a boolean.
8766 if (Op.getResNo() == 0)
8767 break;
8768 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008769 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008770 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
8771 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +00008772 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008773 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008774}
Chris Lattner259e97c2006-01-31 19:43:35 +00008775
Evan Cheng206ee9d2006-07-07 08:33:52 +00008776/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +00008777/// node is a GlobalAddress + offset.
8778bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +00008779 const GlobalValue* &GA,
8780 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +00008781 if (N->getOpcode() == X86ISD::Wrapper) {
8782 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00008783 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00008784 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +00008785 return true;
8786 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00008787 }
Evan Chengad4196b2008-05-12 19:56:52 +00008788 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +00008789}
8790
Evan Cheng206ee9d2006-07-07 08:33:52 +00008791/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
8792/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
8793/// if the load addresses are consecutive, non-overlapping, and in the right
Nate Begemanfdea31a2010-03-24 20:49:50 +00008794/// order.
Dan Gohman475871a2008-07-27 21:46:04 +00008795static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00008796 const TargetLowering &TLI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00008797 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008798 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00008799 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
Mon P Wang1e955802009-04-03 02:43:30 +00008800
Eli Friedman7a5e5552009-06-07 06:52:44 +00008801 if (VT.getSizeInBits() != 128)
8802 return SDValue();
8803
Nate Begemanfdea31a2010-03-24 20:49:50 +00008804 SmallVector<SDValue, 16> Elts;
8805 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
8806 Elts.push_back(DAG.getShuffleScalarElt(SVN, i));
8807
8808 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00008809}
Evan Chengd880b972008-05-09 21:53:03 +00008810
Dan Gohman1bbf72b2010-03-15 23:23:03 +00008811/// PerformShuffleCombine - Detect vector gather/scatter index generation
8812/// and convert it from being a bunch of shuffles and extracts to a simple
8813/// store and scalar loads to extract the elements.
8814static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
8815 const TargetLowering &TLI) {
8816 SDValue InputVector = N->getOperand(0);
8817
8818 // Only operate on vectors of 4 elements, where the alternative shuffling
8819 // gets to be more expensive.
8820 if (InputVector.getValueType() != MVT::v4i32)
8821 return SDValue();
8822
8823 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
8824 // single use which is a sign-extend or zero-extend, and all elements are
8825 // used.
8826 SmallVector<SDNode *, 4> Uses;
8827 unsigned ExtractedElements = 0;
8828 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
8829 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
8830 if (UI.getUse().getResNo() != InputVector.getResNo())
8831 return SDValue();
8832
8833 SDNode *Extract = *UI;
8834 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8835 return SDValue();
8836
8837 if (Extract->getValueType(0) != MVT::i32)
8838 return SDValue();
8839 if (!Extract->hasOneUse())
8840 return SDValue();
8841 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
8842 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
8843 return SDValue();
8844 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
8845 return SDValue();
8846
8847 // Record which element was extracted.
8848 ExtractedElements |=
8849 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
8850
8851 Uses.push_back(Extract);
8852 }
8853
8854 // If not all the elements were used, this may not be worthwhile.
8855 if (ExtractedElements != 15)
8856 return SDValue();
8857
8858 // Ok, we've now decided to do the transformation.
8859 DebugLoc dl = InputVector.getDebugLoc();
8860
8861 // Store the value to a temporary stack slot.
8862 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
8863 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr, NULL, 0,
8864 false, false, 0);
8865
8866 // Replace each use (extract) with a load of the appropriate element.
8867 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
8868 UE = Uses.end(); UI != UE; ++UI) {
8869 SDNode *Extract = *UI;
8870
8871 // Compute the element's address.
8872 SDValue Idx = Extract->getOperand(1);
8873 unsigned EltSize =
8874 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
8875 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
8876 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
8877
8878 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), OffsetVal, StackPtr);
8879
8880 // Load the scalar.
8881 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch, ScalarAddr,
8882 NULL, 0, false, false, 0);
8883
8884 // Replace the exact with the load.
8885 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
8886 }
8887
8888 // The replacement was made in place; don't return anything.
8889 return SDValue();
8890}
8891
Chris Lattner83e6c992006-10-04 06:57:07 +00008892/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008893static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +00008894 const X86Subtarget *Subtarget) {
8895 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008896 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +00008897 // Get the LHS/RHS of the select.
8898 SDValue LHS = N->getOperand(1);
8899 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +00008900
Dan Gohman670e5392009-09-21 18:03:22 +00008901 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +00008902 // instructions match the semantics of the common C idiom x<y?x:y but not
8903 // x<=y?x:y, because of how they handle negative zero (which can be
8904 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +00008905 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00008906 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +00008907 Cond.getOpcode() == ISD::SETCC) {
8908 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008909
Chris Lattner47b4ce82009-03-11 05:48:52 +00008910 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +00008911 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +00008912 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
8913 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +00008914 switch (CC) {
8915 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +00008916 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +00008917 // Converting this to a min would handle NaNs incorrectly, and swapping
8918 // the operands would cause it to handle comparisons between positive
8919 // and negative zero incorrectly.
8920 if (!FiniteOnlyFPMath() &&
8921 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))) {
8922 if (!UnsafeFPMath &&
8923 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8924 break;
8925 std::swap(LHS, RHS);
8926 }
Dan Gohman670e5392009-09-21 18:03:22 +00008927 Opcode = X86ISD::FMIN;
8928 break;
8929 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +00008930 // Converting this to a min would handle comparisons between positive
8931 // and negative zero incorrectly.
8932 if (!UnsafeFPMath &&
8933 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
8934 break;
Dan Gohman670e5392009-09-21 18:03:22 +00008935 Opcode = X86ISD::FMIN;
8936 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +00008937 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +00008938 // Converting this to a min would handle both negative zeros and NaNs
8939 // incorrectly, but we can swap the operands to fix both.
8940 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00008941 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008942 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +00008943 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008944 Opcode = X86ISD::FMIN;
8945 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008946
Dan Gohman670e5392009-09-21 18:03:22 +00008947 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +00008948 // Converting this to a max would handle comparisons between positive
8949 // and negative zero incorrectly.
8950 if (!UnsafeFPMath &&
8951 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
8952 break;
Dan Gohman670e5392009-09-21 18:03:22 +00008953 Opcode = X86ISD::FMAX;
8954 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +00008955 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +00008956 // Converting this to a max would handle NaNs incorrectly, and swapping
8957 // the operands would cause it to handle comparisons between positive
8958 // and negative zero incorrectly.
8959 if (!FiniteOnlyFPMath() &&
8960 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))) {
8961 if (!UnsafeFPMath &&
8962 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8963 break;
8964 std::swap(LHS, RHS);
8965 }
Dan Gohman670e5392009-09-21 18:03:22 +00008966 Opcode = X86ISD::FMAX;
8967 break;
8968 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +00008969 // Converting this to a max would handle both negative zeros and NaNs
8970 // incorrectly, but we can swap the operands to fix both.
8971 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00008972 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008973 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008974 case ISD::SETGE:
8975 Opcode = X86ISD::FMAX;
8976 break;
Chris Lattner83e6c992006-10-04 06:57:07 +00008977 }
Dan Gohman670e5392009-09-21 18:03:22 +00008978 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +00008979 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
8980 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +00008981 switch (CC) {
8982 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +00008983 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +00008984 // Converting this to a min would handle comparisons between positive
8985 // and negative zero incorrectly, and swapping the operands would
8986 // cause it to handle NaNs incorrectly.
8987 if (!UnsafeFPMath &&
8988 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
8989 if (!FiniteOnlyFPMath() &&
8990 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
8991 break;
8992 std::swap(LHS, RHS);
8993 }
Dan Gohman670e5392009-09-21 18:03:22 +00008994 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +00008995 break;
Dan Gohman670e5392009-09-21 18:03:22 +00008996 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +00008997 // Converting this to a min would handle NaNs incorrectly.
8998 if (!UnsafeFPMath &&
8999 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
9000 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009001 Opcode = X86ISD::FMIN;
9002 break;
9003 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +00009004 // Converting this to a min would handle both negative zeros and NaNs
9005 // incorrectly, but we can swap the operands to fix both.
9006 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00009007 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009008 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009009 case ISD::SETGE:
9010 Opcode = X86ISD::FMIN;
9011 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009012
Dan Gohman670e5392009-09-21 18:03:22 +00009013 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +00009014 // Converting this to a max would handle NaNs incorrectly.
9015 if (!FiniteOnlyFPMath() &&
9016 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
9017 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009018 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +00009019 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009020 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +00009021 // Converting this to a max would handle comparisons between positive
9022 // and negative zero incorrectly, and swapping the operands would
9023 // cause it to handle NaNs incorrectly.
9024 if (!UnsafeFPMath &&
9025 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
9026 if (!FiniteOnlyFPMath() &&
9027 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
9028 break;
9029 std::swap(LHS, RHS);
9030 }
Dan Gohman670e5392009-09-21 18:03:22 +00009031 Opcode = X86ISD::FMAX;
9032 break;
9033 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +00009034 // Converting this to a max would handle both negative zeros and NaNs
9035 // incorrectly, but we can swap the operands to fix both.
9036 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00009037 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009038 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +00009039 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009040 Opcode = X86ISD::FMAX;
9041 break;
9042 }
Chris Lattner83e6c992006-10-04 06:57:07 +00009043 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009044
Chris Lattner47b4ce82009-03-11 05:48:52 +00009045 if (Opcode)
9046 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +00009047 }
Eric Christopherfd179292009-08-27 18:07:15 +00009048
Chris Lattnerd1980a52009-03-12 06:52:53 +00009049 // If this is a select between two integer constants, try to do some
9050 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +00009051 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
9052 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +00009053 // Don't do this for crazy integer types.
9054 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
9055 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +00009056 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +00009057 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +00009058
Chris Lattnercee56e72009-03-13 05:53:31 +00009059 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +00009060 // Efficiently invertible.
9061 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
9062 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
9063 isa<ConstantSDNode>(Cond.getOperand(1))))) {
9064 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +00009065 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +00009066 }
Eric Christopherfd179292009-08-27 18:07:15 +00009067
Chris Lattnerd1980a52009-03-12 06:52:53 +00009068 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00009069 if (FalseC->getAPIntValue() == 0 &&
9070 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00009071 if (NeedsCondInvert) // Invert the condition if needed.
9072 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
9073 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009074
Chris Lattnerd1980a52009-03-12 06:52:53 +00009075 // Zero extend the condition if needed.
9076 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009077
Chris Lattnercee56e72009-03-13 05:53:31 +00009078 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +00009079 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +00009080 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +00009081 }
Eric Christopherfd179292009-08-27 18:07:15 +00009082
Chris Lattner97a29a52009-03-13 05:22:11 +00009083 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +00009084 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +00009085 if (NeedsCondInvert) // Invert the condition if needed.
9086 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
9087 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009088
Chris Lattner97a29a52009-03-13 05:22:11 +00009089 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00009090 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
9091 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00009092 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +00009093 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +00009094 }
Eric Christopherfd179292009-08-27 18:07:15 +00009095
Chris Lattnercee56e72009-03-13 05:53:31 +00009096 // Optimize cases that will turn into an LEA instruction. This requires
9097 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +00009098 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +00009099 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00009100 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +00009101
Chris Lattnercee56e72009-03-13 05:53:31 +00009102 bool isFastMultiplier = false;
9103 if (Diff < 10) {
9104 switch ((unsigned char)Diff) {
9105 default: break;
9106 case 1: // result = add base, cond
9107 case 2: // result = lea base( , cond*2)
9108 case 3: // result = lea base(cond, cond*2)
9109 case 4: // result = lea base( , cond*4)
9110 case 5: // result = lea base(cond, cond*4)
9111 case 8: // result = lea base( , cond*8)
9112 case 9: // result = lea base(cond, cond*8)
9113 isFastMultiplier = true;
9114 break;
9115 }
9116 }
Eric Christopherfd179292009-08-27 18:07:15 +00009117
Chris Lattnercee56e72009-03-13 05:53:31 +00009118 if (isFastMultiplier) {
9119 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
9120 if (NeedsCondInvert) // Invert the condition if needed.
9121 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
9122 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009123
Chris Lattnercee56e72009-03-13 05:53:31 +00009124 // Zero extend the condition if needed.
9125 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
9126 Cond);
9127 // Scale the condition by the difference.
9128 if (Diff != 1)
9129 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
9130 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009131
Chris Lattnercee56e72009-03-13 05:53:31 +00009132 // Add the base if non-zero.
9133 if (FalseC->getAPIntValue() != 0)
9134 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9135 SDValue(FalseC, 0));
9136 return Cond;
9137 }
Eric Christopherfd179292009-08-27 18:07:15 +00009138 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00009139 }
9140 }
Eric Christopherfd179292009-08-27 18:07:15 +00009141
Dan Gohman475871a2008-07-27 21:46:04 +00009142 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +00009143}
9144
Chris Lattnerd1980a52009-03-12 06:52:53 +00009145/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
9146static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
9147 TargetLowering::DAGCombinerInfo &DCI) {
9148 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +00009149
Chris Lattnerd1980a52009-03-12 06:52:53 +00009150 // If the flag operand isn't dead, don't touch this CMOV.
9151 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
9152 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +00009153
Chris Lattnerd1980a52009-03-12 06:52:53 +00009154 // If this is a select between two integer constants, try to do some
9155 // optimizations. Note that the operands are ordered the opposite of SELECT
9156 // operands.
9157 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
9158 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
9159 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
9160 // larger than FalseC (the false value).
9161 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
Eric Christopherfd179292009-08-27 18:07:15 +00009162
Chris Lattnerd1980a52009-03-12 06:52:53 +00009163 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
9164 CC = X86::GetOppositeBranchCondition(CC);
9165 std::swap(TrueC, FalseC);
9166 }
Eric Christopherfd179292009-08-27 18:07:15 +00009167
Chris Lattnerd1980a52009-03-12 06:52:53 +00009168 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00009169 // This is efficient for any integer data type (including i8/i16) and
9170 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +00009171 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
9172 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00009173 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9174 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009175
Chris Lattnerd1980a52009-03-12 06:52:53 +00009176 // Zero extend the condition if needed.
9177 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009178
Chris Lattnerd1980a52009-03-12 06:52:53 +00009179 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
9180 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +00009181 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +00009182 if (N->getNumValues() == 2) // Dead flag value?
9183 return DCI.CombineTo(N, Cond, SDValue());
9184 return Cond;
9185 }
Eric Christopherfd179292009-08-27 18:07:15 +00009186
Chris Lattnercee56e72009-03-13 05:53:31 +00009187 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
9188 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +00009189 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
9190 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00009191 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9192 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009193
Chris Lattner97a29a52009-03-13 05:22:11 +00009194 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00009195 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
9196 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00009197 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9198 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +00009199
Chris Lattner97a29a52009-03-13 05:22:11 +00009200 if (N->getNumValues() == 2) // Dead flag value?
9201 return DCI.CombineTo(N, Cond, SDValue());
9202 return Cond;
9203 }
Eric Christopherfd179292009-08-27 18:07:15 +00009204
Chris Lattnercee56e72009-03-13 05:53:31 +00009205 // Optimize cases that will turn into an LEA instruction. This requires
9206 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +00009207 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +00009208 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00009209 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +00009210
Chris Lattnercee56e72009-03-13 05:53:31 +00009211 bool isFastMultiplier = false;
9212 if (Diff < 10) {
9213 switch ((unsigned char)Diff) {
9214 default: break;
9215 case 1: // result = add base, cond
9216 case 2: // result = lea base( , cond*2)
9217 case 3: // result = lea base(cond, cond*2)
9218 case 4: // result = lea base( , cond*4)
9219 case 5: // result = lea base(cond, cond*4)
9220 case 8: // result = lea base( , cond*8)
9221 case 9: // result = lea base(cond, cond*8)
9222 isFastMultiplier = true;
9223 break;
9224 }
9225 }
Eric Christopherfd179292009-08-27 18:07:15 +00009226
Chris Lattnercee56e72009-03-13 05:53:31 +00009227 if (isFastMultiplier) {
9228 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
9229 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00009230 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9231 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +00009232 // Zero extend the condition if needed.
9233 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
9234 Cond);
9235 // Scale the condition by the difference.
9236 if (Diff != 1)
9237 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
9238 DAG.getConstant(Diff, Cond.getValueType()));
9239
9240 // Add the base if non-zero.
9241 if (FalseC->getAPIntValue() != 0)
9242 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9243 SDValue(FalseC, 0));
9244 if (N->getNumValues() == 2) // Dead flag value?
9245 return DCI.CombineTo(N, Cond, SDValue());
9246 return Cond;
9247 }
Eric Christopherfd179292009-08-27 18:07:15 +00009248 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00009249 }
9250 }
9251 return SDValue();
9252}
9253
9254
Evan Cheng0b0cd912009-03-28 05:57:29 +00009255/// PerformMulCombine - Optimize a single multiply with constant into two
9256/// in order to implement it with two cheaper instructions, e.g.
9257/// LEA + SHL, LEA + LEA.
9258static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
9259 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +00009260 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
9261 return SDValue();
9262
Owen Andersone50ed302009-08-10 22:56:29 +00009263 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009264 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +00009265 return SDValue();
9266
9267 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
9268 if (!C)
9269 return SDValue();
9270 uint64_t MulAmt = C->getZExtValue();
9271 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
9272 return SDValue();
9273
9274 uint64_t MulAmt1 = 0;
9275 uint64_t MulAmt2 = 0;
9276 if ((MulAmt % 9) == 0) {
9277 MulAmt1 = 9;
9278 MulAmt2 = MulAmt / 9;
9279 } else if ((MulAmt % 5) == 0) {
9280 MulAmt1 = 5;
9281 MulAmt2 = MulAmt / 5;
9282 } else if ((MulAmt % 3) == 0) {
9283 MulAmt1 = 3;
9284 MulAmt2 = MulAmt / 3;
9285 }
9286 if (MulAmt2 &&
9287 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
9288 DebugLoc DL = N->getDebugLoc();
9289
9290 if (isPowerOf2_64(MulAmt2) &&
9291 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
9292 // If second multiplifer is pow2, issue it first. We want the multiply by
9293 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
9294 // is an add.
9295 std::swap(MulAmt1, MulAmt2);
9296
9297 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +00009298 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +00009299 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00009300 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +00009301 else
Evan Cheng73f24c92009-03-30 21:36:47 +00009302 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +00009303 DAG.getConstant(MulAmt1, VT));
9304
Eric Christopherfd179292009-08-27 18:07:15 +00009305 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +00009306 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +00009307 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +00009308 else
Evan Cheng73f24c92009-03-30 21:36:47 +00009309 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +00009310 DAG.getConstant(MulAmt2, VT));
9311
9312 // Do not add new nodes to DAG combiner worklist.
9313 DCI.CombineTo(N, NewMul, false);
9314 }
9315 return SDValue();
9316}
9317
Evan Chengad9c0a32009-12-15 00:53:42 +00009318static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
9319 SDValue N0 = N->getOperand(0);
9320 SDValue N1 = N->getOperand(1);
9321 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
9322 EVT VT = N0.getValueType();
9323
9324 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
9325 // since the result of setcc_c is all zero's or all ones.
9326 if (N1C && N0.getOpcode() == ISD::AND &&
9327 N0.getOperand(1).getOpcode() == ISD::Constant) {
9328 SDValue N00 = N0.getOperand(0);
9329 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
9330 ((N00.getOpcode() == ISD::ANY_EXTEND ||
9331 N00.getOpcode() == ISD::ZERO_EXTEND) &&
9332 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
9333 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
9334 APInt ShAmt = N1C->getAPIntValue();
9335 Mask = Mask.shl(ShAmt);
9336 if (Mask != 0)
9337 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
9338 N00, DAG.getConstant(Mask, VT));
9339 }
9340 }
9341
9342 return SDValue();
9343}
Evan Cheng0b0cd912009-03-28 05:57:29 +00009344
Nate Begeman740ab032009-01-26 00:52:55 +00009345/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
9346/// when possible.
9347static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
9348 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +00009349 EVT VT = N->getValueType(0);
9350 if (!VT.isVector() && VT.isInteger() &&
9351 N->getOpcode() == ISD::SHL)
9352 return PerformSHLCombine(N, DAG);
9353
Nate Begeman740ab032009-01-26 00:52:55 +00009354 // On X86 with SSE2 support, we can transform this to a vector shift if
9355 // all elements are shifted by the same amount. We can't do this in legalize
9356 // because the a constant vector is typically transformed to a constant pool
9357 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009358 if (!Subtarget->hasSSE2())
9359 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009360
Owen Anderson825b72b2009-08-11 20:47:22 +00009361 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009362 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009363
Mon P Wang3becd092009-01-28 08:12:05 +00009364 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00009365 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +00009366 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +00009367 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +00009368 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
9369 unsigned NumElts = VT.getVectorNumElements();
9370 unsigned i = 0;
9371 for (; i != NumElts; ++i) {
9372 SDValue Arg = ShAmtOp.getOperand(i);
9373 if (Arg.getOpcode() == ISD::UNDEF) continue;
9374 BaseShAmt = Arg;
9375 break;
9376 }
9377 for (; i != NumElts; ++i) {
9378 SDValue Arg = ShAmtOp.getOperand(i);
9379 if (Arg.getOpcode() == ISD::UNDEF) continue;
9380 if (Arg != BaseShAmt) {
9381 return SDValue();
9382 }
9383 }
9384 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +00009385 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +00009386 SDValue InVec = ShAmtOp.getOperand(0);
9387 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
9388 unsigned NumElts = InVec.getValueType().getVectorNumElements();
9389 unsigned i = 0;
9390 for (; i != NumElts; ++i) {
9391 SDValue Arg = InVec.getOperand(i);
9392 if (Arg.getOpcode() == ISD::UNDEF) continue;
9393 BaseShAmt = Arg;
9394 break;
9395 }
9396 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
9397 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +00009398 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +00009399 if (C->getZExtValue() == SplatIdx)
9400 BaseShAmt = InVec.getOperand(1);
9401 }
9402 }
9403 if (BaseShAmt.getNode() == 0)
9404 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
9405 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +00009406 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009407 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +00009408
Mon P Wangefa42202009-09-03 19:56:25 +00009409 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +00009410 if (EltVT.bitsGT(MVT::i32))
9411 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
9412 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +00009413 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +00009414
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009415 // The shift amount is identical so we can do a vector shift.
9416 SDValue ValOp = N->getOperand(0);
9417 switch (N->getOpcode()) {
9418 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009419 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009420 break;
9421 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +00009422 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009423 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009424 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009425 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009426 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009427 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009428 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009429 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009430 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009431 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009432 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009433 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009434 break;
9435 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +00009436 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009437 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009438 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009439 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009440 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009441 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009442 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009443 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009444 break;
9445 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +00009446 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009447 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009448 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009449 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009450 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009451 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009452 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009453 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009454 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009455 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009456 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009457 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009458 break;
Nate Begeman740ab032009-01-26 00:52:55 +00009459 }
9460 return SDValue();
9461}
9462
Evan Cheng760d1942010-01-04 21:22:48 +00009463static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +00009464 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +00009465 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +00009466 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +00009467 return SDValue();
9468
Evan Cheng760d1942010-01-04 21:22:48 +00009469 EVT VT = N->getValueType(0);
Evan Cheng8b1190a2010-04-28 01:18:01 +00009470 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
Evan Cheng760d1942010-01-04 21:22:48 +00009471 return SDValue();
9472
9473 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
9474 SDValue N0 = N->getOperand(0);
9475 SDValue N1 = N->getOperand(1);
9476 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
9477 std::swap(N0, N1);
9478 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
9479 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +00009480 if (!N0.hasOneUse() || !N1.hasOneUse())
9481 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +00009482
9483 SDValue ShAmt0 = N0.getOperand(1);
9484 if (ShAmt0.getValueType() != MVT::i8)
9485 return SDValue();
9486 SDValue ShAmt1 = N1.getOperand(1);
9487 if (ShAmt1.getValueType() != MVT::i8)
9488 return SDValue();
9489 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
9490 ShAmt0 = ShAmt0.getOperand(0);
9491 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
9492 ShAmt1 = ShAmt1.getOperand(0);
9493
9494 DebugLoc DL = N->getDebugLoc();
9495 unsigned Opc = X86ISD::SHLD;
9496 SDValue Op0 = N0.getOperand(0);
9497 SDValue Op1 = N1.getOperand(0);
9498 if (ShAmt0.getOpcode() == ISD::SUB) {
9499 Opc = X86ISD::SHRD;
9500 std::swap(Op0, Op1);
9501 std::swap(ShAmt0, ShAmt1);
9502 }
9503
Evan Cheng8b1190a2010-04-28 01:18:01 +00009504 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +00009505 if (ShAmt1.getOpcode() == ISD::SUB) {
9506 SDValue Sum = ShAmt1.getOperand(0);
9507 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Evan Cheng8b1190a2010-04-28 01:18:01 +00009508 if (SumC->getSExtValue() == Bits &&
Evan Cheng760d1942010-01-04 21:22:48 +00009509 ShAmt1.getOperand(1) == ShAmt0)
9510 return DAG.getNode(Opc, DL, VT,
9511 Op0, Op1,
9512 DAG.getNode(ISD::TRUNCATE, DL,
9513 MVT::i8, ShAmt0));
9514 }
9515 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
9516 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
9517 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +00009518 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +00009519 return DAG.getNode(Opc, DL, VT,
9520 N0.getOperand(0), N1.getOperand(0),
9521 DAG.getNode(ISD::TRUNCATE, DL,
9522 MVT::i8, ShAmt0));
9523 }
9524
9525 return SDValue();
9526}
9527
Chris Lattner149a4e52008-02-22 02:09:43 +00009528/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009529static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +00009530 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +00009531 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
9532 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +00009533 // A preferable solution to the general problem is to figure out the right
9534 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +00009535
9536 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +00009537 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +00009538 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +00009539 if (VT.getSizeInBits() != 64)
9540 return SDValue();
9541
Devang Patel578efa92009-06-05 21:57:13 +00009542 const Function *F = DAG.getMachineFunction().getFunction();
9543 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +00009544 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +00009545 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +00009546 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +00009547 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +00009548 isa<LoadSDNode>(St->getValue()) &&
9549 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
9550 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00009551 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009552 LoadSDNode *Ld = 0;
9553 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +00009554 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +00009555 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009556 // Must be a store of a load. We currently handle two cases: the load
9557 // is a direct child, and it's under an intervening TokenFactor. It is
9558 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +00009559 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +00009560 Ld = cast<LoadSDNode>(St->getChain());
9561 else if (St->getValue().hasOneUse() &&
9562 ChainVal->getOpcode() == ISD::TokenFactor) {
9563 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +00009564 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +00009565 TokenFactorIndex = i;
9566 Ld = cast<LoadSDNode>(St->getValue());
9567 } else
9568 Ops.push_back(ChainVal->getOperand(i));
9569 }
9570 }
Dale Johannesen079f2a62008-02-25 19:20:14 +00009571
Evan Cheng536e6672009-03-12 05:59:15 +00009572 if (!Ld || !ISD::isNormalLoad(Ld))
9573 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009574
Evan Cheng536e6672009-03-12 05:59:15 +00009575 // If this is not the MMX case, i.e. we are just turning i64 load/store
9576 // into f64 load/store, avoid the transformation if there are multiple
9577 // uses of the loaded value.
9578 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
9579 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009580
Evan Cheng536e6672009-03-12 05:59:15 +00009581 DebugLoc LdDL = Ld->getDebugLoc();
9582 DebugLoc StDL = N->getDebugLoc();
9583 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
9584 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
9585 // pair instead.
9586 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009587 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Evan Cheng536e6672009-03-12 05:59:15 +00009588 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(),
9589 Ld->getBasePtr(), Ld->getSrcValue(),
9590 Ld->getSrcValueOffset(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +00009591 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +00009592 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +00009593 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +00009594 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +00009595 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +00009596 Ops.size());
9597 }
Evan Cheng536e6672009-03-12 05:59:15 +00009598 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner149a4e52008-02-22 02:09:43 +00009599 St->getSrcValue(), St->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00009600 St->isVolatile(), St->isNonTemporal(),
9601 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +00009602 }
Evan Cheng536e6672009-03-12 05:59:15 +00009603
9604 // Otherwise, lower to two pairs of 32-bit loads / stores.
9605 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +00009606 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
9607 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +00009608
Owen Anderson825b72b2009-08-11 20:47:22 +00009609 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Evan Cheng536e6672009-03-12 05:59:15 +00009610 Ld->getSrcValue(), Ld->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00009611 Ld->isVolatile(), Ld->isNonTemporal(),
9612 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00009613 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Evan Cheng536e6672009-03-12 05:59:15 +00009614 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
David Greene67c9d422010-02-15 16:53:33 +00009615 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +00009616 MinAlign(Ld->getAlignment(), 4));
9617
9618 SDValue NewChain = LoLd.getValue(1);
9619 if (TokenFactorIndex != -1) {
9620 Ops.push_back(LoLd);
9621 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +00009622 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +00009623 Ops.size());
9624 }
9625
9626 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +00009627 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
9628 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +00009629
9630 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
9631 St->getSrcValue(), St->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00009632 St->isVolatile(), St->isNonTemporal(),
9633 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +00009634 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
9635 St->getSrcValue(),
9636 St->getSrcValueOffset() + 4,
9637 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +00009638 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +00009639 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +00009640 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +00009641 }
Dan Gohman475871a2008-07-27 21:46:04 +00009642 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +00009643}
9644
Chris Lattner6cf73262008-01-25 06:14:17 +00009645/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
9646/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009647static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +00009648 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
9649 // F[X]OR(0.0, x) -> x
9650 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +00009651 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
9652 if (C->getValueAPF().isPosZero())
9653 return N->getOperand(1);
9654 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
9655 if (C->getValueAPF().isPosZero())
9656 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +00009657 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00009658}
9659
9660/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009661static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +00009662 // FAND(0.0, x) -> 0.0
9663 // FAND(x, 0.0) -> 0.0
9664 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
9665 if (C->getValueAPF().isPosZero())
9666 return N->getOperand(0);
9667 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
9668 if (C->getValueAPF().isPosZero())
9669 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +00009670 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00009671}
9672
Dan Gohmane5af2d32009-01-29 01:59:02 +00009673static SDValue PerformBTCombine(SDNode *N,
9674 SelectionDAG &DAG,
9675 TargetLowering::DAGCombinerInfo &DCI) {
9676 // BT ignores high bits in the bit index operand.
9677 SDValue Op1 = N->getOperand(1);
9678 if (Op1.hasOneUse()) {
9679 unsigned BitWidth = Op1.getValueSizeInBits();
9680 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
9681 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +00009682 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
9683 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +00009684 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +00009685 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
9686 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
9687 DCI.CommitTargetLoweringOpt(TLO);
9688 }
9689 return SDValue();
9690}
Chris Lattner83e6c992006-10-04 06:57:07 +00009691
Eli Friedman7a5e5552009-06-07 06:52:44 +00009692static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
9693 SDValue Op = N->getOperand(0);
9694 if (Op.getOpcode() == ISD::BIT_CONVERT)
9695 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00009696 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +00009697 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +00009698 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +00009699 OpVT.getVectorElementType().getSizeInBits()) {
9700 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT, Op);
9701 }
9702 return SDValue();
9703}
9704
Owen Anderson99177002009-06-29 18:04:45 +00009705// On X86 and X86-64, atomic operations are lowered to locked instructions.
9706// Locked instructions, in turn, have implicit fence semantics (all memory
9707// operations are flushed before issuing the locked instruction, and the
Eric Christopherfd179292009-08-27 18:07:15 +00009708// are not buffered), so we can fold away the common pattern of
Owen Anderson99177002009-06-29 18:04:45 +00009709// fence-atomic-fence.
9710static SDValue PerformMEMBARRIERCombine(SDNode* N, SelectionDAG &DAG) {
9711 SDValue atomic = N->getOperand(0);
9712 switch (atomic.getOpcode()) {
9713 case ISD::ATOMIC_CMP_SWAP:
9714 case ISD::ATOMIC_SWAP:
9715 case ISD::ATOMIC_LOAD_ADD:
9716 case ISD::ATOMIC_LOAD_SUB:
9717 case ISD::ATOMIC_LOAD_AND:
9718 case ISD::ATOMIC_LOAD_OR:
9719 case ISD::ATOMIC_LOAD_XOR:
9720 case ISD::ATOMIC_LOAD_NAND:
9721 case ISD::ATOMIC_LOAD_MIN:
9722 case ISD::ATOMIC_LOAD_MAX:
9723 case ISD::ATOMIC_LOAD_UMIN:
9724 case ISD::ATOMIC_LOAD_UMAX:
9725 break;
9726 default:
9727 return SDValue();
9728 }
Eric Christopherfd179292009-08-27 18:07:15 +00009729
Owen Anderson99177002009-06-29 18:04:45 +00009730 SDValue fence = atomic.getOperand(0);
9731 if (fence.getOpcode() != ISD::MEMBARRIER)
9732 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +00009733
Owen Anderson99177002009-06-29 18:04:45 +00009734 switch (atomic.getOpcode()) {
9735 case ISD::ATOMIC_CMP_SWAP:
9736 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
9737 atomic.getOperand(1), atomic.getOperand(2),
9738 atomic.getOperand(3));
9739 case ISD::ATOMIC_SWAP:
9740 case ISD::ATOMIC_LOAD_ADD:
9741 case ISD::ATOMIC_LOAD_SUB:
9742 case ISD::ATOMIC_LOAD_AND:
9743 case ISD::ATOMIC_LOAD_OR:
9744 case ISD::ATOMIC_LOAD_XOR:
9745 case ISD::ATOMIC_LOAD_NAND:
9746 case ISD::ATOMIC_LOAD_MIN:
9747 case ISD::ATOMIC_LOAD_MAX:
9748 case ISD::ATOMIC_LOAD_UMIN:
9749 case ISD::ATOMIC_LOAD_UMAX:
9750 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
9751 atomic.getOperand(1), atomic.getOperand(2));
9752 default:
9753 return SDValue();
9754 }
9755}
9756
Evan Cheng2e489c42009-12-16 00:53:11 +00009757static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
9758 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
9759 // (and (i32 x86isd::setcc_carry), 1)
9760 // This eliminates the zext. This transformation is necessary because
9761 // ISD::SETCC is always legalized to i8.
9762 DebugLoc dl = N->getDebugLoc();
9763 SDValue N0 = N->getOperand(0);
9764 EVT VT = N->getValueType(0);
9765 if (N0.getOpcode() == ISD::AND &&
9766 N0.hasOneUse() &&
9767 N0.getOperand(0).hasOneUse()) {
9768 SDValue N00 = N0.getOperand(0);
9769 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
9770 return SDValue();
9771 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
9772 if (!C || C->getZExtValue() != 1)
9773 return SDValue();
9774 return DAG.getNode(ISD::AND, dl, VT,
9775 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
9776 N00.getOperand(0), N00.getOperand(1)),
9777 DAG.getConstant(1, VT));
9778 }
9779
9780 return SDValue();
9781}
9782
Dan Gohman475871a2008-07-27 21:46:04 +00009783SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +00009784 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +00009785 SelectionDAG &DAG = DCI.DAG;
9786 switch (N->getOpcode()) {
9787 default: break;
Evan Chengad4196b2008-05-12 19:56:52 +00009788 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00009789 case ISD::EXTRACT_VECTOR_ELT:
9790 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +00009791 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +00009792 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +00009793 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +00009794 case ISD::SHL:
9795 case ISD::SRA:
9796 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +00009797 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +00009798 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +00009799 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +00009800 case X86ISD::FOR: return PerformFORCombine(N, DAG);
9801 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +00009802 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +00009803 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Owen Anderson99177002009-06-29 18:04:45 +00009804 case ISD::MEMBARRIER: return PerformMEMBARRIERCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +00009805 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Evan Cheng206ee9d2006-07-07 08:33:52 +00009806 }
9807
Dan Gohman475871a2008-07-27 21:46:04 +00009808 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00009809}
9810
Evan Chenge5b51ac2010-04-17 06:13:15 +00009811/// isTypeDesirableForOp - Return true if the target has native support for
9812/// the specified value type and it is 'desirable' to use the type for the
9813/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
9814/// instruction encodings are longer and some i16 instructions are slow.
9815bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
9816 if (!isTypeLegal(VT))
9817 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00009818 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +00009819 return true;
9820
9821 switch (Opc) {
9822 default:
9823 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +00009824 case ISD::LOAD:
9825 case ISD::SIGN_EXTEND:
9826 case ISD::ZERO_EXTEND:
9827 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +00009828 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +00009829 case ISD::SRL:
9830 case ISD::SUB:
9831 case ISD::ADD:
9832 case ISD::MUL:
9833 case ISD::AND:
9834 case ISD::OR:
9835 case ISD::XOR:
9836 return false;
9837 }
9838}
9839
Evan Chengc82c20b2010-04-24 04:44:57 +00009840static bool MayFoldLoad(SDValue Op) {
9841 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
9842}
9843
9844static bool MayFoldIntoStore(SDValue Op) {
9845 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
9846}
9847
Evan Chenge5b51ac2010-04-17 06:13:15 +00009848/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +00009849/// beneficial for dag combiner to promote the specified node. If true, it
9850/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +00009851bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +00009852 EVT VT = Op.getValueType();
9853 if (VT != MVT::i16)
9854 return false;
9855
Evan Cheng4c26e932010-04-19 19:29:22 +00009856 bool Promote = false;
9857 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +00009858 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +00009859 default: break;
9860 case ISD::LOAD: {
9861 LoadSDNode *LD = cast<LoadSDNode>(Op);
9862 // If the non-extending load has a single use and it's not live out, then it
9863 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00009864 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
9865 Op.hasOneUse()*/) {
9866 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
9867 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
9868 // The only case where we'd want to promote LOAD (rather then it being
9869 // promoted as an operand is when it's only use is liveout.
9870 if (UI->getOpcode() != ISD::CopyToReg)
9871 return false;
9872 }
9873 }
Evan Cheng4c26e932010-04-19 19:29:22 +00009874 Promote = true;
9875 break;
9876 }
9877 case ISD::SIGN_EXTEND:
9878 case ISD::ZERO_EXTEND:
9879 case ISD::ANY_EXTEND:
9880 Promote = true;
9881 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +00009882 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00009883 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +00009884 SDValue N0 = Op.getOperand(0);
9885 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +00009886 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +00009887 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +00009888 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +00009889 break;
9890 }
Evan Cheng64b7bf72010-04-16 06:14:10 +00009891 case ISD::ADD:
9892 case ISD::MUL:
9893 case ISD::AND:
9894 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +00009895 case ISD::XOR:
9896 Commute = true;
9897 // fallthrough
9898 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +00009899 SDValue N0 = Op.getOperand(0);
9900 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +00009901 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +00009902 return false;
9903 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +00009904 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +00009905 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +00009906 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +00009907 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +00009908 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +00009909 }
9910 }
9911
9912 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +00009913 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +00009914}
9915
Evan Cheng60c07e12006-07-05 22:17:51 +00009916//===----------------------------------------------------------------------===//
9917// X86 Inline Assembly Support
9918//===----------------------------------------------------------------------===//
9919
Chris Lattnerb8105652009-07-20 17:51:36 +00009920static bool LowerToBSwap(CallInst *CI) {
9921 // FIXME: this should verify that we are targetting a 486 or better. If not,
9922 // we will turn this bswap into something that will be lowered to logical ops
9923 // instead of emitting the bswap asm. For now, we don't support 486 or lower
9924 // so don't worry about this.
Eric Christopherfd179292009-08-27 18:07:15 +00009925
Chris Lattnerb8105652009-07-20 17:51:36 +00009926 // Verify this is a simple bswap.
9927 if (CI->getNumOperands() != 2 ||
Eric Christopher551754c2010-04-16 23:37:20 +00009928 CI->getType() != CI->getOperand(1)->getType() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009929 !CI->getType()->isIntegerTy())
Chris Lattnerb8105652009-07-20 17:51:36 +00009930 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00009931
Chris Lattnerb8105652009-07-20 17:51:36 +00009932 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
9933 if (!Ty || Ty->getBitWidth() % 16 != 0)
9934 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00009935
Chris Lattnerb8105652009-07-20 17:51:36 +00009936 // Okay, we can do this xform, do so now.
9937 const Type *Tys[] = { Ty };
9938 Module *M = CI->getParent()->getParent()->getParent();
9939 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
Eric Christopherfd179292009-08-27 18:07:15 +00009940
Eric Christopher551754c2010-04-16 23:37:20 +00009941 Value *Op = CI->getOperand(1);
Chris Lattnerb8105652009-07-20 17:51:36 +00009942 Op = CallInst::Create(Int, Op, CI->getName(), CI);
Eric Christopherfd179292009-08-27 18:07:15 +00009943
Chris Lattnerb8105652009-07-20 17:51:36 +00009944 CI->replaceAllUsesWith(Op);
9945 CI->eraseFromParent();
9946 return true;
9947}
9948
9949bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
9950 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
9951 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
9952
9953 std::string AsmStr = IA->getAsmString();
9954
9955 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +00009956 SmallVector<StringRef, 4> AsmPieces;
Chris Lattnerb8105652009-07-20 17:51:36 +00009957 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
9958
9959 switch (AsmPieces.size()) {
9960 default: return false;
9961 case 1:
9962 AsmStr = AsmPieces[0];
9963 AsmPieces.clear();
9964 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
9965
9966 // bswap $0
9967 if (AsmPieces.size() == 2 &&
9968 (AsmPieces[0] == "bswap" ||
9969 AsmPieces[0] == "bswapq" ||
9970 AsmPieces[0] == "bswapl") &&
9971 (AsmPieces[1] == "$0" ||
9972 AsmPieces[1] == "${0:q}")) {
9973 // No need to check constraints, nothing other than the equivalent of
9974 // "=r,0" would be valid here.
9975 return LowerToBSwap(CI);
9976 }
9977 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009978 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +00009979 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +00009980 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +00009981 AsmPieces[1] == "$$8," &&
9982 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +00009983 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
9984 AsmPieces.clear();
Benjamin Kramer018cbd52010-03-12 13:54:59 +00009985 const std::string &Constraints = IA->getConstraintString();
9986 SplitString(StringRef(Constraints).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +00009987 std::sort(AsmPieces.begin(), AsmPieces.end());
9988 if (AsmPieces.size() == 4 &&
9989 AsmPieces[0] == "~{cc}" &&
9990 AsmPieces[1] == "~{dirflag}" &&
9991 AsmPieces[2] == "~{flags}" &&
9992 AsmPieces[3] == "~{fpsr}") {
9993 return LowerToBSwap(CI);
9994 }
Chris Lattnerb8105652009-07-20 17:51:36 +00009995 }
9996 break;
9997 case 3:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009998 if (CI->getType()->isIntegerTy(64) &&
Owen Anderson1d0be152009-08-13 21:58:54 +00009999 Constraints.size() >= 2 &&
Chris Lattnerb8105652009-07-20 17:51:36 +000010000 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
10001 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
10002 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramerd4f19592010-01-11 18:03:24 +000010003 SmallVector<StringRef, 4> Words;
Chris Lattnerb8105652009-07-20 17:51:36 +000010004 SplitString(AsmPieces[0], Words, " \t");
10005 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
10006 Words.clear();
10007 SplitString(AsmPieces[1], Words, " \t");
10008 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
10009 Words.clear();
10010 SplitString(AsmPieces[2], Words, " \t,");
10011 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
10012 Words[2] == "%edx") {
10013 return LowerToBSwap(CI);
10014 }
10015 }
10016 }
10017 }
10018 break;
10019 }
10020 return false;
10021}
10022
10023
10024
Chris Lattnerf4dff842006-07-11 02:54:03 +000010025/// getConstraintType - Given a constraint letter, return the type of
10026/// constraint it is for this target.
10027X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000010028X86TargetLowering::getConstraintType(const std::string &Constraint) const {
10029 if (Constraint.size() == 1) {
10030 switch (Constraint[0]) {
10031 case 'A':
Dale Johannesen330169f2008-11-13 21:52:36 +000010032 return C_Register;
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010033 case 'f':
Chris Lattner4234f572007-03-25 02:14:49 +000010034 case 'r':
10035 case 'R':
10036 case 'l':
10037 case 'q':
10038 case 'Q':
10039 case 'x':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000010040 case 'y':
Chris Lattner4234f572007-03-25 02:14:49 +000010041 case 'Y':
10042 return C_RegisterClass;
Dale Johannesen78e3e522009-02-12 20:58:09 +000010043 case 'e':
10044 case 'Z':
10045 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000010046 default:
10047 break;
10048 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000010049 }
Chris Lattner4234f572007-03-25 02:14:49 +000010050 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000010051}
10052
Dale Johannesenba2a0b92008-01-29 02:21:21 +000010053/// LowerXConstraint - try to replace an X constraint, which matches anything,
10054/// with another that has more specific requirements based on the type of the
10055/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000010056const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000010057LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000010058 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
10059 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000010060 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesenba2a0b92008-01-29 02:21:21 +000010061 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +000010062 return "Y";
10063 if (Subtarget->hasSSE1())
10064 return "x";
10065 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010066
Chris Lattner5e764232008-04-26 23:02:14 +000010067 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000010068}
10069
Chris Lattner48884cd2007-08-25 00:47:38 +000010070/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
10071/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000010072void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +000010073 char Constraint,
Evan Chengda43bcf2008-09-24 00:05:32 +000010074 bool hasMemory,
Dan Gohman475871a2008-07-27 21:46:04 +000010075 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000010076 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010077 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000010078
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010079 switch (Constraint) {
10080 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000010081 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000010082 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000010083 if (C->getZExtValue() <= 31) {
10084 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000010085 break;
10086 }
Devang Patel84f7fd22007-03-17 00:13:28 +000010087 }
Chris Lattner48884cd2007-08-25 00:47:38 +000010088 return;
Evan Cheng364091e2008-09-22 23:57:37 +000010089 case 'J':
10090 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000010091 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000010092 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
10093 break;
10094 }
10095 }
10096 return;
10097 case 'K':
10098 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000010099 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000010100 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
10101 break;
10102 }
10103 }
10104 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000010105 case 'N':
10106 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000010107 if (C->getZExtValue() <= 255) {
10108 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000010109 break;
10110 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000010111 }
Chris Lattner48884cd2007-08-25 00:47:38 +000010112 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000010113 case 'e': {
10114 // 32-bit signed value
10115 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
10116 const ConstantInt *CI = C->getConstantIntValue();
Owen Anderson1d0be152009-08-13 21:58:54 +000010117 if (CI->isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
10118 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000010119 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000010120 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000010121 break;
10122 }
10123 // FIXME gcc accepts some relocatable values here too, but only in certain
10124 // memory models; it's complicated.
10125 }
10126 return;
10127 }
10128 case 'Z': {
10129 // 32-bit unsigned value
10130 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
10131 const ConstantInt *CI = C->getConstantIntValue();
Owen Anderson1d0be152009-08-13 21:58:54 +000010132 if (CI->isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
10133 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000010134 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
10135 break;
10136 }
10137 }
10138 // FIXME gcc accepts some relocatable values here too, but only in certain
10139 // memory models; it's complicated.
10140 return;
10141 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000010142 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010143 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000010144 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000010145 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000010146 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000010147 break;
10148 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010149
Chris Lattnerdc43a882007-05-03 16:52:29 +000010150 // If we are in non-pic codegen mode, we allow the address of a global (with
10151 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000010152 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000010153 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000010154
Chris Lattner49921962009-05-08 18:23:14 +000010155 // Match either (GA), (GA+C), (GA+C1+C2), etc.
10156 while (1) {
10157 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
10158 Offset += GA->getOffset();
10159 break;
10160 } else if (Op.getOpcode() == ISD::ADD) {
10161 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
10162 Offset += C->getZExtValue();
10163 Op = Op.getOperand(0);
10164 continue;
10165 }
10166 } else if (Op.getOpcode() == ISD::SUB) {
10167 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
10168 Offset += -C->getZExtValue();
10169 Op = Op.getOperand(0);
10170 continue;
10171 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000010172 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000010173
Chris Lattner49921962009-05-08 18:23:14 +000010174 // Otherwise, this isn't something we can handle, reject it.
10175 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000010176 }
Eric Christopherfd179292009-08-27 18:07:15 +000010177
Dan Gohman46510a72010-04-15 01:51:59 +000010178 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000010179 // If we require an extra load to get this address, as in PIC mode, we
10180 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000010181 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
10182 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000010183 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000010184
Dale Johannesen60b3ba02009-07-21 00:12:29 +000010185 if (hasMemory)
10186 Op = LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
10187 else
10188 Op = DAG.getTargetGlobalAddress(GV, GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000010189 Result = Op;
10190 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010191 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000010192 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010193
Gabor Greifba36cb52008-08-28 21:40:38 +000010194 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000010195 Ops.push_back(Result);
10196 return;
10197 }
Evan Chengda43bcf2008-09-24 00:05:32 +000010198 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
10199 Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010200}
10201
Chris Lattner259e97c2006-01-31 19:43:35 +000010202std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +000010203getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000010204 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +000010205 if (Constraint.size() == 1) {
10206 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +000010207 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +000010208 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +000010209 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
10210 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000010211 if (VT == MVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010212 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
10213 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
10214 X86::R10D,X86::R11D,X86::R12D,
10215 X86::R13D,X86::R14D,X86::R15D,
10216 X86::EBP, X86::ESP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010217 else if (VT == MVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010218 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
10219 X86::SI, X86::DI, X86::R8W,X86::R9W,
10220 X86::R10W,X86::R11W,X86::R12W,
10221 X86::R13W,X86::R14W,X86::R15W,
10222 X86::BP, X86::SP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010223 else if (VT == MVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010224 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
10225 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
10226 X86::R10B,X86::R11B,X86::R12B,
10227 X86::R13B,X86::R14B,X86::R15B,
10228 X86::BPL, X86::SPL, 0);
10229
Owen Anderson825b72b2009-08-11 20:47:22 +000010230 else if (VT == MVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010231 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
10232 X86::RSI, X86::RDI, X86::R8, X86::R9,
10233 X86::R10, X86::R11, X86::R12,
10234 X86::R13, X86::R14, X86::R15,
10235 X86::RBP, X86::RSP, 0);
10236
10237 break;
10238 }
Eric Christopherfd179292009-08-27 18:07:15 +000010239 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +000010240 case 'Q': // Q_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000010241 if (VT == MVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000010242 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010243 else if (VT == MVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000010244 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010245 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +000010246 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010247 else if (VT == MVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +000010248 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
10249 break;
Chris Lattner259e97c2006-01-31 19:43:35 +000010250 }
10251 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010252
Chris Lattner1efa40f2006-02-22 00:56:39 +000010253 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +000010254}
Chris Lattnerf76d1802006-07-31 23:26:50 +000010255
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010256std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000010257X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000010258 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000010259 // First, see if this is a constraint that directly corresponds to an LLVM
10260 // register class.
10261 if (Constraint.size() == 1) {
10262 // GCC Constraint Letters
10263 switch (Constraint[0]) {
10264 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000010265 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000010266 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000010267 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +000010268 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010269 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000010270 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010271 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000010272 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000010273 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000010274 case 'R': // LEGACY_REGS
10275 if (VT == MVT::i8)
10276 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
10277 if (VT == MVT::i16)
10278 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
10279 if (VT == MVT::i32 || !Subtarget->is64Bit())
10280 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
10281 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010282 case 'f': // FP Stack registers.
10283 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
10284 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000010285 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010286 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010287 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010288 return std::make_pair(0U, X86::RFP64RegisterClass);
10289 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000010290 case 'y': // MMX_REGS if MMX allowed.
10291 if (!Subtarget->hasMMX()) break;
10292 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000010293 case 'Y': // SSE_REGS if SSE2 allowed
10294 if (!Subtarget->hasSSE2()) break;
10295 // FALL THROUGH.
10296 case 'x': // SSE_REGS if SSE1 allowed
10297 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010298
Owen Anderson825b72b2009-08-11 20:47:22 +000010299 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000010300 default: break;
10301 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000010302 case MVT::f32:
10303 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000010304 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010305 case MVT::f64:
10306 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000010307 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000010308 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000010309 case MVT::v16i8:
10310 case MVT::v8i16:
10311 case MVT::v4i32:
10312 case MVT::v2i64:
10313 case MVT::v4f32:
10314 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000010315 return std::make_pair(0U, X86::VR128RegisterClass);
10316 }
Chris Lattnerad043e82007-04-09 05:11:28 +000010317 break;
10318 }
10319 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010320
Chris Lattnerf76d1802006-07-31 23:26:50 +000010321 // Use the default implementation in TargetLowering to convert the register
10322 // constraint into a member of a register class.
10323 std::pair<unsigned, const TargetRegisterClass*> Res;
10324 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000010325
10326 // Not found as a standard register?
10327 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000010328 // Map st(0) -> st(7) -> ST0
10329 if (Constraint.size() == 7 && Constraint[0] == '{' &&
10330 tolower(Constraint[1]) == 's' &&
10331 tolower(Constraint[2]) == 't' &&
10332 Constraint[3] == '(' &&
10333 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
10334 Constraint[5] == ')' &&
10335 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000010336
Chris Lattner56d77c72009-09-13 22:41:48 +000010337 Res.first = X86::ST0+Constraint[4]-'0';
10338 Res.second = X86::RFP80RegisterClass;
10339 return Res;
10340 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000010341
Chris Lattner56d77c72009-09-13 22:41:48 +000010342 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000010343 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000010344 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000010345 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000010346 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000010347 }
Chris Lattner56d77c72009-09-13 22:41:48 +000010348
10349 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000010350 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000010351 Res.first = X86::EFLAGS;
10352 Res.second = X86::CCRRegisterClass;
10353 return Res;
10354 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000010355
Dale Johannesen330169f2008-11-13 21:52:36 +000010356 // 'A' means EAX + EDX.
10357 if (Constraint == "A") {
10358 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000010359 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000010360 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000010361 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000010362 return Res;
10363 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010364
Chris Lattnerf76d1802006-07-31 23:26:50 +000010365 // Otherwise, check to see if this is a register class of the wrong value
10366 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
10367 // turn into {ax},{dx}.
10368 if (Res.second->hasType(VT))
10369 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010370
Chris Lattnerf76d1802006-07-31 23:26:50 +000010371 // All of the single-register GCC register classes map their values onto
10372 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
10373 // really want an 8-bit or 32-bit register, map to the appropriate register
10374 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000010375 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000010376 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010377 unsigned DestReg = 0;
10378 switch (Res.first) {
10379 default: break;
10380 case X86::AX: DestReg = X86::AL; break;
10381 case X86::DX: DestReg = X86::DL; break;
10382 case X86::CX: DestReg = X86::CL; break;
10383 case X86::BX: DestReg = X86::BL; break;
10384 }
10385 if (DestReg) {
10386 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010387 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010388 }
Owen Anderson825b72b2009-08-11 20:47:22 +000010389 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010390 unsigned DestReg = 0;
10391 switch (Res.first) {
10392 default: break;
10393 case X86::AX: DestReg = X86::EAX; break;
10394 case X86::DX: DestReg = X86::EDX; break;
10395 case X86::CX: DestReg = X86::ECX; break;
10396 case X86::BX: DestReg = X86::EBX; break;
10397 case X86::SI: DestReg = X86::ESI; break;
10398 case X86::DI: DestReg = X86::EDI; break;
10399 case X86::BP: DestReg = X86::EBP; break;
10400 case X86::SP: DestReg = X86::ESP; break;
10401 }
10402 if (DestReg) {
10403 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010404 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010405 }
Owen Anderson825b72b2009-08-11 20:47:22 +000010406 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010407 unsigned DestReg = 0;
10408 switch (Res.first) {
10409 default: break;
10410 case X86::AX: DestReg = X86::RAX; break;
10411 case X86::DX: DestReg = X86::RDX; break;
10412 case X86::CX: DestReg = X86::RCX; break;
10413 case X86::BX: DestReg = X86::RBX; break;
10414 case X86::SI: DestReg = X86::RSI; break;
10415 case X86::DI: DestReg = X86::RDI; break;
10416 case X86::BP: DestReg = X86::RBP; break;
10417 case X86::SP: DestReg = X86::RSP; break;
10418 }
10419 if (DestReg) {
10420 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010421 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010422 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000010423 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000010424 } else if (Res.second == X86::FR32RegisterClass ||
10425 Res.second == X86::FR64RegisterClass ||
10426 Res.second == X86::VR128RegisterClass) {
10427 // Handle references to XMM physical registers that got mapped into the
10428 // wrong class. This can happen with constraints like {xmm0} where the
10429 // target independent register mapper will just pick the first match it can
10430 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000010431 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000010432 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000010433 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000010434 Res.second = X86::FR64RegisterClass;
10435 else if (X86::VR128RegisterClass->hasType(VT))
10436 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000010437 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010438
Chris Lattnerf76d1802006-07-31 23:26:50 +000010439 return Res;
10440}