blob: 9fa7bb1d3cc807010ca5718d801e1b3d74dab9b3 [file] [log] [blame]
Arnold Schwaighofer92226dd2007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Evan Chengb1712452010-01-27 06:25:16 +000015#define DEBUG_TYPE "x86-isel"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000016#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000017#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000018#include "X86ISelLowering.h"
19#include "X86TargetMachine.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000020#include "X86TargetObjectFile.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000021#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000022#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000023#include "llvm/DerivedTypes.h"
Chris Lattnerb903bed2009-06-26 21:20:29 +000024#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000025#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000026#include "llvm/Function.h"
Chris Lattnerb8105652009-07-20 17:51:36 +000027#include "llvm/Instructions.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000028#include "llvm/Intrinsics.h"
Owen Andersona90b3dc2009-07-15 21:51:10 +000029#include "llvm/LLVMContext.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000030#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000031#include "llvm/CodeGen/MachineFunction.h"
32#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner5e1df8d2010-01-25 23:38:14 +000033#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000034#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000035#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000036#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner589c6f62010-01-26 06:28:43 +000037#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000038#include "llvm/MC/MCContext.h"
Daniel Dunbar4e815f82010-03-15 23:51:06 +000039#include "llvm/MC/MCExpr.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000040#include "llvm/MC/MCSymbol.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000041#include "llvm/ADT/BitVector.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000042#include "llvm/ADT/SmallSet.h"
Evan Chengb1712452010-01-27 06:25:16 +000043#include "llvm/ADT/Statistic.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000044#include "llvm/ADT/StringExtras.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000045#include "llvm/ADT/VectorExtras.h"
Mon P Wang3c81d352008-11-23 04:37:22 +000046#include "llvm/Support/CommandLine.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000047#include "llvm/Support/Debug.h"
Bill Wendlingec041eb2010-03-12 19:20:40 +000048#include "llvm/Support/Dwarf.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000049#include "llvm/Support/ErrorHandling.h"
50#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000051#include "llvm/Support/raw_ostream.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000052using namespace llvm;
Bill Wendlingec041eb2010-03-12 19:20:40 +000053using namespace dwarf;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000054
Evan Chengb1712452010-01-27 06:25:16 +000055STATISTIC(NumTailCalls, "Number of tail calls");
56
Mon P Wang3c81d352008-11-23 04:37:22 +000057static cl::opt<bool>
Mon P Wang9f22a4a2008-11-24 02:10:43 +000058DisableMMX("disable-mmx", cl::Hidden, cl::desc("Disable use of MMX"));
Mon P Wang3c81d352008-11-23 04:37:22 +000059
Evan Cheng10e86422008-04-25 19:11:04 +000060// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000061static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000062 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000063
Chris Lattnerf0144122009-07-28 03:13:23 +000064static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
65 switch (TM.getSubtarget<X86Subtarget>().TargetType) {
66 default: llvm_unreachable("unknown subtarget type");
67 case X86Subtarget::isDarwin:
Bill Wendling757e75b2010-03-15 19:04:37 +000068 if (TM.getSubtarget<X86Subtarget>().is64Bit())
69 return new X8664_MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +000070 return new TargetLoweringObjectFileMachO();
Chris Lattnerf0144122009-07-28 03:13:23 +000071 case X86Subtarget::isELF:
Anton Korobeynikov9184b252010-02-15 22:35:59 +000072 if (TM.getSubtarget<X86Subtarget>().is64Bit())
73 return new X8664_ELFTargetObjectFile(TM);
74 return new X8632_ELFTargetObjectFile(TM);
Chris Lattnerf0144122009-07-28 03:13:23 +000075 case X86Subtarget::isMingw:
76 case X86Subtarget::isCygwin:
77 case X86Subtarget::isWindows:
78 return new TargetLoweringObjectFileCOFF();
79 }
Chris Lattnerf0144122009-07-28 03:13:23 +000080}
81
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +000082X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +000083 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +000084 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesenf1fc3a82007-09-23 14:52:20 +000085 X86ScalarSSEf64 = Subtarget->hasSSE2();
86 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng25ab6902006-09-08 06:48:29 +000087 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000088
Anton Korobeynikov2365f512007-07-14 14:06:15 +000089 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000090 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000091
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000092 // Set up the TargetLowering object.
93
94 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Owen Anderson825b72b2009-08-11 20:47:22 +000095 setShiftAmountType(MVT::i8);
Duncan Sands03228082008-11-23 15:47:28 +000096 setBooleanContents(ZeroOrOneBooleanContent);
Evan Cheng0b2afbd2006-01-25 09:15:17 +000097 setSchedulingPreference(SchedulingForRegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +000098 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +000099
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000100 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000101 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000102 setUseUnderscoreSetJmp(false);
103 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000104 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000105 // MS runtime is weird: it exports _setjmp, but longjmp!
106 setUseUnderscoreSetJmp(true);
107 setUseUnderscoreLongJmp(false);
108 } else {
109 setUseUnderscoreSetJmp(true);
110 setUseUnderscoreLongJmp(true);
111 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000112
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000113 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000114 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman71edb242010-04-30 18:30:26 +0000115 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000116 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000117 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000118 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000119
Owen Anderson825b72b2009-08-11 20:47:22 +0000120 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000121
Scott Michelfdc40a02009-02-17 22:15:04 +0000122 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000123 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000124 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000125 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000126 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000127 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
128 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000129
130 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000131 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
132 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
133 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
134 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
135 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
136 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000137
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000138 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
139 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000140 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
141 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
142 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000143
Evan Cheng25ab6902006-09-08 06:48:29 +0000144 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000145 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
146 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000147 } else if (!UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000148 // We have an algorithm for SSE2->double, and we turn this into a
149 // 64-bit FILD followed by conditional FADD for other targets.
150 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000151 // We have an algorithm for SSE2, and we turn this into a 64-bit
152 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000153 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000154 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000155
156 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
157 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000158 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
159 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000160
Devang Patel6a784892009-06-05 18:48:29 +0000161 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000162 // SSE has no i16 to fp conversion, only i32
163 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000164 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000165 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000166 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000167 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000168 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
169 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000170 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000171 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000172 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
173 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000174 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000175
Dale Johannesen73328d12007-09-19 23:55:34 +0000176 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
177 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000178 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
179 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000180
Evan Cheng02568ff2006-01-30 22:13:22 +0000181 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
182 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000183 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
184 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000185
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000186 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000187 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000188 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000189 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000190 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000191 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
192 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000193 }
194
195 // Handle FP_TO_UINT by promoting the destination to a larger signed
196 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000197 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
198 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
199 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000200
Evan Cheng25ab6902006-09-08 06:48:29 +0000201 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000202 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
203 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000204 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000205 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000206 // Expand FP_TO_UINT into a select.
207 // FIXME: We would like to use a Custom expander here eventually to do
208 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000209 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000210 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000211 // With SSE3 we can use fisttpll to convert to a signed i64; without
212 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000213 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000214 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000215
Chris Lattner399610a2006-12-05 18:22:22 +0000216 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000217 if (!X86ScalarSSEf64) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000218 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
219 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
Chris Lattnerf3597a12006-12-05 18:45:06 +0000220 }
Chris Lattner21f66852005-12-23 05:15:23 +0000221
Dan Gohmanb00ee212008-02-18 19:34:53 +0000222 // Scalar integer divide and remainder are lowered to use operations that
223 // produce two results, to match the available instructions. This exposes
224 // the two-result form to trivial CSE, which is able to combine x/y and x%y
225 // into a single instruction.
226 //
227 // Scalar integer multiply-high is also lowered to use two-result
228 // operations, to match the available instructions. However, plain multiply
229 // (low) operations are left as Legal, as there are single-result
230 // instructions for this in x86. Using the two-result multiply instructions
231 // when both high and low results are needed must be arranged by dagcombine.
Owen Anderson825b72b2009-08-11 20:47:22 +0000232 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
233 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
234 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
235 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
236 setOperationAction(ISD::SREM , MVT::i8 , Expand);
237 setOperationAction(ISD::UREM , MVT::i8 , Expand);
238 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
239 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
240 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
241 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
242 setOperationAction(ISD::SREM , MVT::i16 , Expand);
243 setOperationAction(ISD::UREM , MVT::i16 , Expand);
244 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
245 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
246 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
247 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
248 setOperationAction(ISD::SREM , MVT::i32 , Expand);
249 setOperationAction(ISD::UREM , MVT::i32 , Expand);
250 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
251 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
252 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
253 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
254 setOperationAction(ISD::SREM , MVT::i64 , Expand);
255 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohmana37c9f72007-09-25 18:23:27 +0000256
Owen Anderson825b72b2009-08-11 20:47:22 +0000257 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
258 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
259 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
260 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000261 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000262 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
263 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
264 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
265 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
266 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
267 setOperationAction(ISD::FREM , MVT::f32 , Expand);
268 setOperationAction(ISD::FREM , MVT::f64 , Expand);
269 setOperationAction(ISD::FREM , MVT::f80 , Expand);
270 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000271
Owen Anderson825b72b2009-08-11 20:47:22 +0000272 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
273 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
274 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
275 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000276 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
277 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000278 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
279 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
280 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000281 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000282 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
283 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
284 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000285 }
286
Owen Anderson825b72b2009-08-11 20:47:22 +0000287 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
288 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000289
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000290 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000291 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000292 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000293 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000294 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000295 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
296 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
297 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
298 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
299 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000300 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000301 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
302 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
303 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
304 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000305 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000306 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
307 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000308 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000309 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000310
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000311 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000312 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
313 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
314 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
315 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000316 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000317 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
318 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000319 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000320 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000321 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
322 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
323 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
324 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000325 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000326 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000327 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000328 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
329 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
330 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000331 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000332 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
333 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
334 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000335 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000336
Evan Chengd2cde682008-03-10 19:38:10 +0000337 if (Subtarget->hasSSE1())
Owen Anderson825b72b2009-08-11 20:47:22 +0000338 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000339
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000340 if (!Subtarget->hasSSE2())
Owen Anderson825b72b2009-08-11 20:47:22 +0000341 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000342
Mon P Wang63307c32008-05-05 19:05:59 +0000343 // Expand certain atomics
Owen Anderson825b72b2009-08-11 20:47:22 +0000344 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Custom);
345 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Custom);
346 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
347 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Bill Wendling5bf1b4e2008-08-20 00:28:16 +0000348
Owen Anderson825b72b2009-08-11 20:47:22 +0000349 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Custom);
350 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Custom);
351 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
352 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000353
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000354 if (!Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000355 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
356 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
357 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
358 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
359 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
360 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
361 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000362 }
363
Evan Cheng3c992d22006-03-07 02:02:57 +0000364 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000365 if (!Subtarget->isTargetDarwin() &&
366 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000367 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000368 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000369 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000370
Owen Anderson825b72b2009-08-11 20:47:22 +0000371 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
372 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
373 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
374 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000375 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000376 setExceptionPointerRegister(X86::RAX);
377 setExceptionSelectorRegister(X86::RDX);
378 } else {
379 setExceptionPointerRegister(X86::EAX);
380 setExceptionSelectorRegister(X86::EDX);
381 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000382 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
383 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000384
Owen Anderson825b72b2009-08-11 20:47:22 +0000385 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000386
Owen Anderson825b72b2009-08-11 20:47:22 +0000387 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000388
Nate Begemanacc398c2006-01-25 18:21:52 +0000389 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000390 setOperationAction(ISD::VASTART , MVT::Other, Custom);
391 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000392 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000393 setOperationAction(ISD::VAARG , MVT::Other, Custom);
394 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000395 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000396 setOperationAction(ISD::VAARG , MVT::Other, Expand);
397 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000398 }
Evan Chengae642192007-03-02 23:16:35 +0000399
Owen Anderson825b72b2009-08-11 20:47:22 +0000400 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
401 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000402 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000403 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000404 if (Subtarget->isTargetCygMing())
Owen Anderson825b72b2009-08-11 20:47:22 +0000405 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000406 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000407 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000408
Evan Chengc7ce29b2009-02-13 22:36:38 +0000409 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000410 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000411 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000412 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
413 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000414
Evan Cheng223547a2006-01-31 22:28:30 +0000415 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000416 setOperationAction(ISD::FABS , MVT::f64, Custom);
417 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000418
419 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000420 setOperationAction(ISD::FNEG , MVT::f64, Custom);
421 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000422
Evan Cheng68c47cb2007-01-05 07:55:56 +0000423 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000424 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
425 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000426
Evan Chengd25e9e82006-02-02 00:28:23 +0000427 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000428 setOperationAction(ISD::FSIN , MVT::f64, Expand);
429 setOperationAction(ISD::FCOS , MVT::f64, Expand);
430 setOperationAction(ISD::FSIN , MVT::f32, Expand);
431 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000432
Chris Lattnera54aa942006-01-29 06:26:08 +0000433 // Expand FP immediates into loads from the stack, except for the special
434 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000435 addLegalFPImmediate(APFloat(+0.0)); // xorpd
436 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000437 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000438 // Use SSE for f32, x87 for f64.
439 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000440 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
441 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000442
443 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000444 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000445
446 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000447 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000448
Owen Anderson825b72b2009-08-11 20:47:22 +0000449 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000450
451 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000452 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
453 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000454
455 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000456 setOperationAction(ISD::FSIN , MVT::f32, Expand);
457 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000458
Nate Begemane1795842008-02-14 08:57:00 +0000459 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000460 addLegalFPImmediate(APFloat(+0.0f)); // xorps
461 addLegalFPImmediate(APFloat(+0.0)); // FLD0
462 addLegalFPImmediate(APFloat(+1.0)); // FLD1
463 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
464 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
465
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000466 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000467 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
468 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000469 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000470 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000471 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000472 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000473 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
474 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000475
Owen Anderson825b72b2009-08-11 20:47:22 +0000476 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
477 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
478 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
479 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000480
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000481 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000482 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
483 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000484 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000485 addLegalFPImmediate(APFloat(+0.0)); // FLD0
486 addLegalFPImmediate(APFloat(+1.0)); // FLD1
487 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
488 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000489 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
490 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
491 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
492 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000493 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000494
Dale Johannesen59a58732007-08-05 18:49:15 +0000495 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000496 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000497 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
498 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
499 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000500 {
501 bool ignored;
502 APFloat TmpFlt(+0.0);
503 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
504 &ignored);
505 addLegalFPImmediate(TmpFlt); // FLD0
506 TmpFlt.changeSign();
507 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
508 APFloat TmpFlt2(+1.0);
509 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
510 &ignored);
511 addLegalFPImmediate(TmpFlt2); // FLD1
512 TmpFlt2.changeSign();
513 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
514 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000515
Evan Chengc7ce29b2009-02-13 22:36:38 +0000516 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000517 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
518 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000519 }
Dale Johannesen2f429012007-09-26 21:10:55 +0000520 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000521
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000522 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000523 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
524 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
525 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000526
Owen Anderson825b72b2009-08-11 20:47:22 +0000527 setOperationAction(ISD::FLOG, MVT::f80, Expand);
528 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
529 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
530 setOperationAction(ISD::FEXP, MVT::f80, Expand);
531 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000532
Mon P Wangf007a8b2008-11-06 05:31:54 +0000533 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000534 // (for widening) or expand (for scalarization). Then we will selectively
535 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000536 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
537 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
538 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
539 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
540 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
541 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
542 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
543 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
544 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
545 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
546 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
547 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
548 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
549 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
550 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
551 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
552 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
553 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
554 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
555 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
556 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
557 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
558 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
559 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
560 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
561 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
562 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
563 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
564 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
565 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
566 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
567 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
568 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
569 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
570 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
571 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
572 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
573 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
574 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
575 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
576 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
577 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
578 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
579 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
580 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
581 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
582 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
583 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
584 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
585 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000586 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000587 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
588 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
589 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
590 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
591 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
592 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
593 setTruncStoreAction((MVT::SimpleValueType)VT,
594 (MVT::SimpleValueType)InnerVT, Expand);
595 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
596 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
597 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000598 }
599
Evan Chengc7ce29b2009-02-13 22:36:38 +0000600 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
601 // with -msoft-float, disable use of MMX as well.
Evan Cheng92722532009-03-26 23:06:32 +0000602 if (!UseSoftFloat && !DisableMMX && Subtarget->hasMMX()) {
Dale Johannesen76090172010-04-20 22:34:09 +0000603 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass, false);
604 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass, false);
605 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass, false);
606 addRegisterClass(MVT::v2f32, X86::VR64RegisterClass, false);
607 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass, false);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000608
Owen Anderson825b72b2009-08-11 20:47:22 +0000609 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
610 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
611 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
612 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000613
Owen Anderson825b72b2009-08-11 20:47:22 +0000614 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
615 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
616 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
617 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000618
Owen Anderson825b72b2009-08-11 20:47:22 +0000619 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
620 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
Bill Wendling74027e92007-03-15 21:24:36 +0000621
Owen Anderson825b72b2009-08-11 20:47:22 +0000622 setOperationAction(ISD::AND, MVT::v8i8, Promote);
623 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
624 setOperationAction(ISD::AND, MVT::v4i16, Promote);
625 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
626 setOperationAction(ISD::AND, MVT::v2i32, Promote);
627 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
628 setOperationAction(ISD::AND, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000629
Owen Anderson825b72b2009-08-11 20:47:22 +0000630 setOperationAction(ISD::OR, MVT::v8i8, Promote);
631 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
632 setOperationAction(ISD::OR, MVT::v4i16, Promote);
633 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
634 setOperationAction(ISD::OR, MVT::v2i32, Promote);
635 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
636 setOperationAction(ISD::OR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000637
Owen Anderson825b72b2009-08-11 20:47:22 +0000638 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
639 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
640 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
641 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
642 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
643 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
644 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000645
Owen Anderson825b72b2009-08-11 20:47:22 +0000646 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
647 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
648 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
649 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
650 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
651 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
652 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
653 AddPromotedToType (ISD::LOAD, MVT::v2f32, MVT::v1i64);
654 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000655
Owen Anderson825b72b2009-08-11 20:47:22 +0000656 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
657 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
658 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
659 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom);
660 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
Bill Wendlinga348c562007-03-22 18:42:45 +0000661
Owen Anderson825b72b2009-08-11 20:47:22 +0000662 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
663 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
664 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
665 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000666
Owen Anderson825b72b2009-08-11 20:47:22 +0000667 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f32, Custom);
668 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
669 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
670 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Bill Wendling3180e202008-07-20 02:32:23 +0000671
Owen Anderson825b72b2009-08-11 20:47:22 +0000672 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
Mon P Wang9e5ecb82008-12-12 01:25:51 +0000673
Owen Anderson825b72b2009-08-11 20:47:22 +0000674 setOperationAction(ISD::SELECT, MVT::v8i8, Promote);
675 setOperationAction(ISD::SELECT, MVT::v4i16, Promote);
676 setOperationAction(ISD::SELECT, MVT::v2i32, Promote);
677 setOperationAction(ISD::SELECT, MVT::v1i64, Custom);
678 setOperationAction(ISD::VSETCC, MVT::v8i8, Custom);
679 setOperationAction(ISD::VSETCC, MVT::v4i16, Custom);
680 setOperationAction(ISD::VSETCC, MVT::v2i32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000681 }
682
Evan Cheng92722532009-03-26 23:06:32 +0000683 if (!UseSoftFloat && Subtarget->hasSSE1()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000684 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000685
Owen Anderson825b72b2009-08-11 20:47:22 +0000686 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
687 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
688 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
689 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
690 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
691 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
692 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
693 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
694 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
695 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
696 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
697 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000698 }
699
Evan Cheng92722532009-03-26 23:06:32 +0000700 if (!UseSoftFloat && Subtarget->hasSSE2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000701 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000702
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000703 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
704 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000705 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
706 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
707 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
708 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000709
Owen Anderson825b72b2009-08-11 20:47:22 +0000710 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
711 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
712 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
713 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
714 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
715 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
716 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
717 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
718 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
719 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
720 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
721 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
722 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
723 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
724 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
725 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000726
Owen Anderson825b72b2009-08-11 20:47:22 +0000727 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
728 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
729 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
730 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000731
Owen Anderson825b72b2009-08-11 20:47:22 +0000732 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
733 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
734 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
735 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
736 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000737
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000738 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
739 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
740 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
741 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
742 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
743
Evan Cheng2c3ae372006-04-12 21:21:57 +0000744 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000745 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
746 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000747 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000748 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000749 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000750 // Do not attempt to custom lower non-128-bit vectors
751 if (!VT.is128BitVector())
752 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000753 setOperationAction(ISD::BUILD_VECTOR,
754 VT.getSimpleVT().SimpleTy, Custom);
755 setOperationAction(ISD::VECTOR_SHUFFLE,
756 VT.getSimpleVT().SimpleTy, Custom);
757 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
758 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000759 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000760
Owen Anderson825b72b2009-08-11 20:47:22 +0000761 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
762 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
763 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
764 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
765 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
766 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000767
Nate Begemancdd1eec2008-02-12 22:51:28 +0000768 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000769 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
770 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000771 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000772
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000773 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000774 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
775 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000776 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000777
778 // Do not attempt to promote non-128-bit vectors
779 if (!VT.is128BitVector()) {
780 continue;
781 }
Eric Christopher4bd24c22010-03-30 01:04:59 +0000782
Owen Andersond6662ad2009-08-10 20:46:15 +0000783 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000784 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000785 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000786 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000787 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000788 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000789 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000790 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000791 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000792 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000793 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000794
Owen Anderson825b72b2009-08-11 20:47:22 +0000795 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000796
Evan Cheng2c3ae372006-04-12 21:21:57 +0000797 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000798 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
799 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
800 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
801 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000802
Owen Anderson825b72b2009-08-11 20:47:22 +0000803 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
804 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Eli Friedman23ef1052009-06-06 03:57:58 +0000805 if (!DisableMMX && Subtarget->hasMMX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000806 setOperationAction(ISD::FP_TO_SINT, MVT::v2i32, Custom);
807 setOperationAction(ISD::SINT_TO_FP, MVT::v2i32, Custom);
Eli Friedman23ef1052009-06-06 03:57:58 +0000808 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000809 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000810
Nate Begeman14d12ca2008-02-11 04:19:36 +0000811 if (Subtarget->hasSSE41()) {
812 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000813 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000814
815 // i8 and i16 vectors are custom , because the source register and source
816 // source memory operand types are not the same width. f32 vectors are
817 // custom since the immediate controlling the insert encodes additional
818 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000819 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
820 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
821 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
822 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000823
Owen Anderson825b72b2009-08-11 20:47:22 +0000824 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
825 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
826 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
827 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000828
829 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000830 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
831 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000832 }
833 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000834
Nate Begeman30a0de92008-07-17 16:51:19 +0000835 if (Subtarget->hasSSE42()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000836 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
Nate Begeman30a0de92008-07-17 16:51:19 +0000837 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000838
David Greene9b9838d2009-06-29 16:47:10 +0000839 if (!UseSoftFloat && Subtarget->hasAVX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000840 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
841 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
842 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
843 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000844
Owen Anderson825b72b2009-08-11 20:47:22 +0000845 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
846 setOperationAction(ISD::LOAD, MVT::v8i32, Legal);
847 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
848 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
849 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
850 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
851 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
852 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
853 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
854 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
855 //setOperationAction(ISD::BUILD_VECTOR, MVT::v8f32, Custom);
856 //setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Custom);
857 //setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8f32, Custom);
858 //setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
859 //setOperationAction(ISD::VSETCC, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000860
861 // Operations to consider commented out -v16i16 v32i8
Owen Anderson825b72b2009-08-11 20:47:22 +0000862 //setOperationAction(ISD::ADD, MVT::v16i16, Legal);
863 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
864 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
865 //setOperationAction(ISD::SUB, MVT::v32i8, Legal);
866 //setOperationAction(ISD::SUB, MVT::v16i16, Legal);
867 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
868 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
869 //setOperationAction(ISD::MUL, MVT::v16i16, Legal);
870 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
871 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
872 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
873 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
874 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
875 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000876
Owen Anderson825b72b2009-08-11 20:47:22 +0000877 setOperationAction(ISD::VSETCC, MVT::v4f64, Custom);
878 // setOperationAction(ISD::VSETCC, MVT::v32i8, Custom);
879 // setOperationAction(ISD::VSETCC, MVT::v16i16, Custom);
880 setOperationAction(ISD::VSETCC, MVT::v8i32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000881
Owen Anderson825b72b2009-08-11 20:47:22 +0000882 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v32i8, Custom);
883 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i16, Custom);
884 // setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i16, Custom);
885 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i32, Custom);
886 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000887
Owen Anderson825b72b2009-08-11 20:47:22 +0000888 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f64, Custom);
889 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i64, Custom);
890 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f64, Custom);
891 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i64, Custom);
892 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f64, Custom);
893 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000894
895#if 0
896 // Not sure we want to do this since there are no 256-bit integer
897 // operations in AVX
898
899 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
900 // This includes 256-bit vectors
Owen Anderson825b72b2009-08-11 20:47:22 +0000901 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; ++i) {
902 EVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000903
904 // Do not attempt to custom lower non-power-of-2 vectors
905 if (!isPowerOf2_32(VT.getVectorNumElements()))
906 continue;
907
908 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
909 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
910 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
911 }
912
913 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000914 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i64, Custom);
915 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i64, Custom);
Eric Christopherfd179292009-08-27 18:07:15 +0000916 }
David Greene9b9838d2009-06-29 16:47:10 +0000917#endif
918
919#if 0
920 // Not sure we want to do this since there are no 256-bit integer
921 // operations in AVX
922
923 // Promote v32i8, v16i16, v8i32 load, select, and, or, xor to v4i64.
924 // Including 256-bit vectors
Owen Anderson825b72b2009-08-11 20:47:22 +0000925 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; i++) {
926 EVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000927
928 if (!VT.is256BitVector()) {
929 continue;
930 }
931 setOperationAction(ISD::AND, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000932 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000933 setOperationAction(ISD::OR, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000934 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000935 setOperationAction(ISD::XOR, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000936 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000937 setOperationAction(ISD::LOAD, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000938 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000939 setOperationAction(ISD::SELECT, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000940 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000941 }
942
Owen Anderson825b72b2009-08-11 20:47:22 +0000943 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
David Greene9b9838d2009-06-29 16:47:10 +0000944#endif
945 }
946
Evan Cheng6be2c582006-04-05 23:38:46 +0000947 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000948 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +0000949
Bill Wendling74c37652008-12-09 22:08:41 +0000950 // Add/Sub/Mul with overflow operations are custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000951 setOperationAction(ISD::SADDO, MVT::i32, Custom);
952 setOperationAction(ISD::SADDO, MVT::i64, Custom);
953 setOperationAction(ISD::UADDO, MVT::i32, Custom);
954 setOperationAction(ISD::UADDO, MVT::i64, Custom);
955 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
956 setOperationAction(ISD::SSUBO, MVT::i64, Custom);
957 setOperationAction(ISD::USUBO, MVT::i32, Custom);
958 setOperationAction(ISD::USUBO, MVT::i64, Custom);
959 setOperationAction(ISD::SMULO, MVT::i32, Custom);
960 setOperationAction(ISD::SMULO, MVT::i64, Custom);
Bill Wendling41ea7e72008-11-24 19:21:46 +0000961
Evan Chengd54f2d52009-03-31 19:38:51 +0000962 if (!Subtarget->is64Bit()) {
963 // These libcalls are not available in 32-bit.
964 setLibcallName(RTLIB::SHL_I128, 0);
965 setLibcallName(RTLIB::SRL_I128, 0);
966 setLibcallName(RTLIB::SRA_I128, 0);
967 }
968
Evan Cheng206ee9d2006-07-07 08:33:52 +0000969 // We have target-specific dag combine patterns for the following nodes:
970 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +0000971 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +0000972 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +0000973 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +0000974 setTargetDAGCombine(ISD::SHL);
975 setTargetDAGCombine(ISD::SRA);
976 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +0000977 setTargetDAGCombine(ISD::OR);
Chris Lattner149a4e52008-02-22 02:09:43 +0000978 setTargetDAGCombine(ISD::STORE);
Owen Anderson99177002009-06-29 18:04:45 +0000979 setTargetDAGCombine(ISD::MEMBARRIER);
Evan Cheng2e489c42009-12-16 00:53:11 +0000980 setTargetDAGCombine(ISD::ZERO_EXTEND);
Evan Cheng0b0cd912009-03-28 05:57:29 +0000981 if (Subtarget->is64Bit())
982 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +0000983
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000984 computeRegisterProperties();
985
Evan Cheng87ed7162006-02-14 08:25:08 +0000986 // FIXME: These should be based on subtarget info. Plus, the values should
987 // be smaller when we are in optimizing for size mode.
Dan Gohman87060f52008-06-30 21:00:56 +0000988 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng255f20f2010-04-01 06:04:33 +0000989 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Dan Gohman87060f52008-06-30 21:00:56 +0000990 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Evan Chengfb8075d2008-02-28 00:43:03 +0000991 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +0000992 benefitFromCodePlacementOpt = true;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000993}
994
Scott Michel5b8f82e2008-03-10 15:42:14 +0000995
Owen Anderson825b72b2009-08-11 20:47:22 +0000996MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
997 return MVT::i8;
Scott Michel5b8f82e2008-03-10 15:42:14 +0000998}
999
1000
Evan Cheng29286502008-01-23 23:17:41 +00001001/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1002/// the desired ByVal argument alignment.
1003static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
1004 if (MaxAlign == 16)
1005 return;
1006 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
1007 if (VTy->getBitWidth() == 128)
1008 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +00001009 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1010 unsigned EltAlign = 0;
1011 getMaxByValAlign(ATy->getElementType(), EltAlign);
1012 if (EltAlign > MaxAlign)
1013 MaxAlign = EltAlign;
1014 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1015 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1016 unsigned EltAlign = 0;
1017 getMaxByValAlign(STy->getElementType(i), EltAlign);
1018 if (EltAlign > MaxAlign)
1019 MaxAlign = EltAlign;
1020 if (MaxAlign == 16)
1021 break;
1022 }
1023 }
1024 return;
1025}
1026
1027/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1028/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001029/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1030/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +00001031unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001032 if (Subtarget->is64Bit()) {
1033 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001034 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001035 if (TyAlign > 8)
1036 return TyAlign;
1037 return 8;
1038 }
1039
Evan Cheng29286502008-01-23 23:17:41 +00001040 unsigned Align = 4;
Dale Johannesen0c191872008-02-08 19:48:20 +00001041 if (Subtarget->hasSSE1())
1042 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001043 return Align;
1044}
Chris Lattner2b02a442007-02-25 08:29:00 +00001045
Evan Chengf0df0312008-05-15 08:39:06 +00001046/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001047/// and store operations as a result of memset, memcpy, and memmove
1048/// lowering. If DstAlign is zero that means it's safe to destination
1049/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1050/// means there isn't a need to check it against alignment requirement,
1051/// probably because the source does not need to be loaded. If
1052/// 'NonScalarIntSafe' is true, that means it's safe to return a
1053/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1054/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1055/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001056/// It returns EVT::Other if the type should be determined using generic
1057/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001058EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001059X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1060 unsigned DstAlign, unsigned SrcAlign,
Evan Chengf28f8bc2010-04-02 19:36:14 +00001061 bool NonScalarIntSafe,
Evan Chengc3b0c342010-04-08 07:37:57 +00001062 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001063 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001064 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1065 // linux. This is because the stack realignment code can't handle certain
1066 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001067 const Function *F = MF.getFunction();
Evan Chengf28f8bc2010-04-02 19:36:14 +00001068 if (NonScalarIntSafe &&
1069 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001070 if (Size >= 16 &&
1071 (Subtarget->isUnalignedMemAccessFast() ||
Chandler Carruthae1d41c2010-04-02 01:31:24 +00001072 ((DstAlign == 0 || DstAlign >= 16) &&
1073 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001074 Subtarget->getStackAlignment() >= 16) {
1075 if (Subtarget->hasSSE2())
1076 return MVT::v4i32;
Evan Chengf28f8bc2010-04-02 19:36:14 +00001077 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001078 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001079 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001080 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001081 Subtarget->getStackAlignment() >= 8 &&
Evan Chengc3b0c342010-04-08 07:37:57 +00001082 Subtarget->hasSSE2()) {
1083 // Do not use f64 to lower memcpy if source is string constant. It's
1084 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001085 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001086 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001087 }
Evan Chengf0df0312008-05-15 08:39:06 +00001088 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001089 return MVT::i64;
1090 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001091}
1092
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001093/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1094/// current function. The returned value is a member of the
1095/// MachineJumpTableInfo::JTEntryKind enum.
1096unsigned X86TargetLowering::getJumpTableEncoding() const {
1097 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1098 // symbol.
1099 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1100 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001101 return MachineJumpTableInfo::EK_Custom32;
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001102
1103 // Otherwise, use the normal jump table encoding heuristics.
1104 return TargetLowering::getJumpTableEncoding();
1105}
1106
Chris Lattner589c6f62010-01-26 06:28:43 +00001107/// getPICBaseSymbol - Return the X86-32 PIC base.
1108MCSymbol *
1109X86TargetLowering::getPICBaseSymbol(const MachineFunction *MF,
1110 MCContext &Ctx) const {
1111 const MCAsmInfo &MAI = *getTargetMachine().getMCAsmInfo();
Chris Lattner9b97a732010-03-30 18:10:53 +00001112 return Ctx.GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix())+
1113 Twine(MF->getFunctionNumber())+"$pb");
Chris Lattner589c6f62010-01-26 06:28:43 +00001114}
1115
1116
Chris Lattnerc64daab2010-01-26 05:02:42 +00001117const MCExpr *
1118X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1119 const MachineBasicBlock *MBB,
1120 unsigned uid,MCContext &Ctx) const{
1121 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1122 Subtarget->isPICStyleGOT());
1123 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1124 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001125 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1126 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001127}
1128
Evan Chengcc415862007-11-09 01:32:10 +00001129/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1130/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001131SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001132 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001133 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001134 // This doesn't have DebugLoc associated with it, but is not really the
1135 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001136 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001137 return Table;
1138}
1139
Chris Lattner589c6f62010-01-26 06:28:43 +00001140/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1141/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1142/// MCExpr.
1143const MCExpr *X86TargetLowering::
1144getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1145 MCContext &Ctx) const {
1146 // X86-64 uses RIP relative addressing based on the jump table label.
1147 if (Subtarget->isPICStyleRIPRel())
1148 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1149
1150 // Otherwise, the reference is relative to the PIC base.
1151 return MCSymbolRefExpr::Create(getPICBaseSymbol(MF, Ctx), Ctx);
1152}
1153
Bill Wendlingb4202b82009-07-01 18:50:55 +00001154/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +00001155unsigned X86TargetLowering::getFunctionAlignment(const Function *F) const {
Dan Gohman25103a22009-08-18 00:20:06 +00001156 return F->hasFnAttr(Attribute::OptimizeForSize) ? 0 : 4;
Bill Wendling20c568f2009-06-30 22:38:32 +00001157}
1158
Chris Lattner2b02a442007-02-25 08:29:00 +00001159//===----------------------------------------------------------------------===//
1160// Return Value Calling Convention Implementation
1161//===----------------------------------------------------------------------===//
1162
Chris Lattner59ed56b2007-02-28 04:55:35 +00001163#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001164
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001165bool
1166X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv, bool isVarArg,
1167 const SmallVectorImpl<EVT> &OutTys,
1168 const SmallVectorImpl<ISD::ArgFlagsTy> &ArgsFlags,
Dan Gohmand858e902010-04-17 15:26:15 +00001169 SelectionDAG &DAG) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001170 SmallVector<CCValAssign, 16> RVLocs;
1171 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1172 RVLocs, *DAG.getContext());
1173 return CCInfo.CheckReturn(OutTys, ArgsFlags, RetCC_X86);
1174}
1175
Dan Gohman98ca4f22009-08-05 01:29:28 +00001176SDValue
1177X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001178 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001179 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmand858e902010-04-17 15:26:15 +00001180 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001181 MachineFunction &MF = DAG.getMachineFunction();
1182 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001183
Chris Lattner9774c912007-02-27 05:28:59 +00001184 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001185 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1186 RVLocs, *DAG.getContext());
1187 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001188
Evan Chengdcea1632010-02-04 02:40:39 +00001189 // Add the regs to the liveout set for the function.
1190 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1191 for (unsigned i = 0; i != RVLocs.size(); ++i)
1192 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1193 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001194
Dan Gohman475871a2008-07-27 21:46:04 +00001195 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001196
Dan Gohman475871a2008-07-27 21:46:04 +00001197 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001198 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1199 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001200 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1201 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001202
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001203 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001204 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1205 CCValAssign &VA = RVLocs[i];
1206 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman98ca4f22009-08-05 01:29:28 +00001207 SDValue ValToCopy = Outs[i].Val;
Scott Michelfdc40a02009-02-17 22:15:04 +00001208
Chris Lattner447ff682008-03-11 03:23:40 +00001209 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1210 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001211 if (VA.getLocReg() == X86::ST0 ||
1212 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001213 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1214 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001215 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001216 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001217 RetOps.push_back(ValToCopy);
1218 // Don't emit a copytoreg.
1219 continue;
1220 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001221
Evan Cheng242b38b2009-02-23 09:03:22 +00001222 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1223 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001224 if (Subtarget->is64Bit()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001225 EVT ValVT = ValToCopy.getValueType();
Evan Cheng242b38b2009-02-23 09:03:22 +00001226 if (ValVT.isVector() && ValVT.getSizeInBits() == 64) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001227 ValToCopy = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, ValToCopy);
Evan Cheng242b38b2009-02-23 09:03:22 +00001228 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1)
Owen Anderson825b72b2009-08-11 20:47:22 +00001229 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, ValToCopy);
Evan Cheng242b38b2009-02-23 09:03:22 +00001230 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001231 }
1232
Dale Johannesendd64c412009-02-04 00:33:20 +00001233 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001234 Flag = Chain.getValue(1);
1235 }
Dan Gohman61a92132008-04-21 23:59:07 +00001236
1237 // The x86-64 ABI for returning structs by value requires that we copy
1238 // the sret argument into %rax for the return. We saved the argument into
1239 // a virtual register in the entry block, so now we copy the value out
1240 // and into %rax.
1241 if (Subtarget->is64Bit() &&
1242 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1243 MachineFunction &MF = DAG.getMachineFunction();
1244 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1245 unsigned Reg = FuncInfo->getSRetReturnReg();
1246 if (!Reg) {
Evan Chengdcea1632010-02-04 02:40:39 +00001247 Reg = MRI.createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001248 FuncInfo->setSRetReturnReg(Reg);
1249 }
Dale Johannesendd64c412009-02-04 00:33:20 +00001250 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001251
Dale Johannesendd64c412009-02-04 00:33:20 +00001252 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001253 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001254
1255 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001256 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001257 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001258
Chris Lattner447ff682008-03-11 03:23:40 +00001259 RetOps[0] = Chain; // Update chain.
1260
1261 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001262 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001263 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001264
1265 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001266 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001267}
1268
Dan Gohman98ca4f22009-08-05 01:29:28 +00001269/// LowerCallResult - Lower the result values of a call into the
1270/// appropriate copies out of appropriate physical registers.
1271///
1272SDValue
1273X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001274 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001275 const SmallVectorImpl<ISD::InputArg> &Ins,
1276 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001277 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001278
Chris Lattnere32bbf62007-02-28 07:09:55 +00001279 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001280 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001281 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001282 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001283 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001284 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001285
Chris Lattner3085e152007-02-25 08:59:22 +00001286 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001287 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001288 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001289 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001290
Torok Edwin3f142c32009-02-01 18:15:56 +00001291 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001292 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Dan Gohman98ca4f22009-08-05 01:29:28 +00001293 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001294 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001295 }
1296
Chris Lattner8e6da152008-03-10 21:08:41 +00001297 // If this is a call to a function that returns an fp value on the floating
1298 // point stack, but where we prefer to use the value in xmm registers, copy
1299 // it out as F80 and use a truncate to move it from fp stack reg to xmm reg.
Dan Gohman37eed792009-02-04 17:28:58 +00001300 if ((VA.getLocReg() == X86::ST0 ||
1301 VA.getLocReg() == X86::ST1) &&
1302 isScalarFPTypeInSSEReg(VA.getValVT())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001303 CopyVT = MVT::f80;
Chris Lattner3085e152007-02-25 08:59:22 +00001304 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001305
Evan Cheng79fb3b42009-02-20 20:43:02 +00001306 SDValue Val;
1307 if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001308 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1309 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1310 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001311 MVT::v2i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001312 Val = Chain.getValue(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001313 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1314 Val, DAG.getConstant(0, MVT::i64));
Evan Cheng242b38b2009-02-23 09:03:22 +00001315 } else {
1316 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001317 MVT::i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001318 Val = Chain.getValue(0);
1319 }
Evan Cheng79fb3b42009-02-20 20:43:02 +00001320 Val = DAG.getNode(ISD::BIT_CONVERT, dl, CopyVT, Val);
1321 } else {
1322 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1323 CopyVT, InFlag).getValue(1);
1324 Val = Chain.getValue(0);
1325 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001326 InFlag = Chain.getValue(2);
Chris Lattner112dedc2007-12-29 06:41:28 +00001327
Dan Gohman37eed792009-02-04 17:28:58 +00001328 if (CopyVT != VA.getValVT()) {
Chris Lattner8e6da152008-03-10 21:08:41 +00001329 // Round the F80 the right size, which also moves to the appropriate xmm
1330 // register.
Dan Gohman37eed792009-02-04 17:28:58 +00001331 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
Chris Lattner8e6da152008-03-10 21:08:41 +00001332 // This truncation won't change the value.
1333 DAG.getIntPtrConstant(1));
1334 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001335
Dan Gohman98ca4f22009-08-05 01:29:28 +00001336 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001337 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001338
Dan Gohman98ca4f22009-08-05 01:29:28 +00001339 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001340}
1341
1342
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001343//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001344// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001345//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001346// StdCall calling convention seems to be standard for many Windows' API
1347// routines and around. It differs from C calling convention just a little:
1348// callee should clean up the stack, not caller. Symbols should be also
1349// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001350// For info on fast calling convention see Fast Calling Convention (tail call)
1351// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001352
Dan Gohman98ca4f22009-08-05 01:29:28 +00001353/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001354/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001355static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1356 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001357 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001358
Dan Gohman98ca4f22009-08-05 01:29:28 +00001359 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001360}
1361
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001362/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001363/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001364static bool
1365ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1366 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001367 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001368
Dan Gohman98ca4f22009-08-05 01:29:28 +00001369 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001370}
1371
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001372/// IsCalleePop - Determines whether the callee is required to pop its
1373/// own arguments. Callee pop is necessary to support tail calls.
Dan Gohmand858e902010-04-17 15:26:15 +00001374bool X86TargetLowering::IsCalleePop(bool IsVarArg,
1375 CallingConv::ID CallingConv) const {
Gordon Henriksen86737662008-01-05 16:56:59 +00001376 if (IsVarArg)
1377 return false;
1378
Dan Gohman095cc292008-09-13 01:54:27 +00001379 switch (CallingConv) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001380 default:
1381 return false;
1382 case CallingConv::X86_StdCall:
1383 return !Subtarget->is64Bit();
1384 case CallingConv::X86_FastCall:
1385 return !Subtarget->is64Bit();
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001386 case CallingConv::X86_ThisCall:
1387 return !Subtarget->is64Bit();
Gordon Henriksen86737662008-01-05 16:56:59 +00001388 case CallingConv::Fast:
Dan Gohman1797ed52010-02-08 20:27:50 +00001389 return GuaranteedTailCallOpt;
Chris Lattner29689432010-03-11 00:22:57 +00001390 case CallingConv::GHC:
1391 return GuaranteedTailCallOpt;
Gordon Henriksen86737662008-01-05 16:56:59 +00001392 }
1393}
1394
Dan Gohman095cc292008-09-13 01:54:27 +00001395/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1396/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001397CCAssignFn *X86TargetLowering::CCAssignFnForNode(CallingConv::ID CC) const {
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001398 if (Subtarget->is64Bit()) {
Chris Lattner29689432010-03-11 00:22:57 +00001399 if (CC == CallingConv::GHC)
1400 return CC_X86_64_GHC;
1401 else if (Subtarget->isTargetWin64())
Anton Korobeynikov8f88cb02008-03-22 20:37:30 +00001402 return CC_X86_Win64_C;
Evan Chenge9ac9e62008-09-07 09:07:23 +00001403 else
1404 return CC_X86_64_C;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001405 }
1406
Gordon Henriksen86737662008-01-05 16:56:59 +00001407 if (CC == CallingConv::X86_FastCall)
1408 return CC_X86_32_FastCall;
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001409 else if (CC == CallingConv::X86_ThisCall)
1410 return CC_X86_32_ThisCall;
Evan Chengb188dd92008-09-10 18:25:29 +00001411 else if (CC == CallingConv::Fast)
1412 return CC_X86_32_FastCC;
Chris Lattner29689432010-03-11 00:22:57 +00001413 else if (CC == CallingConv::GHC)
1414 return CC_X86_32_GHC;
Gordon Henriksen86737662008-01-05 16:56:59 +00001415 else
1416 return CC_X86_32_C;
1417}
1418
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001419/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1420/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001421/// the specific parameter attribute. The copy will be passed as a byval
1422/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001423static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001424CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001425 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1426 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001427 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesendd64c412009-02-04 00:33:20 +00001428 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001429 /*isVolatile*/false, /*AlwaysInline=*/true,
1430 NULL, 0, NULL, 0);
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001431}
1432
Chris Lattner29689432010-03-11 00:22:57 +00001433/// IsTailCallConvention - Return true if the calling convention is one that
1434/// supports tail call optimization.
1435static bool IsTailCallConvention(CallingConv::ID CC) {
1436 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1437}
1438
Evan Cheng0c439eb2010-01-27 00:07:07 +00001439/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1440/// a tailcall target by changing its ABI.
1441static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001442 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001443}
1444
Dan Gohman98ca4f22009-08-05 01:29:28 +00001445SDValue
1446X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001447 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001448 const SmallVectorImpl<ISD::InputArg> &Ins,
1449 DebugLoc dl, SelectionDAG &DAG,
1450 const CCValAssign &VA,
1451 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001452 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001453 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001454 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001455 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001456 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001457 EVT ValVT;
1458
1459 // If value is passed by pointer we have address passed instead of the value
1460 // itself.
1461 if (VA.getLocInfo() == CCValAssign::Indirect)
1462 ValVT = VA.getLocVT();
1463 else
1464 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001465
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001466 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001467 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001468 // In case of tail call optimization mark all arguments mutable. Since they
1469 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001470 if (Flags.isByVal()) {
1471 int FI = MFI->CreateFixedObject(Flags.getByValSize(),
1472 VA.getLocMemOffset(), isImmutable, false);
1473 return DAG.getFrameIndex(FI, getPointerTy());
1474 } else {
1475 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
1476 VA.getLocMemOffset(), isImmutable, false);
1477 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1478 return DAG.getLoad(ValVT, dl, Chain, FIN,
David Greene67c9d422010-02-15 16:53:33 +00001479 PseudoSourceValue::getFixedStack(FI), 0,
1480 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001481 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001482}
1483
Dan Gohman475871a2008-07-27 21:46:04 +00001484SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001485X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001486 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001487 bool isVarArg,
1488 const SmallVectorImpl<ISD::InputArg> &Ins,
1489 DebugLoc dl,
1490 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001491 SmallVectorImpl<SDValue> &InVals)
1492 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001493 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001494 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001495
Gordon Henriksen86737662008-01-05 16:56:59 +00001496 const Function* Fn = MF.getFunction();
1497 if (Fn->hasExternalLinkage() &&
1498 Subtarget->isTargetCygMing() &&
1499 Fn->getName() == "main")
1500 FuncInfo->setForceFramePointer(true);
1501
Evan Cheng1bc78042006-04-26 01:20:17 +00001502 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001503 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001504 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001505
Chris Lattner29689432010-03-11 00:22:57 +00001506 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1507 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001508
Chris Lattner638402b2007-02-28 07:00:42 +00001509 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001510 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001511 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1512 ArgLocs, *DAG.getContext());
1513 CCInfo.AnalyzeFormalArguments(Ins, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001514
Chris Lattnerf39f7712007-02-28 05:46:49 +00001515 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001516 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001517 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1518 CCValAssign &VA = ArgLocs[i];
1519 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1520 // places.
1521 assert(VA.getValNo() != LastVal &&
1522 "Don't support value assigned to multiple locs yet");
1523 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001524
Chris Lattnerf39f7712007-02-28 05:46:49 +00001525 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001526 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001527 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001528 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001529 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001530 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001531 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001532 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001533 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001534 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001535 RC = X86::FR64RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001536 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001537 RC = X86::VR128RegisterClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001538 else if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
1539 RC = X86::VR64RegisterClass;
1540 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001541 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001542
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001543 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001544 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001545
Chris Lattnerf39f7712007-02-28 05:46:49 +00001546 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1547 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1548 // right size.
1549 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001550 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001551 DAG.getValueType(VA.getValVT()));
1552 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001553 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001554 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001555 else if (VA.getLocInfo() == CCValAssign::BCvt)
Anton Korobeynikov6dde14b2009-08-03 08:14:14 +00001556 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001557
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001558 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001559 // Handle MMX values passed in XMM regs.
1560 if (RegVT.isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001561 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1562 ArgValue, DAG.getConstant(0, MVT::i64));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001563 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1564 } else
1565 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001566 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001567 } else {
1568 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001569 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001570 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001571
1572 // If value is passed via pointer - do a load.
1573 if (VA.getLocInfo() == CCValAssign::Indirect)
David Greene67c9d422010-02-15 16:53:33 +00001574 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue, NULL, 0,
1575 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001576
Dan Gohman98ca4f22009-08-05 01:29:28 +00001577 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001578 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001579
Dan Gohman61a92132008-04-21 23:59:07 +00001580 // The x86-64 ABI for returning structs by value requires that we copy
1581 // the sret argument into %rax for the return. Save the argument into
1582 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001583 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001584 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1585 unsigned Reg = FuncInfo->getSRetReturnReg();
1586 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001587 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001588 FuncInfo->setSRetReturnReg(Reg);
1589 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001590 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001591 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001592 }
1593
Chris Lattnerf39f7712007-02-28 05:46:49 +00001594 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001595 // Align stack specially for tail calls.
1596 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001597 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001598
Evan Cheng1bc78042006-04-26 01:20:17 +00001599 // If the function takes variable number of arguments, make a frame index for
1600 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001601 if (isVarArg) {
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001602 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1603 CallConv != CallingConv::X86_ThisCall)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001604 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,
1605 true, false));
Gordon Henriksen86737662008-01-05 16:56:59 +00001606 }
1607 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001608 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1609
1610 // FIXME: We should really autogenerate these arrays
1611 static const unsigned GPR64ArgRegsWin64[] = {
1612 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001613 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001614 static const unsigned XMMArgRegsWin64[] = {
1615 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1616 };
1617 static const unsigned GPR64ArgRegs64Bit[] = {
1618 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1619 };
1620 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001621 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1622 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1623 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001624 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1625
1626 if (IsWin64) {
1627 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1628 GPR64ArgRegs = GPR64ArgRegsWin64;
1629 XMMArgRegs = XMMArgRegsWin64;
1630 } else {
1631 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1632 GPR64ArgRegs = GPR64ArgRegs64Bit;
1633 XMMArgRegs = XMMArgRegs64Bit;
1634 }
1635 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1636 TotalNumIntRegs);
1637 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1638 TotalNumXMMRegs);
1639
Devang Patel578efa92009-06-05 21:57:13 +00001640 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Evan Chengc7ce29b2009-02-13 22:36:38 +00001641 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001642 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001643 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001644 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001645 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00001646 // Kernel mode asks for SSE to be disabled, so don't push them
1647 // on the stack.
1648 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001649
Gordon Henriksen86737662008-01-05 16:56:59 +00001650 // For X86-64, if there are vararg parameters that are passed via
1651 // registers, then we must store them to their spots on the stack so they
1652 // may be loaded by deferencing the result of va_next.
Dan Gohman1e93df62010-04-17 14:41:14 +00001653 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1654 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1655 FuncInfo->setRegSaveFrameIndex(
1656 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
1657 false));
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001658
Gordon Henriksen86737662008-01-05 16:56:59 +00001659 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001660 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001661 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1662 getPointerTy());
1663 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001664 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001665 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1666 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001667 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
1668 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001669 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001670 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001671 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Dan Gohman1e93df62010-04-17 14:41:14 +00001672 PseudoSourceValue::getFixedStack(
1673 FuncInfo->getRegSaveFrameIndex()),
David Greene67c9d422010-02-15 16:53:33 +00001674 Offset, false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001675 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001676 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001677 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001678
Dan Gohmanface41a2009-08-16 21:24:25 +00001679 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1680 // Now store the XMM (fp + vector) parameter registers.
1681 SmallVector<SDValue, 11> SaveXMMOps;
1682 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001683
Dan Gohmanface41a2009-08-16 21:24:25 +00001684 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
1685 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1686 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001687
Dan Gohman1e93df62010-04-17 14:41:14 +00001688 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1689 FuncInfo->getRegSaveFrameIndex()));
1690 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1691 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001692
Dan Gohmanface41a2009-08-16 21:24:25 +00001693 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
1694 unsigned VReg = MF.addLiveIn(XMMArgRegs[NumXMMRegs],
1695 X86::VR128RegisterClass);
1696 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1697 SaveXMMOps.push_back(Val);
1698 }
1699 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1700 MVT::Other,
1701 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001702 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001703
1704 if (!MemOps.empty())
1705 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1706 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001707 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001708 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001709
Gordon Henriksen86737662008-01-05 16:56:59 +00001710 // Some CCs need callee pop.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001711 if (IsCalleePop(isVarArg, CallConv)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001712 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001713 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001714 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001715 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001716 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001717 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001718 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001719
Gordon Henriksen86737662008-01-05 16:56:59 +00001720 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001721 // RegSaveFrameIndex is X86-64 only.
1722 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001723 if (CallConv == CallingConv::X86_FastCall ||
1724 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001725 // fastcc functions can't have varargs.
1726 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001727 }
Evan Cheng25caf632006-05-23 21:06:34 +00001728
Dan Gohman98ca4f22009-08-05 01:29:28 +00001729 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001730}
1731
Dan Gohman475871a2008-07-27 21:46:04 +00001732SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001733X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1734 SDValue StackPtr, SDValue Arg,
1735 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001736 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001737 ISD::ArgFlagsTy Flags) const {
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +00001738 const unsigned FirstStackArgOffset = (Subtarget->isTargetWin64() ? 32 : 0);
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +00001739 unsigned LocMemOffset = FirstStackArgOffset + VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001740 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001741 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001742 if (Flags.isByVal()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001743 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Evan Chengdffbd832008-01-10 00:09:10 +00001744 }
Dale Johannesenace16102009-02-03 19:33:06 +00001745 return DAG.getStore(Chain, dl, Arg, PtrOff,
David Greene67c9d422010-02-15 16:53:33 +00001746 PseudoSourceValue::getStack(), LocMemOffset,
1747 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001748}
1749
Bill Wendling64e87322009-01-16 19:25:27 +00001750/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001751/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001752SDValue
1753X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001754 SDValue &OutRetAddr, SDValue Chain,
1755 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001756 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001757 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001758 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001759 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001760
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001761 // Load the "old" Return address.
David Greene67c9d422010-02-15 16:53:33 +00001762 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, NULL, 0, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001763 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001764}
1765
1766/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1767/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001768static SDValue
1769EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001770 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001771 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001772 // Store the return address to the appropriate stack slot.
1773 if (!FPDiff) return Chain;
1774 // Calculate the new stack slot for the return address.
1775 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001776 int NewReturnAddrFI =
Arnold Schwaighofer92652752010-02-22 16:18:09 +00001777 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001778 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001779 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001780 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
David Greene67c9d422010-02-15 16:53:33 +00001781 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0,
1782 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001783 return Chain;
1784}
1785
Dan Gohman98ca4f22009-08-05 01:29:28 +00001786SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001787X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001788 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001789 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001790 const SmallVectorImpl<ISD::OutputArg> &Outs,
1791 const SmallVectorImpl<ISD::InputArg> &Ins,
1792 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001793 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001794 MachineFunction &MF = DAG.getMachineFunction();
1795 bool Is64Bit = Subtarget->is64Bit();
1796 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00001797 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001798
Evan Cheng5f941932010-02-05 02:21:12 +00001799 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001800 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00001801 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1802 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Evan Cheng022d9e12010-02-02 23:55:14 +00001803 Outs, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00001804
1805 // Sibcalls are automatically detected tailcalls which do not require
1806 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00001807 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00001808 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00001809
1810 if (isTailCall)
1811 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00001812 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00001813
Chris Lattner29689432010-03-11 00:22:57 +00001814 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1815 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001816
Chris Lattner638402b2007-02-28 07:00:42 +00001817 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001818 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001819 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1820 ArgLocs, *DAG.getContext());
1821 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001822
Chris Lattner423c5f42007-02-28 05:31:48 +00001823 // Get a count of how many bytes are to be pushed on the stack.
1824 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00001825 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00001826 // This is a sibcall. The memory operands are available in caller's
1827 // own caller's stack.
1828 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00001829 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00001830 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001831
Gordon Henriksen86737662008-01-05 16:56:59 +00001832 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00001833 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001834 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00001835 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00001836 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1837 FPDiff = NumBytesCallerPushed - NumBytes;
1838
1839 // Set the delta of movement of the returnaddr stackslot.
1840 // But only set if delta is greater than previous delta.
1841 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1842 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1843 }
1844
Evan Chengf22f9b32010-02-06 03:28:46 +00001845 if (!IsSibcall)
1846 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001847
Dan Gohman475871a2008-07-27 21:46:04 +00001848 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001849 // Load return adress for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00001850 if (isTailCall && FPDiff)
1851 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
1852 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001853
Dan Gohman475871a2008-07-27 21:46:04 +00001854 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1855 SmallVector<SDValue, 8> MemOpChains;
1856 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001857
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001858 // Walk the register/memloc assignments, inserting copies/loads. In the case
1859 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00001860 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1861 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001862 EVT RegVT = VA.getLocVT();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001863 SDValue Arg = Outs[i].Val;
1864 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00001865 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00001866
Chris Lattner423c5f42007-02-28 05:31:48 +00001867 // Promote the value if needed.
1868 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001869 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00001870 case CCValAssign::Full: break;
1871 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001872 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001873 break;
1874 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001875 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001876 break;
1877 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001878 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
1879 // Special case: passing MMX values in XMM registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001880 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
1881 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
1882 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001883 } else
1884 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
1885 break;
1886 case CCValAssign::BCvt:
1887 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001888 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001889 case CCValAssign::Indirect: {
1890 // Store the argument.
1891 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00001892 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001893 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
David Greene67c9d422010-02-15 16:53:33 +00001894 PseudoSourceValue::getFixedStack(FI), 0,
1895 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001896 Arg = SpillSlot;
1897 break;
1898 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00001899 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001900
Chris Lattner423c5f42007-02-28 05:31:48 +00001901 if (VA.isRegLoc()) {
1902 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Evan Chengf22f9b32010-02-06 03:28:46 +00001903 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00001904 assert(VA.isMemLoc());
1905 if (StackPtr.getNode() == 0)
1906 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
1907 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1908 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001909 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001910 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001911
Evan Cheng32fe1032006-05-25 00:59:30 +00001912 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001913 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001914 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001915
Evan Cheng347d5f72006-04-28 21:29:37 +00001916 // Build a sequence of copy-to-reg nodes chained together with token chain
1917 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001918 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001919 // Tail call byval lowering might overwrite argument registers so in case of
1920 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001921 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001922 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001923 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001924 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001925 InFlag = Chain.getValue(1);
1926 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001927
Chris Lattner88e1fd52009-07-09 04:24:46 +00001928 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001929 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1930 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001931 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001932 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
1933 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001934 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001935 InFlag);
1936 InFlag = Chain.getValue(1);
1937 } else {
1938 // If we are tail calling and generating PIC/GOT style code load the
1939 // address of the callee into ECX. The value in ecx is used as target of
1940 // the tail jump. This is done to circumvent the ebx/callee-saved problem
1941 // for tail calls on PIC/GOT architectures. Normally we would just put the
1942 // address of GOT into ebx and then call target@PLT. But for tail calls
1943 // ebx would be restored (since ebx is callee saved) before jumping to the
1944 // target@PLT.
1945
1946 // Note: The actual moving to ECX is done further down.
1947 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1948 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1949 !G->getGlobal()->hasProtectedVisibility())
1950 Callee = LowerGlobalAddress(Callee, DAG);
1951 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00001952 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001953 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001954 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001955
Gordon Henriksen86737662008-01-05 16:56:59 +00001956 if (Is64Bit && isVarArg) {
1957 // From AMD64 ABI document:
1958 // For calls that may call functions that use varargs or stdargs
1959 // (prototype-less calls or calls to functions containing ellipsis (...) in
1960 // the declaration) %al is used as hidden argument to specify the number
1961 // of SSE registers used. The contents of %al do not need to match exactly
1962 // the number of registers, but must be an ubound on the number of SSE
1963 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001964
1965 // FIXME: Verify this on Win64
Gordon Henriksen86737662008-01-05 16:56:59 +00001966 // Count the number of XMM registers allocated.
1967 static const unsigned XMMArgRegs[] = {
1968 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1969 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1970 };
1971 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Scott Michelfdc40a02009-02-17 22:15:04 +00001972 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00001973 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001974
Dale Johannesendd64c412009-02-04 00:33:20 +00001975 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00001976 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00001977 InFlag = Chain.getValue(1);
1978 }
1979
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001980
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001981 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001982 if (isTailCall) {
1983 // Force all the incoming stack arguments to be loaded from the stack
1984 // before any new outgoing arguments are stored to the stack, because the
1985 // outgoing stack slots may alias the incoming argument stack slots, and
1986 // the alias isn't otherwise explicit. This is slightly more conservative
1987 // than necessary, because it means that each store effectively depends
1988 // on every argument instead of just those arguments it would clobber.
1989 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
1990
Dan Gohman475871a2008-07-27 21:46:04 +00001991 SmallVector<SDValue, 8> MemOpChains2;
1992 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00001993 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001994 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00001995 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00001996 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00001997 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1998 CCValAssign &VA = ArgLocs[i];
1999 if (VA.isRegLoc())
2000 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002001 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00002002 SDValue Arg = Outs[i].Val;
2003 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002004 // Create frame index.
2005 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002006 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
David Greene3f2bf852009-11-12 20:49:22 +00002007 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true, false);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002008 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002009
Duncan Sands276dcbd2008-03-21 09:14:45 +00002010 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002011 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002012 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002013 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002014 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002015 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002016 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002017
Dan Gohman98ca4f22009-08-05 01:29:28 +00002018 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2019 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002020 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002021 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002022 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002023 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002024 DAG.getStore(ArgChain, dl, Arg, FIN,
David Greene67c9d422010-02-15 16:53:33 +00002025 PseudoSourceValue::getFixedStack(FI), 0,
2026 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002027 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002028 }
2029 }
2030
2031 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002032 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002033 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002034
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002035 // Copy arguments to their registers.
2036 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002037 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002038 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002039 InFlag = Chain.getValue(1);
2040 }
Dan Gohman475871a2008-07-27 21:46:04 +00002041 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002042
Gordon Henriksen86737662008-01-05 16:56:59 +00002043 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002044 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002045 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002046 }
2047
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002048 bool WasGlobalOrExternal = false;
2049 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2050 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2051 // In the 64-bit large code model, we have to make all calls
2052 // through a register, since the call instruction's 32-bit
2053 // pc-relative offset may not be large enough to hold the whole
2054 // address.
2055 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
2056 WasGlobalOrExternal = true;
2057 // If the callee is a GlobalAddress node (quite common, every direct call
2058 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2059 // it.
2060
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002061 // We should use extra load for direct calls to dllimported functions in
2062 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002063 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002064 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002065 unsigned char OpFlags = 0;
Eric Christopherfd179292009-08-27 18:07:15 +00002066
Chris Lattner48a7d022009-07-09 05:02:21 +00002067 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2068 // external symbols most go through the PLT in PIC mode. If the symbol
2069 // has hidden or protected visibility, or if it is static or local, then
2070 // we don't need to use the PLT - we can directly call it.
2071 if (Subtarget->isTargetELF() &&
2072 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002073 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002074 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002075 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002076 (GV->isDeclaration() || GV->isWeakForLinker()) &&
2077 Subtarget->getDarwinVers() < 9) {
2078 // PC-relative references to external symbols should go through $stub,
2079 // unless we're building with the leopard linker or later, which
2080 // automatically synthesizes these stubs.
2081 OpFlags = X86II::MO_DARWIN_STUB;
2082 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002083
Chris Lattner74e726e2009-07-09 05:27:35 +00002084 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002085 G->getOffset(), OpFlags);
2086 }
Bill Wendling056292f2008-09-16 21:48:12 +00002087 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002088 WasGlobalOrExternal = true;
Chris Lattner48a7d022009-07-09 05:02:21 +00002089 unsigned char OpFlags = 0;
2090
2091 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to external
2092 // symbols should go through the PLT.
2093 if (Subtarget->isTargetELF() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002094 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002095 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002096 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002097 Subtarget->getDarwinVers() < 9) {
2098 // PC-relative references to external symbols should go through $stub,
2099 // unless we're building with the leopard linker or later, which
2100 // automatically synthesizes these stubs.
2101 OpFlags = X86II::MO_DARWIN_STUB;
2102 }
Eric Christopherfd179292009-08-27 18:07:15 +00002103
Chris Lattner48a7d022009-07-09 05:02:21 +00002104 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2105 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002106 }
2107
Chris Lattnerd96d0722007-02-25 06:40:16 +00002108 // Returns a chain & a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +00002109 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00002110 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002111
Evan Chengf22f9b32010-02-06 03:28:46 +00002112 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002113 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2114 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002115 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002116 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002117
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002118 Ops.push_back(Chain);
2119 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002120
Dan Gohman98ca4f22009-08-05 01:29:28 +00002121 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002122 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002123
Gordon Henriksen86737662008-01-05 16:56:59 +00002124 // Add argument registers to the end of the list so that they are known live
2125 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002126 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2127 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2128 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002129
Evan Cheng586ccac2008-03-18 23:36:35 +00002130 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002131 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002132 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2133
2134 // Add an implicit use of AL for x86 vararg functions.
2135 if (Is64Bit && isVarArg)
Owen Anderson825b72b2009-08-11 20:47:22 +00002136 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002137
Gabor Greifba36cb52008-08-28 21:40:38 +00002138 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002139 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002140
Dan Gohman98ca4f22009-08-05 01:29:28 +00002141 if (isTailCall) {
2142 // If this is the first return lowered for this function, add the regs
2143 // to the liveout set for the function.
2144 if (MF.getRegInfo().liveout_empty()) {
2145 SmallVector<CCValAssign, 16> RVLocs;
2146 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
2147 *DAG.getContext());
2148 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
2149 for (unsigned i = 0; i != RVLocs.size(); ++i)
2150 if (RVLocs[i].isRegLoc())
2151 MF.getRegInfo().addLiveOut(RVLocs[i].getLocReg());
2152 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002153 return DAG.getNode(X86ISD::TC_RETURN, dl,
2154 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002155 }
2156
Dale Johannesenace16102009-02-03 19:33:06 +00002157 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002158 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002159
Chris Lattner2d297092006-05-23 18:50:38 +00002160 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002161 unsigned NumBytesForCalleeToPush;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002162 if (IsCalleePop(isVarArg, CallConv))
Gordon Henriksen86737662008-01-05 16:56:59 +00002163 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002164 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002165 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002166 // pops the hidden struct pointer, so we have to push it back.
2167 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002168 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002169 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002170 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002171
Gordon Henriksenae636f82008-01-03 16:47:34 +00002172 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002173 if (!IsSibcall) {
2174 Chain = DAG.getCALLSEQ_END(Chain,
2175 DAG.getIntPtrConstant(NumBytes, true),
2176 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2177 true),
2178 InFlag);
2179 InFlag = Chain.getValue(1);
2180 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002181
Chris Lattner3085e152007-02-25 08:59:22 +00002182 // Handle result values, copying them out of physregs into vregs that we
2183 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002184 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2185 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002186}
2187
Evan Cheng25ab6902006-09-08 06:48:29 +00002188
2189//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002190// Fast Calling Convention (tail call) implementation
2191//===----------------------------------------------------------------------===//
2192
2193// Like std call, callee cleans arguments, convention except that ECX is
2194// reserved for storing the tail called function address. Only 2 registers are
2195// free for argument passing (inreg). Tail call optimization is performed
2196// provided:
2197// * tailcallopt is enabled
2198// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002199// On X86_64 architecture with GOT-style position independent code only local
2200// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002201// To keep the stack aligned according to platform abi the function
2202// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2203// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002204// If a tail called function callee has more arguments than the caller the
2205// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002206// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002207// original REtADDR, but before the saved framepointer or the spilled registers
2208// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2209// stack layout:
2210// arg1
2211// arg2
2212// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002213// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002214// move area ]
2215// (possible EBP)
2216// ESI
2217// EDI
2218// local1 ..
2219
2220/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2221/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002222unsigned
2223X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2224 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002225 MachineFunction &MF = DAG.getMachineFunction();
2226 const TargetMachine &TM = MF.getTarget();
2227 const TargetFrameInfo &TFI = *TM.getFrameInfo();
2228 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002229 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002230 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002231 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002232 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2233 // Number smaller than 12 so just add the difference.
2234 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2235 } else {
2236 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002237 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002238 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002239 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002240 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002241}
2242
Evan Cheng5f941932010-02-05 02:21:12 +00002243/// MatchingStackOffset - Return true if the given stack call argument is
2244/// already available in the same position (relatively) of the caller's
2245/// incoming argument stack.
2246static
2247bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2248 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2249 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002250 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2251 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002252 if (Arg.getOpcode() == ISD::CopyFromReg) {
2253 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
2254 if (!VR || TargetRegisterInfo::isPhysicalRegister(VR))
2255 return false;
2256 MachineInstr *Def = MRI->getVRegDef(VR);
2257 if (!Def)
2258 return false;
2259 if (!Flags.isByVal()) {
2260 if (!TII->isLoadFromStackSlot(Def, FI))
2261 return false;
2262 } else {
2263 unsigned Opcode = Def->getOpcode();
2264 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2265 Def->getOperand(1).isFI()) {
2266 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002267 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002268 } else
2269 return false;
2270 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002271 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2272 if (Flags.isByVal())
2273 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002274 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002275 // define @foo(%struct.X* %A) {
2276 // tail call @bar(%struct.X* byval %A)
2277 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002278 return false;
2279 SDValue Ptr = Ld->getBasePtr();
2280 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2281 if (!FINode)
2282 return false;
2283 FI = FINode->getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002284 } else
2285 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002286
Evan Cheng4cae1332010-03-05 08:38:04 +00002287 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002288 if (!MFI->isFixedObjectIndex(FI))
2289 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002290 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002291}
2292
Dan Gohman98ca4f22009-08-05 01:29:28 +00002293/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2294/// for tail call optimization. Targets which want to do tail call
2295/// optimization should implement this function.
2296bool
2297X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002298 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002299 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002300 bool isCalleeStructRet,
2301 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002302 const SmallVectorImpl<ISD::OutputArg> &Outs,
2303 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002304 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002305 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002306 CalleeCC != CallingConv::C)
2307 return false;
2308
Evan Cheng7096ae42010-01-29 06:45:59 +00002309 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002310 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002311 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002312 CallingConv::ID CallerCC = CallerF->getCallingConv();
2313 bool CCMatch = CallerCC == CalleeCC;
2314
Dan Gohman1797ed52010-02-08 20:27:50 +00002315 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002316 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002317 return true;
2318 return false;
2319 }
2320
Evan Chengb2c92902010-02-02 02:22:50 +00002321 // Look for obvious safe cases to perform tail call optimization that does not
2322 // requite ABI changes. This is what gcc calls sibcall.
2323
Evan Cheng2c12cb42010-03-26 16:26:03 +00002324 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2325 // emit a special epilogue.
2326 if (RegInfo->needsStackRealignment(MF))
2327 return false;
2328
Evan Cheng3c262ee2010-03-26 02:13:13 +00002329 // Do not sibcall optimize vararg calls unless the call site is not passing any
2330 // arguments.
2331 if (isVarArg && !Outs.empty())
Evan Cheng843bd692010-01-31 06:44:49 +00002332 return false;
2333
Evan Chenga375d472010-03-15 18:54:48 +00002334 // Also avoid sibcall optimization if either caller or callee uses struct
2335 // return semantics.
2336 if (isCalleeStructRet || isCallerStructRet)
2337 return false;
2338
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002339 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2340 // Therefore if it's not used by the call it is not safe to optimize this into
2341 // a sibcall.
2342 bool Unused = false;
2343 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2344 if (!Ins[i].Used) {
2345 Unused = true;
2346 break;
2347 }
2348 }
2349 if (Unused) {
2350 SmallVector<CCValAssign, 16> RVLocs;
2351 CCState CCInfo(CalleeCC, false, getTargetMachine(),
2352 RVLocs, *DAG.getContext());
2353 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002354 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002355 CCValAssign &VA = RVLocs[i];
2356 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2357 return false;
2358 }
2359 }
2360
Evan Cheng13617962010-04-30 01:12:32 +00002361 // If the calling conventions do not match, then we'd better make sure the
2362 // results are returned in the same way as what the caller expects.
2363 if (!CCMatch) {
2364 SmallVector<CCValAssign, 16> RVLocs1;
2365 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
2366 RVLocs1, *DAG.getContext());
2367 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2368
2369 SmallVector<CCValAssign, 16> RVLocs2;
2370 CCState CCInfo2(CallerCC, false, getTargetMachine(),
2371 RVLocs2, *DAG.getContext());
2372 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2373
2374 if (RVLocs1.size() != RVLocs2.size())
2375 return false;
2376 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2377 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2378 return false;
2379 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2380 return false;
2381 if (RVLocs1[i].isRegLoc()) {
2382 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2383 return false;
2384 } else {
2385 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2386 return false;
2387 }
2388 }
2389 }
2390
Evan Chenga6bff982010-01-30 01:22:00 +00002391 // If the callee takes no arguments then go on to check the results of the
2392 // call.
2393 if (!Outs.empty()) {
2394 // Check if stack adjustment is needed. For now, do not do this if any
2395 // argument is passed on the stack.
2396 SmallVector<CCValAssign, 16> ArgLocs;
2397 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
2398 ArgLocs, *DAG.getContext());
2399 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CalleeCC));
Evan Chengb2c92902010-02-02 02:22:50 +00002400 if (CCInfo.getNextStackOffset()) {
2401 MachineFunction &MF = DAG.getMachineFunction();
2402 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2403 return false;
2404 if (Subtarget->isTargetWin64())
2405 // Win64 ABI has additional complications.
2406 return false;
2407
2408 // Check if the arguments are already laid out in the right way as
2409 // the caller's fixed stack objects.
2410 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002411 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2412 const X86InstrInfo *TII =
2413 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002414 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2415 CCValAssign &VA = ArgLocs[i];
2416 EVT RegVT = VA.getLocVT();
2417 SDValue Arg = Outs[i].Val;
2418 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002419 if (VA.getLocInfo() == CCValAssign::Indirect)
2420 return false;
2421 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002422 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2423 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002424 return false;
2425 }
2426 }
2427 }
Evan Chenga6bff982010-01-30 01:22:00 +00002428 }
Evan Chengb1712452010-01-27 06:25:16 +00002429
Evan Cheng86809cc2010-02-03 03:28:02 +00002430 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002431}
2432
Dan Gohman3df24e62008-09-03 23:12:08 +00002433FastISel *
Chris Lattnered3a8062010-04-05 06:05:26 +00002434X86TargetLowering::createFastISel(MachineFunction &mf,
Evan Chengddc419c2010-01-26 19:04:47 +00002435 DenseMap<const Value *, unsigned> &vm,
2436 DenseMap<const BasicBlock*, MachineBasicBlock*> &bm,
Dan Gohmanf81eca02010-04-22 20:46:50 +00002437 DenseMap<const AllocaInst *, int> &am,
2438 std::vector<std::pair<MachineInstr*, unsigned> > &pn
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002439#ifndef NDEBUG
Dan Gohman25208642010-04-14 19:53:31 +00002440 , SmallSet<const Instruction *, 8> &cil
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002441#endif
Dan Gohmand858e902010-04-17 15:26:15 +00002442 ) const {
Dan Gohmanf81eca02010-04-22 20:46:50 +00002443 return X86::createFastISel(mf, vm, bm, am, pn
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002444#ifndef NDEBUG
2445 , cil
2446#endif
2447 );
Dan Gohmand9f3c482008-08-19 21:32:53 +00002448}
2449
2450
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002451//===----------------------------------------------------------------------===//
2452// Other Lowering Hooks
2453//===----------------------------------------------------------------------===//
2454
2455
Dan Gohmand858e902010-04-17 15:26:15 +00002456SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002457 MachineFunction &MF = DAG.getMachineFunction();
2458 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2459 int ReturnAddrIndex = FuncInfo->getRAIndex();
2460
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002461 if (ReturnAddrIndex == 0) {
2462 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002463 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002464 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Arnold Schwaighofer92652752010-02-22 16:18:09 +00002465 false, false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002466 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002467 }
2468
Evan Cheng25ab6902006-09-08 06:48:29 +00002469 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002470}
2471
2472
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002473bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2474 bool hasSymbolicDisplacement) {
2475 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002476 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002477 return false;
2478
2479 // If we don't have a symbolic displacement - we don't have any extra
2480 // restrictions.
2481 if (!hasSymbolicDisplacement)
2482 return true;
2483
2484 // FIXME: Some tweaks might be needed for medium code model.
2485 if (M != CodeModel::Small && M != CodeModel::Kernel)
2486 return false;
2487
2488 // For small code model we assume that latest object is 16MB before end of 31
2489 // bits boundary. We may also accept pretty large negative constants knowing
2490 // that all objects are in the positive half of address space.
2491 if (M == CodeModel::Small && Offset < 16*1024*1024)
2492 return true;
2493
2494 // For kernel code model we know that all object resist in the negative half
2495 // of 32bits address space. We may not accept negative offsets, since they may
2496 // be just off and we may accept pretty large positive ones.
2497 if (M == CodeModel::Kernel && Offset > 0)
2498 return true;
2499
2500 return false;
2501}
2502
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002503/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2504/// specific condition code, returning the condition code and the LHS/RHS of the
2505/// comparison to make.
2506static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2507 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002508 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002509 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2510 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2511 // X > -1 -> X == 0, jump !sign.
2512 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002513 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002514 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2515 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002516 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002517 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002518 // X < 1 -> X <= 0
2519 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002520 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002521 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002522 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002523
Evan Chengd9558e02006-01-06 00:43:03 +00002524 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002525 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002526 case ISD::SETEQ: return X86::COND_E;
2527 case ISD::SETGT: return X86::COND_G;
2528 case ISD::SETGE: return X86::COND_GE;
2529 case ISD::SETLT: return X86::COND_L;
2530 case ISD::SETLE: return X86::COND_LE;
2531 case ISD::SETNE: return X86::COND_NE;
2532 case ISD::SETULT: return X86::COND_B;
2533 case ISD::SETUGT: return X86::COND_A;
2534 case ISD::SETULE: return X86::COND_BE;
2535 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002536 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002537 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002538
Chris Lattner4c78e022008-12-23 23:42:27 +00002539 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002540
Chris Lattner4c78e022008-12-23 23:42:27 +00002541 // If LHS is a foldable load, but RHS is not, flip the condition.
2542 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2543 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2544 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2545 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002546 }
2547
Chris Lattner4c78e022008-12-23 23:42:27 +00002548 switch (SetCCOpcode) {
2549 default: break;
2550 case ISD::SETOLT:
2551 case ISD::SETOLE:
2552 case ISD::SETUGT:
2553 case ISD::SETUGE:
2554 std::swap(LHS, RHS);
2555 break;
2556 }
2557
2558 // On a floating point condition, the flags are set as follows:
2559 // ZF PF CF op
2560 // 0 | 0 | 0 | X > Y
2561 // 0 | 0 | 1 | X < Y
2562 // 1 | 0 | 0 | X == Y
2563 // 1 | 1 | 1 | unordered
2564 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002565 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002566 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002567 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002568 case ISD::SETOLT: // flipped
2569 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002570 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002571 case ISD::SETOLE: // flipped
2572 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002573 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002574 case ISD::SETUGT: // flipped
2575 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002576 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002577 case ISD::SETUGE: // flipped
2578 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002579 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002580 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002581 case ISD::SETNE: return X86::COND_NE;
2582 case ISD::SETUO: return X86::COND_P;
2583 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002584 case ISD::SETOEQ:
2585 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002586 }
Evan Chengd9558e02006-01-06 00:43:03 +00002587}
2588
Evan Cheng4a460802006-01-11 00:33:36 +00002589/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2590/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002591/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002592static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002593 switch (X86CC) {
2594 default:
2595 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002596 case X86::COND_B:
2597 case X86::COND_BE:
2598 case X86::COND_E:
2599 case X86::COND_P:
2600 case X86::COND_A:
2601 case X86::COND_AE:
2602 case X86::COND_NE:
2603 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002604 return true;
2605 }
2606}
2607
Evan Chengeb2f9692009-10-27 19:56:55 +00002608/// isFPImmLegal - Returns true if the target can instruction select the
2609/// specified FP immediate natively. If false, the legalizer will
2610/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002611bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00002612 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
2613 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
2614 return true;
2615 }
2616 return false;
2617}
2618
Nate Begeman9008ca62009-04-27 18:41:29 +00002619/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2620/// the specified range (L, H].
2621static bool isUndefOrInRange(int Val, int Low, int Hi) {
2622 return (Val < 0) || (Val >= Low && Val < Hi);
2623}
2624
2625/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2626/// specified value.
2627static bool isUndefOrEqual(int Val, int CmpVal) {
2628 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002629 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002630 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002631}
2632
Nate Begeman9008ca62009-04-27 18:41:29 +00002633/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2634/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2635/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00002636static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002637 if (VT == MVT::v4f32 || VT == MVT::v4i32 || VT == MVT::v4i16)
Nate Begeman9008ca62009-04-27 18:41:29 +00002638 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002639 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00002640 return (Mask[0] < 2 && Mask[1] < 2);
2641 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002642}
2643
Nate Begeman9008ca62009-04-27 18:41:29 +00002644bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002645 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002646 N->getMask(M);
2647 return ::isPSHUFDMask(M, N->getValueType(0));
2648}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002649
Nate Begeman9008ca62009-04-27 18:41:29 +00002650/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2651/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00002652static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002653 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002654 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002655
Nate Begeman9008ca62009-04-27 18:41:29 +00002656 // Lower quadword copied in order or undef.
2657 for (int i = 0; i != 4; ++i)
2658 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002659 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002660
Evan Cheng506d3df2006-03-29 23:07:14 +00002661 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002662 for (int i = 4; i != 8; ++i)
2663 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002664 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002665
Evan Cheng506d3df2006-03-29 23:07:14 +00002666 return true;
2667}
2668
Nate Begeman9008ca62009-04-27 18:41:29 +00002669bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002670 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002671 N->getMask(M);
2672 return ::isPSHUFHWMask(M, N->getValueType(0));
2673}
Evan Cheng506d3df2006-03-29 23:07:14 +00002674
Nate Begeman9008ca62009-04-27 18:41:29 +00002675/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2676/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00002677static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002678 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002679 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002680
Rafael Espindola15684b22009-04-24 12:40:33 +00002681 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00002682 for (int i = 4; i != 8; ++i)
2683 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00002684 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002685
Rafael Espindola15684b22009-04-24 12:40:33 +00002686 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002687 for (int i = 0; i != 4; ++i)
2688 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00002689 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002690
Rafael Espindola15684b22009-04-24 12:40:33 +00002691 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002692}
2693
Nate Begeman9008ca62009-04-27 18:41:29 +00002694bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002695 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002696 N->getMask(M);
2697 return ::isPSHUFLWMask(M, N->getValueType(0));
2698}
2699
Nate Begemana09008b2009-10-19 02:17:23 +00002700/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
2701/// is suitable for input to PALIGNR.
2702static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
2703 bool hasSSSE3) {
2704 int i, e = VT.getVectorNumElements();
2705
2706 // Do not handle v2i64 / v2f64 shuffles with palignr.
2707 if (e < 4 || !hasSSSE3)
2708 return false;
2709
2710 for (i = 0; i != e; ++i)
2711 if (Mask[i] >= 0)
2712 break;
2713
2714 // All undef, not a palignr.
2715 if (i == e)
2716 return false;
2717
2718 // Determine if it's ok to perform a palignr with only the LHS, since we
2719 // don't have access to the actual shuffle elements to see if RHS is undef.
2720 bool Unary = Mask[i] < (int)e;
2721 bool NeedsUnary = false;
2722
2723 int s = Mask[i] - i;
2724
2725 // Check the rest of the elements to see if they are consecutive.
2726 for (++i; i != e; ++i) {
2727 int m = Mask[i];
2728 if (m < 0)
2729 continue;
2730
2731 Unary = Unary && (m < (int)e);
2732 NeedsUnary = NeedsUnary || (m < s);
2733
2734 if (NeedsUnary && !Unary)
2735 return false;
2736 if (Unary && m != ((s+i) & (e-1)))
2737 return false;
2738 if (!Unary && m != (s+i))
2739 return false;
2740 }
2741 return true;
2742}
2743
2744bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
2745 SmallVector<int, 8> M;
2746 N->getMask(M);
2747 return ::isPALIGNRMask(M, N->getValueType(0), true);
2748}
2749
Evan Cheng14aed5e2006-03-24 01:18:28 +00002750/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2751/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00002752static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002753 int NumElems = VT.getVectorNumElements();
2754 if (NumElems != 2 && NumElems != 4)
2755 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002756
Nate Begeman9008ca62009-04-27 18:41:29 +00002757 int Half = NumElems / 2;
2758 for (int i = 0; i < Half; ++i)
2759 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002760 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002761 for (int i = Half; i < NumElems; ++i)
2762 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002763 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002764
Evan Cheng14aed5e2006-03-24 01:18:28 +00002765 return true;
2766}
2767
Nate Begeman9008ca62009-04-27 18:41:29 +00002768bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
2769 SmallVector<int, 8> M;
2770 N->getMask(M);
2771 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002772}
2773
Evan Cheng213d2cf2007-05-17 18:45:50 +00002774/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002775/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2776/// half elements to come from vector 1 (which would equal the dest.) and
2777/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00002778static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002779 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002780
2781 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00002782 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002783
Nate Begeman9008ca62009-04-27 18:41:29 +00002784 int Half = NumElems / 2;
2785 for (int i = 0; i < Half; ++i)
2786 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002787 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002788 for (int i = Half; i < NumElems; ++i)
2789 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002790 return false;
2791 return true;
2792}
2793
Nate Begeman9008ca62009-04-27 18:41:29 +00002794static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
2795 SmallVector<int, 8> M;
2796 N->getMask(M);
2797 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002798}
2799
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002800/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2801/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002802bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
2803 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002804 return false;
2805
Evan Cheng2064a2b2006-03-28 06:50:32 +00002806 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00002807 return isUndefOrEqual(N->getMaskElt(0), 6) &&
2808 isUndefOrEqual(N->getMaskElt(1), 7) &&
2809 isUndefOrEqual(N->getMaskElt(2), 2) &&
2810 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002811}
2812
Nate Begeman0b10b912009-11-07 23:17:15 +00002813/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2814/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2815/// <2, 3, 2, 3>
2816bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
2817 unsigned NumElems = N->getValueType(0).getVectorNumElements();
2818
2819 if (NumElems != 4)
2820 return false;
2821
2822 return isUndefOrEqual(N->getMaskElt(0), 2) &&
2823 isUndefOrEqual(N->getMaskElt(1), 3) &&
2824 isUndefOrEqual(N->getMaskElt(2), 2) &&
2825 isUndefOrEqual(N->getMaskElt(3), 3);
2826}
2827
Evan Cheng5ced1d82006-04-06 23:23:56 +00002828/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2829/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00002830bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
2831 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002832
Evan Cheng5ced1d82006-04-06 23:23:56 +00002833 if (NumElems != 2 && NumElems != 4)
2834 return false;
2835
Evan Chengc5cdff22006-04-07 21:53:05 +00002836 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002837 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002838 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002839
Evan Chengc5cdff22006-04-07 21:53:05 +00002840 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002841 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002842 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002843
2844 return true;
2845}
2846
Nate Begeman0b10b912009-11-07 23:17:15 +00002847/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
2848/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
2849bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002850 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002851
Evan Cheng5ced1d82006-04-06 23:23:56 +00002852 if (NumElems != 2 && NumElems != 4)
2853 return false;
2854
Evan Chengc5cdff22006-04-07 21:53:05 +00002855 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002856 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002857 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002858
Nate Begeman9008ca62009-04-27 18:41:29 +00002859 for (unsigned i = 0; i < NumElems/2; ++i)
2860 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002861 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002862
2863 return true;
2864}
2865
Evan Cheng0038e592006-03-28 00:39:58 +00002866/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2867/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00002868static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002869 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002870 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002871 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00002872 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002873
Nate Begeman9008ca62009-04-27 18:41:29 +00002874 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2875 int BitI = Mask[i];
2876 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002877 if (!isUndefOrEqual(BitI, j))
2878 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002879 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002880 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002881 return false;
2882 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002883 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002884 return false;
2885 }
Evan Cheng0038e592006-03-28 00:39:58 +00002886 }
Evan Cheng0038e592006-03-28 00:39:58 +00002887 return true;
2888}
2889
Nate Begeman9008ca62009-04-27 18:41:29 +00002890bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2891 SmallVector<int, 8> M;
2892 N->getMask(M);
2893 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002894}
2895
Evan Cheng4fcb9222006-03-28 02:43:26 +00002896/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2897/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00002898static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002899 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002900 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002901 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00002902 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002903
Nate Begeman9008ca62009-04-27 18:41:29 +00002904 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2905 int BitI = Mask[i];
2906 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00002907 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00002908 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002909 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002910 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002911 return false;
2912 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002913 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002914 return false;
2915 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002916 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002917 return true;
2918}
2919
Nate Begeman9008ca62009-04-27 18:41:29 +00002920bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2921 SmallVector<int, 8> M;
2922 N->getMask(M);
2923 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002924}
2925
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002926/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2927/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2928/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00002929static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002930 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002931 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002932 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002933
Nate Begeman9008ca62009-04-27 18:41:29 +00002934 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
2935 int BitI = Mask[i];
2936 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002937 if (!isUndefOrEqual(BitI, j))
2938 return false;
2939 if (!isUndefOrEqual(BitI1, j))
2940 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002941 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002942 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002943}
2944
Nate Begeman9008ca62009-04-27 18:41:29 +00002945bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
2946 SmallVector<int, 8> M;
2947 N->getMask(M);
2948 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
2949}
2950
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002951/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2952/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2953/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00002954static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002955 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002956 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2957 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002958
Nate Begeman9008ca62009-04-27 18:41:29 +00002959 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2960 int BitI = Mask[i];
2961 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002962 if (!isUndefOrEqual(BitI, j))
2963 return false;
2964 if (!isUndefOrEqual(BitI1, j))
2965 return false;
2966 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002967 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002968}
2969
Nate Begeman9008ca62009-04-27 18:41:29 +00002970bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
2971 SmallVector<int, 8> M;
2972 N->getMask(M);
2973 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
2974}
2975
Evan Cheng017dcc62006-04-21 01:05:10 +00002976/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2977/// specifies a shuffle of elements that is suitable for input to MOVSS,
2978/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00002979static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00002980 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002981 return false;
Eli Friedman10415532009-06-06 06:05:10 +00002982
2983 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002984
Nate Begeman9008ca62009-04-27 18:41:29 +00002985 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002986 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002987
Nate Begeman9008ca62009-04-27 18:41:29 +00002988 for (int i = 1; i < NumElts; ++i)
2989 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002990 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002991
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002992 return true;
2993}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002994
Nate Begeman9008ca62009-04-27 18:41:29 +00002995bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
2996 SmallVector<int, 8> M;
2997 N->getMask(M);
2998 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002999}
3000
Evan Cheng017dcc62006-04-21 01:05:10 +00003001/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3002/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003003/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003004static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003005 bool V2IsSplat = false, bool V2IsUndef = false) {
3006 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003007 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003008 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003009
Nate Begeman9008ca62009-04-27 18:41:29 +00003010 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003011 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003012
Nate Begeman9008ca62009-04-27 18:41:29 +00003013 for (int i = 1; i < NumOps; ++i)
3014 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3015 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3016 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003017 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003018
Evan Cheng39623da2006-04-20 08:58:49 +00003019 return true;
3020}
3021
Nate Begeman9008ca62009-04-27 18:41:29 +00003022static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003023 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003024 SmallVector<int, 8> M;
3025 N->getMask(M);
3026 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003027}
3028
Evan Chengd9539472006-04-14 21:59:03 +00003029/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3030/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003031bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
3032 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003033 return false;
3034
3035 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00003036 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003037 int Elt = N->getMaskElt(i);
3038 if (Elt >= 0 && Elt != 1)
3039 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00003040 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003041
3042 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003043 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003044 int Elt = N->getMaskElt(i);
3045 if (Elt >= 0 && Elt != 3)
3046 return false;
3047 if (Elt == 3)
3048 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003049 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003050 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00003051 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003052 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003053}
3054
3055/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3056/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003057bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
3058 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003059 return false;
3060
3061 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00003062 for (unsigned i = 0; i < 2; ++i)
3063 if (N->getMaskElt(i) > 0)
3064 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003065
3066 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003067 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003068 int Elt = N->getMaskElt(i);
3069 if (Elt >= 0 && Elt != 2)
3070 return false;
3071 if (Elt == 2)
3072 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003073 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003074 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003075 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003076}
3077
Evan Cheng0b457f02008-09-25 20:50:48 +00003078/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3079/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003080bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3081 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003082
Nate Begeman9008ca62009-04-27 18:41:29 +00003083 for (int i = 0; i < e; ++i)
3084 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003085 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003086 for (int i = 0; i < e; ++i)
3087 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003088 return false;
3089 return true;
3090}
3091
Evan Cheng63d33002006-03-22 08:01:21 +00003092/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003093/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003094unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003095 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3096 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3097
Evan Chengb9df0ca2006-03-22 02:53:00 +00003098 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3099 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003100 for (int i = 0; i < NumOperands; ++i) {
3101 int Val = SVOp->getMaskElt(NumOperands-i-1);
3102 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003103 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003104 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003105 if (i != NumOperands - 1)
3106 Mask <<= Shift;
3107 }
Evan Cheng63d33002006-03-22 08:01:21 +00003108 return Mask;
3109}
3110
Evan Cheng506d3df2006-03-29 23:07:14 +00003111/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003112/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003113unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003114 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003115 unsigned Mask = 0;
3116 // 8 nodes, but we only care about the last 4.
3117 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003118 int Val = SVOp->getMaskElt(i);
3119 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003120 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003121 if (i != 4)
3122 Mask <<= 2;
3123 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003124 return Mask;
3125}
3126
3127/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003128/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003129unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003130 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003131 unsigned Mask = 0;
3132 // 8 nodes, but we only care about the first 4.
3133 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003134 int Val = SVOp->getMaskElt(i);
3135 if (Val >= 0)
3136 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003137 if (i != 0)
3138 Mask <<= 2;
3139 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003140 return Mask;
3141}
3142
Nate Begemana09008b2009-10-19 02:17:23 +00003143/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3144/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3145unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3146 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3147 EVT VVT = N->getValueType(0);
3148 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3149 int Val = 0;
3150
3151 unsigned i, e;
3152 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3153 Val = SVOp->getMaskElt(i);
3154 if (Val >= 0)
3155 break;
3156 }
3157 return (Val - i) * EltSize;
3158}
3159
Evan Cheng37b73872009-07-30 08:33:02 +00003160/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3161/// constant +0.0.
3162bool X86::isZeroNode(SDValue Elt) {
3163 return ((isa<ConstantSDNode>(Elt) &&
3164 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
3165 (isa<ConstantFPSDNode>(Elt) &&
3166 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3167}
3168
Nate Begeman9008ca62009-04-27 18:41:29 +00003169/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3170/// their permute mask.
3171static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3172 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003173 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003174 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003175 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003176
Nate Begeman5a5ca152009-04-29 05:20:52 +00003177 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003178 int idx = SVOp->getMaskElt(i);
3179 if (idx < 0)
3180 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003181 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003182 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003183 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003184 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003185 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003186 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3187 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003188}
3189
Evan Cheng779ccea2007-12-07 21:30:01 +00003190/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3191/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003192static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003193 unsigned NumElems = VT.getVectorNumElements();
3194 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003195 int idx = Mask[i];
3196 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003197 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003198 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003199 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003200 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003201 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003202 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003203}
3204
Evan Cheng533a0aa2006-04-19 20:35:22 +00003205/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3206/// match movhlps. The lower half elements should come from upper half of
3207/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003208/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003209static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3210 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003211 return false;
3212 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003213 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003214 return false;
3215 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003216 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003217 return false;
3218 return true;
3219}
3220
Evan Cheng5ced1d82006-04-06 23:23:56 +00003221/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003222/// is promoted to a vector. It also returns the LoadSDNode by reference if
3223/// required.
3224static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003225 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3226 return false;
3227 N = N->getOperand(0).getNode();
3228 if (!ISD::isNON_EXTLoad(N))
3229 return false;
3230 if (LD)
3231 *LD = cast<LoadSDNode>(N);
3232 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003233}
3234
Evan Cheng533a0aa2006-04-19 20:35:22 +00003235/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3236/// match movlp{s|d}. The lower half elements should come from lower half of
3237/// V1 (and in order), and the upper half elements should come from the upper
3238/// half of V2 (and in order). And since V1 will become the source of the
3239/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003240static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3241 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003242 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003243 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003244 // Is V2 is a vector load, don't do this transformation. We will try to use
3245 // load folding shufps op.
3246 if (ISD::isNON_EXTLoad(V2))
3247 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003248
Nate Begeman5a5ca152009-04-29 05:20:52 +00003249 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003250
Evan Cheng533a0aa2006-04-19 20:35:22 +00003251 if (NumElems != 2 && NumElems != 4)
3252 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003253 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003254 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003255 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003256 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003257 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003258 return false;
3259 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003260}
3261
Evan Cheng39623da2006-04-20 08:58:49 +00003262/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3263/// all the same.
3264static bool isSplatVector(SDNode *N) {
3265 if (N->getOpcode() != ISD::BUILD_VECTOR)
3266 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003267
Dan Gohman475871a2008-07-27 21:46:04 +00003268 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003269 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3270 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003271 return false;
3272 return true;
3273}
3274
Evan Cheng213d2cf2007-05-17 18:45:50 +00003275/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003276/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003277/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003278static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003279 SDValue V1 = N->getOperand(0);
3280 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003281 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3282 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003283 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003284 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003285 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003286 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3287 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003288 if (Opc != ISD::BUILD_VECTOR ||
3289 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003290 return false;
3291 } else if (Idx >= 0) {
3292 unsigned Opc = V1.getOpcode();
3293 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3294 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003295 if (Opc != ISD::BUILD_VECTOR ||
3296 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003297 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003298 }
3299 }
3300 return true;
3301}
3302
3303/// getZeroVector - Returns a vector of specified type with all zero elements.
3304///
Owen Andersone50ed302009-08-10 22:56:29 +00003305static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003306 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003307 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003308
Chris Lattner8a594482007-11-25 00:24:49 +00003309 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3310 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003311 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003312 if (VT.getSizeInBits() == 64) { // MMX
Owen Anderson825b72b2009-08-11 20:47:22 +00003313 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3314 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003315 } else if (HasSSE2) { // SSE2
Owen Anderson825b72b2009-08-11 20:47:22 +00003316 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3317 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003318 } else { // SSE1
Owen Anderson825b72b2009-08-11 20:47:22 +00003319 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3320 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003321 }
Dale Johannesenace16102009-02-03 19:33:06 +00003322 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003323}
3324
Chris Lattner8a594482007-11-25 00:24:49 +00003325/// getOnesVector - Returns a vector of specified type with all bits set.
3326///
Owen Andersone50ed302009-08-10 22:56:29 +00003327static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003328 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003329
Chris Lattner8a594482007-11-25 00:24:49 +00003330 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3331 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003332 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003333 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003334 if (VT.getSizeInBits() == 64) // MMX
Owen Anderson825b72b2009-08-11 20:47:22 +00003335 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Chris Lattner8a594482007-11-25 00:24:49 +00003336 else // SSE
Owen Anderson825b72b2009-08-11 20:47:22 +00003337 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Dale Johannesenace16102009-02-03 19:33:06 +00003338 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003339}
3340
3341
Evan Cheng39623da2006-04-20 08:58:49 +00003342/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3343/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003344static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003345 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003346 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003347
Evan Cheng39623da2006-04-20 08:58:49 +00003348 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003349 SmallVector<int, 8> MaskVec;
3350 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003351
Nate Begeman5a5ca152009-04-29 05:20:52 +00003352 for (unsigned i = 0; i != NumElems; ++i) {
3353 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003354 MaskVec[i] = NumElems;
3355 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003356 }
Evan Cheng39623da2006-04-20 08:58:49 +00003357 }
Evan Cheng39623da2006-04-20 08:58:49 +00003358 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003359 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3360 SVOp->getOperand(1), &MaskVec[0]);
3361 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003362}
3363
Evan Cheng017dcc62006-04-21 01:05:10 +00003364/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3365/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003366static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003367 SDValue V2) {
3368 unsigned NumElems = VT.getVectorNumElements();
3369 SmallVector<int, 8> Mask;
3370 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003371 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003372 Mask.push_back(i);
3373 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003374}
3375
Nate Begeman9008ca62009-04-27 18:41:29 +00003376/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003377static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003378 SDValue V2) {
3379 unsigned NumElems = VT.getVectorNumElements();
3380 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003381 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003382 Mask.push_back(i);
3383 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003384 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003385 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003386}
3387
Nate Begeman9008ca62009-04-27 18:41:29 +00003388/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003389static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003390 SDValue V2) {
3391 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003392 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003393 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003394 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003395 Mask.push_back(i + Half);
3396 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003397 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003398 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003399}
3400
Evan Cheng0c0f83f2008-04-05 00:30:36 +00003401/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Eric Christopherfd179292009-08-27 18:07:15 +00003402static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00003403 bool HasSSE2) {
3404 if (SV->getValueType(0).getVectorNumElements() <= 4)
3405 return SDValue(SV, 0);
Eric Christopherfd179292009-08-27 18:07:15 +00003406
Owen Anderson825b72b2009-08-11 20:47:22 +00003407 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003408 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003409 DebugLoc dl = SV->getDebugLoc();
3410 SDValue V1 = SV->getOperand(0);
3411 int NumElems = VT.getVectorNumElements();
3412 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003413
Nate Begeman9008ca62009-04-27 18:41:29 +00003414 // unpack elements to the correct location
3415 while (NumElems > 4) {
3416 if (EltNo < NumElems/2) {
3417 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3418 } else {
3419 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3420 EltNo -= NumElems/2;
3421 }
3422 NumElems >>= 1;
3423 }
Eric Christopherfd179292009-08-27 18:07:15 +00003424
Nate Begeman9008ca62009-04-27 18:41:29 +00003425 // Perform the splat.
3426 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Dale Johannesenace16102009-02-03 19:33:06 +00003427 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003428 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
3429 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003430}
3431
Evan Chengba05f722006-04-21 23:03:30 +00003432/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003433/// vector of zero or undef vector. This produces a shuffle where the low
3434/// element of V2 is swizzled into the zero/undef vector, landing at element
3435/// 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 +00003436static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003437 bool isZero, bool HasSSE2,
3438 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003439 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003440 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003441 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3442 unsigned NumElems = VT.getVectorNumElements();
3443 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003444 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003445 // If this is the insertion idx, put the low elt of V2 here.
3446 MaskVec.push_back(i == Idx ? NumElems : i);
3447 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003448}
3449
Evan Chengf26ffe92008-05-29 08:22:04 +00003450/// getNumOfConsecutiveZeros - Return the number of elements in a result of
3451/// a shuffle that is zero.
3452static
Nate Begeman9008ca62009-04-27 18:41:29 +00003453unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, int NumElems,
3454 bool Low, SelectionDAG &DAG) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003455 unsigned NumZeros = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003456 for (int i = 0; i < NumElems; ++i) {
Evan Chengab262272008-06-25 20:52:59 +00003457 unsigned Index = Low ? i : NumElems-i-1;
Nate Begeman9008ca62009-04-27 18:41:29 +00003458 int Idx = SVOp->getMaskElt(Index);
3459 if (Idx < 0) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003460 ++NumZeros;
3461 continue;
3462 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003463 SDValue Elt = DAG.getShuffleScalarElt(SVOp, Index);
Evan Cheng37b73872009-07-30 08:33:02 +00003464 if (Elt.getNode() && X86::isZeroNode(Elt))
Evan Chengf26ffe92008-05-29 08:22:04 +00003465 ++NumZeros;
3466 else
3467 break;
3468 }
3469 return NumZeros;
3470}
3471
3472/// isVectorShift - Returns true if the shuffle can be implemented as a
3473/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003474/// FIXME: split into pslldqi, psrldqi, palignr variants.
3475static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00003476 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
John McCallb1fb4492010-04-07 01:49:15 +00003477 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
Evan Chengf26ffe92008-05-29 08:22:04 +00003478
3479 isLeft = true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003480 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, true, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003481 if (!NumZeros) {
3482 isLeft = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003483 NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, false, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003484 if (!NumZeros)
3485 return false;
3486 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003487 bool SeenV1 = false;
3488 bool SeenV2 = false;
John McCallb1fb4492010-04-07 01:49:15 +00003489 for (unsigned i = NumZeros; i < NumElems; ++i) {
3490 unsigned Val = isLeft ? (i - NumZeros) : i;
3491 int Idx_ = SVOp->getMaskElt(isLeft ? i : (i - NumZeros));
3492 if (Idx_ < 0)
Evan Chengf26ffe92008-05-29 08:22:04 +00003493 continue;
John McCallb1fb4492010-04-07 01:49:15 +00003494 unsigned Idx = (unsigned) Idx_;
Nate Begeman9008ca62009-04-27 18:41:29 +00003495 if (Idx < NumElems)
Evan Chengf26ffe92008-05-29 08:22:04 +00003496 SeenV1 = true;
3497 else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003498 Idx -= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00003499 SeenV2 = true;
3500 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003501 if (Idx != Val)
Evan Chengf26ffe92008-05-29 08:22:04 +00003502 return false;
3503 }
3504 if (SeenV1 && SeenV2)
3505 return false;
3506
Nate Begeman9008ca62009-04-27 18:41:29 +00003507 ShVal = SeenV1 ? SVOp->getOperand(0) : SVOp->getOperand(1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003508 ShAmt = NumZeros;
3509 return true;
3510}
3511
3512
Evan Chengc78d3b42006-04-24 18:01:45 +00003513/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3514///
Dan Gohman475871a2008-07-27 21:46:04 +00003515static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003516 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00003517 SelectionDAG &DAG,
3518 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003519 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00003520 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003521
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003522 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003523 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003524 bool First = true;
3525 for (unsigned i = 0; i < 16; ++i) {
3526 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3527 if (ThisIsNonZero && First) {
3528 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003529 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003530 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003531 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003532 First = false;
3533 }
3534
3535 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00003536 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003537 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3538 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003539 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003540 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00003541 }
3542 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003543 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
3544 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
3545 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00003546 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003547 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00003548 } else
3549 ThisElt = LastElt;
3550
Gabor Greifba36cb52008-08-28 21:40:38 +00003551 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00003552 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00003553 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00003554 }
3555 }
3556
Owen Anderson825b72b2009-08-11 20:47:22 +00003557 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00003558}
3559
Bill Wendlinga348c562007-03-22 18:42:45 +00003560/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003561///
Dan Gohman475871a2008-07-27 21:46:04 +00003562static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00003563 unsigned NumNonZero, unsigned NumZero,
3564 SelectionDAG &DAG,
3565 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003566 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00003567 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003568
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003569 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003570 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003571 bool First = true;
3572 for (unsigned i = 0; i < 8; ++i) {
3573 bool isNonZero = (NonZeros & (1 << i)) != 0;
3574 if (isNonZero) {
3575 if (First) {
3576 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003577 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003578 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003579 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003580 First = false;
3581 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003582 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003583 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00003584 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00003585 }
3586 }
3587
3588 return V;
3589}
3590
Evan Chengf26ffe92008-05-29 08:22:04 +00003591/// getVShift - Return a vector logical shift node.
3592///
Owen Andersone50ed302009-08-10 22:56:29 +00003593static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00003594 unsigned NumBits, SelectionDAG &DAG,
3595 const TargetLowering &TLI, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003596 bool isMMX = VT.getSizeInBits() == 64;
Owen Anderson825b72b2009-08-11 20:47:22 +00003597 EVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00003598 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Dale Johannesenace16102009-02-03 19:33:06 +00003599 SrcOp = DAG.getNode(ISD::BIT_CONVERT, dl, ShVT, SrcOp);
3600 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3601 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00003602 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00003603}
3604
Dan Gohman475871a2008-07-27 21:46:04 +00003605SDValue
Evan Chengc3630942009-12-09 21:00:30 +00003606X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00003607 SelectionDAG &DAG) const {
Evan Chengc3630942009-12-09 21:00:30 +00003608
3609 // Check if the scalar load can be widened into a vector load. And if
3610 // the address is "base + cst" see if the cst can be "absorbed" into
3611 // the shuffle mask.
3612 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
3613 SDValue Ptr = LD->getBasePtr();
3614 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
3615 return SDValue();
3616 EVT PVT = LD->getValueType(0);
3617 if (PVT != MVT::i32 && PVT != MVT::f32)
3618 return SDValue();
3619
3620 int FI = -1;
3621 int64_t Offset = 0;
3622 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
3623 FI = FINode->getIndex();
3624 Offset = 0;
3625 } else if (Ptr.getOpcode() == ISD::ADD &&
3626 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
3627 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
3628 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
3629 Offset = Ptr.getConstantOperandVal(1);
3630 Ptr = Ptr.getOperand(0);
3631 } else {
3632 return SDValue();
3633 }
3634
3635 SDValue Chain = LD->getChain();
3636 // Make sure the stack object alignment is at least 16.
3637 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3638 if (DAG.InferPtrAlignment(Ptr) < 16) {
3639 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00003640 // Can't change the alignment. FIXME: It's possible to compute
3641 // the exact stack offset and reference FI + adjust offset instead.
3642 // If someone *really* cares about this. That's the way to implement it.
3643 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00003644 } else {
3645 MFI->setObjectAlignment(FI, 16);
3646 }
3647 }
3648
3649 // (Offset % 16) must be multiple of 4. Then address is then
3650 // Ptr + (Offset & ~15).
3651 if (Offset < 0)
3652 return SDValue();
3653 if ((Offset % 16) & 3)
3654 return SDValue();
3655 int64_t StartOffset = Offset & ~15;
3656 if (StartOffset)
3657 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
3658 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
3659
3660 int EltNo = (Offset - StartOffset) >> 2;
3661 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
3662 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
David Greene67c9d422010-02-15 16:53:33 +00003663 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,LD->getSrcValue(),0,
3664 false, false, 0);
Evan Chengc3630942009-12-09 21:00:30 +00003665 // Canonicalize it to a v4i32 shuffle.
3666 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32, V1);
3667 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3668 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
3669 DAG.getUNDEF(MVT::v4i32), &Mask[0]));
3670 }
3671
3672 return SDValue();
3673}
3674
Nate Begeman1449f292010-03-24 22:19:06 +00003675/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
3676/// vector of type 'VT', see if the elements can be replaced by a single large
3677/// load which has the same value as a build_vector whose operands are 'elts'.
3678///
3679/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
3680///
3681/// FIXME: we'd also like to handle the case where the last elements are zero
3682/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
3683/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00003684static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
3685 DebugLoc &dl, SelectionDAG &DAG) {
3686 EVT EltVT = VT.getVectorElementType();
3687 unsigned NumElems = Elts.size();
3688
Nate Begemanfdea31a2010-03-24 20:49:50 +00003689 LoadSDNode *LDBase = NULL;
3690 unsigned LastLoadedElt = -1U;
Nate Begeman1449f292010-03-24 22:19:06 +00003691
3692 // For each element in the initializer, see if we've found a load or an undef.
3693 // If we don't find an initial load element, or later load elements are
3694 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00003695 for (unsigned i = 0; i < NumElems; ++i) {
3696 SDValue Elt = Elts[i];
3697
3698 if (!Elt.getNode() ||
3699 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
3700 return SDValue();
3701 if (!LDBase) {
3702 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
3703 return SDValue();
3704 LDBase = cast<LoadSDNode>(Elt.getNode());
3705 LastLoadedElt = i;
3706 continue;
3707 }
3708 if (Elt.getOpcode() == ISD::UNDEF)
3709 continue;
3710
3711 LoadSDNode *LD = cast<LoadSDNode>(Elt);
3712 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
3713 return SDValue();
3714 LastLoadedElt = i;
3715 }
Nate Begeman1449f292010-03-24 22:19:06 +00003716
3717 // If we have found an entire vector of loads and undefs, then return a large
3718 // load of the entire vector width starting at the base pointer. If we found
3719 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00003720 if (LastLoadedElt == NumElems - 1) {
3721 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
3722 return DAG.getLoad(VT, dl, LDBase->getChain(), LDBase->getBasePtr(),
3723 LDBase->getSrcValue(), LDBase->getSrcValueOffset(),
3724 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
3725 return DAG.getLoad(VT, dl, LDBase->getChain(), LDBase->getBasePtr(),
3726 LDBase->getSrcValue(), LDBase->getSrcValueOffset(),
3727 LDBase->isVolatile(), LDBase->isNonTemporal(),
3728 LDBase->getAlignment());
3729 } else if (NumElems == 4 && LastLoadedElt == 1) {
3730 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
3731 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
3732 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2);
3733 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, ResNode);
3734 }
3735 return SDValue();
3736}
3737
Evan Chengc3630942009-12-09 21:00:30 +00003738SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00003739X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003740 DebugLoc dl = Op.getDebugLoc();
Chris Lattner8a594482007-11-25 00:24:49 +00003741 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif327ef032008-08-28 23:19:51 +00003742 if (ISD::isBuildVectorAllZeros(Op.getNode())
3743 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattner8a594482007-11-25 00:24:49 +00003744 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3745 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3746 // eliminated on x86-32 hosts.
Owen Anderson825b72b2009-08-11 20:47:22 +00003747 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
Chris Lattner8a594482007-11-25 00:24:49 +00003748 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003749
Gabor Greifba36cb52008-08-28 21:40:38 +00003750 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00003751 return getOnesVector(Op.getValueType(), DAG, dl);
3752 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00003753 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003754
Owen Andersone50ed302009-08-10 22:56:29 +00003755 EVT VT = Op.getValueType();
3756 EVT ExtVT = VT.getVectorElementType();
3757 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003758
3759 unsigned NumElems = Op.getNumOperands();
3760 unsigned NumZero = 0;
3761 unsigned NumNonZero = 0;
3762 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003763 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00003764 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003765 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003766 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00003767 if (Elt.getOpcode() == ISD::UNDEF)
3768 continue;
3769 Values.insert(Elt);
3770 if (Elt.getOpcode() != ISD::Constant &&
3771 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003772 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00003773 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00003774 NumZero++;
3775 else {
3776 NonZeros |= (1 << i);
3777 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003778 }
3779 }
3780
Dan Gohman7f321562007-06-25 16:23:39 +00003781 if (NumNonZero == 0) {
Chris Lattner8a594482007-11-25 00:24:49 +00003782 // All undef vector. Return an UNDEF. All zero vectors were handled above.
Dale Johannesene8d72302009-02-06 23:05:02 +00003783 return DAG.getUNDEF(VT);
Dan Gohman7f321562007-06-25 16:23:39 +00003784 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003785
Chris Lattner67f453a2008-03-09 05:42:06 +00003786 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00003787 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003788 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00003789 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00003790
Chris Lattner62098042008-03-09 01:05:04 +00003791 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3792 // the value are obviously zero, truncate the value to i32 and do the
3793 // insertion that way. Only do this if the value is non-constant or if the
3794 // value is a constant being inserted into element 0. It is cheaper to do
3795 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00003796 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00003797 (!IsAllConstants || Idx == 0)) {
3798 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3799 // Handle MMX and SSE both.
Owen Anderson825b72b2009-08-11 20:47:22 +00003800 EVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3801 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Scott Michelfdc40a02009-02-17 22:15:04 +00003802
Chris Lattner62098042008-03-09 01:05:04 +00003803 // Truncate the value (which may itself be a constant) to i32, and
3804 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00003805 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00003806 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00003807 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3808 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00003809
Chris Lattner62098042008-03-09 01:05:04 +00003810 // Now we have our 32-bit value zero extended in the low element of
3811 // a vector. If Idx != 0, swizzle it into place.
3812 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003813 SmallVector<int, 4> Mask;
3814 Mask.push_back(Idx);
3815 for (unsigned i = 1; i != VecElts; ++i)
3816 Mask.push_back(i);
3817 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00003818 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00003819 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003820 }
Dale Johannesenace16102009-02-03 19:33:06 +00003821 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00003822 }
3823 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003824
Chris Lattner19f79692008-03-08 22:59:52 +00003825 // If we have a constant or non-constant insertion into the low element of
3826 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3827 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00003828 // depending on what the source datatype is.
3829 if (Idx == 0) {
3830 if (NumZero == 0) {
3831 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00003832 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
3833 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00003834 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
3835 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
3836 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
3837 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00003838 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
3839 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
3840 EVT MiddleVT = VT.getSizeInBits() == 64 ? MVT::v2i32 : MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00003841 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
3842 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3843 Subtarget->hasSSE2(), DAG);
3844 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Item);
3845 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003846 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003847
3848 // Is it a vector logical left shift?
3849 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00003850 X86::isZeroNode(Op.getOperand(0)) &&
3851 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003852 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00003853 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00003854 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00003855 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00003856 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00003857 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003858
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003859 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00003860 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003861
Chris Lattner19f79692008-03-08 22:59:52 +00003862 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3863 // is a non-constant being inserted into an element other than the low one,
3864 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3865 // movd/movss) to move this into the low element, then shuffle it into
3866 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003867 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00003868 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00003869
Evan Cheng0db9fe62006-04-25 20:13:52 +00003870 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003871 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3872 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00003873 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003874 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00003875 MaskVec.push_back(i == Idx ? 0 : 1);
3876 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003877 }
3878 }
3879
Chris Lattner67f453a2008-03-09 05:42:06 +00003880 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00003881 if (Values.size() == 1) {
3882 if (EVTBits == 32) {
3883 // Instead of a shuffle like this:
3884 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
3885 // Check if it's possible to issue this instead.
3886 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
3887 unsigned Idx = CountTrailingZeros_32(NonZeros);
3888 SDValue Item = Op.getOperand(Idx);
3889 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
3890 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
3891 }
Dan Gohman475871a2008-07-27 21:46:04 +00003892 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00003893 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003894
Dan Gohmana3941172007-07-24 22:55:08 +00003895 // A vector full of immediates; various special cases are already
3896 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003897 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00003898 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00003899
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003900 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003901 if (EVTBits == 64) {
3902 if (NumNonZero == 1) {
3903 // One half is zero or undef.
3904 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00003905 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003906 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00003907 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3908 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00003909 }
Dan Gohman475871a2008-07-27 21:46:04 +00003910 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00003911 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003912
3913 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00003914 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00003915 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003916 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003917 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003918 }
3919
Bill Wendling826f36f2007-03-28 00:57:11 +00003920 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00003921 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003922 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003923 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003924 }
3925
3926 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003927 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00003928 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003929 if (NumElems == 4 && NumZero > 0) {
3930 for (unsigned i = 0; i < 4; ++i) {
3931 bool isZero = !(NonZeros & (1 << i));
3932 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003933 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003934 else
Dale Johannesenace16102009-02-03 19:33:06 +00003935 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003936 }
3937
3938 for (unsigned i = 0; i < 2; ++i) {
3939 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3940 default: break;
3941 case 0:
3942 V[i] = V[i*2]; // Must be a zero vector.
3943 break;
3944 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00003945 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003946 break;
3947 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00003948 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003949 break;
3950 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00003951 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003952 break;
3953 }
3954 }
3955
Nate Begeman9008ca62009-04-27 18:41:29 +00003956 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003957 bool Reverse = (NonZeros & 0x3) == 2;
3958 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003959 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003960 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3961 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003962 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
3963 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003964 }
3965
Nate Begemanfdea31a2010-03-24 20:49:50 +00003966 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
3967 // Check for a build vector of consecutive loads.
3968 for (unsigned i = 0; i < NumElems; ++i)
3969 V[i] = Op.getOperand(i);
3970
3971 // Check for elements which are consecutive loads.
3972 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
3973 if (LD.getNode())
3974 return LD;
3975
3976 // For SSE 4.1, use inserts into undef.
3977 if (getSubtarget()->hasSSE41()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003978 V[0] = DAG.getUNDEF(VT);
3979 for (unsigned i = 0; i < NumElems; ++i)
3980 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
3981 V[0] = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, V[0],
3982 Op.getOperand(i), DAG.getIntPtrConstant(i));
3983 return V[0];
3984 }
Nate Begemanfdea31a2010-03-24 20:49:50 +00003985
3986 // Otherwise, expand into a number of unpckl*
Evan Cheng0db9fe62006-04-25 20:13:52 +00003987 // e.g. for v4f32
3988 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3989 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3990 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Evan Cheng0db9fe62006-04-25 20:13:52 +00003991 for (unsigned i = 0; i < NumElems; ++i)
Dale Johannesenace16102009-02-03 19:33:06 +00003992 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003993 NumElems >>= 1;
3994 while (NumElems != 0) {
3995 for (unsigned i = 0; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003996 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + NumElems]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003997 NumElems >>= 1;
3998 }
3999 return V[0];
4000 }
Dan Gohman475871a2008-07-27 21:46:04 +00004001 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004002}
4003
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004004SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004005X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004006 // We support concatenate two MMX registers and place them in a MMX
4007 // register. This is better than doing a stack convert.
4008 DebugLoc dl = Op.getDebugLoc();
4009 EVT ResVT = Op.getValueType();
4010 assert(Op.getNumOperands() == 2);
4011 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
4012 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
4013 int Mask[2];
4014 SDValue InVec = DAG.getNode(ISD::BIT_CONVERT,dl, MVT::v1i64, Op.getOperand(0));
4015 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4016 InVec = Op.getOperand(1);
4017 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4018 unsigned NumElts = ResVT.getVectorNumElements();
4019 VecOp = DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
4020 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
4021 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
4022 } else {
4023 InVec = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v1i64, InVec);
4024 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4025 Mask[0] = 0; Mask[1] = 2;
4026 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
4027 }
4028 return DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
4029}
4030
Nate Begemanb9a47b82009-02-23 08:49:38 +00004031// v8i16 shuffles - Prefer shuffles in the following order:
4032// 1. [all] pshuflw, pshufhw, optional move
4033// 2. [ssse3] 1 x pshufb
4034// 3. [ssse3] 2 x pshufb + 1 x por
4035// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004036static
Nate Begeman9008ca62009-04-27 18:41:29 +00004037SDValue LowerVECTOR_SHUFFLEv8i16(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00004038 SelectionDAG &DAG,
4039 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004040 SDValue V1 = SVOp->getOperand(0);
4041 SDValue V2 = SVOp->getOperand(1);
4042 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004043 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00004044
Nate Begemanb9a47b82009-02-23 08:49:38 +00004045 // Determine if more than 1 of the words in each of the low and high quadwords
4046 // of the result come from the same quadword of one of the two inputs. Undef
4047 // mask values count as coming from any quadword, for better codegen.
4048 SmallVector<unsigned, 4> LoQuad(4);
4049 SmallVector<unsigned, 4> HiQuad(4);
4050 BitVector InputQuads(4);
4051 for (unsigned i = 0; i < 8; ++i) {
4052 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00004053 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004054 MaskVals.push_back(EltIdx);
4055 if (EltIdx < 0) {
4056 ++Quad[0];
4057 ++Quad[1];
4058 ++Quad[2];
4059 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00004060 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004061 }
4062 ++Quad[EltIdx / 4];
4063 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00004064 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004065
Nate Begemanb9a47b82009-02-23 08:49:38 +00004066 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004067 unsigned MaxQuad = 1;
4068 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004069 if (LoQuad[i] > MaxQuad) {
4070 BestLoQuad = i;
4071 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004072 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004073 }
4074
Nate Begemanb9a47b82009-02-23 08:49:38 +00004075 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004076 MaxQuad = 1;
4077 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004078 if (HiQuad[i] > MaxQuad) {
4079 BestHiQuad = i;
4080 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004081 }
4082 }
4083
Nate Begemanb9a47b82009-02-23 08:49:38 +00004084 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00004085 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00004086 // single pshufb instruction is necessary. If There are more than 2 input
4087 // quads, disable the next transformation since it does not help SSSE3.
4088 bool V1Used = InputQuads[0] || InputQuads[1];
4089 bool V2Used = InputQuads[2] || InputQuads[3];
4090 if (TLI.getSubtarget()->hasSSSE3()) {
4091 if (InputQuads.count() == 2 && V1Used && V2Used) {
4092 BestLoQuad = InputQuads.find_first();
4093 BestHiQuad = InputQuads.find_next(BestLoQuad);
4094 }
4095 if (InputQuads.count() > 2) {
4096 BestLoQuad = -1;
4097 BestHiQuad = -1;
4098 }
4099 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004100
Nate Begemanb9a47b82009-02-23 08:49:38 +00004101 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
4102 // the shuffle mask. If a quad is scored as -1, that means that it contains
4103 // words from all 4 input quadwords.
4104 SDValue NewV;
4105 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004106 SmallVector<int, 8> MaskV;
4107 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
4108 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00004109 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004110 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V1),
4111 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V2), &MaskV[0]);
4112 NewV = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004113
Nate Begemanb9a47b82009-02-23 08:49:38 +00004114 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
4115 // source words for the shuffle, to aid later transformations.
4116 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00004117 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00004118 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004119 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00004120 if (idx != (int)i)
4121 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004122 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00004123 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004124 AllWordsInNewV = false;
4125 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00004126 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004127
Nate Begemanb9a47b82009-02-23 08:49:38 +00004128 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
4129 if (AllWordsInNewV) {
4130 for (int i = 0; i != 8; ++i) {
4131 int idx = MaskVals[i];
4132 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004133 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004134 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004135 if ((idx != i) && idx < 4)
4136 pshufhw = false;
4137 if ((idx != i) && idx > 3)
4138 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00004139 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00004140 V1 = NewV;
4141 V2Used = false;
4142 BestLoQuad = 0;
4143 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004144 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004145
Nate Begemanb9a47b82009-02-23 08:49:38 +00004146 // If we've eliminated the use of V2, and the new mask is a pshuflw or
4147 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00004148 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Eric Christopherfd179292009-08-27 18:07:15 +00004149 return DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00004150 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Evan Cheng14b32e12007-12-11 01:46:18 +00004151 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004152 }
Eric Christopherfd179292009-08-27 18:07:15 +00004153
Nate Begemanb9a47b82009-02-23 08:49:38 +00004154 // If we have SSSE3, and all words of the result are from 1 input vector,
4155 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
4156 // is present, fall back to case 4.
4157 if (TLI.getSubtarget()->hasSSSE3()) {
4158 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004159
Nate Begemanb9a47b82009-02-23 08:49:38 +00004160 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00004161 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00004162 // mask, and elements that come from V1 in the V2 mask, so that the two
4163 // results can be OR'd together.
4164 bool TwoInputs = V1Used && V2Used;
4165 for (unsigned i = 0; i != 8; ++i) {
4166 int EltIdx = MaskVals[i] * 2;
4167 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004168 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4169 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004170 continue;
4171 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004172 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
4173 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004174 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004175 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004176 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004177 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004178 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004179 if (!TwoInputs)
Owen Anderson825b72b2009-08-11 20:47:22 +00004180 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004181
Nate Begemanb9a47b82009-02-23 08:49:38 +00004182 // Calculate the shuffle mask for the second input, shuffle it, and
4183 // OR it with the first shuffled input.
4184 pshufbMask.clear();
4185 for (unsigned i = 0; i != 8; ++i) {
4186 int EltIdx = MaskVals[i] * 2;
4187 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004188 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4189 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004190 continue;
4191 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004192 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
4193 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004194 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004195 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00004196 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004197 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004198 MVT::v16i8, &pshufbMask[0], 16));
4199 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
4200 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004201 }
4202
4203 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
4204 // and update MaskVals with new element order.
4205 BitVector InOrder(8);
4206 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004207 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004208 for (int i = 0; i != 4; ++i) {
4209 int idx = MaskVals[i];
4210 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004211 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004212 InOrder.set(i);
4213 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004214 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004215 InOrder.set(i);
4216 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004217 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004218 }
4219 }
4220 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004221 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00004222 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004223 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004224 }
Eric Christopherfd179292009-08-27 18:07:15 +00004225
Nate Begemanb9a47b82009-02-23 08:49:38 +00004226 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
4227 // and update MaskVals with the new element order.
4228 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004229 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004230 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004231 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004232 for (unsigned i = 4; i != 8; ++i) {
4233 int idx = MaskVals[i];
4234 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004235 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004236 InOrder.set(i);
4237 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004238 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004239 InOrder.set(i);
4240 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004241 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004242 }
4243 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004244 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004245 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004246 }
Eric Christopherfd179292009-08-27 18:07:15 +00004247
Nate Begemanb9a47b82009-02-23 08:49:38 +00004248 // In case BestHi & BestLo were both -1, which means each quadword has a word
4249 // from each of the four input quadwords, calculate the InOrder bitvector now
4250 // before falling through to the insert/extract cleanup.
4251 if (BestLoQuad == -1 && BestHiQuad == -1) {
4252 NewV = V1;
4253 for (int i = 0; i != 8; ++i)
4254 if (MaskVals[i] < 0 || MaskVals[i] == i)
4255 InOrder.set(i);
4256 }
Eric Christopherfd179292009-08-27 18:07:15 +00004257
Nate Begemanb9a47b82009-02-23 08:49:38 +00004258 // The other elements are put in the right place using pextrw and pinsrw.
4259 for (unsigned i = 0; i != 8; ++i) {
4260 if (InOrder[i])
4261 continue;
4262 int EltIdx = MaskVals[i];
4263 if (EltIdx < 0)
4264 continue;
4265 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004266 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004267 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00004268 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004269 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004270 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004271 DAG.getIntPtrConstant(i));
4272 }
4273 return NewV;
4274}
4275
4276// v16i8 shuffles - Prefer shuffles in the following order:
4277// 1. [ssse3] 1 x pshufb
4278// 2. [ssse3] 2 x pshufb + 1 x por
4279// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
4280static
Nate Begeman9008ca62009-04-27 18:41:29 +00004281SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00004282 SelectionDAG &DAG,
4283 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004284 SDValue V1 = SVOp->getOperand(0);
4285 SDValue V2 = SVOp->getOperand(1);
4286 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004287 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00004288 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00004289
Nate Begemanb9a47b82009-02-23 08:49:38 +00004290 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00004291 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00004292 // present, fall back to case 3.
4293 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
4294 bool V1Only = true;
4295 bool V2Only = true;
4296 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004297 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00004298 if (EltIdx < 0)
4299 continue;
4300 if (EltIdx < 16)
4301 V2Only = false;
4302 else
4303 V1Only = false;
4304 }
Eric Christopherfd179292009-08-27 18:07:15 +00004305
Nate Begemanb9a47b82009-02-23 08:49:38 +00004306 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
4307 if (TLI.getSubtarget()->hasSSSE3()) {
4308 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004309
Nate Begemanb9a47b82009-02-23 08:49:38 +00004310 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00004311 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004312 //
4313 // Otherwise, we have elements from both input vectors, and must zero out
4314 // elements that come from V2 in the first mask, and V1 in the second mask
4315 // so that we can OR them together.
4316 bool TwoInputs = !(V1Only || V2Only);
4317 for (unsigned i = 0; i != 16; ++i) {
4318 int EltIdx = MaskVals[i];
4319 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004320 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004321 continue;
4322 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004323 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004324 }
4325 // If all the elements are from V2, assign it to V1 and return after
4326 // building the first pshufb.
4327 if (V2Only)
4328 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00004329 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004330 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004331 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004332 if (!TwoInputs)
4333 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00004334
Nate Begemanb9a47b82009-02-23 08:49:38 +00004335 // Calculate the shuffle mask for the second input, shuffle it, and
4336 // OR it with the first shuffled input.
4337 pshufbMask.clear();
4338 for (unsigned i = 0; i != 16; ++i) {
4339 int EltIdx = MaskVals[i];
4340 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004341 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004342 continue;
4343 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004344 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004345 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004346 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004347 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004348 MVT::v16i8, &pshufbMask[0], 16));
4349 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004350 }
Eric Christopherfd179292009-08-27 18:07:15 +00004351
Nate Begemanb9a47b82009-02-23 08:49:38 +00004352 // No SSSE3 - Calculate in place words and then fix all out of place words
4353 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
4354 // the 16 different words that comprise the two doublequadword input vectors.
Owen Anderson825b72b2009-08-11 20:47:22 +00004355 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
4356 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004357 SDValue NewV = V2Only ? V2 : V1;
4358 for (int i = 0; i != 8; ++i) {
4359 int Elt0 = MaskVals[i*2];
4360 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00004361
Nate Begemanb9a47b82009-02-23 08:49:38 +00004362 // This word of the result is all undef, skip it.
4363 if (Elt0 < 0 && Elt1 < 0)
4364 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004365
Nate Begemanb9a47b82009-02-23 08:49:38 +00004366 // This word of the result is already in the correct place, skip it.
4367 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
4368 continue;
4369 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
4370 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004371
Nate Begemanb9a47b82009-02-23 08:49:38 +00004372 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
4373 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
4374 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00004375
4376 // If Elt0 and Elt1 are defined, are consecutive, and can be load
4377 // using a single extract together, load it and store it.
4378 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004379 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004380 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00004381 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004382 DAG.getIntPtrConstant(i));
4383 continue;
4384 }
4385
Nate Begemanb9a47b82009-02-23 08:49:38 +00004386 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00004387 // source byte is not also odd, shift the extracted word left 8 bits
4388 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004389 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004390 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004391 DAG.getIntPtrConstant(Elt1 / 2));
4392 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004393 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004394 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004395 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004396 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
4397 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004398 }
4399 // If Elt0 is defined, extract it from the appropriate source. If the
4400 // source byte is not also even, shift the extracted word right 8 bits. If
4401 // Elt1 was also defined, OR the extracted values together before
4402 // inserting them in the result.
4403 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004404 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004405 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
4406 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004407 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004408 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004409 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004410 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
4411 DAG.getConstant(0x00FF, MVT::i16));
4412 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00004413 : InsElt0;
4414 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004415 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004416 DAG.getIntPtrConstant(i));
4417 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004418 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004419}
4420
Evan Cheng7a831ce2007-12-15 03:00:47 +00004421/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
4422/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
4423/// done when every pair / quad of shuffle mask elements point to elements in
4424/// the right sequence. e.g.
Evan Cheng14b32e12007-12-11 01:46:18 +00004425/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
4426static
Nate Begeman9008ca62009-04-27 18:41:29 +00004427SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
4428 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00004429 const TargetLowering &TLI, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00004430 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00004431 SDValue V1 = SVOp->getOperand(0);
4432 SDValue V2 = SVOp->getOperand(1);
4433 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00004434 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Owen Anderson825b72b2009-08-11 20:47:22 +00004435 EVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Owen Andersone50ed302009-08-10 22:56:29 +00004436 EVT MaskEltVT = MaskVT.getVectorElementType();
4437 EVT NewVT = MaskVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00004438 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004439 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004440 case MVT::v4f32: NewVT = MVT::v2f64; break;
4441 case MVT::v4i32: NewVT = MVT::v2i64; break;
4442 case MVT::v8i16: NewVT = MVT::v4i32; break;
4443 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004444 }
4445
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004446 if (NewWidth == 2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004447 if (VT.isInteger())
Owen Anderson825b72b2009-08-11 20:47:22 +00004448 NewVT = MVT::v2i64;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004449 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004450 NewVT = MVT::v2f64;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004451 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004452 int Scale = NumElems / NewWidth;
4453 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00004454 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004455 int StartIdx = -1;
4456 for (int j = 0; j < Scale; ++j) {
4457 int EltIdx = SVOp->getMaskElt(i+j);
4458 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004459 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00004460 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00004461 StartIdx = EltIdx - (EltIdx % Scale);
4462 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00004463 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004464 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004465 if (StartIdx == -1)
4466 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00004467 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004468 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004469 }
4470
Dale Johannesenace16102009-02-03 19:33:06 +00004471 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V1);
4472 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00004473 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004474}
4475
Evan Chengd880b972008-05-09 21:53:03 +00004476/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004477///
Owen Andersone50ed302009-08-10 22:56:29 +00004478static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00004479 SDValue SrcOp, SelectionDAG &DAG,
4480 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004481 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004482 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00004483 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00004484 LD = dyn_cast<LoadSDNode>(SrcOp);
4485 if (!LD) {
4486 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
4487 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00004488 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
4489 if ((ExtVT.SimpleTy != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00004490 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4491 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00004492 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004493 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00004494 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Dale Johannesenace16102009-02-03 19:33:06 +00004495 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4496 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
4497 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
4498 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00004499 SrcOp.getOperand(0)
4500 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004501 }
4502 }
4503 }
4504
Dale Johannesenace16102009-02-03 19:33:06 +00004505 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4506 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004507 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00004508 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004509}
4510
Evan Chengace3c172008-07-22 21:13:36 +00004511/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
4512/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004513static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00004514LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
4515 SDValue V1 = SVOp->getOperand(0);
4516 SDValue V2 = SVOp->getOperand(1);
4517 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004518 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00004519
Evan Chengace3c172008-07-22 21:13:36 +00004520 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00004521 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00004522 SmallVector<int, 8> Mask1(4U, -1);
4523 SmallVector<int, 8> PermMask;
4524 SVOp->getMask(PermMask);
4525
Evan Chengace3c172008-07-22 21:13:36 +00004526 unsigned NumHi = 0;
4527 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00004528 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004529 int Idx = PermMask[i];
4530 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004531 Locs[i] = std::make_pair(-1, -1);
4532 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004533 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
4534 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004535 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00004536 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004537 NumLo++;
4538 } else {
4539 Locs[i] = std::make_pair(1, NumHi);
4540 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004541 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004542 NumHi++;
4543 }
4544 }
4545 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004546
Evan Chengace3c172008-07-22 21:13:36 +00004547 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004548 // If no more than two elements come from either vector. This can be
4549 // implemented with two shuffles. First shuffle gather the elements.
4550 // The second shuffle, which takes the first shuffle as both of its
4551 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00004552 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004553
Nate Begeman9008ca62009-04-27 18:41:29 +00004554 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00004555
Evan Chengace3c172008-07-22 21:13:36 +00004556 for (unsigned i = 0; i != 4; ++i) {
4557 if (Locs[i].first == -1)
4558 continue;
4559 else {
4560 unsigned Idx = (i < 2) ? 0 : 4;
4561 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004562 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004563 }
4564 }
4565
Nate Begeman9008ca62009-04-27 18:41:29 +00004566 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004567 } else if (NumLo == 3 || NumHi == 3) {
4568 // Otherwise, we must have three elements from one vector, call it X, and
4569 // one element from the other, call it Y. First, use a shufps to build an
4570 // intermediate vector with the one element from Y and the element from X
4571 // that will be in the same half in the final destination (the indexes don't
4572 // matter). Then, use a shufps to build the final vector, taking the half
4573 // containing the element from Y from the intermediate, and the other half
4574 // from X.
4575 if (NumHi == 3) {
4576 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00004577 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004578 std::swap(V1, V2);
4579 }
4580
4581 // Find the element from V2.
4582 unsigned HiIndex;
4583 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004584 int Val = PermMask[HiIndex];
4585 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004586 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004587 if (Val >= 4)
4588 break;
4589 }
4590
Nate Begeman9008ca62009-04-27 18:41:29 +00004591 Mask1[0] = PermMask[HiIndex];
4592 Mask1[1] = -1;
4593 Mask1[2] = PermMask[HiIndex^1];
4594 Mask1[3] = -1;
4595 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004596
4597 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004598 Mask1[0] = PermMask[0];
4599 Mask1[1] = PermMask[1];
4600 Mask1[2] = HiIndex & 1 ? 6 : 4;
4601 Mask1[3] = HiIndex & 1 ? 4 : 6;
4602 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004603 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004604 Mask1[0] = HiIndex & 1 ? 2 : 0;
4605 Mask1[1] = HiIndex & 1 ? 0 : 2;
4606 Mask1[2] = PermMask[2];
4607 Mask1[3] = PermMask[3];
4608 if (Mask1[2] >= 0)
4609 Mask1[2] += 4;
4610 if (Mask1[3] >= 0)
4611 Mask1[3] += 4;
4612 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004613 }
Evan Chengace3c172008-07-22 21:13:36 +00004614 }
4615
4616 // Break it into (shuffle shuffle_hi, shuffle_lo).
4617 Locs.clear();
Nate Begeman9008ca62009-04-27 18:41:29 +00004618 SmallVector<int,8> LoMask(4U, -1);
4619 SmallVector<int,8> HiMask(4U, -1);
4620
4621 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00004622 unsigned MaskIdx = 0;
4623 unsigned LoIdx = 0;
4624 unsigned HiIdx = 2;
4625 for (unsigned i = 0; i != 4; ++i) {
4626 if (i == 2) {
4627 MaskPtr = &HiMask;
4628 MaskIdx = 1;
4629 LoIdx = 0;
4630 HiIdx = 2;
4631 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004632 int Idx = PermMask[i];
4633 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004634 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00004635 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004636 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004637 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004638 LoIdx++;
4639 } else {
4640 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004641 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004642 HiIdx++;
4643 }
4644 }
4645
Nate Begeman9008ca62009-04-27 18:41:29 +00004646 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
4647 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
4648 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00004649 for (unsigned i = 0; i != 4; ++i) {
4650 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004651 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00004652 } else {
4653 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004654 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00004655 }
4656 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004657 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00004658}
4659
Dan Gohman475871a2008-07-27 21:46:04 +00004660SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004661X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00004662 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00004663 SDValue V1 = Op.getOperand(0);
4664 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00004665 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004666 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00004667 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004668 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004669 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
4670 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00004671 bool V1IsSplat = false;
4672 bool V2IsSplat = false;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004673
Nate Begeman9008ca62009-04-27 18:41:29 +00004674 if (isZeroShuffle(SVOp))
Dale Johannesenace16102009-02-03 19:33:06 +00004675 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004676
Nate Begeman9008ca62009-04-27 18:41:29 +00004677 // Promote splats to v4f32.
4678 if (SVOp->isSplat()) {
Eric Christopherfd179292009-08-27 18:07:15 +00004679 if (isMMX || NumElems < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004680 return Op;
4681 return PromoteSplat(SVOp, DAG, Subtarget->hasSSE2());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004682 }
4683
Evan Cheng7a831ce2007-12-15 03:00:47 +00004684 // If the shuffle can be profitably rewritten as a narrower shuffle, then
4685 // do it!
Owen Anderson825b72b2009-08-11 20:47:22 +00004686 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004687 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004688 if (NewOp.getNode())
Scott Michelfdc40a02009-02-17 22:15:04 +00004689 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004690 LowerVECTOR_SHUFFLE(NewOp, DAG));
Owen Anderson825b72b2009-08-11 20:47:22 +00004691 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Evan Cheng7a831ce2007-12-15 03:00:47 +00004692 // FIXME: Figure out a cleaner way to do this.
4693 // Try to make use of movq to zero out the top part.
Gabor Greifba36cb52008-08-28 21:40:38 +00004694 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004695 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004696 if (NewOp.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004697 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
4698 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
4699 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004700 }
Gabor Greifba36cb52008-08-28 21:40:38 +00004701 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004702 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
4703 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
Evan Chengd880b972008-05-09 21:53:03 +00004704 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Nate Begeman9008ca62009-04-27 18:41:29 +00004705 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004706 }
4707 }
Eric Christopherfd179292009-08-27 18:07:15 +00004708
Nate Begeman9008ca62009-04-27 18:41:29 +00004709 if (X86::isPSHUFDMask(SVOp))
4710 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004711
Evan Chengf26ffe92008-05-29 08:22:04 +00004712 // Check if this can be converted into a logical shift.
4713 bool isLeft = false;
4714 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00004715 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00004716 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00004717 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00004718 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004719 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00004720 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00004721 EVT EltVT = VT.getVectorElementType();
4722 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004723 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004724 }
Eric Christopherfd179292009-08-27 18:07:15 +00004725
Nate Begeman9008ca62009-04-27 18:41:29 +00004726 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004727 if (V1IsUndef)
4728 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00004729 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004730 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Nate Begemanfb8ead02008-07-25 19:05:58 +00004731 if (!isMMX)
4732 return Op;
Evan Cheng7e2ff772008-05-08 00:57:18 +00004733 }
Eric Christopherfd179292009-08-27 18:07:15 +00004734
Nate Begeman9008ca62009-04-27 18:41:29 +00004735 // FIXME: fold these into legal mask.
4736 if (!isMMX && (X86::isMOVSHDUPMask(SVOp) ||
4737 X86::isMOVSLDUPMask(SVOp) ||
4738 X86::isMOVHLPSMask(SVOp) ||
Nate Begeman0b10b912009-11-07 23:17:15 +00004739 X86::isMOVLHPSMask(SVOp) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00004740 X86::isMOVLPMask(SVOp)))
Evan Cheng9bbbb982006-10-25 20:48:19 +00004741 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004742
Nate Begeman9008ca62009-04-27 18:41:29 +00004743 if (ShouldXformToMOVHLPS(SVOp) ||
4744 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
4745 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004746
Evan Chengf26ffe92008-05-29 08:22:04 +00004747 if (isShift) {
4748 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00004749 EVT EltVT = VT.getVectorElementType();
4750 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004751 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004752 }
Eric Christopherfd179292009-08-27 18:07:15 +00004753
Evan Cheng9eca5e82006-10-25 21:49:50 +00004754 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00004755 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4756 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00004757 V1IsSplat = isSplatVector(V1.getNode());
4758 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00004759
Chris Lattner8a594482007-11-25 00:24:49 +00004760 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00004761 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004762 Op = CommuteVectorShuffle(SVOp, DAG);
4763 SVOp = cast<ShuffleVectorSDNode>(Op);
4764 V1 = SVOp->getOperand(0);
4765 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00004766 std::swap(V1IsSplat, V2IsSplat);
4767 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00004768 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00004769 }
4770
Nate Begeman9008ca62009-04-27 18:41:29 +00004771 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
4772 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00004773 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00004774 return V1;
4775 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
4776 // the instruction selector will not match, so get a canonical MOVL with
4777 // swapped operands to undo the commute.
4778 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00004779 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004780
Nate Begeman9008ca62009-04-27 18:41:29 +00004781 if (X86::isUNPCKL_v_undef_Mask(SVOp) ||
4782 X86::isUNPCKH_v_undef_Mask(SVOp) ||
4783 X86::isUNPCKLMask(SVOp) ||
4784 X86::isUNPCKHMask(SVOp))
Evan Chengd9b8e402006-10-16 06:36:00 +00004785 return Op;
Evan Chenge1113032006-10-04 18:33:38 +00004786
Evan Cheng9bbbb982006-10-25 20:48:19 +00004787 if (V2IsSplat) {
4788 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004789 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00004790 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00004791 SDValue NewMask = NormalizeMask(SVOp, DAG);
4792 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
4793 if (NSVOp != SVOp) {
4794 if (X86::isUNPCKLMask(NSVOp, true)) {
4795 return NewMask;
4796 } else if (X86::isUNPCKHMask(NSVOp, true)) {
4797 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004798 }
4799 }
4800 }
4801
Evan Cheng9eca5e82006-10-25 21:49:50 +00004802 if (Commuted) {
4803 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00004804 // FIXME: this seems wrong.
4805 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
4806 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
4807 if (X86::isUNPCKL_v_undef_Mask(NewSVOp) ||
4808 X86::isUNPCKH_v_undef_Mask(NewSVOp) ||
4809 X86::isUNPCKLMask(NewSVOp) ||
4810 X86::isUNPCKHMask(NewSVOp))
4811 return NewOp;
Evan Cheng9eca5e82006-10-25 21:49:50 +00004812 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004813
Nate Begemanb9a47b82009-02-23 08:49:38 +00004814 // FIXME: for mmx, bitcast v2i32 to v4i16 for shuffle.
Nate Begeman9008ca62009-04-27 18:41:29 +00004815
4816 // Normalize the node to match x86 shuffle ops if needed
4817 if (!isMMX && V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
4818 return CommuteVectorShuffle(SVOp, DAG);
4819
4820 // Check for legal shuffle and return?
4821 SmallVector<int, 16> PermMask;
4822 SVOp->getMask(PermMask);
4823 if (isShuffleMaskLegal(PermMask, VT))
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004824 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004825
Evan Cheng14b32e12007-12-11 01:46:18 +00004826 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00004827 if (VT == MVT::v8i16) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004828 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(SVOp, DAG, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004829 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00004830 return NewOp;
4831 }
4832
Owen Anderson825b72b2009-08-11 20:47:22 +00004833 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004834 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004835 if (NewOp.getNode())
4836 return NewOp;
4837 }
Eric Christopherfd179292009-08-27 18:07:15 +00004838
Evan Chengace3c172008-07-22 21:13:36 +00004839 // Handle all 4 wide cases with a number of shuffles except for MMX.
4840 if (NumElems == 4 && !isMMX)
Nate Begeman9008ca62009-04-27 18:41:29 +00004841 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004842
Dan Gohman475871a2008-07-27 21:46:04 +00004843 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004844}
4845
Dan Gohman475871a2008-07-27 21:46:04 +00004846SDValue
4847X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00004848 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00004849 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004850 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004851 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004852 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004853 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004854 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004855 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004856 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004857 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00004858 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
4859 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
4860 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004861 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4862 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004863 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004864 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00004865 Op.getOperand(0)),
4866 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00004867 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004868 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004869 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004870 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004871 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00004872 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00004873 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4874 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004875 // result has a single use which is a store or a bitcast to i32. And in
4876 // the case of a store, it's not worth it if the index is a constant 0,
4877 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00004878 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00004879 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004880 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004881 if ((User->getOpcode() != ISD::STORE ||
4882 (isa<ConstantSDNode>(Op.getOperand(1)) &&
4883 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman171c11e2008-04-16 02:32:24 +00004884 (User->getOpcode() != ISD::BIT_CONVERT ||
Owen Anderson825b72b2009-08-11 20:47:22 +00004885 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00004886 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00004887 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
4888 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004889 Op.getOperand(0)),
4890 Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004891 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Extract);
4892 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00004893 // ExtractPS works with constant index.
4894 if (isa<ConstantSDNode>(Op.getOperand(1)))
4895 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004896 }
Dan Gohman475871a2008-07-27 21:46:04 +00004897 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004898}
4899
4900
Dan Gohman475871a2008-07-27 21:46:04 +00004901SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004902X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
4903 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004904 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00004905 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004906
Evan Cheng62a3f152008-03-24 21:52:23 +00004907 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004908 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004909 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00004910 return Res;
4911 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00004912
Owen Andersone50ed302009-08-10 22:56:29 +00004913 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004914 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004915 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004916 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004917 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004918 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004919 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004920 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4921 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Scott Michelfdc40a02009-02-17 22:15:04 +00004922 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004923 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00004924 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004925 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00004926 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00004927 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004928 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00004929 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004930 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004931 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004932 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004933 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004934 if (Idx == 0)
4935 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004936
Evan Cheng0db9fe62006-04-25 20:13:52 +00004937 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00004938 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00004939 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00004940 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00004941 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004942 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004943 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004944 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004945 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4946 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4947 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004948 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004949 if (Idx == 0)
4950 return Op;
4951
4952 // UNPCKHPD the element to the lowest double word, then movsd.
4953 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4954 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00004955 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00004956 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00004957 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00004958 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004959 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004960 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004961 }
4962
Dan Gohman475871a2008-07-27 21:46:04 +00004963 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004964}
4965
Dan Gohman475871a2008-07-27 21:46:04 +00004966SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004967X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
4968 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00004969 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00004970 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004971 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004972
Dan Gohman475871a2008-07-27 21:46:04 +00004973 SDValue N0 = Op.getOperand(0);
4974 SDValue N1 = Op.getOperand(1);
4975 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004976
Dan Gohman8a55ce42009-09-23 21:02:20 +00004977 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00004978 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00004979 unsigned Opc;
4980 if (VT == MVT::v8i16)
4981 Opc = X86ISD::PINSRW;
4982 else if (VT == MVT::v4i16)
4983 Opc = X86ISD::MMX_PINSRW;
4984 else if (VT == MVT::v16i8)
4985 Opc = X86ISD::PINSRB;
4986 else
4987 Opc = X86ISD::PINSRB;
4988
Nate Begeman14d12ca2008-02-11 04:19:36 +00004989 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4990 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00004991 if (N1.getValueType() != MVT::i32)
4992 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
4993 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004994 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00004995 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004996 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004997 // Bits [7:6] of the constant are the source select. This will always be
4998 // zero here. The DAG Combiner may combine an extract_elt index into these
4999 // bits. For example (insert (extract, 3), 2) could be matched by putting
5000 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00005001 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00005002 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00005003 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00005004 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005005 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00005006 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00005007 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00005008 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00005009 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00005010 // PINSR* works with constant index.
5011 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00005012 }
Dan Gohman475871a2008-07-27 21:46:04 +00005013 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005014}
5015
Dan Gohman475871a2008-07-27 21:46:04 +00005016SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005017X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005018 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00005019 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005020
5021 if (Subtarget->hasSSE41())
5022 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
5023
Dan Gohman8a55ce42009-09-23 21:02:20 +00005024 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00005025 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00005026
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005027 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005028 SDValue N0 = Op.getOperand(0);
5029 SDValue N1 = Op.getOperand(1);
5030 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00005031
Dan Gohman8a55ce42009-09-23 21:02:20 +00005032 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00005033 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
5034 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00005035 if (N1.getValueType() != MVT::i32)
5036 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
5037 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005038 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00005039 return DAG.getNode(VT == MVT::v8i16 ? X86ISD::PINSRW : X86ISD::MMX_PINSRW,
5040 dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005041 }
Dan Gohman475871a2008-07-27 21:46:04 +00005042 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005043}
5044
Dan Gohman475871a2008-07-27 21:46:04 +00005045SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005046X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005047 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00005048 if (Op.getValueType() == MVT::v2f32)
5049 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f32,
5050 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i32,
5051 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32,
Evan Cheng52672b82008-07-22 18:39:19 +00005052 Op.getOperand(0))));
5053
Owen Anderson825b72b2009-08-11 20:47:22 +00005054 if (Op.getValueType() == MVT::v1i64 && Op.getOperand(0).getValueType() == MVT::i64)
5055 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00005056
Owen Anderson825b72b2009-08-11 20:47:22 +00005057 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
5058 EVT VT = MVT::v2i32;
5059 switch (Op.getValueType().getSimpleVT().SimpleTy) {
Evan Chengefec7512008-02-18 23:04:32 +00005060 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00005061 case MVT::v16i8:
5062 case MVT::v8i16:
5063 VT = MVT::v4i32;
Evan Chengefec7512008-02-18 23:04:32 +00005064 break;
5065 }
Dale Johannesenace16102009-02-03 19:33:06 +00005066 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(),
5067 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005068}
5069
Bill Wendling056292f2008-09-16 21:48:12 +00005070// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
5071// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
5072// one of the above mentioned nodes. It has to be wrapped because otherwise
5073// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
5074// be used to form addressing mode. These wrapped nodes will be selected
5075// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00005076SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005077X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005078 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00005079
Chris Lattner41621a22009-06-26 19:22:52 +00005080 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5081 // global base reg.
5082 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005083 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005084 CodeModel::Model M = getTargetMachine().getCodeModel();
5085
Chris Lattner4f066492009-07-11 20:29:19 +00005086 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005087 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005088 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005089 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005090 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005091 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005092 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005093
Evan Cheng1606e8e2009-03-13 07:51:59 +00005094 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00005095 CP->getAlignment(),
5096 CP->getOffset(), OpFlag);
5097 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00005098 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005099 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00005100 if (OpFlag) {
5101 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005102 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005103 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005104 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005105 }
5106
5107 return Result;
5108}
5109
Dan Gohmand858e902010-04-17 15:26:15 +00005110SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00005111 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00005112
Chris Lattner18c59872009-06-27 04:16:01 +00005113 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5114 // global base reg.
5115 unsigned char OpFlag = 0;
5116 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005117 CodeModel::Model M = getTargetMachine().getCodeModel();
5118
Chris Lattner4f066492009-07-11 20:29:19 +00005119 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005120 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005121 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005122 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005123 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005124 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005125 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005126
Chris Lattner18c59872009-06-27 04:16:01 +00005127 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
5128 OpFlag);
5129 DebugLoc DL = JT->getDebugLoc();
5130 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005131
Chris Lattner18c59872009-06-27 04:16:01 +00005132 // With PIC, the address is actually $g + Offset.
5133 if (OpFlag) {
5134 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5135 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005136 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00005137 Result);
5138 }
Eric Christopherfd179292009-08-27 18:07:15 +00005139
Chris Lattner18c59872009-06-27 04:16:01 +00005140 return Result;
5141}
5142
5143SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005144X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00005145 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00005146
Chris Lattner18c59872009-06-27 04:16:01 +00005147 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5148 // global base reg.
5149 unsigned char OpFlag = 0;
5150 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005151 CodeModel::Model M = getTargetMachine().getCodeModel();
5152
Chris Lattner4f066492009-07-11 20:29:19 +00005153 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005154 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005155 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005156 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005157 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005158 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005159 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005160
Chris Lattner18c59872009-06-27 04:16:01 +00005161 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00005162
Chris Lattner18c59872009-06-27 04:16:01 +00005163 DebugLoc DL = Op.getDebugLoc();
5164 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005165
5166
Chris Lattner18c59872009-06-27 04:16:01 +00005167 // With PIC, the address is actually $g + Offset.
5168 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00005169 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00005170 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5171 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005172 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00005173 Result);
5174 }
Eric Christopherfd179292009-08-27 18:07:15 +00005175
Chris Lattner18c59872009-06-27 04:16:01 +00005176 return Result;
5177}
5178
Dan Gohman475871a2008-07-27 21:46:04 +00005179SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005180X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00005181 // Create the TargetBlockAddressAddress node.
5182 unsigned char OpFlags =
5183 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00005184 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00005185 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00005186 DebugLoc dl = Op.getDebugLoc();
5187 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
5188 /*isTarget=*/true, OpFlags);
5189
Dan Gohmanf705adb2009-10-30 01:28:02 +00005190 if (Subtarget->isPICStyleRIPRel() &&
5191 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00005192 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
5193 else
5194 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00005195
Dan Gohman29cbade2009-11-20 23:18:13 +00005196 // With PIC, the address is actually $g + Offset.
5197 if (isGlobalRelativeToPICBase(OpFlags)) {
5198 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5199 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
5200 Result);
5201 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00005202
5203 return Result;
5204}
5205
5206SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00005207X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00005208 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00005209 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00005210 // Create the TargetGlobalAddress node, folding in the constant
5211 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00005212 unsigned char OpFlags =
5213 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005214 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00005215 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005216 if (OpFlags == X86II::MO_NO_FLAG &&
5217 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00005218 // A direct static reference to a global.
Dale Johannesen60b3ba02009-07-21 00:12:29 +00005219 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00005220 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005221 } else {
Chris Lattnerb1acd682009-06-27 05:39:56 +00005222 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00005223 }
Eric Christopherfd179292009-08-27 18:07:15 +00005224
Chris Lattner4f066492009-07-11 20:29:19 +00005225 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005226 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00005227 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
5228 else
5229 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00005230
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005231 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00005232 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00005233 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5234 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005235 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005236 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005237
Chris Lattner36c25012009-07-10 07:34:39 +00005238 // For globals that require a load from a stub to get the address, emit the
5239 // load.
5240 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00005241 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
David Greene67c9d422010-02-15 16:53:33 +00005242 PseudoSourceValue::getGOT(), 0, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005243
Dan Gohman6520e202008-10-18 02:06:02 +00005244 // If there was a non-zero offset that we didn't fold, create an explicit
5245 // addition for it.
5246 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00005247 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00005248 DAG.getConstant(Offset, getPointerTy()));
5249
Evan Cheng0db9fe62006-04-25 20:13:52 +00005250 return Result;
5251}
5252
Evan Chengda43bcf2008-09-24 00:05:32 +00005253SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005254X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00005255 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00005256 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005257 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00005258}
5259
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005260static SDValue
5261GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00005262 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00005263 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00005264 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Owen Anderson825b72b2009-08-11 20:47:22 +00005265 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005266 DebugLoc dl = GA->getDebugLoc();
5267 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
5268 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00005269 GA->getOffset(),
5270 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005271 if (InFlag) {
5272 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00005273 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005274 } else {
5275 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00005276 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005277 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00005278
5279 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00005280 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00005281
Rafael Espindola15f1b662009-04-24 12:59:40 +00005282 SDValue Flag = Chain.getValue(1);
5283 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005284}
5285
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005286// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00005287static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005288LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005289 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005290 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00005291 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
5292 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005293 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005294 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005295 InFlag = Chain.getValue(1);
5296
Chris Lattnerb903bed2009-06-26 21:20:29 +00005297 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005298}
5299
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005300// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00005301static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005302LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005303 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00005304 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
5305 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005306}
5307
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005308// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
5309// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00005310static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005311 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00005312 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00005313 DebugLoc dl = GA->getDebugLoc();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005314 // Get the Thread Pointer
Rafael Espindola094fad32009-04-08 21:14:34 +00005315 SDValue Base = DAG.getNode(X86ISD::SegmentBaseAddress,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005316 DebugLoc(), PtrVT,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00005317 DAG.getRegister(is64Bit? X86::FS : X86::GS,
Owen Anderson825b72b2009-08-11 20:47:22 +00005318 MVT::i32));
Rafael Espindola094fad32009-04-08 21:14:34 +00005319
5320 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Base,
David Greene67c9d422010-02-15 16:53:33 +00005321 NULL, 0, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00005322
Chris Lattnerb903bed2009-06-26 21:20:29 +00005323 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005324 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
5325 // initialexec.
5326 unsigned WrapperKind = X86ISD::Wrapper;
5327 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00005328 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00005329 } else if (is64Bit) {
5330 assert(model == TLSModel::InitialExec);
5331 OperandFlags = X86II::MO_GOTTPOFF;
5332 WrapperKind = X86ISD::WrapperRIP;
5333 } else {
5334 assert(model == TLSModel::InitialExec);
5335 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00005336 }
Eric Christopherfd179292009-08-27 18:07:15 +00005337
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005338 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
5339 // exec)
Chris Lattner4150c082009-06-21 02:22:34 +00005340 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00005341 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00005342 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00005343
Rafael Espindola9a580232009-02-27 13:37:18 +00005344 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00005345 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
David Greene67c9d422010-02-15 16:53:33 +00005346 PseudoSourceValue::getGOT(), 0, false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00005347
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005348 // The address of the thread local variable is the add of the thread
5349 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00005350 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005351}
5352
Dan Gohman475871a2008-07-27 21:46:04 +00005353SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005354X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005355 // TODO: implement the "local dynamic" model
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00005356 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005357 assert(Subtarget->isTargetELF() &&
5358 "TLS not implemented for non-ELF targets");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005359 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00005360 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00005361
Chris Lattnerb903bed2009-06-26 21:20:29 +00005362 // If GV is an alias then use the aliasee for determining
5363 // thread-localness.
5364 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
5365 GV = GA->resolveAliasedGlobal(false);
Eric Christopherfd179292009-08-27 18:07:15 +00005366
Chris Lattnerb903bed2009-06-26 21:20:29 +00005367 TLSModel::Model model = getTLSModel(GV,
5368 getTargetMachine().getRelocationModel());
Eric Christopherfd179292009-08-27 18:07:15 +00005369
Chris Lattnerb903bed2009-06-26 21:20:29 +00005370 switch (model) {
5371 case TLSModel::GeneralDynamic:
5372 case TLSModel::LocalDynamic: // not implemented
5373 if (Subtarget->is64Bit())
Rafael Espindola9a580232009-02-27 13:37:18 +00005374 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
Chris Lattnerb903bed2009-06-26 21:20:29 +00005375 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00005376
Chris Lattnerb903bed2009-06-26 21:20:29 +00005377 case TLSModel::InitialExec:
5378 case TLSModel::LocalExec:
5379 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
5380 Subtarget->is64Bit());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005381 }
Eric Christopherfd179292009-08-27 18:07:15 +00005382
Torok Edwinc23197a2009-07-14 16:55:14 +00005383 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00005384 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005385}
5386
Evan Cheng0db9fe62006-04-25 20:13:52 +00005387
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005388/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00005389/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00005390SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00005391 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00005392 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005393 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005394 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005395 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00005396 SDValue ShOpLo = Op.getOperand(0);
5397 SDValue ShOpHi = Op.getOperand(1);
5398 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00005399 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00005400 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00005401 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00005402
Dan Gohman475871a2008-07-27 21:46:04 +00005403 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005404 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00005405 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
5406 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005407 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005408 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
5409 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005410 }
Evan Chenge3413162006-01-09 18:33:28 +00005411
Owen Anderson825b72b2009-08-11 20:47:22 +00005412 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
5413 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00005414 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00005415 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00005416
Dan Gohman475871a2008-07-27 21:46:04 +00005417 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00005418 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00005419 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
5420 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00005421
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005422 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00005423 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
5424 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005425 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005426 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
5427 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005428 }
5429
Dan Gohman475871a2008-07-27 21:46:04 +00005430 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00005431 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005432}
Evan Chenga3195e82006-01-12 22:54:21 +00005433
Dan Gohmand858e902010-04-17 15:26:15 +00005434SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
5435 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005436 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00005437
5438 if (SrcVT.isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005439 if (SrcVT == MVT::v2i32 && Op.getValueType() == MVT::v2f64) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005440 return Op;
5441 }
5442 return SDValue();
5443 }
5444
Owen Anderson825b72b2009-08-11 20:47:22 +00005445 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00005446 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00005447
Eli Friedman36df4992009-05-27 00:47:34 +00005448 // These are really Legal; return the operand so the caller accepts it as
5449 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00005450 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00005451 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00005452 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00005453 Subtarget->is64Bit()) {
5454 return Op;
5455 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005456
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005457 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005458 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005459 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005460 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005461 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00005462 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00005463 StackSlot,
David Greene67c9d422010-02-15 16:53:33 +00005464 PseudoSourceValue::getFixedStack(SSFI), 0,
5465 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00005466 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
5467}
Evan Cheng0db9fe62006-04-25 20:13:52 +00005468
Owen Andersone50ed302009-08-10 22:56:29 +00005469SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Dale Johannesen8d908eb2010-05-15 18:51:12 +00005470 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00005471 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005472 // Build the FILD
Eli Friedman948e95a2009-05-23 09:59:16 +00005473 DebugLoc dl = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00005474 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00005475 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005476 if (useSSE)
Owen Anderson825b72b2009-08-11 20:47:22 +00005477 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00005478 else
Owen Anderson825b72b2009-08-11 20:47:22 +00005479 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005480 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Dale Johannesenace16102009-02-03 19:33:06 +00005481 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD, dl,
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005482 Tys, Ops, array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005483
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005484 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005485 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00005486 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005487
5488 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
5489 // shouldn't be necessary except that RFP cannot be live across
5490 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005491 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005492 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005493 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00005494 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005495 SDValue Ops[] = {
5496 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
5497 };
5498 Chain = DAG.getNode(X86ISD::FST, dl, Tys, Ops, array_lengthof(Ops));
Dale Johannesenace16102009-02-03 19:33:06 +00005499 Result = DAG.getLoad(Op.getValueType(), dl, Chain, StackSlot,
David Greene67c9d422010-02-15 16:53:33 +00005500 PseudoSourceValue::getFixedStack(SSFI), 0,
5501 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005502 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005503
Evan Cheng0db9fe62006-04-25 20:13:52 +00005504 return Result;
5505}
5506
Bill Wendling8b8a6362009-01-17 03:56:04 +00005507// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00005508SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
5509 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00005510 // This algorithm is not obvious. Here it is in C code, more or less:
5511 /*
5512 double uint64_to_double( uint32_t hi, uint32_t lo ) {
5513 static const __m128i exp = { 0x4330000045300000ULL, 0 };
5514 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00005515
Bill Wendling8b8a6362009-01-17 03:56:04 +00005516 // Copy ints to xmm registers.
5517 __m128i xh = _mm_cvtsi32_si128( hi );
5518 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00005519
Bill Wendling8b8a6362009-01-17 03:56:04 +00005520 // Combine into low half of a single xmm register.
5521 __m128i x = _mm_unpacklo_epi32( xh, xl );
5522 __m128d d;
5523 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00005524
Bill Wendling8b8a6362009-01-17 03:56:04 +00005525 // Merge in appropriate exponents to give the integer bits the right
5526 // magnitude.
5527 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00005528
Bill Wendling8b8a6362009-01-17 03:56:04 +00005529 // Subtract away the biases to deal with the IEEE-754 double precision
5530 // implicit 1.
5531 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00005532
Bill Wendling8b8a6362009-01-17 03:56:04 +00005533 // All conversions up to here are exact. The correctly rounded result is
5534 // calculated using the current rounding mode using the following
5535 // horizontal add.
5536 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
5537 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
5538 // store doesn't really need to be here (except
5539 // maybe to zero the other double)
5540 return sd;
5541 }
5542 */
Dale Johannesen040225f2008-10-21 23:07:49 +00005543
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005544 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00005545 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00005546
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005547 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00005548 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00005549 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
5550 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
5551 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
5552 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00005553 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005554 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005555
Bill Wendling8b8a6362009-01-17 03:56:04 +00005556 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00005557 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005558 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00005559 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005560 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00005561 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005562 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005563
Owen Anderson825b72b2009-08-11 20:47:22 +00005564 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5565 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00005566 Op.getOperand(0),
5567 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005568 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5569 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00005570 Op.getOperand(0),
5571 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005572 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
5573 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005574 PseudoSourceValue::getConstantPool(), 0,
David Greene67c9d422010-02-15 16:53:33 +00005575 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00005576 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
5577 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Unpck2);
5578 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005579 PseudoSourceValue::getConstantPool(), 0,
David Greene67c9d422010-02-15 16:53:33 +00005580 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00005581 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005582
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005583 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00005584 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00005585 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
5586 DAG.getUNDEF(MVT::v2f64), ShufMask);
5587 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
5588 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005589 DAG.getIntPtrConstant(0));
5590}
5591
Bill Wendling8b8a6362009-01-17 03:56:04 +00005592// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00005593SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
5594 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005595 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00005596 // FP constant to bias correct the final result.
5597 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00005598 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005599
5600 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00005601 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5602 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005603 Op.getOperand(0),
5604 DAG.getIntPtrConstant(0)));
5605
Owen Anderson825b72b2009-08-11 20:47:22 +00005606 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5607 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00005608 DAG.getIntPtrConstant(0));
5609
5610 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00005611 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
5612 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005613 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005614 MVT::v2f64, Load)),
5615 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005616 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005617 MVT::v2f64, Bias)));
5618 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5619 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00005620 DAG.getIntPtrConstant(0));
5621
5622 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00005623 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005624
5625 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00005626 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00005627
Owen Anderson825b72b2009-08-11 20:47:22 +00005628 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005629 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00005630 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00005631 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005632 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00005633 }
5634
5635 // Handle final rounding.
5636 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00005637}
5638
Dan Gohmand858e902010-04-17 15:26:15 +00005639SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
5640 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00005641 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005642 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00005643
Dale Johannesen8d908eb2010-05-15 18:51:12 +00005644 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00005645 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
5646 // the optimization here.
5647 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00005648 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00005649
Owen Andersone50ed302009-08-10 22:56:29 +00005650 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00005651 EVT DstVT = Op.getValueType();
5652 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00005653 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00005654 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00005655 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00005656
5657 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00005658 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00005659 if (SrcVT == MVT::i32) {
5660 SDValue WordOff = DAG.getConstant(4, getPointerTy());
5661 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
5662 getPointerTy(), StackSlot, WordOff);
5663 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
5664 StackSlot, NULL, 0, false, false, 0);
5665 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
5666 OffsetSlot, NULL, 0, false, false, 0);
5667 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
5668 return Fild;
5669 }
5670
5671 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
5672 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
David Greene67c9d422010-02-15 16:53:33 +00005673 StackSlot, NULL, 0, false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00005674 // For i64 source, we need to add the appropriate power of 2 if the input
5675 // was negative. This is the same as the optimization in
5676 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
5677 // we must be careful to do the computation in x87 extended precision, not
5678 // in SSE. (The generic code can't know it's OK to do this, or how to.)
5679 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
5680 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
5681 SDValue Fild = DAG.getNode(X86ISD::FILD, dl, Tys, Ops, 3);
5682
5683 APInt FF(32, 0x5F800000ULL);
5684
5685 // Check whether the sign bit is set.
5686 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
5687 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
5688 ISD::SETLT);
5689
5690 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
5691 SDValue FudgePtr = DAG.getConstantPool(
5692 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
5693 getPointerTy());
5694
5695 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
5696 SDValue Zero = DAG.getIntPtrConstant(0);
5697 SDValue Four = DAG.getIntPtrConstant(4);
5698 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
5699 Zero, Four);
5700 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
5701
5702 // Load the value out, extending it from f32 to f80.
5703 // FIXME: Avoid the extend by constructing the right constant pool?
5704 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
5705 FudgePtr, PseudoSourceValue::getConstantPool(),
5706 0, MVT::f32, false, false, 4);
5707 // Extend everything to 80 bits to force it to be done on x87.
5708 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
5709 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00005710}
5711
Dan Gohman475871a2008-07-27 21:46:04 +00005712std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00005713FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005714 DebugLoc dl = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00005715
Owen Andersone50ed302009-08-10 22:56:29 +00005716 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00005717
5718 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005719 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
5720 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00005721 }
5722
Owen Anderson825b72b2009-08-11 20:47:22 +00005723 assert(DstTy.getSimpleVT() <= MVT::i64 &&
5724 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00005725 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00005726
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005727 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00005728 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00005729 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005730 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00005731 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00005732 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00005733 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005734 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005735
Evan Cheng87c89352007-10-15 20:11:21 +00005736 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
5737 // stack slot.
5738 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00005739 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00005740 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005741 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00005742
Evan Cheng0db9fe62006-04-25 20:13:52 +00005743 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00005744 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005745 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005746 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
5747 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
5748 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005749 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005750
Dan Gohman475871a2008-07-27 21:46:04 +00005751 SDValue Chain = DAG.getEntryNode();
5752 SDValue Value = Op.getOperand(0);
Chris Lattner78631162008-01-16 06:24:21 +00005753 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005754 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dale Johannesenace16102009-02-03 19:33:06 +00005755 Chain = DAG.getStore(Chain, dl, Value, StackSlot,
David Greene67c9d422010-02-15 16:53:33 +00005756 PseudoSourceValue::getFixedStack(SSFI), 0,
5757 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005758 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00005759 SDValue Ops[] = {
Chris Lattner5a88b832007-02-25 07:10:00 +00005760 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
5761 };
Dale Johannesenace16102009-02-03 19:33:06 +00005762 Value = DAG.getNode(X86ISD::FLD, dl, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005763 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00005764 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005765 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5766 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005767
Evan Cheng0db9fe62006-04-25 20:13:52 +00005768 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00005769 SDValue Ops[] = { Chain, Value, StackSlot };
Owen Anderson825b72b2009-08-11 20:47:22 +00005770 SDValue FIST = DAG.getNode(Opc, dl, MVT::Other, Ops, 3);
Evan Chengd9558e02006-01-06 00:43:03 +00005771
Chris Lattner27a6c732007-11-24 07:07:01 +00005772 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005773}
5774
Dan Gohmand858e902010-04-17 15:26:15 +00005775SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
5776 SelectionDAG &DAG) const {
Eli Friedman23ef1052009-06-06 03:57:58 +00005777 if (Op.getValueType().isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005778 if (Op.getValueType() == MVT::v2i32 &&
5779 Op.getOperand(0).getValueType() == MVT::v2f64) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005780 return Op;
5781 }
5782 return SDValue();
5783 }
5784
Eli Friedman948e95a2009-05-23 09:59:16 +00005785 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00005786 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00005787 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
5788 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00005789
Chris Lattner27a6c732007-11-24 07:07:01 +00005790 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005791 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
David Greene67c9d422010-02-15 16:53:33 +00005792 FIST, StackSlot, NULL, 0, false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00005793}
5794
Dan Gohmand858e902010-04-17 15:26:15 +00005795SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
5796 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00005797 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
5798 SDValue FIST = Vals.first, StackSlot = Vals.second;
5799 assert(FIST.getNode() && "Unexpected failure");
5800
5801 // Load the result.
5802 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
David Greene67c9d422010-02-15 16:53:33 +00005803 FIST, StackSlot, NULL, 0, false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00005804}
5805
Dan Gohmand858e902010-04-17 15:26:15 +00005806SDValue X86TargetLowering::LowerFABS(SDValue Op,
5807 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005808 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005809 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005810 EVT VT = Op.getValueType();
5811 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005812 if (VT.isVector())
5813 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005814 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005815 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005816 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00005817 CV.push_back(C);
5818 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005819 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005820 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00005821 CV.push_back(C);
5822 CV.push_back(C);
5823 CV.push_back(C);
5824 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005825 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005826 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005827 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005828 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005829 PseudoSourceValue::getConstantPool(), 0,
5830 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005831 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005832}
5833
Dan Gohmand858e902010-04-17 15:26:15 +00005834SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005835 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005836 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005837 EVT VT = Op.getValueType();
5838 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00005839 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00005840 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005841 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005842 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005843 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00005844 CV.push_back(C);
5845 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005846 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005847 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00005848 CV.push_back(C);
5849 CV.push_back(C);
5850 CV.push_back(C);
5851 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005852 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005853 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005854 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005855 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005856 PseudoSourceValue::getConstantPool(), 0,
5857 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005858 if (VT.isVector()) {
Dale Johannesenace16102009-02-03 19:33:06 +00005859 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00005860 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
5861 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005862 Op.getOperand(0)),
Owen Anderson825b72b2009-08-11 20:47:22 +00005863 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00005864 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005865 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00005866 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005867}
5868
Dan Gohmand858e902010-04-17 15:26:15 +00005869SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005870 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00005871 SDValue Op0 = Op.getOperand(0);
5872 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005873 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005874 EVT VT = Op.getValueType();
5875 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00005876
5877 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005878 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005879 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005880 SrcVT = VT;
5881 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005882 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005883 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005884 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005885 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005886 }
5887
5888 // At this point the operands and the result should have the same
5889 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00005890
Evan Cheng68c47cb2007-01-05 07:55:56 +00005891 // First get the sign bit of second operand.
5892 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005893 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005894 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
5895 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005896 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005897 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
5898 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5899 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5900 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005901 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005902 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005903 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005904 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005905 PseudoSourceValue::getConstantPool(), 0,
5906 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005907 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005908
5909 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005910 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005911 // Op0 is MVT::f32, Op1 is MVT::f64.
5912 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
5913 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
5914 DAG.getConstant(32, MVT::i32));
5915 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, SignBit);
5916 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00005917 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005918 }
5919
Evan Cheng73d6cf12007-01-05 21:37:56 +00005920 // Clear first operand sign bit.
5921 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00005922 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005923 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
5924 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005925 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005926 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
5927 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5928 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5929 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005930 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005931 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005932 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005933 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005934 PseudoSourceValue::getConstantPool(), 0,
5935 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005936 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005937
5938 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00005939 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005940}
5941
Dan Gohman076aee32009-03-04 19:44:21 +00005942/// Emit nodes that will be selected as "test Op0,Op0", or something
5943/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005944SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00005945 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00005946 DebugLoc dl = Op.getDebugLoc();
5947
Dan Gohman31125812009-03-07 01:58:32 +00005948 // CF and OF aren't always set the way we want. Determine which
5949 // of these we need.
5950 bool NeedCF = false;
5951 bool NeedOF = false;
5952 switch (X86CC) {
5953 case X86::COND_A: case X86::COND_AE:
5954 case X86::COND_B: case X86::COND_BE:
5955 NeedCF = true;
5956 break;
5957 case X86::COND_G: case X86::COND_GE:
5958 case X86::COND_L: case X86::COND_LE:
5959 case X86::COND_O: case X86::COND_NO:
5960 NeedOF = true;
5961 break;
5962 default: break;
5963 }
5964
Dan Gohman076aee32009-03-04 19:44:21 +00005965 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00005966 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
5967 // we prove that the arithmetic won't overflow, we can't use OF or CF.
5968 if (Op.getResNo() == 0 && !NeedOF && !NeedCF) {
Dan Gohman076aee32009-03-04 19:44:21 +00005969 unsigned Opcode = 0;
Dan Gohman51bb4742009-03-05 21:29:28 +00005970 unsigned NumOperands = 0;
Dan Gohman076aee32009-03-04 19:44:21 +00005971 switch (Op.getNode()->getOpcode()) {
5972 case ISD::ADD:
Stuart Hastings5a6a65b2010-04-28 00:35:10 +00005973 // Due to an isel shortcoming, be conservative if this add is
5974 // likely to be selected as part of a load-modify-store
5975 // instruction. When the root node in a match is a store, isel
5976 // doesn't know how to remap non-chain non-flag uses of other
5977 // nodes in the match, such as the ADD in this case. This leads
5978 // to the ADD being left around and reselected, with the result
5979 // being two adds in the output. Alas, even if none our users
5980 // are stores, that doesn't prove we're O.K. Ergo, if we have
5981 // any parents that aren't CopyToReg or SETCC, eschew INC/DEC.
5982 // A better fix seems to require climbing the DAG back to the
5983 // root, and it doesn't seem to be worth the effort.
Dan Gohman076aee32009-03-04 19:44:21 +00005984 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Stuart Hastings5a6a65b2010-04-28 00:35:10 +00005985 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5986 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
Dan Gohman076aee32009-03-04 19:44:21 +00005987 goto default_case;
Dan Gohman076aee32009-03-04 19:44:21 +00005988 if (ConstantSDNode *C =
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005989 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
5990 // An add of one will be selected as an INC.
Dan Gohman076aee32009-03-04 19:44:21 +00005991 if (C->getAPIntValue() == 1) {
5992 Opcode = X86ISD::INC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005993 NumOperands = 1;
Dan Gohman076aee32009-03-04 19:44:21 +00005994 break;
5995 }
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005996 // An add of negative one (subtract of one) will be selected as a DEC.
5997 if (C->getAPIntValue().isAllOnesValue()) {
5998 Opcode = X86ISD::DEC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005999 NumOperands = 1;
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00006000 break;
6001 }
6002 }
Dan Gohman076aee32009-03-04 19:44:21 +00006003 // Otherwise use a regular EFLAGS-setting add.
6004 Opcode = X86ISD::ADD;
Dan Gohman51bb4742009-03-05 21:29:28 +00006005 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00006006 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00006007 case ISD::AND: {
6008 // If the primary and result isn't used, don't bother using X86ISD::AND,
6009 // because a TEST instruction will be better.
6010 bool NonFlagUse = false;
6011 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Evan Cheng17751da2010-01-07 00:54:06 +00006012 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
6013 SDNode *User = *UI;
6014 unsigned UOpNo = UI.getOperandNo();
6015 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
6016 // Look pass truncate.
6017 UOpNo = User->use_begin().getOperandNo();
6018 User = *User->use_begin();
6019 }
6020 if (User->getOpcode() != ISD::BRCOND &&
6021 User->getOpcode() != ISD::SETCC &&
6022 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
Dan Gohmane220c4b2009-09-18 19:59:53 +00006023 NonFlagUse = true;
6024 break;
6025 }
Evan Cheng17751da2010-01-07 00:54:06 +00006026 }
Dan Gohmane220c4b2009-09-18 19:59:53 +00006027 if (!NonFlagUse)
6028 break;
6029 }
6030 // FALL THROUGH
Dan Gohman076aee32009-03-04 19:44:21 +00006031 case ISD::SUB:
Dan Gohmane220c4b2009-09-18 19:59:53 +00006032 case ISD::OR:
6033 case ISD::XOR:
6034 // Due to the ISEL shortcoming noted above, be conservative if this op is
Dan Gohman076aee32009-03-04 19:44:21 +00006035 // likely to be selected as part of a load-modify-store instruction.
6036 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
6037 UE = Op.getNode()->use_end(); UI != UE; ++UI)
6038 if (UI->getOpcode() == ISD::STORE)
6039 goto default_case;
Dan Gohmane220c4b2009-09-18 19:59:53 +00006040 // Otherwise use a regular EFLAGS-setting instruction.
6041 switch (Op.getNode()->getOpcode()) {
6042 case ISD::SUB: Opcode = X86ISD::SUB; break;
6043 case ISD::OR: Opcode = X86ISD::OR; break;
6044 case ISD::XOR: Opcode = X86ISD::XOR; break;
6045 case ISD::AND: Opcode = X86ISD::AND; break;
6046 default: llvm_unreachable("unexpected operator!");
6047 }
Dan Gohman51bb4742009-03-05 21:29:28 +00006048 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00006049 break;
6050 case X86ISD::ADD:
6051 case X86ISD::SUB:
6052 case X86ISD::INC:
6053 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +00006054 case X86ISD::OR:
6055 case X86ISD::XOR:
6056 case X86ISD::AND:
Dan Gohman076aee32009-03-04 19:44:21 +00006057 return SDValue(Op.getNode(), 1);
6058 default:
6059 default_case:
6060 break;
6061 }
6062 if (Opcode != 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006063 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
Dan Gohman076aee32009-03-04 19:44:21 +00006064 SmallVector<SDValue, 4> Ops;
Dan Gohman31125812009-03-07 01:58:32 +00006065 for (unsigned i = 0; i != NumOperands; ++i)
Dan Gohman076aee32009-03-04 19:44:21 +00006066 Ops.push_back(Op.getOperand(i));
Dan Gohmanfc166572009-04-09 23:54:40 +00006067 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
Dan Gohman076aee32009-03-04 19:44:21 +00006068 DAG.ReplaceAllUsesWith(Op, New);
6069 return SDValue(New.getNode(), 1);
6070 }
6071 }
6072
6073 // Otherwise just emit a CMP with 0, which is the TEST pattern.
Owen Anderson825b72b2009-08-11 20:47:22 +00006074 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
Dan Gohman076aee32009-03-04 19:44:21 +00006075 DAG.getConstant(0, Op.getValueType()));
6076}
6077
6078/// Emit nodes that will be selected as "cmp Op0,Op1", or something
6079/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00006080SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00006081 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00006082 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
6083 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00006084 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00006085
6086 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00006087 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00006088}
6089
Evan Chengd40d03e2010-01-06 19:38:29 +00006090/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
6091/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00006092SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
6093 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00006094 SDValue Op0 = And.getOperand(0);
6095 SDValue Op1 = And.getOperand(1);
6096 if (Op0.getOpcode() == ISD::TRUNCATE)
6097 Op0 = Op0.getOperand(0);
6098 if (Op1.getOpcode() == ISD::TRUNCATE)
6099 Op1 = Op1.getOperand(0);
6100
Evan Chengd40d03e2010-01-06 19:38:29 +00006101 SDValue LHS, RHS;
Evan Cheng2c755ba2010-02-27 07:36:59 +00006102 if (Op1.getOpcode() == ISD::SHL) {
6103 if (ConstantSDNode *And10C = dyn_cast<ConstantSDNode>(Op1.getOperand(0)))
6104 if (And10C->getZExtValue() == 1) {
6105 LHS = Op0;
6106 RHS = Op1.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00006107 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00006108 } else if (Op0.getOpcode() == ISD::SHL) {
6109 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
6110 if (And00C->getZExtValue() == 1) {
6111 LHS = Op1;
6112 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00006113 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00006114 } else if (Op1.getOpcode() == ISD::Constant) {
6115 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
6116 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00006117 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
6118 LHS = AndLHS.getOperand(0);
6119 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00006120 }
Evan Chengd40d03e2010-01-06 19:38:29 +00006121 }
Evan Cheng0488db92007-09-25 01:57:46 +00006122
Evan Chengd40d03e2010-01-06 19:38:29 +00006123 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00006124 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00006125 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00006126 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00006127 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00006128 // Also promote i16 to i32 for performance / code size reason.
6129 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00006130 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00006131 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00006132
Evan Chengd40d03e2010-01-06 19:38:29 +00006133 // If the operand types disagree, extend the shift amount to match. Since
6134 // BT ignores high bits (like shifts) we can use anyextend.
6135 if (LHS.getValueType() != RHS.getValueType())
6136 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00006137
Evan Chengd40d03e2010-01-06 19:38:29 +00006138 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
6139 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
6140 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6141 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00006142 }
6143
Evan Cheng54de3ea2010-01-05 06:52:31 +00006144 return SDValue();
6145}
6146
Dan Gohmand858e902010-04-17 15:26:15 +00006147SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng54de3ea2010-01-05 06:52:31 +00006148 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
6149 SDValue Op0 = Op.getOperand(0);
6150 SDValue Op1 = Op.getOperand(1);
6151 DebugLoc dl = Op.getDebugLoc();
6152 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
6153
6154 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00006155 // Lower (X & (1 << N)) == 0 to BT(X, N).
6156 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
6157 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
6158 if (Op0.getOpcode() == ISD::AND &&
6159 Op0.hasOneUse() &&
6160 Op1.getOpcode() == ISD::Constant &&
6161 cast<ConstantSDNode>(Op1)->getZExtValue() == 0 &&
6162 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
6163 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
6164 if (NewSetCC.getNode())
6165 return NewSetCC;
6166 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00006167
Evan Cheng2c755ba2010-02-27 07:36:59 +00006168 // Look for "(setcc) == / != 1" to avoid unncessary setcc.
6169 if (Op0.getOpcode() == X86ISD::SETCC &&
6170 Op1.getOpcode() == ISD::Constant &&
6171 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
6172 cast<ConstantSDNode>(Op1)->isNullValue()) &&
6173 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
6174 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
6175 bool Invert = (CC == ISD::SETNE) ^
6176 cast<ConstantSDNode>(Op1)->isNullValue();
6177 if (Invert)
6178 CCode = X86::GetOppositeBranchCondition(CCode);
6179 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6180 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
6181 }
6182
Evan Chenge5b51ac2010-04-17 06:13:15 +00006183 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00006184 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00006185 if (X86CC == X86::COND_INVALID)
6186 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00006187
Evan Cheng552f09a2010-04-26 19:06:11 +00006188 SDValue Cond = EmitCmp(Op0, Op1, X86CC, DAG);
Evan Chengad9c0a32009-12-15 00:53:42 +00006189
6190 // Use sbb x, x to materialize carry bit into a GPR.
Evan Cheng2e489c42009-12-16 00:53:11 +00006191 if (X86CC == X86::COND_B)
Evan Chengad9c0a32009-12-15 00:53:42 +00006192 return DAG.getNode(ISD::AND, dl, MVT::i8,
6193 DAG.getNode(X86ISD::SETCC_CARRY, dl, MVT::i8,
6194 DAG.getConstant(X86CC, MVT::i8), Cond),
6195 DAG.getConstant(1, MVT::i8));
Evan Chengad9c0a32009-12-15 00:53:42 +00006196
Owen Anderson825b72b2009-08-11 20:47:22 +00006197 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6198 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00006199}
6200
Dan Gohmand858e902010-04-17 15:26:15 +00006201SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00006202 SDValue Cond;
6203 SDValue Op0 = Op.getOperand(0);
6204 SDValue Op1 = Op.getOperand(1);
6205 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00006206 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00006207 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
6208 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006209 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00006210
6211 if (isFP) {
6212 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00006213 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00006214 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
6215 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00006216 bool Swap = false;
6217
6218 switch (SetCCOpcode) {
6219 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00006220 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00006221 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00006222 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00006223 case ISD::SETGT: Swap = true; // Fallthrough
6224 case ISD::SETLT:
6225 case ISD::SETOLT: SSECC = 1; break;
6226 case ISD::SETOGE:
6227 case ISD::SETGE: Swap = true; // Fallthrough
6228 case ISD::SETLE:
6229 case ISD::SETOLE: SSECC = 2; break;
6230 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00006231 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00006232 case ISD::SETNE: SSECC = 4; break;
6233 case ISD::SETULE: Swap = true;
6234 case ISD::SETUGE: SSECC = 5; break;
6235 case ISD::SETULT: Swap = true;
6236 case ISD::SETUGT: SSECC = 6; break;
6237 case ISD::SETO: SSECC = 7; break;
6238 }
6239 if (Swap)
6240 std::swap(Op0, Op1);
6241
Nate Begemanfb8ead02008-07-25 19:05:58 +00006242 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00006243 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00006244 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00006245 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00006246 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
6247 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00006248 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00006249 }
6250 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00006251 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00006252 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
6253 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00006254 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00006255 }
Torok Edwinc23197a2009-07-14 16:55:14 +00006256 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00006257 }
6258 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00006259 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00006260 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006261
Nate Begeman30a0de92008-07-17 16:51:19 +00006262 // We are handling one of the integer comparisons here. Since SSE only has
6263 // GT and EQ comparisons for integer, swapping operands and multiple
6264 // operations may be required for some comparisons.
6265 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
6266 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00006267
Owen Anderson825b72b2009-08-11 20:47:22 +00006268 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00006269 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00006270 case MVT::v8i8:
6271 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
6272 case MVT::v4i16:
6273 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
6274 case MVT::v2i32:
6275 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
6276 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00006277 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006278
Nate Begeman30a0de92008-07-17 16:51:19 +00006279 switch (SetCCOpcode) {
6280 default: break;
6281 case ISD::SETNE: Invert = true;
6282 case ISD::SETEQ: Opc = EQOpc; break;
6283 case ISD::SETLT: Swap = true;
6284 case ISD::SETGT: Opc = GTOpc; break;
6285 case ISD::SETGE: Swap = true;
6286 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
6287 case ISD::SETULT: Swap = true;
6288 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
6289 case ISD::SETUGE: Swap = true;
6290 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
6291 }
6292 if (Swap)
6293 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006294
Nate Begeman30a0de92008-07-17 16:51:19 +00006295 // Since SSE has no unsigned integer comparisons, we need to flip the sign
6296 // bits of the inputs before performing those operations.
6297 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00006298 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00006299 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
6300 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00006301 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00006302 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
6303 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00006304 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
6305 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00006306 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006307
Dale Johannesenace16102009-02-03 19:33:06 +00006308 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00006309
6310 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00006311 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00006312 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00006313
Nate Begeman30a0de92008-07-17 16:51:19 +00006314 return Result;
6315}
Evan Cheng0488db92007-09-25 01:57:46 +00006316
Evan Cheng370e5342008-12-03 08:38:43 +00006317// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00006318static bool isX86LogicalCmp(SDValue Op) {
6319 unsigned Opc = Op.getNode()->getOpcode();
6320 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
6321 return true;
6322 if (Op.getResNo() == 1 &&
6323 (Opc == X86ISD::ADD ||
6324 Opc == X86ISD::SUB ||
6325 Opc == X86ISD::SMUL ||
6326 Opc == X86ISD::UMUL ||
6327 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00006328 Opc == X86ISD::DEC ||
6329 Opc == X86ISD::OR ||
6330 Opc == X86ISD::XOR ||
6331 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00006332 return true;
6333
6334 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00006335}
6336
Dan Gohmand858e902010-04-17 15:26:15 +00006337SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00006338 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00006339 SDValue Cond = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006340 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00006341 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00006342
Dan Gohman1a492952009-10-20 16:22:37 +00006343 if (Cond.getOpcode() == ISD::SETCC) {
6344 SDValue NewCond = LowerSETCC(Cond, DAG);
6345 if (NewCond.getNode())
6346 Cond = NewCond;
6347 }
Evan Cheng734503b2006-09-11 02:19:56 +00006348
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006349 // (select (x == 0), -1, 0) -> (sign_bit (x - 1))
6350 SDValue Op1 = Op.getOperand(1);
6351 SDValue Op2 = Op.getOperand(2);
6352 if (Cond.getOpcode() == X86ISD::SETCC &&
6353 cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue() == X86::COND_E) {
6354 SDValue Cmp = Cond.getOperand(1);
6355 if (Cmp.getOpcode() == X86ISD::CMP) {
6356 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op1);
6357 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
6358 ConstantSDNode *RHSC =
6359 dyn_cast<ConstantSDNode>(Cmp.getOperand(1).getNode());
6360 if (N1C && N1C->isAllOnesValue() &&
6361 N2C && N2C->isNullValue() &&
6362 RHSC && RHSC->isNullValue()) {
6363 SDValue CmpOp0 = Cmp.getOperand(0);
Chris Lattnerda0688e2010-03-14 18:44:35 +00006364 Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006365 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
6366 return DAG.getNode(X86ISD::SETCC_CARRY, dl, Op.getValueType(),
6367 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
6368 }
6369 }
6370 }
6371
Evan Chengad9c0a32009-12-15 00:53:42 +00006372 // Look pass (and (setcc_carry (cmp ...)), 1).
6373 if (Cond.getOpcode() == ISD::AND &&
6374 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
6375 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
6376 if (C && C->getAPIntValue() == 1)
6377 Cond = Cond.getOperand(0);
6378 }
6379
Evan Cheng3f41d662007-10-08 22:16:29 +00006380 // If condition flag is set by a X86ISD::CMP, then use it as the condition
6381 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00006382 if (Cond.getOpcode() == X86ISD::SETCC ||
6383 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00006384 CC = Cond.getOperand(0);
6385
Dan Gohman475871a2008-07-27 21:46:04 +00006386 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00006387 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00006388 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00006389
Evan Cheng3f41d662007-10-08 22:16:29 +00006390 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006391 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00006392 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00006393 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00006394
Chris Lattnerd1980a52009-03-12 06:52:53 +00006395 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
6396 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00006397 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00006398 addTest = false;
6399 }
6400 }
6401
6402 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00006403 // Look pass the truncate.
6404 if (Cond.getOpcode() == ISD::TRUNCATE)
6405 Cond = Cond.getOperand(0);
6406
6407 // We know the result of AND is compared against zero. Try to match
6408 // it to BT.
6409 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
6410 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
6411 if (NewSetCC.getNode()) {
6412 CC = NewSetCC.getOperand(0);
6413 Cond = NewSetCC.getOperand(1);
6414 addTest = false;
6415 }
6416 }
6417 }
6418
6419 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006420 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00006421 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00006422 }
6423
Evan Cheng0488db92007-09-25 01:57:46 +00006424 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
6425 // condition is true.
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006426 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
6427 SDValue Ops[] = { Op2, Op1, CC, Cond };
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006428 return DAG.getNode(X86ISD::CMOV, dl, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00006429}
6430
Evan Cheng370e5342008-12-03 08:38:43 +00006431// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
6432// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
6433// from the AND / OR.
6434static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
6435 Opc = Op.getOpcode();
6436 if (Opc != ISD::OR && Opc != ISD::AND)
6437 return false;
6438 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
6439 Op.getOperand(0).hasOneUse() &&
6440 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
6441 Op.getOperand(1).hasOneUse());
6442}
6443
Evan Cheng961d6d42009-02-02 08:19:07 +00006444// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
6445// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00006446static bool isXor1OfSetCC(SDValue Op) {
6447 if (Op.getOpcode() != ISD::XOR)
6448 return false;
6449 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6450 if (N1C && N1C->getAPIntValue() == 1) {
6451 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
6452 Op.getOperand(0).hasOneUse();
6453 }
6454 return false;
6455}
6456
Dan Gohmand858e902010-04-17 15:26:15 +00006457SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00006458 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00006459 SDValue Chain = Op.getOperand(0);
6460 SDValue Cond = Op.getOperand(1);
6461 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006462 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00006463 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00006464
Dan Gohman1a492952009-10-20 16:22:37 +00006465 if (Cond.getOpcode() == ISD::SETCC) {
6466 SDValue NewCond = LowerSETCC(Cond, DAG);
6467 if (NewCond.getNode())
6468 Cond = NewCond;
6469 }
Chris Lattnere55484e2008-12-25 05:34:37 +00006470#if 0
6471 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00006472 else if (Cond.getOpcode() == X86ISD::ADD ||
6473 Cond.getOpcode() == X86ISD::SUB ||
6474 Cond.getOpcode() == X86ISD::SMUL ||
6475 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00006476 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00006477#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00006478
Evan Chengad9c0a32009-12-15 00:53:42 +00006479 // Look pass (and (setcc_carry (cmp ...)), 1).
6480 if (Cond.getOpcode() == ISD::AND &&
6481 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
6482 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
6483 if (C && C->getAPIntValue() == 1)
6484 Cond = Cond.getOperand(0);
6485 }
6486
Evan Cheng3f41d662007-10-08 22:16:29 +00006487 // If condition flag is set by a X86ISD::CMP, then use it as the condition
6488 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00006489 if (Cond.getOpcode() == X86ISD::SETCC ||
6490 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00006491 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006492
Dan Gohman475871a2008-07-27 21:46:04 +00006493 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00006494 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00006495 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00006496 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00006497 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00006498 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00006499 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00006500 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00006501 default: break;
6502 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00006503 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00006504 // These can only come from an arithmetic instruction with overflow,
6505 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00006506 Cond = Cond.getNode()->getOperand(1);
6507 addTest = false;
6508 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00006509 }
Evan Cheng0488db92007-09-25 01:57:46 +00006510 }
Evan Cheng370e5342008-12-03 08:38:43 +00006511 } else {
6512 unsigned CondOpc;
6513 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
6514 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00006515 if (CondOpc == ISD::OR) {
6516 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
6517 // two branches instead of an explicit OR instruction with a
6518 // separate test.
6519 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00006520 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00006521 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006522 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00006523 Chain, Dest, CC, Cmp);
6524 CC = Cond.getOperand(1).getOperand(0);
6525 Cond = Cmp;
6526 addTest = false;
6527 }
6528 } else { // ISD::AND
6529 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
6530 // two branches instead of an explicit AND instruction with a
6531 // separate test. However, we only do this if this block doesn't
6532 // have a fall-through edge, because this requires an explicit
6533 // jmp when the condition is false.
6534 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00006535 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00006536 Op.getNode()->hasOneUse()) {
6537 X86::CondCode CCode =
6538 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
6539 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006540 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00006541 SDValue User = SDValue(*Op.getNode()->use_begin(), 0);
6542 // Look for an unconditional branch following this conditional branch.
6543 // We need this because we need to reverse the successors in order
6544 // to implement FCMP_OEQ.
6545 if (User.getOpcode() == ISD::BR) {
6546 SDValue FalseBB = User.getOperand(1);
6547 SDValue NewBR =
6548 DAG.UpdateNodeOperands(User, User.getOperand(0), Dest);
6549 assert(NewBR == User);
6550 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00006551
Dale Johannesene4d209d2009-02-03 20:21:25 +00006552 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00006553 Chain, Dest, CC, Cmp);
6554 X86::CondCode CCode =
6555 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
6556 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006557 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00006558 Cond = Cmp;
6559 addTest = false;
6560 }
6561 }
Dan Gohman279c22e2008-10-21 03:29:32 +00006562 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00006563 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
6564 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
6565 // It should be transformed during dag combiner except when the condition
6566 // is set by a arithmetics with overflow node.
6567 X86::CondCode CCode =
6568 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
6569 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006570 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00006571 Cond = Cond.getOperand(0).getOperand(1);
6572 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00006573 }
Evan Cheng0488db92007-09-25 01:57:46 +00006574 }
6575
6576 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00006577 // Look pass the truncate.
6578 if (Cond.getOpcode() == ISD::TRUNCATE)
6579 Cond = Cond.getOperand(0);
6580
6581 // We know the result of AND is compared against zero. Try to match
6582 // it to BT.
6583 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
6584 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
6585 if (NewSetCC.getNode()) {
6586 CC = NewSetCC.getOperand(0);
6587 Cond = NewSetCC.getOperand(1);
6588 addTest = false;
6589 }
6590 }
6591 }
6592
6593 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006594 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00006595 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00006596 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00006597 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00006598 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00006599}
6600
Anton Korobeynikove060b532007-04-17 19:34:00 +00006601
6602// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
6603// Calls to _alloca is needed to probe the stack when allocating more than 4k
6604// bytes in one go. Touching the stack at 4K increments is necessary to ensure
6605// that the guard pages used by the OS virtual memory manager are allocated in
6606// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00006607SDValue
6608X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006609 SelectionDAG &DAG) const {
Anton Korobeynikove060b532007-04-17 19:34:00 +00006610 assert(Subtarget->isTargetCygMing() &&
6611 "This should be used only on Cygwin/Mingw targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006612 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006613
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006614 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00006615 SDValue Chain = Op.getOperand(0);
6616 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006617 // FIXME: Ensure alignment here
6618
Dan Gohman475871a2008-07-27 21:46:04 +00006619 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006620
Owen Andersone50ed302009-08-10 22:56:29 +00006621 EVT IntPtr = getPointerTy();
Owen Anderson825b72b2009-08-11 20:47:22 +00006622 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006623
Dale Johannesendd64c412009-02-04 00:33:20 +00006624 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006625 Flag = Chain.getValue(1);
6626
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00006627 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006628
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00006629 Chain = DAG.getNode(X86ISD::MINGW_ALLOCA, dl, NodeTys, Chain, Flag);
6630 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006631
Dale Johannesendd64c412009-02-04 00:33:20 +00006632 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006633
Dan Gohman475871a2008-07-27 21:46:04 +00006634 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006635 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006636}
6637
Dan Gohmand858e902010-04-17 15:26:15 +00006638SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00006639 MachineFunction &MF = DAG.getMachineFunction();
6640 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
6641
Dan Gohman69de1932008-02-06 22:27:42 +00006642 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006643 DebugLoc dl = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00006644
Evan Cheng25ab6902006-09-08 06:48:29 +00006645 if (!Subtarget->is64Bit()) {
6646 // vastart just stores the address of the VarArgsFrameIndex slot into the
6647 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00006648 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
6649 getPointerTy());
David Greene67c9d422010-02-15 16:53:33 +00006650 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0,
6651 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006652 }
6653
6654 // __va_list_tag:
6655 // gp_offset (0 - 6 * 8)
6656 // fp_offset (48 - 48 + 8 * 16)
6657 // overflow_arg_area (point to parameters coming in memory).
6658 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00006659 SmallVector<SDValue, 8> MemOps;
6660 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00006661 // Store gp_offset
Dale Johannesene4d209d2009-02-03 20:21:25 +00006662 SDValue Store = DAG.getStore(Op.getOperand(0), dl,
Dan Gohman1e93df62010-04-17 14:41:14 +00006663 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
6664 MVT::i32),
David Greene67c9d422010-02-15 16:53:33 +00006665 FIN, SV, 0, false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006666 MemOps.push_back(Store);
6667
6668 // Store fp_offset
Scott Michelfdc40a02009-02-17 22:15:04 +00006669 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006670 FIN, DAG.getIntPtrConstant(4));
6671 Store = DAG.getStore(Op.getOperand(0), dl,
Dan Gohman1e93df62010-04-17 14:41:14 +00006672 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
6673 MVT::i32),
David Greene67c9d422010-02-15 16:53:33 +00006674 FIN, SV, 0, false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006675 MemOps.push_back(Store);
6676
6677 // Store ptr to overflow_arg_area
Scott Michelfdc40a02009-02-17 22:15:04 +00006678 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006679 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00006680 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
6681 getPointerTy());
David Greene67c9d422010-02-15 16:53:33 +00006682 Store = DAG.getStore(Op.getOperand(0), dl, OVFIN, FIN, SV, 0,
6683 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006684 MemOps.push_back(Store);
6685
6686 // Store ptr to reg_save_area.
Scott Michelfdc40a02009-02-17 22:15:04 +00006687 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006688 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00006689 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
6690 getPointerTy());
David Greene67c9d422010-02-15 16:53:33 +00006691 Store = DAG.getStore(Op.getOperand(0), dl, RSFIN, FIN, SV, 0,
6692 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006693 MemOps.push_back(Store);
Owen Anderson825b72b2009-08-11 20:47:22 +00006694 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006695 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00006696}
6697
Dan Gohmand858e902010-04-17 15:26:15 +00006698SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman9018e832008-05-10 01:26:14 +00006699 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
6700 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman475871a2008-07-27 21:46:04 +00006701 SDValue Chain = Op.getOperand(0);
6702 SDValue SrcPtr = Op.getOperand(1);
6703 SDValue SrcSV = Op.getOperand(2);
Dan Gohman9018e832008-05-10 01:26:14 +00006704
Chris Lattner75361b62010-04-07 22:58:41 +00006705 report_fatal_error("VAArgInst is not yet implemented for x86-64!");
Dan Gohman475871a2008-07-27 21:46:04 +00006706 return SDValue();
Dan Gohman9018e832008-05-10 01:26:14 +00006707}
6708
Dan Gohmand858e902010-04-17 15:26:15 +00006709SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00006710 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00006711 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00006712 SDValue Chain = Op.getOperand(0);
6713 SDValue DstPtr = Op.getOperand(1);
6714 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00006715 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
6716 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006717 DebugLoc dl = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00006718
Dale Johannesendd64c412009-02-04 00:33:20 +00006719 return DAG.getMemcpy(Chain, dl, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00006720 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
6721 false, DstSV, 0, SrcSV, 0);
Evan Chengae642192007-03-02 23:16:35 +00006722}
6723
Dan Gohman475871a2008-07-27 21:46:04 +00006724SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006725X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006726 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006727 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006728 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00006729 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00006730 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00006731 case Intrinsic::x86_sse_comieq_ss:
6732 case Intrinsic::x86_sse_comilt_ss:
6733 case Intrinsic::x86_sse_comile_ss:
6734 case Intrinsic::x86_sse_comigt_ss:
6735 case Intrinsic::x86_sse_comige_ss:
6736 case Intrinsic::x86_sse_comineq_ss:
6737 case Intrinsic::x86_sse_ucomieq_ss:
6738 case Intrinsic::x86_sse_ucomilt_ss:
6739 case Intrinsic::x86_sse_ucomile_ss:
6740 case Intrinsic::x86_sse_ucomigt_ss:
6741 case Intrinsic::x86_sse_ucomige_ss:
6742 case Intrinsic::x86_sse_ucomineq_ss:
6743 case Intrinsic::x86_sse2_comieq_sd:
6744 case Intrinsic::x86_sse2_comilt_sd:
6745 case Intrinsic::x86_sse2_comile_sd:
6746 case Intrinsic::x86_sse2_comigt_sd:
6747 case Intrinsic::x86_sse2_comige_sd:
6748 case Intrinsic::x86_sse2_comineq_sd:
6749 case Intrinsic::x86_sse2_ucomieq_sd:
6750 case Intrinsic::x86_sse2_ucomilt_sd:
6751 case Intrinsic::x86_sse2_ucomile_sd:
6752 case Intrinsic::x86_sse2_ucomigt_sd:
6753 case Intrinsic::x86_sse2_ucomige_sd:
6754 case Intrinsic::x86_sse2_ucomineq_sd: {
6755 unsigned Opc = 0;
6756 ISD::CondCode CC = ISD::SETCC_INVALID;
6757 switch (IntNo) {
6758 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006759 case Intrinsic::x86_sse_comieq_ss:
6760 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006761 Opc = X86ISD::COMI;
6762 CC = ISD::SETEQ;
6763 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006764 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006765 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006766 Opc = X86ISD::COMI;
6767 CC = ISD::SETLT;
6768 break;
6769 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006770 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006771 Opc = X86ISD::COMI;
6772 CC = ISD::SETLE;
6773 break;
6774 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006775 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006776 Opc = X86ISD::COMI;
6777 CC = ISD::SETGT;
6778 break;
6779 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006780 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006781 Opc = X86ISD::COMI;
6782 CC = ISD::SETGE;
6783 break;
6784 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006785 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006786 Opc = X86ISD::COMI;
6787 CC = ISD::SETNE;
6788 break;
6789 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006790 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006791 Opc = X86ISD::UCOMI;
6792 CC = ISD::SETEQ;
6793 break;
6794 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006795 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006796 Opc = X86ISD::UCOMI;
6797 CC = ISD::SETLT;
6798 break;
6799 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006800 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006801 Opc = X86ISD::UCOMI;
6802 CC = ISD::SETLE;
6803 break;
6804 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006805 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006806 Opc = X86ISD::UCOMI;
6807 CC = ISD::SETGT;
6808 break;
6809 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006810 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006811 Opc = X86ISD::UCOMI;
6812 CC = ISD::SETGE;
6813 break;
6814 case Intrinsic::x86_sse_ucomineq_ss:
6815 case Intrinsic::x86_sse2_ucomineq_sd:
6816 Opc = X86ISD::UCOMI;
6817 CC = ISD::SETNE;
6818 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006819 }
Evan Cheng734503b2006-09-11 02:19:56 +00006820
Dan Gohman475871a2008-07-27 21:46:04 +00006821 SDValue LHS = Op.getOperand(1);
6822 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00006823 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00006824 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00006825 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
6826 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6827 DAG.getConstant(X86CC, MVT::i8), Cond);
6828 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00006829 }
Eric Christopher71c67532009-07-29 00:28:05 +00006830 // ptest intrinsics. The intrinsic these come from are designed to return
Eric Christopher794bfed2009-07-29 01:01:19 +00006831 // an integer value, not just an instruction so lower it to the ptest
6832 // pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00006833 case Intrinsic::x86_sse41_ptestz:
6834 case Intrinsic::x86_sse41_ptestc:
6835 case Intrinsic::x86_sse41_ptestnzc:{
6836 unsigned X86CC = 0;
6837 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00006838 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Eric Christopher71c67532009-07-29 00:28:05 +00006839 case Intrinsic::x86_sse41_ptestz:
6840 // ZF = 1
6841 X86CC = X86::COND_E;
6842 break;
6843 case Intrinsic::x86_sse41_ptestc:
6844 // CF = 1
6845 X86CC = X86::COND_B;
6846 break;
Eric Christopherfd179292009-08-27 18:07:15 +00006847 case Intrinsic::x86_sse41_ptestnzc:
Eric Christopher71c67532009-07-29 00:28:05 +00006848 // ZF and CF = 0
6849 X86CC = X86::COND_A;
6850 break;
6851 }
Eric Christopherfd179292009-08-27 18:07:15 +00006852
Eric Christopher71c67532009-07-29 00:28:05 +00006853 SDValue LHS = Op.getOperand(1);
6854 SDValue RHS = Op.getOperand(2);
Owen Anderson825b72b2009-08-11 20:47:22 +00006855 SDValue Test = DAG.getNode(X86ISD::PTEST, dl, MVT::i32, LHS, RHS);
6856 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
6857 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
6858 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00006859 }
Evan Cheng5759f972008-05-04 09:15:50 +00006860
6861 // Fix vector shift instructions where the last operand is a non-immediate
6862 // i32 value.
6863 case Intrinsic::x86_sse2_pslli_w:
6864 case Intrinsic::x86_sse2_pslli_d:
6865 case Intrinsic::x86_sse2_pslli_q:
6866 case Intrinsic::x86_sse2_psrli_w:
6867 case Intrinsic::x86_sse2_psrli_d:
6868 case Intrinsic::x86_sse2_psrli_q:
6869 case Intrinsic::x86_sse2_psrai_w:
6870 case Intrinsic::x86_sse2_psrai_d:
6871 case Intrinsic::x86_mmx_pslli_w:
6872 case Intrinsic::x86_mmx_pslli_d:
6873 case Intrinsic::x86_mmx_pslli_q:
6874 case Intrinsic::x86_mmx_psrli_w:
6875 case Intrinsic::x86_mmx_psrli_d:
6876 case Intrinsic::x86_mmx_psrli_q:
6877 case Intrinsic::x86_mmx_psrai_w:
6878 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00006879 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00006880 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00006881 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00006882
6883 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00006884 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00006885 switch (IntNo) {
6886 case Intrinsic::x86_sse2_pslli_w:
6887 NewIntNo = Intrinsic::x86_sse2_psll_w;
6888 break;
6889 case Intrinsic::x86_sse2_pslli_d:
6890 NewIntNo = Intrinsic::x86_sse2_psll_d;
6891 break;
6892 case Intrinsic::x86_sse2_pslli_q:
6893 NewIntNo = Intrinsic::x86_sse2_psll_q;
6894 break;
6895 case Intrinsic::x86_sse2_psrli_w:
6896 NewIntNo = Intrinsic::x86_sse2_psrl_w;
6897 break;
6898 case Intrinsic::x86_sse2_psrli_d:
6899 NewIntNo = Intrinsic::x86_sse2_psrl_d;
6900 break;
6901 case Intrinsic::x86_sse2_psrli_q:
6902 NewIntNo = Intrinsic::x86_sse2_psrl_q;
6903 break;
6904 case Intrinsic::x86_sse2_psrai_w:
6905 NewIntNo = Intrinsic::x86_sse2_psra_w;
6906 break;
6907 case Intrinsic::x86_sse2_psrai_d:
6908 NewIntNo = Intrinsic::x86_sse2_psra_d;
6909 break;
6910 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00006911 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00006912 switch (IntNo) {
6913 case Intrinsic::x86_mmx_pslli_w:
6914 NewIntNo = Intrinsic::x86_mmx_psll_w;
6915 break;
6916 case Intrinsic::x86_mmx_pslli_d:
6917 NewIntNo = Intrinsic::x86_mmx_psll_d;
6918 break;
6919 case Intrinsic::x86_mmx_pslli_q:
6920 NewIntNo = Intrinsic::x86_mmx_psll_q;
6921 break;
6922 case Intrinsic::x86_mmx_psrli_w:
6923 NewIntNo = Intrinsic::x86_mmx_psrl_w;
6924 break;
6925 case Intrinsic::x86_mmx_psrli_d:
6926 NewIntNo = Intrinsic::x86_mmx_psrl_d;
6927 break;
6928 case Intrinsic::x86_mmx_psrli_q:
6929 NewIntNo = Intrinsic::x86_mmx_psrl_q;
6930 break;
6931 case Intrinsic::x86_mmx_psrai_w:
6932 NewIntNo = Intrinsic::x86_mmx_psra_w;
6933 break;
6934 case Intrinsic::x86_mmx_psrai_d:
6935 NewIntNo = Intrinsic::x86_mmx_psra_d;
6936 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006937 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00006938 }
6939 break;
6940 }
6941 }
Mon P Wangefa42202009-09-03 19:56:25 +00006942
6943 // The vector shift intrinsics with scalars uses 32b shift amounts but
6944 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
6945 // to be zero.
6946 SDValue ShOps[4];
6947 ShOps[0] = ShAmt;
6948 ShOps[1] = DAG.getConstant(0, MVT::i32);
6949 if (ShAmtVT == MVT::v4i32) {
6950 ShOps[2] = DAG.getUNDEF(MVT::i32);
6951 ShOps[3] = DAG.getUNDEF(MVT::i32);
6952 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
6953 } else {
6954 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
6955 }
6956
Owen Andersone50ed302009-08-10 22:56:29 +00006957 EVT VT = Op.getValueType();
Mon P Wangefa42202009-09-03 19:56:25 +00006958 ShAmt = DAG.getNode(ISD::BIT_CONVERT, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006959 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00006960 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00006961 Op.getOperand(1), ShAmt);
6962 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00006963 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006964}
Evan Cheng72261582005-12-20 06:22:03 +00006965
Dan Gohmand858e902010-04-17 15:26:15 +00006966SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
6967 SelectionDAG &DAG) const {
Bill Wendling64e87322009-01-16 19:25:27 +00006968 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006969 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00006970
6971 if (Depth > 0) {
6972 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
6973 SDValue Offset =
6974 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00006975 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006976 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00006977 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006978 FrameAddr, Offset),
David Greene67c9d422010-02-15 16:53:33 +00006979 NULL, 0, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00006980 }
6981
6982 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00006983 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00006984 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
David Greene67c9d422010-02-15 16:53:33 +00006985 RetAddrFI, NULL, 0, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00006986}
6987
Dan Gohmand858e902010-04-17 15:26:15 +00006988SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00006989 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6990 MFI->setFrameAddressIsTaken(true);
Owen Andersone50ed302009-08-10 22:56:29 +00006991 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006992 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00006993 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6994 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00006995 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00006996 while (Depth--)
David Greene67c9d422010-02-15 16:53:33 +00006997 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0,
6998 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00006999 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00007000}
7001
Dan Gohman475871a2008-07-27 21:46:04 +00007002SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007003 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00007004 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007005}
7006
Dan Gohmand858e902010-04-17 15:26:15 +00007007SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007008 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00007009 SDValue Chain = Op.getOperand(0);
7010 SDValue Offset = Op.getOperand(1);
7011 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007012 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007013
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007014 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
7015 getPointerTy());
7016 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007017
Dale Johannesene4d209d2009-02-03 20:21:25 +00007018 SDValue StoreAddr = DAG.getNode(ISD::SUB, dl, getPointerTy(), Frame,
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00007019 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007020 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
David Greene67c9d422010-02-15 16:53:33 +00007021 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, NULL, 0, false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00007022 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007023 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007024
Dale Johannesene4d209d2009-02-03 20:21:25 +00007025 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007026 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007027 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007028}
7029
Dan Gohman475871a2008-07-27 21:46:04 +00007030SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007031 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007032 SDValue Root = Op.getOperand(0);
7033 SDValue Trmp = Op.getOperand(1); // trampoline
7034 SDValue FPtr = Op.getOperand(2); // nested function
7035 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007036 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00007037
Dan Gohman69de1932008-02-06 22:27:42 +00007038 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00007039
7040 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00007041 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00007042
7043 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00007044 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
7045 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00007046
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00007047 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
7048 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00007049
7050 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
7051
7052 // Load the pointer to the nested function into R11.
7053 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00007054 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00007055 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
David Greene67c9d422010-02-15 16:53:33 +00007056 Addr, TrmpAddr, 0, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007057
Owen Anderson825b72b2009-08-11 20:47:22 +00007058 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7059 DAG.getConstant(2, MVT::i64));
David Greene67c9d422010-02-15 16:53:33 +00007060 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr, TrmpAddr, 2,
7061 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00007062
7063 // Load the 'nest' parameter value into R10.
7064 // R10 is specified in X86CallingConv.td
7065 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00007066 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7067 DAG.getConstant(10, MVT::i64));
7068 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
David Greene67c9d422010-02-15 16:53:33 +00007069 Addr, TrmpAddr, 10, 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(12, MVT::i64));
David Greene67c9d422010-02-15 16:53:33 +00007073 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 12,
7074 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00007075
7076 // Jump to the nested function.
7077 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00007078 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7079 DAG.getConstant(20, MVT::i64));
7080 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
David Greene67c9d422010-02-15 16:53:33 +00007081 Addr, TrmpAddr, 20, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007082
7083 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00007084 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7085 DAG.getConstant(22, MVT::i64));
7086 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
David Greene67c9d422010-02-15 16:53:33 +00007087 TrmpAddr, 22, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007088
Dan Gohman475871a2008-07-27 21:46:04 +00007089 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00007090 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007091 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007092 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00007093 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00007094 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00007095 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00007096 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007097
7098 switch (CC) {
7099 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00007100 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00007101 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00007102 case CallingConv::X86_StdCall: {
7103 // Pass 'nest' parameter in ECX.
7104 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00007105 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007106
7107 // Check that ECX wasn't needed by an 'inreg' parameter.
7108 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00007109 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00007110
Chris Lattner58d74912008-03-12 17:45:29 +00007111 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00007112 unsigned InRegCount = 0;
7113 unsigned Idx = 1;
7114
7115 for (FunctionType::param_iterator I = FTy->param_begin(),
7116 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00007117 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00007118 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00007119 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007120
7121 if (InRegCount > 2) {
Chris Lattner75361b62010-04-07 22:58:41 +00007122 report_fatal_error("Nest register in use - reduce number of inreg parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00007123 }
7124 }
7125 break;
7126 }
7127 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00007128 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00007129 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00007130 // Pass 'nest' parameter in EAX.
7131 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00007132 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007133 break;
7134 }
7135
Dan Gohman475871a2008-07-27 21:46:04 +00007136 SDValue OutChains[4];
7137 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007138
Owen Anderson825b72b2009-08-11 20:47:22 +00007139 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7140 DAG.getConstant(10, MVT::i32));
7141 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007142
Chris Lattnera62fe662010-02-05 19:20:30 +00007143 // This is storing the opcode for MOV32ri.
7144 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00007145 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00007146 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007147 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
David Greene67c9d422010-02-15 16:53:33 +00007148 Trmp, TrmpAddr, 0, false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007149
Owen Anderson825b72b2009-08-11 20:47:22 +00007150 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7151 DAG.getConstant(1, MVT::i32));
David Greene67c9d422010-02-15 16:53:33 +00007152 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 1,
7153 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007154
Chris Lattnera62fe662010-02-05 19:20:30 +00007155 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00007156 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7157 DAG.getConstant(5, MVT::i32));
7158 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
David Greene67c9d422010-02-15 16:53:33 +00007159 TrmpAddr, 5, false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007160
Owen Anderson825b72b2009-08-11 20:47:22 +00007161 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7162 DAG.getConstant(6, MVT::i32));
David Greene67c9d422010-02-15 16:53:33 +00007163 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr, TrmpAddr, 6,
7164 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007165
Dan Gohman475871a2008-07-27 21:46:04 +00007166 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00007167 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007168 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007169 }
7170}
7171
Dan Gohmand858e902010-04-17 15:26:15 +00007172SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
7173 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007174 /*
7175 The rounding mode is in bits 11:10 of FPSR, and has the following
7176 settings:
7177 00 Round to nearest
7178 01 Round to -inf
7179 10 Round to +inf
7180 11 Round to 0
7181
7182 FLT_ROUNDS, on the other hand, expects the following:
7183 -1 Undefined
7184 0 Round to 0
7185 1 Round to nearest
7186 2 Round to +inf
7187 3 Round to -inf
7188
7189 To perform the conversion, we do:
7190 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
7191 */
7192
7193 MachineFunction &MF = DAG.getMachineFunction();
7194 const TargetMachine &TM = MF.getTarget();
7195 const TargetFrameInfo &TFI = *TM.getFrameInfo();
7196 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00007197 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007198 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007199
7200 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00007201 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007202 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007203
Owen Anderson825b72b2009-08-11 20:47:22 +00007204 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, dl, MVT::Other,
Evan Cheng8a186ae2008-09-24 23:26:36 +00007205 DAG.getEntryNode(), StackSlot);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007206
7207 // Load FP Control Word from stack slot
David Greene67c9d422010-02-15 16:53:33 +00007208 SDValue CWD = DAG.getLoad(MVT::i16, dl, Chain, StackSlot, NULL, 0,
7209 false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007210
7211 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00007212 SDValue CWD1 =
Owen Anderson825b72b2009-08-11 20:47:22 +00007213 DAG.getNode(ISD::SRL, dl, MVT::i16,
7214 DAG.getNode(ISD::AND, dl, MVT::i16,
7215 CWD, DAG.getConstant(0x800, MVT::i16)),
7216 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00007217 SDValue CWD2 =
Owen Anderson825b72b2009-08-11 20:47:22 +00007218 DAG.getNode(ISD::SRL, dl, MVT::i16,
7219 DAG.getNode(ISD::AND, dl, MVT::i16,
7220 CWD, DAG.getConstant(0x400, MVT::i16)),
7221 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007222
Dan Gohman475871a2008-07-27 21:46:04 +00007223 SDValue RetVal =
Owen Anderson825b72b2009-08-11 20:47:22 +00007224 DAG.getNode(ISD::AND, dl, MVT::i16,
7225 DAG.getNode(ISD::ADD, dl, MVT::i16,
7226 DAG.getNode(ISD::OR, dl, MVT::i16, CWD1, CWD2),
7227 DAG.getConstant(1, MVT::i16)),
7228 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007229
7230
Duncan Sands83ec4b62008-06-06 12:08:01 +00007231 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007232 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007233}
7234
Dan Gohmand858e902010-04-17 15:26:15 +00007235SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007236 EVT VT = Op.getValueType();
7237 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007238 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007239 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00007240
7241 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007242 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00007243 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00007244 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007245 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007246 }
Evan Cheng18efe262007-12-14 02:13:44 +00007247
Evan Cheng152804e2007-12-14 08:30:15 +00007248 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007249 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007250 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00007251
7252 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007253 SDValue Ops[] = {
7254 Op,
7255 DAG.getConstant(NumBits+NumBits-1, OpVT),
7256 DAG.getConstant(X86::COND_E, MVT::i8),
7257 Op.getValue(1)
7258 };
7259 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00007260
7261 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00007262 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00007263
Owen Anderson825b72b2009-08-11 20:47:22 +00007264 if (VT == MVT::i8)
7265 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007266 return Op;
7267}
7268
Dan Gohmand858e902010-04-17 15:26:15 +00007269SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007270 EVT VT = Op.getValueType();
7271 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007272 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007273 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00007274
7275 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007276 if (VT == MVT::i8) {
7277 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007278 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007279 }
Evan Cheng152804e2007-12-14 08:30:15 +00007280
7281 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007282 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007283 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00007284
7285 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007286 SDValue Ops[] = {
7287 Op,
7288 DAG.getConstant(NumBits, OpVT),
7289 DAG.getConstant(X86::COND_E, MVT::i8),
7290 Op.getValue(1)
7291 };
7292 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00007293
Owen Anderson825b72b2009-08-11 20:47:22 +00007294 if (VT == MVT::i8)
7295 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007296 return Op;
7297}
7298
Dan Gohmand858e902010-04-17 15:26:15 +00007299SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007300 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007301 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007302 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00007303
Mon P Wangaf9b9522008-12-18 21:42:19 +00007304 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
7305 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
7306 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
7307 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
7308 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
7309 //
7310 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
7311 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
7312 // return AloBlo + AloBhi + AhiBlo;
7313
7314 SDValue A = Op.getOperand(0);
7315 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007316
Dale Johannesene4d209d2009-02-03 20:21:25 +00007317 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007318 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7319 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007320 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007321 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7322 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007323 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007324 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007325 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007326 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007327 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007328 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007329 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007330 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007331 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007332 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007333 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7334 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007335 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007336 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7337 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007338 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
7339 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00007340 return Res;
7341}
7342
7343
Dan Gohmand858e902010-04-17 15:26:15 +00007344SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +00007345 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
7346 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00007347 // looks for this combo and may remove the "setcc" instruction if the "setcc"
7348 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00007349 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00007350 SDValue LHS = N->getOperand(0);
7351 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00007352 unsigned BaseOp = 0;
7353 unsigned Cond = 0;
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007354 DebugLoc dl = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00007355
7356 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007357 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00007358 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00007359 // A subtract of one will be selected as a INC. Note that INC doesn't
7360 // set CF, so we can't do this for UADDO.
7361 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
7362 if (C->getAPIntValue() == 1) {
7363 BaseOp = X86ISD::INC;
7364 Cond = X86::COND_O;
7365 break;
7366 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007367 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00007368 Cond = X86::COND_O;
7369 break;
7370 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007371 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00007372 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007373 break;
7374 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00007375 // A subtract of one will be selected as a DEC. Note that DEC doesn't
7376 // set CF, so we can't do this for USUBO.
7377 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
7378 if (C->getAPIntValue() == 1) {
7379 BaseOp = X86ISD::DEC;
7380 Cond = X86::COND_O;
7381 break;
7382 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007383 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00007384 Cond = X86::COND_O;
7385 break;
7386 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007387 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00007388 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007389 break;
7390 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00007391 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00007392 Cond = X86::COND_O;
7393 break;
7394 case ISD::UMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00007395 BaseOp = X86ISD::UMUL;
Dan Gohman653456c2009-01-07 00:15:08 +00007396 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007397 break;
7398 }
Bill Wendling3fafd932008-11-26 22:37:40 +00007399
Bill Wendling61edeb52008-12-02 01:06:39 +00007400 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007401 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007402 SDValue Sum = DAG.getNode(BaseOp, dl, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00007403
Bill Wendling61edeb52008-12-02 01:06:39 +00007404 SDValue SetCC =
Dale Johannesene4d209d2009-02-03 20:21:25 +00007405 DAG.getNode(X86ISD::SETCC, dl, N->getValueType(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00007406 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00007407
Bill Wendling61edeb52008-12-02 01:06:39 +00007408 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
7409 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00007410}
7411
Dan Gohmand858e902010-04-17 15:26:15 +00007412SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007413 EVT T = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007414 DebugLoc dl = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00007415 unsigned Reg = 0;
7416 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00007417 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007418 default:
7419 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007420 case MVT::i8: Reg = X86::AL; size = 1; break;
7421 case MVT::i16: Reg = X86::AX; size = 2; break;
7422 case MVT::i32: Reg = X86::EAX; size = 4; break;
7423 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00007424 assert(Subtarget->is64Bit() && "Node not type legal!");
7425 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00007426 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007427 }
Dale Johannesendd64c412009-02-04 00:33:20 +00007428 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), dl, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00007429 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00007430 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00007431 Op.getOperand(1),
7432 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00007433 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00007434 cpIn.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00007435 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007436 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, dl, Tys, Ops, 5);
Scott Michelfdc40a02009-02-17 22:15:04 +00007437 SDValue cpOut =
Dale Johannesendd64c412009-02-04 00:33:20 +00007438 DAG.getCopyFromReg(Result.getValue(0), dl, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00007439 return cpOut;
7440}
7441
Duncan Sands1607f052008-12-01 11:39:25 +00007442SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007443 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +00007444 assert(Subtarget->is64Bit() && "Result not type legalized?");
Owen Anderson825b72b2009-08-11 20:47:22 +00007445 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00007446 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007447 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007448 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007449 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
7450 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00007451 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00007452 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
7453 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00007454 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00007455 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00007456 rdx.getValue(1)
7457 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007458 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007459}
7460
Dan Gohmand858e902010-04-17 15:26:15 +00007461SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +00007462 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007463 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007464 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007465 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00007466 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007467 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007468 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00007469 Node->getOperand(0),
7470 Node->getOperand(1), negOp,
7471 cast<AtomicSDNode>(Node)->getSrcValue(),
7472 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00007473}
7474
Evan Cheng0db9fe62006-04-25 20:13:52 +00007475/// LowerOperation - Provide custom lowering hooks for some operations.
7476///
Dan Gohmand858e902010-04-17 15:26:15 +00007477SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007478 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007479 default: llvm_unreachable("Should not custom lower this!");
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007480 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
7481 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007482 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00007483 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007484 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
7485 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
7486 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
7487 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
7488 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
7489 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007490 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00007491 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007492 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007493 case ISD::SHL_PARTS:
7494 case ISD::SRA_PARTS:
7495 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
7496 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007497 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007498 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00007499 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007500 case ISD::FABS: return LowerFABS(Op, DAG);
7501 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007502 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00007503 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00007504 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00007505 case ISD::SELECT: return LowerSELECT(Op, DAG);
7506 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007507 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007508 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00007509 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00007510 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007511 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00007512 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
7513 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007514 case ISD::FRAME_TO_ARGS_OFFSET:
7515 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007516 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007517 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007518 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00007519 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00007520 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
7521 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00007522 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00007523 case ISD::SADDO:
7524 case ISD::UADDO:
7525 case ISD::SSUBO:
7526 case ISD::USUBO:
7527 case ISD::SMULO:
7528 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00007529 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007530 }
Chris Lattner27a6c732007-11-24 07:07:01 +00007531}
7532
Duncan Sands1607f052008-12-01 11:39:25 +00007533void X86TargetLowering::
7534ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00007535 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007536 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007537 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00007538 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00007539
7540 SDValue Chain = Node->getOperand(0);
7541 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007542 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007543 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007544 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007545 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00007546 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00007547 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00007548 SDValue Result =
7549 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
7550 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00007551 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00007552 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007553 Results.push_back(Result.getValue(2));
7554}
7555
Duncan Sands126d9072008-07-04 11:47:58 +00007556/// ReplaceNodeResults - Replace a node with an illegal result type
7557/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00007558void X86TargetLowering::ReplaceNodeResults(SDNode *N,
7559 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00007560 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007561 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00007562 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00007563 default:
Duncan Sands1607f052008-12-01 11:39:25 +00007564 assert(false && "Do not know how to custom type legalize this operation!");
7565 return;
7566 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00007567 std::pair<SDValue,SDValue> Vals =
7568 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00007569 SDValue FIST = Vals.first, StackSlot = Vals.second;
7570 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00007571 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00007572 // Return a load from the stack slot.
David Greene67c9d422010-02-15 16:53:33 +00007573 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot, NULL, 0,
7574 false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00007575 }
7576 return;
7577 }
7578 case ISD::READCYCLECOUNTER: {
Owen Anderson825b72b2009-08-11 20:47:22 +00007579 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00007580 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007581 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007582 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00007583 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007584 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007585 eax.getValue(2));
7586 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
7587 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00007588 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007589 Results.push_back(edx.getValue(1));
7590 return;
7591 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007592 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00007593 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007594 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00007595 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00007596 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
7597 DAG.getConstant(0, MVT::i32));
7598 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
7599 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00007600 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
7601 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00007602 cpInL.getValue(1));
7603 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00007604 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
7605 DAG.getConstant(0, MVT::i32));
7606 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
7607 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00007608 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00007609 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00007610 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00007611 swapInL.getValue(1));
7612 SDValue Ops[] = { swapInH.getValue(0),
7613 N->getOperand(1),
7614 swapInH.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00007615 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007616 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, dl, Tys, Ops, 3);
Dale Johannesendd64c412009-02-04 00:33:20 +00007617 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00007618 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00007619 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00007620 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00007621 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00007622 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007623 Results.push_back(cpOutH.getValue(1));
7624 return;
7625 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007626 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00007627 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
7628 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007629 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00007630 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
7631 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007632 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00007633 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
7634 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007635 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00007636 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
7637 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007638 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00007639 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
7640 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007641 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00007642 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
7643 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007644 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00007645 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
7646 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00007647 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007648}
7649
Evan Cheng72261582005-12-20 06:22:03 +00007650const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
7651 switch (Opcode) {
7652 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00007653 case X86ISD::BSF: return "X86ISD::BSF";
7654 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00007655 case X86ISD::SHLD: return "X86ISD::SHLD";
7656 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00007657 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00007658 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00007659 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00007660 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00007661 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00007662 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00007663 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
7664 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
7665 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00007666 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00007667 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00007668 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00007669 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00007670 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00007671 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00007672 case X86ISD::COMI: return "X86ISD::COMI";
7673 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00007674 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00007675 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Evan Cheng72261582005-12-20 06:22:03 +00007676 case X86ISD::CMOV: return "X86ISD::CMOV";
7677 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00007678 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00007679 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
7680 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00007681 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00007682 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00007683 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00007684 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00007685 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00007686 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
7687 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00007688 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007689 case X86ISD::MMX_PINSRW: return "X86ISD::MMX_PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00007690 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Evan Cheng8ca29322006-11-10 21:43:37 +00007691 case X86ISD::FMAX: return "X86ISD::FMAX";
7692 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00007693 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
7694 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007695 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Rafael Espindola094fad32009-04-08 21:14:34 +00007696 case X86ISD::SegmentBaseAddress: return "X86ISD::SegmentBaseAddress";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007697 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00007698 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007699 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00007700 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
7701 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007702 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
7703 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
7704 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
7705 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
7706 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
7707 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00007708 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
7709 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00007710 case X86ISD::VSHL: return "X86ISD::VSHL";
7711 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00007712 case X86ISD::CMPPD: return "X86ISD::CMPPD";
7713 case X86ISD::CMPPS: return "X86ISD::CMPPS";
7714 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
7715 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
7716 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
7717 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
7718 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
7719 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
7720 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
7721 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007722 case X86ISD::ADD: return "X86ISD::ADD";
7723 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingd350e022008-12-12 21:15:41 +00007724 case X86ISD::SMUL: return "X86ISD::SMUL";
7725 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00007726 case X86ISD::INC: return "X86ISD::INC";
7727 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00007728 case X86ISD::OR: return "X86ISD::OR";
7729 case X86ISD::XOR: return "X86ISD::XOR";
7730 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00007731 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00007732 case X86ISD::PTEST: return "X86ISD::PTEST";
Dan Gohmand6708ea2009-08-15 01:38:56 +00007733 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00007734 case X86ISD::MINGW_ALLOCA: return "X86ISD::MINGW_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00007735 }
7736}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007737
Chris Lattnerc9addb72007-03-30 23:15:24 +00007738// isLegalAddressingMode - Return true if the addressing mode represented
7739// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00007740bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00007741 const Type *Ty) const {
7742 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007743 CodeModel::Model M = getTargetMachine().getCodeModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00007744
Chris Lattnerc9addb72007-03-30 23:15:24 +00007745 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007746 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00007747 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007748
Chris Lattnerc9addb72007-03-30 23:15:24 +00007749 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00007750 unsigned GVFlags =
7751 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007752
Chris Lattnerdfed4132009-07-10 07:38:24 +00007753 // If a reference to this global requires an extra load, we can't fold it.
7754 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00007755 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007756
Chris Lattnerdfed4132009-07-10 07:38:24 +00007757 // If BaseGV requires a register for the PIC base, we cannot also have a
7758 // BaseReg specified.
7759 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00007760 return false;
Evan Cheng52787842007-08-01 23:46:47 +00007761
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007762 // If lower 4G is not available, then we must use rip-relative addressing.
7763 if (Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
7764 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00007765 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007766
Chris Lattnerc9addb72007-03-30 23:15:24 +00007767 switch (AM.Scale) {
7768 case 0:
7769 case 1:
7770 case 2:
7771 case 4:
7772 case 8:
7773 // These scales always work.
7774 break;
7775 case 3:
7776 case 5:
7777 case 9:
7778 // These scales are formed with basereg+scalereg. Only accept if there is
7779 // no basereg yet.
7780 if (AM.HasBaseReg)
7781 return false;
7782 break;
7783 default: // Other stuff never works.
7784 return false;
7785 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007786
Chris Lattnerc9addb72007-03-30 23:15:24 +00007787 return true;
7788}
7789
7790
Evan Cheng2bd122c2007-10-26 01:56:11 +00007791bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00007792 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00007793 return false;
Evan Chenge127a732007-10-29 07:57:50 +00007794 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
7795 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00007796 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00007797 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00007798 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00007799}
7800
Owen Andersone50ed302009-08-10 22:56:29 +00007801bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007802 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007803 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007804 unsigned NumBits1 = VT1.getSizeInBits();
7805 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00007806 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007807 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00007808 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007809}
Evan Cheng2bd122c2007-10-26 01:56:11 +00007810
Dan Gohman97121ba2009-04-08 00:15:30 +00007811bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00007812 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00007813 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00007814}
7815
Owen Andersone50ed302009-08-10 22:56:29 +00007816bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00007817 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00007818 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00007819}
7820
Owen Andersone50ed302009-08-10 22:56:29 +00007821bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00007822 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00007823 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00007824}
7825
Evan Cheng60c07e12006-07-05 22:17:51 +00007826/// isShuffleMaskLegal - Targets can use this to indicate that they only
7827/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
7828/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
7829/// are assumed to be legal.
7830bool
Eric Christopherfd179292009-08-27 18:07:15 +00007831X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00007832 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +00007833 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +00007834 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +00007835 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +00007836
Nate Begemana09008b2009-10-19 02:17:23 +00007837 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00007838 return (VT.getVectorNumElements() == 2 ||
7839 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
7840 isMOVLMask(M, VT) ||
7841 isSHUFPMask(M, VT) ||
7842 isPSHUFDMask(M, VT) ||
7843 isPSHUFHWMask(M, VT) ||
7844 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00007845 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00007846 isUNPCKLMask(M, VT) ||
7847 isUNPCKHMask(M, VT) ||
7848 isUNPCKL_v_undef_Mask(M, VT) ||
7849 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00007850}
7851
Dan Gohman7d8143f2008-04-09 20:09:42 +00007852bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00007853X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00007854 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00007855 unsigned NumElts = VT.getVectorNumElements();
7856 // FIXME: This collection of masks seems suspect.
7857 if (NumElts == 2)
7858 return true;
7859 if (NumElts == 4 && VT.getSizeInBits() == 128) {
7860 return (isMOVLMask(Mask, VT) ||
7861 isCommutedMOVLMask(Mask, VT, true) ||
7862 isSHUFPMask(Mask, VT) ||
7863 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00007864 }
7865 return false;
7866}
7867
7868//===----------------------------------------------------------------------===//
7869// X86 Scheduler Hooks
7870//===----------------------------------------------------------------------===//
7871
Mon P Wang63307c32008-05-05 19:05:59 +00007872// private utility function
7873MachineBasicBlock *
7874X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
7875 MachineBasicBlock *MBB,
7876 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007877 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007878 unsigned LoadOpc,
7879 unsigned CXchgOpc,
7880 unsigned copyOpc,
7881 unsigned notOpc,
7882 unsigned EAXreg,
7883 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007884 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00007885 // For the atomic bitwise operator, we generate
7886 // thisMBB:
7887 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00007888 // ld t1 = [bitinstr.addr]
7889 // op t2 = t1, [bitinstr.val]
7890 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00007891 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
7892 // bz newMBB
7893 // fallthrough -->nextMBB
7894 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7895 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007896 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00007897 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007898
Mon P Wang63307c32008-05-05 19:05:59 +00007899 /// First build the CFG
7900 MachineFunction *F = MBB->getParent();
7901 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007902 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7903 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7904 F->insert(MBBIter, newMBB);
7905 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007906
Mon P Wang63307c32008-05-05 19:05:59 +00007907 // Move all successors to thisMBB to nextMBB
7908 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007909
Mon P Wang63307c32008-05-05 19:05:59 +00007910 // Update thisMBB to fall through to newMBB
7911 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007912
Mon P Wang63307c32008-05-05 19:05:59 +00007913 // newMBB jumps to itself and fall through to nextMBB
7914 newMBB->addSuccessor(nextMBB);
7915 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007916
Mon P Wang63307c32008-05-05 19:05:59 +00007917 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007918 assert(bInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00007919 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00007920 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00007921 MachineOperand& destOper = bInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007922 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00007923 int numArgs = bInstr->getNumOperands() - 1;
7924 for (int i=0; i < numArgs; ++i)
7925 argOpers[i] = &bInstr->getOperand(i+1);
7926
7927 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007928 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
7929 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00007930
Dale Johannesen140be2d2008-08-19 18:47:28 +00007931 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007932 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00007933 for (int i=0; i <= lastAddrIndx; ++i)
7934 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007935
Dale Johannesen140be2d2008-08-19 18:47:28 +00007936 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007937 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007938 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007939 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007940 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007941 tt = t1;
7942
Dale Johannesen140be2d2008-08-19 18:47:28 +00007943 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00007944 assert((argOpers[valArgIndx]->isReg() ||
7945 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00007946 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00007947 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007948 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00007949 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007950 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007951 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00007952 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007953
Dale Johannesene4d209d2009-02-03 20:21:25 +00007954 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00007955 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007956
Dale Johannesene4d209d2009-02-03 20:21:25 +00007957 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00007958 for (int i=0; i <= lastAddrIndx; ++i)
7959 (*MIB).addOperand(*argOpers[i]);
7960 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00007961 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00007962 (*MIB).setMemRefs(bInstr->memoperands_begin(),
7963 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00007964
Dale Johannesene4d209d2009-02-03 20:21:25 +00007965 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00007966 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00007967
Mon P Wang63307c32008-05-05 19:05:59 +00007968 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00007969 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00007970
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007971 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00007972 return nextMBB;
7973}
7974
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00007975// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00007976MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007977X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
7978 MachineBasicBlock *MBB,
7979 unsigned regOpcL,
7980 unsigned regOpcH,
7981 unsigned immOpcL,
7982 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007983 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007984 // For the atomic bitwise operator, we generate
7985 // thisMBB (instructions are in pairs, except cmpxchg8b)
7986 // ld t1,t2 = [bitinstr.addr]
7987 // newMBB:
7988 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
7989 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00007990 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007991 // mov ECX, EBX <- t5, t6
7992 // mov EAX, EDX <- t1, t2
7993 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
7994 // mov t3, t4 <- EAX, EDX
7995 // bz newMBB
7996 // result in out1, out2
7997 // fallthrough -->nextMBB
7998
7999 const TargetRegisterClass *RC = X86::GR32RegisterClass;
8000 const unsigned LoadOpc = X86::MOV32rm;
8001 const unsigned copyOpc = X86::MOV32rr;
8002 const unsigned NotOpc = X86::NOT32r;
8003 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8004 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
8005 MachineFunction::iterator MBBIter = MBB;
8006 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00008007
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008008 /// First build the CFG
8009 MachineFunction *F = MBB->getParent();
8010 MachineBasicBlock *thisMBB = MBB;
8011 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
8012 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
8013 F->insert(MBBIter, newMBB);
8014 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008015
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008016 // Move all successors to thisMBB to nextMBB
8017 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008018
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008019 // Update thisMBB to fall through to newMBB
8020 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008021
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008022 // newMBB jumps to itself and fall through to nextMBB
8023 newMBB->addSuccessor(nextMBB);
8024 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008025
Dale Johannesene4d209d2009-02-03 20:21:25 +00008026 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008027 // Insert instructions into newMBB based on incoming instruction
8028 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008029 assert(bInstr->getNumOperands() < X86AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00008030 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008031 MachineOperand& dest1Oper = bInstr->getOperand(0);
8032 MachineOperand& dest2Oper = bInstr->getOperand(1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008033 MachineOperand* argOpers[2 + X86AddrNumOperands];
Dan Gohman71ea4e52010-05-14 21:01:44 +00008034 for (int i=0; i < 2 + X86AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008035 argOpers[i] = &bInstr->getOperand(i+2);
8036
Dan Gohman71ea4e52010-05-14 21:01:44 +00008037 // We use some of the operands multiple times, so conservatively just
8038 // clear any kill flags that might be present.
8039 if (argOpers[i]->isReg() && argOpers[i]->isUse())
8040 argOpers[i]->setIsKill(false);
8041 }
8042
Evan Chengad5b52f2010-01-08 19:14:57 +00008043 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008044 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00008045
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008046 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008047 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008048 for (int i=0; i <= lastAddrIndx; ++i)
8049 (*MIB).addOperand(*argOpers[i]);
8050 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008051 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00008052 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00008053 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008054 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00008055 MachineOperand newOp3 = *(argOpers[3]);
8056 if (newOp3.isImm())
8057 newOp3.setImm(newOp3.getImm()+4);
8058 else
8059 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008060 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00008061 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008062
8063 // t3/4 are defined later, at the bottom of the loop
8064 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
8065 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008066 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008067 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008068 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008069 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
8070
Evan Cheng306b4ca2010-01-08 23:41:50 +00008071 // The subsequent operations should be using the destination registers of
8072 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00008073 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00008074 t1 = F->getRegInfo().createVirtualRegister(RC);
8075 t2 = F->getRegInfo().createVirtualRegister(RC);
8076 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
8077 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008078 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00008079 t1 = dest1Oper.getReg();
8080 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008081 }
8082
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008083 int valArgIndx = lastAddrIndx + 1;
8084 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00008085 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008086 "invalid operand");
8087 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
8088 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008089 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008090 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008091 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008092 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00008093 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00008094 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008095 (*MIB).addOperand(*argOpers[valArgIndx]);
8096 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00008097 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008098 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00008099 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008100 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008101 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008102 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008103 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00008104 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00008105 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008106 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008107
Dale Johannesene4d209d2009-02-03 20:21:25 +00008108 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008109 MIB.addReg(t1);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008110 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008111 MIB.addReg(t2);
8112
Dale Johannesene4d209d2009-02-03 20:21:25 +00008113 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008114 MIB.addReg(t5);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008115 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008116 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00008117
Dale Johannesene4d209d2009-02-03 20:21:25 +00008118 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008119 for (int i=0; i <= lastAddrIndx; ++i)
8120 (*MIB).addOperand(*argOpers[i]);
8121
8122 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00008123 (*MIB).setMemRefs(bInstr->memoperands_begin(),
8124 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008125
Dale Johannesene4d209d2009-02-03 20:21:25 +00008126 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008127 MIB.addReg(X86::EAX);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008128 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008129 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00008130
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008131 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008132 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008133
8134 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
8135 return nextMBB;
8136}
8137
8138// private utility function
8139MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00008140X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
8141 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00008142 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00008143 // For the atomic min/max operator, we generate
8144 // thisMBB:
8145 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00008146 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00008147 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00008148 // cmp t1, t2
8149 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00008150 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00008151 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
8152 // bz newMBB
8153 // fallthrough -->nextMBB
8154 //
8155 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8156 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008157 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00008158 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00008159
Mon P Wang63307c32008-05-05 19:05:59 +00008160 /// First build the CFG
8161 MachineFunction *F = MBB->getParent();
8162 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008163 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
8164 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
8165 F->insert(MBBIter, newMBB);
8166 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008167
Dan Gohmand6708ea2009-08-15 01:38:56 +00008168 // Move all successors of thisMBB to nextMBB
Mon P Wang63307c32008-05-05 19:05:59 +00008169 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008170
Mon P Wang63307c32008-05-05 19:05:59 +00008171 // Update thisMBB to fall through to newMBB
8172 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008173
Mon P Wang63307c32008-05-05 19:05:59 +00008174 // newMBB jumps to newMBB and fall through to nextMBB
8175 newMBB->addSuccessor(nextMBB);
8176 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008177
Dale Johannesene4d209d2009-02-03 20:21:25 +00008178 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00008179 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008180 assert(mInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00008181 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00008182 MachineOperand& destOper = mInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008183 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00008184 int numArgs = mInstr->getNumOperands() - 1;
8185 for (int i=0; i < numArgs; ++i)
8186 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008187
Mon P Wang63307c32008-05-05 19:05:59 +00008188 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008189 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
8190 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00008191
Mon P Wangab3e7472008-05-05 22:56:23 +00008192 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008193 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00008194 for (int i=0; i <= lastAddrIndx; ++i)
8195 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00008196
Mon P Wang63307c32008-05-05 19:05:59 +00008197 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00008198 assert((argOpers[valArgIndx]->isReg() ||
8199 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00008200 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00008201
8202 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00008203 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008204 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00008205 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008206 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00008207 (*MIB).addOperand(*argOpers[valArgIndx]);
8208
Dale Johannesene4d209d2009-02-03 20:21:25 +00008209 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00008210 MIB.addReg(t1);
8211
Dale Johannesene4d209d2009-02-03 20:21:25 +00008212 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00008213 MIB.addReg(t1);
8214 MIB.addReg(t2);
8215
8216 // Generate movc
8217 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008218 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00008219 MIB.addReg(t2);
8220 MIB.addReg(t1);
8221
8222 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00008223 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00008224 for (int i=0; i <= lastAddrIndx; ++i)
8225 (*MIB).addOperand(*argOpers[i]);
8226 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00008227 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00008228 (*MIB).setMemRefs(mInstr->memoperands_begin(),
8229 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +00008230
Dale Johannesene4d209d2009-02-03 20:21:25 +00008231 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00008232 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00008233
Mon P Wang63307c32008-05-05 19:05:59 +00008234 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008235 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00008236
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008237 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00008238 return nextMBB;
8239}
8240
Eric Christopherf83a5de2009-08-27 18:08:16 +00008241// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
8242// all of this code can be replaced with that in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +00008243MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +00008244X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +00008245 unsigned numArgs, bool memArg) const {
Eric Christopherb120ab42009-08-18 22:50:32 +00008246
8247 MachineFunction *F = BB->getParent();
8248 DebugLoc dl = MI->getDebugLoc();
8249 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8250
8251 unsigned Opc;
Evan Chengce319102009-09-19 09:51:03 +00008252 if (memArg)
8253 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
8254 else
8255 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
Eric Christopherb120ab42009-08-18 22:50:32 +00008256
8257 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(Opc));
8258
8259 for (unsigned i = 0; i < numArgs; ++i) {
8260 MachineOperand &Op = MI->getOperand(i+1);
8261
8262 if (!(Op.isReg() && Op.isImplicit()))
8263 MIB.addOperand(Op);
8264 }
8265
8266 BuildMI(BB, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
8267 .addReg(X86::XMM0);
8268
8269 F->DeleteMachineInstr(MI);
8270
8271 return BB;
8272}
8273
8274MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +00008275X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
8276 MachineInstr *MI,
8277 MachineBasicBlock *MBB) const {
8278 // Emit code to save XMM registers to the stack. The ABI says that the
8279 // number of registers to save is given in %al, so it's theoretically
8280 // possible to do an indirect jump trick to avoid saving all of them,
8281 // however this code takes a simpler approach and just executes all
8282 // of the stores if %al is non-zero. It's less code, and it's probably
8283 // easier on the hardware branch predictor, and stores aren't all that
8284 // expensive anyway.
8285
8286 // Create the new basic blocks. One block contains all the XMM stores,
8287 // and one block is the final destination regardless of whether any
8288 // stores were performed.
8289 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
8290 MachineFunction *F = MBB->getParent();
8291 MachineFunction::iterator MBBIter = MBB;
8292 ++MBBIter;
8293 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
8294 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
8295 F->insert(MBBIter, XMMSaveMBB);
8296 F->insert(MBBIter, EndMBB);
8297
8298 // Set up the CFG.
8299 // Move any original successors of MBB to the end block.
8300 EndMBB->transferSuccessors(MBB);
8301 // The original block will now fall through to the XMM save block.
8302 MBB->addSuccessor(XMMSaveMBB);
8303 // The XMMSaveMBB will fall through to the end block.
8304 XMMSaveMBB->addSuccessor(EndMBB);
8305
8306 // Now add the instructions.
8307 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8308 DebugLoc DL = MI->getDebugLoc();
8309
8310 unsigned CountReg = MI->getOperand(0).getReg();
8311 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
8312 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
8313
8314 if (!Subtarget->isTargetWin64()) {
8315 // If %al is 0, branch around the XMM save block.
8316 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008317 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008318 MBB->addSuccessor(EndMBB);
8319 }
8320
8321 // In the XMM save block, save all the XMM argument registers.
8322 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
8323 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +00008324 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +00008325 F->getMachineMemOperand(
8326 PseudoSourceValue::getFixedStack(RegSaveFrameIndex),
8327 MachineMemOperand::MOStore, Offset,
8328 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008329 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
8330 .addFrameIndex(RegSaveFrameIndex)
8331 .addImm(/*Scale=*/1)
8332 .addReg(/*IndexReg=*/0)
8333 .addImm(/*Disp=*/Offset)
8334 .addReg(/*Segment=*/0)
8335 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +00008336 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008337 }
8338
8339 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8340
8341 return EndMBB;
8342}
Mon P Wang63307c32008-05-05 19:05:59 +00008343
Evan Cheng60c07e12006-07-05 22:17:51 +00008344MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +00008345X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00008346 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +00008347 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8348 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +00008349
Chris Lattner52600972009-09-02 05:57:00 +00008350 // To "insert" a SELECT_CC instruction, we actually have to insert the
8351 // diamond control-flow pattern. The incoming instruction knows the
8352 // destination vreg to set, the condition code register to branch on, the
8353 // true/false values to select between, and a branch opcode to use.
8354 const BasicBlock *LLVM_BB = BB->getBasicBlock();
8355 MachineFunction::iterator It = BB;
8356 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +00008357
Chris Lattner52600972009-09-02 05:57:00 +00008358 // thisMBB:
8359 // ...
8360 // TrueVal = ...
8361 // cmpTY ccX, r1, r2
8362 // bCC copy1MBB
8363 // fallthrough --> copy0MBB
8364 MachineBasicBlock *thisMBB = BB;
8365 MachineFunction *F = BB->getParent();
8366 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
8367 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
8368 unsigned Opc =
8369 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
8370 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
8371 F->insert(It, copy0MBB);
8372 F->insert(It, sinkMBB);
Evan Chengce319102009-09-19 09:51:03 +00008373 // Update machine-CFG edges by first adding all successors of the current
Chris Lattner52600972009-09-02 05:57:00 +00008374 // block to the new block which will contain the Phi node for the select.
Daniel Dunbara279bc32009-09-20 02:20:51 +00008375 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00008376 E = BB->succ_end(); I != E; ++I)
Evan Chengce319102009-09-19 09:51:03 +00008377 sinkMBB->addSuccessor(*I);
Evan Chengce319102009-09-19 09:51:03 +00008378 // Next, remove all successors of the current block, and add the true
8379 // and fallthrough blocks as its successors.
8380 while (!BB->succ_empty())
8381 BB->removeSuccessor(BB->succ_begin());
Chris Lattner52600972009-09-02 05:57:00 +00008382 // Add the true and fallthrough blocks as its successors.
8383 BB->addSuccessor(copy0MBB);
8384 BB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00008385
Chris Lattner52600972009-09-02 05:57:00 +00008386 // copy0MBB:
8387 // %FalseValue = ...
8388 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +00008389 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00008390
Chris Lattner52600972009-09-02 05:57:00 +00008391 // sinkMBB:
8392 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
8393 // ...
Dan Gohman3335a222010-04-30 20:14:26 +00008394 BuildMI(sinkMBB, DL, TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +00008395 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
8396 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
8397
8398 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +00008399 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +00008400}
8401
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008402MachineBasicBlock *
8403X86TargetLowering::EmitLoweredMingwAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00008404 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008405 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8406 DebugLoc DL = MI->getDebugLoc();
8407 MachineFunction *F = BB->getParent();
8408
8409 // The lowering is pretty easy: we're just emitting the call to _alloca. The
8410 // non-trivial part is impdef of ESP.
8411 // FIXME: The code should be tweaked as soon as we'll try to do codegen for
8412 // mingw-w64.
8413
8414 BuildMI(BB, DL, TII->get(X86::CALLpcrel32))
8415 .addExternalSymbol("_alloca")
8416 .addReg(X86::EAX, RegState::Implicit)
8417 .addReg(X86::ESP, RegState::Implicit)
8418 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
8419 .addReg(X86::ESP, RegState::Define | RegState::Implicit);
8420
8421 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8422 return BB;
8423}
Chris Lattner52600972009-09-02 05:57:00 +00008424
8425MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00008426X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00008427 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00008428 switch (MI->getOpcode()) {
8429 default: assert(false && "Unexpected instr type to insert");
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008430 case X86::MINGW_ALLOCA:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00008431 return EmitLoweredMingwAlloca(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +00008432 case X86::CMOV_GR8:
Mon P Wang9e5ecb82008-12-12 01:25:51 +00008433 case X86::CMOV_V1I64:
Evan Cheng60c07e12006-07-05 22:17:51 +00008434 case X86::CMOV_FR32:
8435 case X86::CMOV_FR64:
8436 case X86::CMOV_V4F32:
8437 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +00008438 case X86::CMOV_V2I64:
Chris Lattner314a1132010-03-14 18:31:44 +00008439 case X86::CMOV_GR16:
8440 case X86::CMOV_GR32:
8441 case X86::CMOV_RFP32:
8442 case X86::CMOV_RFP64:
8443 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00008444 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +00008445
Dale Johannesen849f2142007-07-03 00:53:03 +00008446 case X86::FP32_TO_INT16_IN_MEM:
8447 case X86::FP32_TO_INT32_IN_MEM:
8448 case X86::FP32_TO_INT64_IN_MEM:
8449 case X86::FP64_TO_INT16_IN_MEM:
8450 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00008451 case X86::FP64_TO_INT64_IN_MEM:
8452 case X86::FP80_TO_INT16_IN_MEM:
8453 case X86::FP80_TO_INT32_IN_MEM:
8454 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +00008455 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8456 DebugLoc DL = MI->getDebugLoc();
8457
Evan Cheng60c07e12006-07-05 22:17:51 +00008458 // Change the floating point control register to use "round towards zero"
8459 // mode when truncating to an integer value.
8460 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +00008461 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Chris Lattner52600972009-09-02 05:57:00 +00008462 addFrameReference(BuildMI(BB, DL, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008463
8464 // Load the old value of the high byte of the control word...
8465 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +00008466 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Chris Lattner52600972009-09-02 05:57:00 +00008467 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008468 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008469
8470 // Set the high part to be round to zero...
Chris Lattner52600972009-09-02 05:57:00 +00008471 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00008472 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00008473
8474 // Reload the modified control word now...
Chris Lattner52600972009-09-02 05:57:00 +00008475 addFrameReference(BuildMI(BB, DL, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008476
8477 // Restore the memory image of control word to original value
Chris Lattner52600972009-09-02 05:57:00 +00008478 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00008479 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00008480
8481 // Get the X86 opcode to use.
8482 unsigned Opc;
8483 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008484 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00008485 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
8486 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
8487 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
8488 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
8489 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
8490 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00008491 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
8492 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
8493 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00008494 }
8495
8496 X86AddressMode AM;
8497 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +00008498 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00008499 AM.BaseType = X86AddressMode::RegBase;
8500 AM.Base.Reg = Op.getReg();
8501 } else {
8502 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +00008503 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +00008504 }
8505 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +00008506 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00008507 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008508 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +00008509 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00008510 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008511 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +00008512 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00008513 AM.GV = Op.getGlobal();
8514 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00008515 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008516 }
Chris Lattner52600972009-09-02 05:57:00 +00008517 addFullAddress(BuildMI(BB, DL, TII->get(Opc)), AM)
Rafael Espindola8ef2b892009-04-08 08:09:33 +00008518 .addReg(MI->getOperand(X86AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00008519
8520 // Reload the original control word now.
Chris Lattner52600972009-09-02 05:57:00 +00008521 addFrameReference(BuildMI(BB, DL, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008522
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008523 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00008524 return BB;
8525 }
Eric Christopherb120ab42009-08-18 22:50:32 +00008526 // String/text processing lowering.
8527 case X86::PCMPISTRM128REG:
8528 return EmitPCMP(MI, BB, 3, false /* in-mem */);
8529 case X86::PCMPISTRM128MEM:
8530 return EmitPCMP(MI, BB, 3, true /* in-mem */);
8531 case X86::PCMPESTRM128REG:
8532 return EmitPCMP(MI, BB, 5, false /* in mem */);
8533 case X86::PCMPESTRM128MEM:
8534 return EmitPCMP(MI, BB, 5, true /* in mem */);
8535
8536 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +00008537 case X86::ATOMAND32:
8538 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008539 X86::AND32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008540 X86::LCMPXCHG32, X86::MOV32rr,
8541 X86::NOT32r, X86::EAX,
8542 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00008543 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +00008544 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
8545 X86::OR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008546 X86::LCMPXCHG32, X86::MOV32rr,
8547 X86::NOT32r, X86::EAX,
8548 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00008549 case X86::ATOMXOR32:
8550 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008551 X86::XOR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008552 X86::LCMPXCHG32, X86::MOV32rr,
8553 X86::NOT32r, X86::EAX,
8554 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008555 case X86::ATOMNAND32:
8556 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008557 X86::AND32ri, X86::MOV32rm,
8558 X86::LCMPXCHG32, X86::MOV32rr,
8559 X86::NOT32r, X86::EAX,
8560 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +00008561 case X86::ATOMMIN32:
8562 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
8563 case X86::ATOMMAX32:
8564 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
8565 case X86::ATOMUMIN32:
8566 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
8567 case X86::ATOMUMAX32:
8568 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +00008569
8570 case X86::ATOMAND16:
8571 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
8572 X86::AND16ri, X86::MOV16rm,
8573 X86::LCMPXCHG16, X86::MOV16rr,
8574 X86::NOT16r, X86::AX,
8575 X86::GR16RegisterClass);
8576 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +00008577 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008578 X86::OR16ri, X86::MOV16rm,
8579 X86::LCMPXCHG16, X86::MOV16rr,
8580 X86::NOT16r, X86::AX,
8581 X86::GR16RegisterClass);
8582 case X86::ATOMXOR16:
8583 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
8584 X86::XOR16ri, X86::MOV16rm,
8585 X86::LCMPXCHG16, X86::MOV16rr,
8586 X86::NOT16r, X86::AX,
8587 X86::GR16RegisterClass);
8588 case X86::ATOMNAND16:
8589 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
8590 X86::AND16ri, X86::MOV16rm,
8591 X86::LCMPXCHG16, X86::MOV16rr,
8592 X86::NOT16r, X86::AX,
8593 X86::GR16RegisterClass, true);
8594 case X86::ATOMMIN16:
8595 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
8596 case X86::ATOMMAX16:
8597 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
8598 case X86::ATOMUMIN16:
8599 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
8600 case X86::ATOMUMAX16:
8601 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
8602
8603 case X86::ATOMAND8:
8604 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
8605 X86::AND8ri, X86::MOV8rm,
8606 X86::LCMPXCHG8, X86::MOV8rr,
8607 X86::NOT8r, X86::AL,
8608 X86::GR8RegisterClass);
8609 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +00008610 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008611 X86::OR8ri, X86::MOV8rm,
8612 X86::LCMPXCHG8, X86::MOV8rr,
8613 X86::NOT8r, X86::AL,
8614 X86::GR8RegisterClass);
8615 case X86::ATOMXOR8:
8616 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
8617 X86::XOR8ri, X86::MOV8rm,
8618 X86::LCMPXCHG8, X86::MOV8rr,
8619 X86::NOT8r, X86::AL,
8620 X86::GR8RegisterClass);
8621 case X86::ATOMNAND8:
8622 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
8623 X86::AND8ri, X86::MOV8rm,
8624 X86::LCMPXCHG8, X86::MOV8rr,
8625 X86::NOT8r, X86::AL,
8626 X86::GR8RegisterClass, true);
8627 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008628 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +00008629 case X86::ATOMAND64:
8630 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008631 X86::AND64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008632 X86::LCMPXCHG64, X86::MOV64rr,
8633 X86::NOT64r, X86::RAX,
8634 X86::GR64RegisterClass);
8635 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +00008636 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
8637 X86::OR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008638 X86::LCMPXCHG64, X86::MOV64rr,
8639 X86::NOT64r, X86::RAX,
8640 X86::GR64RegisterClass);
8641 case X86::ATOMXOR64:
8642 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008643 X86::XOR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008644 X86::LCMPXCHG64, X86::MOV64rr,
8645 X86::NOT64r, X86::RAX,
8646 X86::GR64RegisterClass);
8647 case X86::ATOMNAND64:
8648 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
8649 X86::AND64ri32, X86::MOV64rm,
8650 X86::LCMPXCHG64, X86::MOV64rr,
8651 X86::NOT64r, X86::RAX,
8652 X86::GR64RegisterClass, true);
8653 case X86::ATOMMIN64:
8654 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
8655 case X86::ATOMMAX64:
8656 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
8657 case X86::ATOMUMIN64:
8658 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
8659 case X86::ATOMUMAX64:
8660 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008661
8662 // This group does 64-bit operations on a 32-bit host.
8663 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008664 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008665 X86::AND32rr, X86::AND32rr,
8666 X86::AND32ri, X86::AND32ri,
8667 false);
8668 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008669 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008670 X86::OR32rr, X86::OR32rr,
8671 X86::OR32ri, X86::OR32ri,
8672 false);
8673 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008674 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008675 X86::XOR32rr, X86::XOR32rr,
8676 X86::XOR32ri, X86::XOR32ri,
8677 false);
8678 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008679 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008680 X86::AND32rr, X86::AND32rr,
8681 X86::AND32ri, X86::AND32ri,
8682 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008683 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008684 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008685 X86::ADD32rr, X86::ADC32rr,
8686 X86::ADD32ri, X86::ADC32ri,
8687 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008688 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008689 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008690 X86::SUB32rr, X86::SBB32rr,
8691 X86::SUB32ri, X86::SBB32ri,
8692 false);
Dale Johannesen880ae362008-10-03 22:25:52 +00008693 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008694 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +00008695 X86::MOV32rr, X86::MOV32rr,
8696 X86::MOV32ri, X86::MOV32ri,
8697 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008698 case X86::VASTART_SAVE_XMM_REGS:
8699 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +00008700 }
8701}
8702
8703//===----------------------------------------------------------------------===//
8704// X86 Optimization Hooks
8705//===----------------------------------------------------------------------===//
8706
Dan Gohman475871a2008-07-27 21:46:04 +00008707void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00008708 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008709 APInt &KnownZero,
8710 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00008711 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00008712 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008713 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00008714 assert((Opc >= ISD::BUILTIN_OP_END ||
8715 Opc == ISD::INTRINSIC_WO_CHAIN ||
8716 Opc == ISD::INTRINSIC_W_CHAIN ||
8717 Opc == ISD::INTRINSIC_VOID) &&
8718 "Should use MaskedValueIsZero if you don't know whether Op"
8719 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008720
Dan Gohmanf4f92f52008-02-13 23:07:24 +00008721 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008722 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00008723 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +00008724 case X86ISD::ADD:
8725 case X86ISD::SUB:
8726 case X86ISD::SMUL:
8727 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +00008728 case X86ISD::INC:
8729 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +00008730 case X86ISD::OR:
8731 case X86ISD::XOR:
8732 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +00008733 // These nodes' second result is a boolean.
8734 if (Op.getResNo() == 0)
8735 break;
8736 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008737 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008738 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
8739 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +00008740 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008741 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008742}
Chris Lattner259e97c2006-01-31 19:43:35 +00008743
Evan Cheng206ee9d2006-07-07 08:33:52 +00008744/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +00008745/// node is a GlobalAddress + offset.
8746bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +00008747 const GlobalValue* &GA,
8748 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +00008749 if (N->getOpcode() == X86ISD::Wrapper) {
8750 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00008751 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00008752 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +00008753 return true;
8754 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00008755 }
Evan Chengad4196b2008-05-12 19:56:52 +00008756 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +00008757}
8758
Evan Cheng206ee9d2006-07-07 08:33:52 +00008759/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
8760/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
8761/// if the load addresses are consecutive, non-overlapping, and in the right
Nate Begemanfdea31a2010-03-24 20:49:50 +00008762/// order.
Dan Gohman475871a2008-07-27 21:46:04 +00008763static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00008764 const TargetLowering &TLI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00008765 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008766 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00008767 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
Mon P Wang1e955802009-04-03 02:43:30 +00008768
Eli Friedman7a5e5552009-06-07 06:52:44 +00008769 if (VT.getSizeInBits() != 128)
8770 return SDValue();
8771
Nate Begemanfdea31a2010-03-24 20:49:50 +00008772 SmallVector<SDValue, 16> Elts;
8773 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
8774 Elts.push_back(DAG.getShuffleScalarElt(SVN, i));
8775
8776 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00008777}
Evan Chengd880b972008-05-09 21:53:03 +00008778
Dan Gohman1bbf72b2010-03-15 23:23:03 +00008779/// PerformShuffleCombine - Detect vector gather/scatter index generation
8780/// and convert it from being a bunch of shuffles and extracts to a simple
8781/// store and scalar loads to extract the elements.
8782static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
8783 const TargetLowering &TLI) {
8784 SDValue InputVector = N->getOperand(0);
8785
8786 // Only operate on vectors of 4 elements, where the alternative shuffling
8787 // gets to be more expensive.
8788 if (InputVector.getValueType() != MVT::v4i32)
8789 return SDValue();
8790
8791 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
8792 // single use which is a sign-extend or zero-extend, and all elements are
8793 // used.
8794 SmallVector<SDNode *, 4> Uses;
8795 unsigned ExtractedElements = 0;
8796 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
8797 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
8798 if (UI.getUse().getResNo() != InputVector.getResNo())
8799 return SDValue();
8800
8801 SDNode *Extract = *UI;
8802 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8803 return SDValue();
8804
8805 if (Extract->getValueType(0) != MVT::i32)
8806 return SDValue();
8807 if (!Extract->hasOneUse())
8808 return SDValue();
8809 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
8810 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
8811 return SDValue();
8812 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
8813 return SDValue();
8814
8815 // Record which element was extracted.
8816 ExtractedElements |=
8817 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
8818
8819 Uses.push_back(Extract);
8820 }
8821
8822 // If not all the elements were used, this may not be worthwhile.
8823 if (ExtractedElements != 15)
8824 return SDValue();
8825
8826 // Ok, we've now decided to do the transformation.
8827 DebugLoc dl = InputVector.getDebugLoc();
8828
8829 // Store the value to a temporary stack slot.
8830 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
8831 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr, NULL, 0,
8832 false, false, 0);
8833
8834 // Replace each use (extract) with a load of the appropriate element.
8835 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
8836 UE = Uses.end(); UI != UE; ++UI) {
8837 SDNode *Extract = *UI;
8838
8839 // Compute the element's address.
8840 SDValue Idx = Extract->getOperand(1);
8841 unsigned EltSize =
8842 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
8843 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
8844 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
8845
8846 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), OffsetVal, StackPtr);
8847
8848 // Load the scalar.
8849 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch, ScalarAddr,
8850 NULL, 0, false, false, 0);
8851
8852 // Replace the exact with the load.
8853 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
8854 }
8855
8856 // The replacement was made in place; don't return anything.
8857 return SDValue();
8858}
8859
Chris Lattner83e6c992006-10-04 06:57:07 +00008860/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008861static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +00008862 const X86Subtarget *Subtarget) {
8863 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008864 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +00008865 // Get the LHS/RHS of the select.
8866 SDValue LHS = N->getOperand(1);
8867 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +00008868
Dan Gohman670e5392009-09-21 18:03:22 +00008869 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +00008870 // instructions match the semantics of the common C idiom x<y?x:y but not
8871 // x<=y?x:y, because of how they handle negative zero (which can be
8872 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +00008873 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00008874 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +00008875 Cond.getOpcode() == ISD::SETCC) {
8876 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008877
Chris Lattner47b4ce82009-03-11 05:48:52 +00008878 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +00008879 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +00008880 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
8881 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +00008882 switch (CC) {
8883 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +00008884 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +00008885 // Converting this to a min would handle NaNs incorrectly, and swapping
8886 // the operands would cause it to handle comparisons between positive
8887 // and negative zero incorrectly.
8888 if (!FiniteOnlyFPMath() &&
8889 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))) {
8890 if (!UnsafeFPMath &&
8891 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8892 break;
8893 std::swap(LHS, RHS);
8894 }
Dan Gohman670e5392009-09-21 18:03:22 +00008895 Opcode = X86ISD::FMIN;
8896 break;
8897 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +00008898 // Converting this to a min would handle comparisons between positive
8899 // and negative zero incorrectly.
8900 if (!UnsafeFPMath &&
8901 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
8902 break;
Dan Gohman670e5392009-09-21 18:03:22 +00008903 Opcode = X86ISD::FMIN;
8904 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +00008905 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +00008906 // Converting this to a min would handle both negative zeros and NaNs
8907 // incorrectly, but we can swap the operands to fix both.
8908 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00008909 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008910 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +00008911 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008912 Opcode = X86ISD::FMIN;
8913 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008914
Dan Gohman670e5392009-09-21 18:03:22 +00008915 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +00008916 // Converting this to a max would handle comparisons between positive
8917 // and negative zero incorrectly.
8918 if (!UnsafeFPMath &&
8919 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
8920 break;
Dan Gohman670e5392009-09-21 18:03:22 +00008921 Opcode = X86ISD::FMAX;
8922 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +00008923 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +00008924 // Converting this to a max would handle NaNs incorrectly, and swapping
8925 // the operands would cause it to handle comparisons between positive
8926 // and negative zero incorrectly.
8927 if (!FiniteOnlyFPMath() &&
8928 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))) {
8929 if (!UnsafeFPMath &&
8930 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8931 break;
8932 std::swap(LHS, RHS);
8933 }
Dan Gohman670e5392009-09-21 18:03:22 +00008934 Opcode = X86ISD::FMAX;
8935 break;
8936 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +00008937 // Converting this to a max would handle both negative zeros and NaNs
8938 // incorrectly, but we can swap the operands to fix both.
8939 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00008940 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008941 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008942 case ISD::SETGE:
8943 Opcode = X86ISD::FMAX;
8944 break;
Chris Lattner83e6c992006-10-04 06:57:07 +00008945 }
Dan Gohman670e5392009-09-21 18:03:22 +00008946 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +00008947 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
8948 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +00008949 switch (CC) {
8950 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +00008951 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +00008952 // Converting this to a min would handle comparisons between positive
8953 // and negative zero incorrectly, and swapping the operands would
8954 // cause it to handle NaNs incorrectly.
8955 if (!UnsafeFPMath &&
8956 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
8957 if (!FiniteOnlyFPMath() &&
8958 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
8959 break;
8960 std::swap(LHS, RHS);
8961 }
Dan Gohman670e5392009-09-21 18:03:22 +00008962 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +00008963 break;
Dan Gohman670e5392009-09-21 18:03:22 +00008964 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +00008965 // Converting this to a min would handle NaNs incorrectly.
8966 if (!UnsafeFPMath &&
8967 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
8968 break;
Dan Gohman670e5392009-09-21 18:03:22 +00008969 Opcode = X86ISD::FMIN;
8970 break;
8971 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +00008972 // Converting this to a min would handle both negative zeros and NaNs
8973 // incorrectly, but we can swap the operands to fix both.
8974 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00008975 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008976 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008977 case ISD::SETGE:
8978 Opcode = X86ISD::FMIN;
8979 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008980
Dan Gohman670e5392009-09-21 18:03:22 +00008981 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +00008982 // Converting this to a max would handle NaNs incorrectly.
8983 if (!FiniteOnlyFPMath() &&
8984 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
8985 break;
Dan Gohman670e5392009-09-21 18:03:22 +00008986 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +00008987 break;
Dan Gohman670e5392009-09-21 18:03:22 +00008988 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +00008989 // Converting this to a max would handle comparisons between positive
8990 // and negative zero incorrectly, and swapping the operands would
8991 // cause it to handle NaNs incorrectly.
8992 if (!UnsafeFPMath &&
8993 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
8994 if (!FiniteOnlyFPMath() &&
8995 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
8996 break;
8997 std::swap(LHS, RHS);
8998 }
Dan Gohman670e5392009-09-21 18:03:22 +00008999 Opcode = X86ISD::FMAX;
9000 break;
9001 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +00009002 // Converting this to a max would handle both negative zeros and NaNs
9003 // incorrectly, but we can swap the operands to fix both.
9004 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00009005 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009006 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +00009007 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009008 Opcode = X86ISD::FMAX;
9009 break;
9010 }
Chris Lattner83e6c992006-10-04 06:57:07 +00009011 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009012
Chris Lattner47b4ce82009-03-11 05:48:52 +00009013 if (Opcode)
9014 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +00009015 }
Eric Christopherfd179292009-08-27 18:07:15 +00009016
Chris Lattnerd1980a52009-03-12 06:52:53 +00009017 // If this is a select between two integer constants, try to do some
9018 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +00009019 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
9020 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +00009021 // Don't do this for crazy integer types.
9022 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
9023 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +00009024 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +00009025 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +00009026
Chris Lattnercee56e72009-03-13 05:53:31 +00009027 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +00009028 // Efficiently invertible.
9029 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
9030 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
9031 isa<ConstantSDNode>(Cond.getOperand(1))))) {
9032 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +00009033 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +00009034 }
Eric Christopherfd179292009-08-27 18:07:15 +00009035
Chris Lattnerd1980a52009-03-12 06:52:53 +00009036 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00009037 if (FalseC->getAPIntValue() == 0 &&
9038 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00009039 if (NeedsCondInvert) // Invert the condition if needed.
9040 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
9041 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009042
Chris Lattnerd1980a52009-03-12 06:52:53 +00009043 // Zero extend the condition if needed.
9044 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009045
Chris Lattnercee56e72009-03-13 05:53:31 +00009046 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +00009047 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +00009048 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +00009049 }
Eric Christopherfd179292009-08-27 18:07:15 +00009050
Chris Lattner97a29a52009-03-13 05:22:11 +00009051 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +00009052 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +00009053 if (NeedsCondInvert) // Invert the condition if needed.
9054 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
9055 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009056
Chris Lattner97a29a52009-03-13 05:22:11 +00009057 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00009058 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
9059 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00009060 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +00009061 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +00009062 }
Eric Christopherfd179292009-08-27 18:07:15 +00009063
Chris Lattnercee56e72009-03-13 05:53:31 +00009064 // Optimize cases that will turn into an LEA instruction. This requires
9065 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +00009066 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +00009067 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00009068 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +00009069
Chris Lattnercee56e72009-03-13 05:53:31 +00009070 bool isFastMultiplier = false;
9071 if (Diff < 10) {
9072 switch ((unsigned char)Diff) {
9073 default: break;
9074 case 1: // result = add base, cond
9075 case 2: // result = lea base( , cond*2)
9076 case 3: // result = lea base(cond, cond*2)
9077 case 4: // result = lea base( , cond*4)
9078 case 5: // result = lea base(cond, cond*4)
9079 case 8: // result = lea base( , cond*8)
9080 case 9: // result = lea base(cond, cond*8)
9081 isFastMultiplier = true;
9082 break;
9083 }
9084 }
Eric Christopherfd179292009-08-27 18:07:15 +00009085
Chris Lattnercee56e72009-03-13 05:53:31 +00009086 if (isFastMultiplier) {
9087 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
9088 if (NeedsCondInvert) // Invert the condition if needed.
9089 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
9090 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009091
Chris Lattnercee56e72009-03-13 05:53:31 +00009092 // Zero extend the condition if needed.
9093 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
9094 Cond);
9095 // Scale the condition by the difference.
9096 if (Diff != 1)
9097 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
9098 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009099
Chris Lattnercee56e72009-03-13 05:53:31 +00009100 // Add the base if non-zero.
9101 if (FalseC->getAPIntValue() != 0)
9102 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9103 SDValue(FalseC, 0));
9104 return Cond;
9105 }
Eric Christopherfd179292009-08-27 18:07:15 +00009106 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00009107 }
9108 }
Eric Christopherfd179292009-08-27 18:07:15 +00009109
Dan Gohman475871a2008-07-27 21:46:04 +00009110 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +00009111}
9112
Chris Lattnerd1980a52009-03-12 06:52:53 +00009113/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
9114static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
9115 TargetLowering::DAGCombinerInfo &DCI) {
9116 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +00009117
Chris Lattnerd1980a52009-03-12 06:52:53 +00009118 // If the flag operand isn't dead, don't touch this CMOV.
9119 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
9120 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +00009121
Chris Lattnerd1980a52009-03-12 06:52:53 +00009122 // If this is a select between two integer constants, try to do some
9123 // optimizations. Note that the operands are ordered the opposite of SELECT
9124 // operands.
9125 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
9126 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
9127 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
9128 // larger than FalseC (the false value).
9129 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
Eric Christopherfd179292009-08-27 18:07:15 +00009130
Chris Lattnerd1980a52009-03-12 06:52:53 +00009131 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
9132 CC = X86::GetOppositeBranchCondition(CC);
9133 std::swap(TrueC, FalseC);
9134 }
Eric Christopherfd179292009-08-27 18:07:15 +00009135
Chris Lattnerd1980a52009-03-12 06:52:53 +00009136 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00009137 // This is efficient for any integer data type (including i8/i16) and
9138 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +00009139 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
9140 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00009141 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9142 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009143
Chris Lattnerd1980a52009-03-12 06:52:53 +00009144 // Zero extend the condition if needed.
9145 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009146
Chris Lattnerd1980a52009-03-12 06:52:53 +00009147 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
9148 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +00009149 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +00009150 if (N->getNumValues() == 2) // Dead flag value?
9151 return DCI.CombineTo(N, Cond, SDValue());
9152 return Cond;
9153 }
Eric Christopherfd179292009-08-27 18:07:15 +00009154
Chris Lattnercee56e72009-03-13 05:53:31 +00009155 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
9156 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +00009157 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
9158 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00009159 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9160 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009161
Chris Lattner97a29a52009-03-13 05:22:11 +00009162 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00009163 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
9164 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00009165 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9166 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +00009167
Chris Lattner97a29a52009-03-13 05:22:11 +00009168 if (N->getNumValues() == 2) // Dead flag value?
9169 return DCI.CombineTo(N, Cond, SDValue());
9170 return Cond;
9171 }
Eric Christopherfd179292009-08-27 18:07:15 +00009172
Chris Lattnercee56e72009-03-13 05:53:31 +00009173 // Optimize cases that will turn into an LEA instruction. This requires
9174 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +00009175 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +00009176 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00009177 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +00009178
Chris Lattnercee56e72009-03-13 05:53:31 +00009179 bool isFastMultiplier = false;
9180 if (Diff < 10) {
9181 switch ((unsigned char)Diff) {
9182 default: break;
9183 case 1: // result = add base, cond
9184 case 2: // result = lea base( , cond*2)
9185 case 3: // result = lea base(cond, cond*2)
9186 case 4: // result = lea base( , cond*4)
9187 case 5: // result = lea base(cond, cond*4)
9188 case 8: // result = lea base( , cond*8)
9189 case 9: // result = lea base(cond, cond*8)
9190 isFastMultiplier = true;
9191 break;
9192 }
9193 }
Eric Christopherfd179292009-08-27 18:07:15 +00009194
Chris Lattnercee56e72009-03-13 05:53:31 +00009195 if (isFastMultiplier) {
9196 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
9197 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00009198 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9199 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +00009200 // Zero extend the condition if needed.
9201 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
9202 Cond);
9203 // Scale the condition by the difference.
9204 if (Diff != 1)
9205 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
9206 DAG.getConstant(Diff, Cond.getValueType()));
9207
9208 // Add the base if non-zero.
9209 if (FalseC->getAPIntValue() != 0)
9210 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9211 SDValue(FalseC, 0));
9212 if (N->getNumValues() == 2) // Dead flag value?
9213 return DCI.CombineTo(N, Cond, SDValue());
9214 return Cond;
9215 }
Eric Christopherfd179292009-08-27 18:07:15 +00009216 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00009217 }
9218 }
9219 return SDValue();
9220}
9221
9222
Evan Cheng0b0cd912009-03-28 05:57:29 +00009223/// PerformMulCombine - Optimize a single multiply with constant into two
9224/// in order to implement it with two cheaper instructions, e.g.
9225/// LEA + SHL, LEA + LEA.
9226static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
9227 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +00009228 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
9229 return SDValue();
9230
Owen Andersone50ed302009-08-10 22:56:29 +00009231 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009232 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +00009233 return SDValue();
9234
9235 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
9236 if (!C)
9237 return SDValue();
9238 uint64_t MulAmt = C->getZExtValue();
9239 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
9240 return SDValue();
9241
9242 uint64_t MulAmt1 = 0;
9243 uint64_t MulAmt2 = 0;
9244 if ((MulAmt % 9) == 0) {
9245 MulAmt1 = 9;
9246 MulAmt2 = MulAmt / 9;
9247 } else if ((MulAmt % 5) == 0) {
9248 MulAmt1 = 5;
9249 MulAmt2 = MulAmt / 5;
9250 } else if ((MulAmt % 3) == 0) {
9251 MulAmt1 = 3;
9252 MulAmt2 = MulAmt / 3;
9253 }
9254 if (MulAmt2 &&
9255 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
9256 DebugLoc DL = N->getDebugLoc();
9257
9258 if (isPowerOf2_64(MulAmt2) &&
9259 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
9260 // If second multiplifer is pow2, issue it first. We want the multiply by
9261 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
9262 // is an add.
9263 std::swap(MulAmt1, MulAmt2);
9264
9265 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +00009266 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +00009267 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00009268 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +00009269 else
Evan Cheng73f24c92009-03-30 21:36:47 +00009270 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +00009271 DAG.getConstant(MulAmt1, VT));
9272
Eric Christopherfd179292009-08-27 18:07:15 +00009273 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +00009274 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +00009275 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +00009276 else
Evan Cheng73f24c92009-03-30 21:36:47 +00009277 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +00009278 DAG.getConstant(MulAmt2, VT));
9279
9280 // Do not add new nodes to DAG combiner worklist.
9281 DCI.CombineTo(N, NewMul, false);
9282 }
9283 return SDValue();
9284}
9285
Evan Chengad9c0a32009-12-15 00:53:42 +00009286static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
9287 SDValue N0 = N->getOperand(0);
9288 SDValue N1 = N->getOperand(1);
9289 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
9290 EVT VT = N0.getValueType();
9291
9292 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
9293 // since the result of setcc_c is all zero's or all ones.
9294 if (N1C && N0.getOpcode() == ISD::AND &&
9295 N0.getOperand(1).getOpcode() == ISD::Constant) {
9296 SDValue N00 = N0.getOperand(0);
9297 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
9298 ((N00.getOpcode() == ISD::ANY_EXTEND ||
9299 N00.getOpcode() == ISD::ZERO_EXTEND) &&
9300 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
9301 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
9302 APInt ShAmt = N1C->getAPIntValue();
9303 Mask = Mask.shl(ShAmt);
9304 if (Mask != 0)
9305 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
9306 N00, DAG.getConstant(Mask, VT));
9307 }
9308 }
9309
9310 return SDValue();
9311}
Evan Cheng0b0cd912009-03-28 05:57:29 +00009312
Nate Begeman740ab032009-01-26 00:52:55 +00009313/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
9314/// when possible.
9315static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
9316 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +00009317 EVT VT = N->getValueType(0);
9318 if (!VT.isVector() && VT.isInteger() &&
9319 N->getOpcode() == ISD::SHL)
9320 return PerformSHLCombine(N, DAG);
9321
Nate Begeman740ab032009-01-26 00:52:55 +00009322 // On X86 with SSE2 support, we can transform this to a vector shift if
9323 // all elements are shifted by the same amount. We can't do this in legalize
9324 // because the a constant vector is typically transformed to a constant pool
9325 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009326 if (!Subtarget->hasSSE2())
9327 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009328
Owen Anderson825b72b2009-08-11 20:47:22 +00009329 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009330 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009331
Mon P Wang3becd092009-01-28 08:12:05 +00009332 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00009333 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +00009334 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +00009335 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +00009336 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
9337 unsigned NumElts = VT.getVectorNumElements();
9338 unsigned i = 0;
9339 for (; i != NumElts; ++i) {
9340 SDValue Arg = ShAmtOp.getOperand(i);
9341 if (Arg.getOpcode() == ISD::UNDEF) continue;
9342 BaseShAmt = Arg;
9343 break;
9344 }
9345 for (; i != NumElts; ++i) {
9346 SDValue Arg = ShAmtOp.getOperand(i);
9347 if (Arg.getOpcode() == ISD::UNDEF) continue;
9348 if (Arg != BaseShAmt) {
9349 return SDValue();
9350 }
9351 }
9352 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +00009353 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +00009354 SDValue InVec = ShAmtOp.getOperand(0);
9355 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
9356 unsigned NumElts = InVec.getValueType().getVectorNumElements();
9357 unsigned i = 0;
9358 for (; i != NumElts; ++i) {
9359 SDValue Arg = InVec.getOperand(i);
9360 if (Arg.getOpcode() == ISD::UNDEF) continue;
9361 BaseShAmt = Arg;
9362 break;
9363 }
9364 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
9365 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +00009366 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +00009367 if (C->getZExtValue() == SplatIdx)
9368 BaseShAmt = InVec.getOperand(1);
9369 }
9370 }
9371 if (BaseShAmt.getNode() == 0)
9372 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
9373 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +00009374 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009375 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +00009376
Mon P Wangefa42202009-09-03 19:56:25 +00009377 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +00009378 if (EltVT.bitsGT(MVT::i32))
9379 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
9380 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +00009381 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +00009382
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009383 // The shift amount is identical so we can do a vector shift.
9384 SDValue ValOp = N->getOperand(0);
9385 switch (N->getOpcode()) {
9386 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009387 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009388 break;
9389 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +00009390 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009391 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009392 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009393 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009394 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009395 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009396 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009397 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009398 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009399 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009400 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009401 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009402 break;
9403 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +00009404 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009405 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009406 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009407 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009408 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009409 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009410 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009411 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009412 break;
9413 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +00009414 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009415 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009416 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009417 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009418 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009419 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009420 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009421 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009422 if (VT == MVT::v8i16)
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_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009425 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009426 break;
Nate Begeman740ab032009-01-26 00:52:55 +00009427 }
9428 return SDValue();
9429}
9430
Evan Cheng760d1942010-01-04 21:22:48 +00009431static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +00009432 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +00009433 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +00009434 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +00009435 return SDValue();
9436
Evan Cheng760d1942010-01-04 21:22:48 +00009437 EVT VT = N->getValueType(0);
Evan Cheng8b1190a2010-04-28 01:18:01 +00009438 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
Evan Cheng760d1942010-01-04 21:22:48 +00009439 return SDValue();
9440
9441 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
9442 SDValue N0 = N->getOperand(0);
9443 SDValue N1 = N->getOperand(1);
9444 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
9445 std::swap(N0, N1);
9446 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
9447 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +00009448 if (!N0.hasOneUse() || !N1.hasOneUse())
9449 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +00009450
9451 SDValue ShAmt0 = N0.getOperand(1);
9452 if (ShAmt0.getValueType() != MVT::i8)
9453 return SDValue();
9454 SDValue ShAmt1 = N1.getOperand(1);
9455 if (ShAmt1.getValueType() != MVT::i8)
9456 return SDValue();
9457 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
9458 ShAmt0 = ShAmt0.getOperand(0);
9459 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
9460 ShAmt1 = ShAmt1.getOperand(0);
9461
9462 DebugLoc DL = N->getDebugLoc();
9463 unsigned Opc = X86ISD::SHLD;
9464 SDValue Op0 = N0.getOperand(0);
9465 SDValue Op1 = N1.getOperand(0);
9466 if (ShAmt0.getOpcode() == ISD::SUB) {
9467 Opc = X86ISD::SHRD;
9468 std::swap(Op0, Op1);
9469 std::swap(ShAmt0, ShAmt1);
9470 }
9471
Evan Cheng8b1190a2010-04-28 01:18:01 +00009472 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +00009473 if (ShAmt1.getOpcode() == ISD::SUB) {
9474 SDValue Sum = ShAmt1.getOperand(0);
9475 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Evan Cheng8b1190a2010-04-28 01:18:01 +00009476 if (SumC->getSExtValue() == Bits &&
Evan Cheng760d1942010-01-04 21:22:48 +00009477 ShAmt1.getOperand(1) == ShAmt0)
9478 return DAG.getNode(Opc, DL, VT,
9479 Op0, Op1,
9480 DAG.getNode(ISD::TRUNCATE, DL,
9481 MVT::i8, ShAmt0));
9482 }
9483 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
9484 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
9485 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +00009486 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +00009487 return DAG.getNode(Opc, DL, VT,
9488 N0.getOperand(0), N1.getOperand(0),
9489 DAG.getNode(ISD::TRUNCATE, DL,
9490 MVT::i8, ShAmt0));
9491 }
9492
9493 return SDValue();
9494}
9495
Chris Lattner149a4e52008-02-22 02:09:43 +00009496/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009497static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +00009498 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +00009499 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
9500 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +00009501 // A preferable solution to the general problem is to figure out the right
9502 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +00009503
9504 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +00009505 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +00009506 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +00009507 if (VT.getSizeInBits() != 64)
9508 return SDValue();
9509
Devang Patel578efa92009-06-05 21:57:13 +00009510 const Function *F = DAG.getMachineFunction().getFunction();
9511 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +00009512 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +00009513 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +00009514 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +00009515 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +00009516 isa<LoadSDNode>(St->getValue()) &&
9517 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
9518 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00009519 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009520 LoadSDNode *Ld = 0;
9521 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +00009522 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +00009523 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009524 // Must be a store of a load. We currently handle two cases: the load
9525 // is a direct child, and it's under an intervening TokenFactor. It is
9526 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +00009527 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +00009528 Ld = cast<LoadSDNode>(St->getChain());
9529 else if (St->getValue().hasOneUse() &&
9530 ChainVal->getOpcode() == ISD::TokenFactor) {
9531 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +00009532 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +00009533 TokenFactorIndex = i;
9534 Ld = cast<LoadSDNode>(St->getValue());
9535 } else
9536 Ops.push_back(ChainVal->getOperand(i));
9537 }
9538 }
Dale Johannesen079f2a62008-02-25 19:20:14 +00009539
Evan Cheng536e6672009-03-12 05:59:15 +00009540 if (!Ld || !ISD::isNormalLoad(Ld))
9541 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009542
Evan Cheng536e6672009-03-12 05:59:15 +00009543 // If this is not the MMX case, i.e. we are just turning i64 load/store
9544 // into f64 load/store, avoid the transformation if there are multiple
9545 // uses of the loaded value.
9546 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
9547 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009548
Evan Cheng536e6672009-03-12 05:59:15 +00009549 DebugLoc LdDL = Ld->getDebugLoc();
9550 DebugLoc StDL = N->getDebugLoc();
9551 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
9552 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
9553 // pair instead.
9554 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009555 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Evan Cheng536e6672009-03-12 05:59:15 +00009556 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(),
9557 Ld->getBasePtr(), Ld->getSrcValue(),
9558 Ld->getSrcValueOffset(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +00009559 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +00009560 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +00009561 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +00009562 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +00009563 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +00009564 Ops.size());
9565 }
Evan Cheng536e6672009-03-12 05:59:15 +00009566 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner149a4e52008-02-22 02:09:43 +00009567 St->getSrcValue(), St->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00009568 St->isVolatile(), St->isNonTemporal(),
9569 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +00009570 }
Evan Cheng536e6672009-03-12 05:59:15 +00009571
9572 // Otherwise, lower to two pairs of 32-bit loads / stores.
9573 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +00009574 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
9575 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +00009576
Owen Anderson825b72b2009-08-11 20:47:22 +00009577 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Evan Cheng536e6672009-03-12 05:59:15 +00009578 Ld->getSrcValue(), Ld->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00009579 Ld->isVolatile(), Ld->isNonTemporal(),
9580 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00009581 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Evan Cheng536e6672009-03-12 05:59:15 +00009582 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
David Greene67c9d422010-02-15 16:53:33 +00009583 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +00009584 MinAlign(Ld->getAlignment(), 4));
9585
9586 SDValue NewChain = LoLd.getValue(1);
9587 if (TokenFactorIndex != -1) {
9588 Ops.push_back(LoLd);
9589 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +00009590 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +00009591 Ops.size());
9592 }
9593
9594 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +00009595 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
9596 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +00009597
9598 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
9599 St->getSrcValue(), St->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00009600 St->isVolatile(), St->isNonTemporal(),
9601 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +00009602 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
9603 St->getSrcValue(),
9604 St->getSrcValueOffset() + 4,
9605 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +00009606 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +00009607 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +00009608 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +00009609 }
Dan Gohman475871a2008-07-27 21:46:04 +00009610 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +00009611}
9612
Chris Lattner6cf73262008-01-25 06:14:17 +00009613/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
9614/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009615static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +00009616 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
9617 // F[X]OR(0.0, x) -> x
9618 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +00009619 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
9620 if (C->getValueAPF().isPosZero())
9621 return N->getOperand(1);
9622 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
9623 if (C->getValueAPF().isPosZero())
9624 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +00009625 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00009626}
9627
9628/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009629static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +00009630 // FAND(0.0, x) -> 0.0
9631 // FAND(x, 0.0) -> 0.0
9632 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
9633 if (C->getValueAPF().isPosZero())
9634 return N->getOperand(0);
9635 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
9636 if (C->getValueAPF().isPosZero())
9637 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +00009638 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00009639}
9640
Dan Gohmane5af2d32009-01-29 01:59:02 +00009641static SDValue PerformBTCombine(SDNode *N,
9642 SelectionDAG &DAG,
9643 TargetLowering::DAGCombinerInfo &DCI) {
9644 // BT ignores high bits in the bit index operand.
9645 SDValue Op1 = N->getOperand(1);
9646 if (Op1.hasOneUse()) {
9647 unsigned BitWidth = Op1.getValueSizeInBits();
9648 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
9649 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +00009650 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
9651 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +00009652 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +00009653 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
9654 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
9655 DCI.CommitTargetLoweringOpt(TLO);
9656 }
9657 return SDValue();
9658}
Chris Lattner83e6c992006-10-04 06:57:07 +00009659
Eli Friedman7a5e5552009-06-07 06:52:44 +00009660static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
9661 SDValue Op = N->getOperand(0);
9662 if (Op.getOpcode() == ISD::BIT_CONVERT)
9663 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00009664 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +00009665 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +00009666 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +00009667 OpVT.getVectorElementType().getSizeInBits()) {
9668 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT, Op);
9669 }
9670 return SDValue();
9671}
9672
Owen Anderson99177002009-06-29 18:04:45 +00009673// On X86 and X86-64, atomic operations are lowered to locked instructions.
9674// Locked instructions, in turn, have implicit fence semantics (all memory
9675// operations are flushed before issuing the locked instruction, and the
Eric Christopherfd179292009-08-27 18:07:15 +00009676// are not buffered), so we can fold away the common pattern of
Owen Anderson99177002009-06-29 18:04:45 +00009677// fence-atomic-fence.
9678static SDValue PerformMEMBARRIERCombine(SDNode* N, SelectionDAG &DAG) {
9679 SDValue atomic = N->getOperand(0);
9680 switch (atomic.getOpcode()) {
9681 case ISD::ATOMIC_CMP_SWAP:
9682 case ISD::ATOMIC_SWAP:
9683 case ISD::ATOMIC_LOAD_ADD:
9684 case ISD::ATOMIC_LOAD_SUB:
9685 case ISD::ATOMIC_LOAD_AND:
9686 case ISD::ATOMIC_LOAD_OR:
9687 case ISD::ATOMIC_LOAD_XOR:
9688 case ISD::ATOMIC_LOAD_NAND:
9689 case ISD::ATOMIC_LOAD_MIN:
9690 case ISD::ATOMIC_LOAD_MAX:
9691 case ISD::ATOMIC_LOAD_UMIN:
9692 case ISD::ATOMIC_LOAD_UMAX:
9693 break;
9694 default:
9695 return SDValue();
9696 }
Eric Christopherfd179292009-08-27 18:07:15 +00009697
Owen Anderson99177002009-06-29 18:04:45 +00009698 SDValue fence = atomic.getOperand(0);
9699 if (fence.getOpcode() != ISD::MEMBARRIER)
9700 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +00009701
Owen Anderson99177002009-06-29 18:04:45 +00009702 switch (atomic.getOpcode()) {
9703 case ISD::ATOMIC_CMP_SWAP:
9704 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
9705 atomic.getOperand(1), atomic.getOperand(2),
9706 atomic.getOperand(3));
9707 case ISD::ATOMIC_SWAP:
9708 case ISD::ATOMIC_LOAD_ADD:
9709 case ISD::ATOMIC_LOAD_SUB:
9710 case ISD::ATOMIC_LOAD_AND:
9711 case ISD::ATOMIC_LOAD_OR:
9712 case ISD::ATOMIC_LOAD_XOR:
9713 case ISD::ATOMIC_LOAD_NAND:
9714 case ISD::ATOMIC_LOAD_MIN:
9715 case ISD::ATOMIC_LOAD_MAX:
9716 case ISD::ATOMIC_LOAD_UMIN:
9717 case ISD::ATOMIC_LOAD_UMAX:
9718 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
9719 atomic.getOperand(1), atomic.getOperand(2));
9720 default:
9721 return SDValue();
9722 }
9723}
9724
Evan Cheng2e489c42009-12-16 00:53:11 +00009725static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
9726 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
9727 // (and (i32 x86isd::setcc_carry), 1)
9728 // This eliminates the zext. This transformation is necessary because
9729 // ISD::SETCC is always legalized to i8.
9730 DebugLoc dl = N->getDebugLoc();
9731 SDValue N0 = N->getOperand(0);
9732 EVT VT = N->getValueType(0);
9733 if (N0.getOpcode() == ISD::AND &&
9734 N0.hasOneUse() &&
9735 N0.getOperand(0).hasOneUse()) {
9736 SDValue N00 = N0.getOperand(0);
9737 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
9738 return SDValue();
9739 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
9740 if (!C || C->getZExtValue() != 1)
9741 return SDValue();
9742 return DAG.getNode(ISD::AND, dl, VT,
9743 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
9744 N00.getOperand(0), N00.getOperand(1)),
9745 DAG.getConstant(1, VT));
9746 }
9747
9748 return SDValue();
9749}
9750
Dan Gohman475871a2008-07-27 21:46:04 +00009751SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +00009752 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +00009753 SelectionDAG &DAG = DCI.DAG;
9754 switch (N->getOpcode()) {
9755 default: break;
Evan Chengad4196b2008-05-12 19:56:52 +00009756 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00009757 case ISD::EXTRACT_VECTOR_ELT:
9758 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +00009759 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +00009760 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +00009761 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +00009762 case ISD::SHL:
9763 case ISD::SRA:
9764 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +00009765 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +00009766 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +00009767 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +00009768 case X86ISD::FOR: return PerformFORCombine(N, DAG);
9769 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +00009770 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +00009771 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Owen Anderson99177002009-06-29 18:04:45 +00009772 case ISD::MEMBARRIER: return PerformMEMBARRIERCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +00009773 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Evan Cheng206ee9d2006-07-07 08:33:52 +00009774 }
9775
Dan Gohman475871a2008-07-27 21:46:04 +00009776 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00009777}
9778
Evan Chenge5b51ac2010-04-17 06:13:15 +00009779/// isTypeDesirableForOp - Return true if the target has native support for
9780/// the specified value type and it is 'desirable' to use the type for the
9781/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
9782/// instruction encodings are longer and some i16 instructions are slow.
9783bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
9784 if (!isTypeLegal(VT))
9785 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00009786 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +00009787 return true;
9788
9789 switch (Opc) {
9790 default:
9791 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +00009792 case ISD::LOAD:
9793 case ISD::SIGN_EXTEND:
9794 case ISD::ZERO_EXTEND:
9795 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +00009796 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +00009797 case ISD::SRL:
9798 case ISD::SUB:
9799 case ISD::ADD:
9800 case ISD::MUL:
9801 case ISD::AND:
9802 case ISD::OR:
9803 case ISD::XOR:
9804 return false;
9805 }
9806}
9807
Evan Chengc82c20b2010-04-24 04:44:57 +00009808static bool MayFoldLoad(SDValue Op) {
9809 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
9810}
9811
9812static bool MayFoldIntoStore(SDValue Op) {
9813 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
9814}
9815
Evan Chenge5b51ac2010-04-17 06:13:15 +00009816/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +00009817/// beneficial for dag combiner to promote the specified node. If true, it
9818/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +00009819bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +00009820 EVT VT = Op.getValueType();
9821 if (VT != MVT::i16)
9822 return false;
9823
Evan Cheng4c26e932010-04-19 19:29:22 +00009824 bool Promote = false;
9825 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +00009826 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +00009827 default: break;
9828 case ISD::LOAD: {
9829 LoadSDNode *LD = cast<LoadSDNode>(Op);
9830 // If the non-extending load has a single use and it's not live out, then it
9831 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00009832 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
9833 Op.hasOneUse()*/) {
9834 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
9835 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
9836 // The only case where we'd want to promote LOAD (rather then it being
9837 // promoted as an operand is when it's only use is liveout.
9838 if (UI->getOpcode() != ISD::CopyToReg)
9839 return false;
9840 }
9841 }
Evan Cheng4c26e932010-04-19 19:29:22 +00009842 Promote = true;
9843 break;
9844 }
9845 case ISD::SIGN_EXTEND:
9846 case ISD::ZERO_EXTEND:
9847 case ISD::ANY_EXTEND:
9848 Promote = true;
9849 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +00009850 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00009851 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +00009852 SDValue N0 = Op.getOperand(0);
9853 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +00009854 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +00009855 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +00009856 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +00009857 break;
9858 }
Evan Cheng64b7bf72010-04-16 06:14:10 +00009859 case ISD::ADD:
9860 case ISD::MUL:
9861 case ISD::AND:
9862 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +00009863 case ISD::XOR:
9864 Commute = true;
9865 // fallthrough
9866 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +00009867 SDValue N0 = Op.getOperand(0);
9868 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +00009869 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +00009870 return false;
9871 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +00009872 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +00009873 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +00009874 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +00009875 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +00009876 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +00009877 }
9878 }
9879
9880 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +00009881 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +00009882}
9883
Evan Cheng60c07e12006-07-05 22:17:51 +00009884//===----------------------------------------------------------------------===//
9885// X86 Inline Assembly Support
9886//===----------------------------------------------------------------------===//
9887
Chris Lattnerb8105652009-07-20 17:51:36 +00009888static bool LowerToBSwap(CallInst *CI) {
9889 // FIXME: this should verify that we are targetting a 486 or better. If not,
9890 // we will turn this bswap into something that will be lowered to logical ops
9891 // instead of emitting the bswap asm. For now, we don't support 486 or lower
9892 // so don't worry about this.
Eric Christopherfd179292009-08-27 18:07:15 +00009893
Chris Lattnerb8105652009-07-20 17:51:36 +00009894 // Verify this is a simple bswap.
9895 if (CI->getNumOperands() != 2 ||
Eric Christopher551754c2010-04-16 23:37:20 +00009896 CI->getType() != CI->getOperand(1)->getType() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009897 !CI->getType()->isIntegerTy())
Chris Lattnerb8105652009-07-20 17:51:36 +00009898 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00009899
Chris Lattnerb8105652009-07-20 17:51:36 +00009900 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
9901 if (!Ty || Ty->getBitWidth() % 16 != 0)
9902 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00009903
Chris Lattnerb8105652009-07-20 17:51:36 +00009904 // Okay, we can do this xform, do so now.
9905 const Type *Tys[] = { Ty };
9906 Module *M = CI->getParent()->getParent()->getParent();
9907 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
Eric Christopherfd179292009-08-27 18:07:15 +00009908
Eric Christopher551754c2010-04-16 23:37:20 +00009909 Value *Op = CI->getOperand(1);
Chris Lattnerb8105652009-07-20 17:51:36 +00009910 Op = CallInst::Create(Int, Op, CI->getName(), CI);
Eric Christopherfd179292009-08-27 18:07:15 +00009911
Chris Lattnerb8105652009-07-20 17:51:36 +00009912 CI->replaceAllUsesWith(Op);
9913 CI->eraseFromParent();
9914 return true;
9915}
9916
9917bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
9918 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
9919 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
9920
9921 std::string AsmStr = IA->getAsmString();
9922
9923 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +00009924 SmallVector<StringRef, 4> AsmPieces;
Chris Lattnerb8105652009-07-20 17:51:36 +00009925 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
9926
9927 switch (AsmPieces.size()) {
9928 default: return false;
9929 case 1:
9930 AsmStr = AsmPieces[0];
9931 AsmPieces.clear();
9932 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
9933
9934 // bswap $0
9935 if (AsmPieces.size() == 2 &&
9936 (AsmPieces[0] == "bswap" ||
9937 AsmPieces[0] == "bswapq" ||
9938 AsmPieces[0] == "bswapl") &&
9939 (AsmPieces[1] == "$0" ||
9940 AsmPieces[1] == "${0:q}")) {
9941 // No need to check constraints, nothing other than the equivalent of
9942 // "=r,0" would be valid here.
9943 return LowerToBSwap(CI);
9944 }
9945 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009946 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +00009947 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +00009948 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +00009949 AsmPieces[1] == "$$8," &&
9950 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +00009951 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
9952 AsmPieces.clear();
Benjamin Kramer018cbd52010-03-12 13:54:59 +00009953 const std::string &Constraints = IA->getConstraintString();
9954 SplitString(StringRef(Constraints).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +00009955 std::sort(AsmPieces.begin(), AsmPieces.end());
9956 if (AsmPieces.size() == 4 &&
9957 AsmPieces[0] == "~{cc}" &&
9958 AsmPieces[1] == "~{dirflag}" &&
9959 AsmPieces[2] == "~{flags}" &&
9960 AsmPieces[3] == "~{fpsr}") {
9961 return LowerToBSwap(CI);
9962 }
Chris Lattnerb8105652009-07-20 17:51:36 +00009963 }
9964 break;
9965 case 3:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009966 if (CI->getType()->isIntegerTy(64) &&
Owen Anderson1d0be152009-08-13 21:58:54 +00009967 Constraints.size() >= 2 &&
Chris Lattnerb8105652009-07-20 17:51:36 +00009968 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
9969 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
9970 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramerd4f19592010-01-11 18:03:24 +00009971 SmallVector<StringRef, 4> Words;
Chris Lattnerb8105652009-07-20 17:51:36 +00009972 SplitString(AsmPieces[0], Words, " \t");
9973 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
9974 Words.clear();
9975 SplitString(AsmPieces[1], Words, " \t");
9976 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
9977 Words.clear();
9978 SplitString(AsmPieces[2], Words, " \t,");
9979 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
9980 Words[2] == "%edx") {
9981 return LowerToBSwap(CI);
9982 }
9983 }
9984 }
9985 }
9986 break;
9987 }
9988 return false;
9989}
9990
9991
9992
Chris Lattnerf4dff842006-07-11 02:54:03 +00009993/// getConstraintType - Given a constraint letter, return the type of
9994/// constraint it is for this target.
9995X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00009996X86TargetLowering::getConstraintType(const std::string &Constraint) const {
9997 if (Constraint.size() == 1) {
9998 switch (Constraint[0]) {
9999 case 'A':
Dale Johannesen330169f2008-11-13 21:52:36 +000010000 return C_Register;
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010001 case 'f':
Chris Lattner4234f572007-03-25 02:14:49 +000010002 case 'r':
10003 case 'R':
10004 case 'l':
10005 case 'q':
10006 case 'Q':
10007 case 'x':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000010008 case 'y':
Chris Lattner4234f572007-03-25 02:14:49 +000010009 case 'Y':
10010 return C_RegisterClass;
Dale Johannesen78e3e522009-02-12 20:58:09 +000010011 case 'e':
10012 case 'Z':
10013 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000010014 default:
10015 break;
10016 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000010017 }
Chris Lattner4234f572007-03-25 02:14:49 +000010018 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000010019}
10020
Dale Johannesenba2a0b92008-01-29 02:21:21 +000010021/// LowerXConstraint - try to replace an X constraint, which matches anything,
10022/// with another that has more specific requirements based on the type of the
10023/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000010024const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000010025LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000010026 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
10027 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000010028 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesenba2a0b92008-01-29 02:21:21 +000010029 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +000010030 return "Y";
10031 if (Subtarget->hasSSE1())
10032 return "x";
10033 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010034
Chris Lattner5e764232008-04-26 23:02:14 +000010035 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000010036}
10037
Chris Lattner48884cd2007-08-25 00:47:38 +000010038/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
10039/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000010040void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +000010041 char Constraint,
Evan Chengda43bcf2008-09-24 00:05:32 +000010042 bool hasMemory,
Dan Gohman475871a2008-07-27 21:46:04 +000010043 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000010044 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010045 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000010046
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010047 switch (Constraint) {
10048 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000010049 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000010050 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000010051 if (C->getZExtValue() <= 31) {
10052 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000010053 break;
10054 }
Devang Patel84f7fd22007-03-17 00:13:28 +000010055 }
Chris Lattner48884cd2007-08-25 00:47:38 +000010056 return;
Evan Cheng364091e2008-09-22 23:57:37 +000010057 case 'J':
10058 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000010059 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000010060 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
10061 break;
10062 }
10063 }
10064 return;
10065 case 'K':
10066 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000010067 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000010068 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
10069 break;
10070 }
10071 }
10072 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000010073 case 'N':
10074 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000010075 if (C->getZExtValue() <= 255) {
10076 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000010077 break;
10078 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000010079 }
Chris Lattner48884cd2007-08-25 00:47:38 +000010080 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000010081 case 'e': {
10082 // 32-bit signed value
10083 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
10084 const ConstantInt *CI = C->getConstantIntValue();
Owen Anderson1d0be152009-08-13 21:58:54 +000010085 if (CI->isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
10086 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000010087 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000010088 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000010089 break;
10090 }
10091 // FIXME gcc accepts some relocatable values here too, but only in certain
10092 // memory models; it's complicated.
10093 }
10094 return;
10095 }
10096 case 'Z': {
10097 // 32-bit unsigned value
10098 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
10099 const ConstantInt *CI = C->getConstantIntValue();
Owen Anderson1d0be152009-08-13 21:58:54 +000010100 if (CI->isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
10101 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000010102 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
10103 break;
10104 }
10105 }
10106 // FIXME gcc accepts some relocatable values here too, but only in certain
10107 // memory models; it's complicated.
10108 return;
10109 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000010110 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010111 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000010112 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000010113 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000010114 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000010115 break;
10116 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010117
Chris Lattnerdc43a882007-05-03 16:52:29 +000010118 // If we are in non-pic codegen mode, we allow the address of a global (with
10119 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000010120 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000010121 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000010122
Chris Lattner49921962009-05-08 18:23:14 +000010123 // Match either (GA), (GA+C), (GA+C1+C2), etc.
10124 while (1) {
10125 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
10126 Offset += GA->getOffset();
10127 break;
10128 } else if (Op.getOpcode() == ISD::ADD) {
10129 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
10130 Offset += C->getZExtValue();
10131 Op = Op.getOperand(0);
10132 continue;
10133 }
10134 } else if (Op.getOpcode() == ISD::SUB) {
10135 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
10136 Offset += -C->getZExtValue();
10137 Op = Op.getOperand(0);
10138 continue;
10139 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000010140 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000010141
Chris Lattner49921962009-05-08 18:23:14 +000010142 // Otherwise, this isn't something we can handle, reject it.
10143 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000010144 }
Eric Christopherfd179292009-08-27 18:07:15 +000010145
Dan Gohman46510a72010-04-15 01:51:59 +000010146 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000010147 // If we require an extra load to get this address, as in PIC mode, we
10148 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000010149 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
10150 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000010151 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000010152
Dale Johannesen60b3ba02009-07-21 00:12:29 +000010153 if (hasMemory)
10154 Op = LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
10155 else
10156 Op = DAG.getTargetGlobalAddress(GV, GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000010157 Result = Op;
10158 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010159 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000010160 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010161
Gabor Greifba36cb52008-08-28 21:40:38 +000010162 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000010163 Ops.push_back(Result);
10164 return;
10165 }
Evan Chengda43bcf2008-09-24 00:05:32 +000010166 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
10167 Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010168}
10169
Chris Lattner259e97c2006-01-31 19:43:35 +000010170std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +000010171getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000010172 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +000010173 if (Constraint.size() == 1) {
10174 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +000010175 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +000010176 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +000010177 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
10178 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000010179 if (VT == MVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010180 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
10181 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
10182 X86::R10D,X86::R11D,X86::R12D,
10183 X86::R13D,X86::R14D,X86::R15D,
10184 X86::EBP, X86::ESP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010185 else if (VT == MVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010186 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
10187 X86::SI, X86::DI, X86::R8W,X86::R9W,
10188 X86::R10W,X86::R11W,X86::R12W,
10189 X86::R13W,X86::R14W,X86::R15W,
10190 X86::BP, X86::SP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010191 else if (VT == MVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010192 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
10193 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
10194 X86::R10B,X86::R11B,X86::R12B,
10195 X86::R13B,X86::R14B,X86::R15B,
10196 X86::BPL, X86::SPL, 0);
10197
Owen Anderson825b72b2009-08-11 20:47:22 +000010198 else if (VT == MVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010199 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
10200 X86::RSI, X86::RDI, X86::R8, X86::R9,
10201 X86::R10, X86::R11, X86::R12,
10202 X86::R13, X86::R14, X86::R15,
10203 X86::RBP, X86::RSP, 0);
10204
10205 break;
10206 }
Eric Christopherfd179292009-08-27 18:07:15 +000010207 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +000010208 case 'Q': // Q_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000010209 if (VT == MVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000010210 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010211 else if (VT == MVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000010212 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010213 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +000010214 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010215 else if (VT == MVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +000010216 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
10217 break;
Chris Lattner259e97c2006-01-31 19:43:35 +000010218 }
10219 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010220
Chris Lattner1efa40f2006-02-22 00:56:39 +000010221 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +000010222}
Chris Lattnerf76d1802006-07-31 23:26:50 +000010223
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010224std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000010225X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000010226 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000010227 // First, see if this is a constraint that directly corresponds to an LLVM
10228 // register class.
10229 if (Constraint.size() == 1) {
10230 // GCC Constraint Letters
10231 switch (Constraint[0]) {
10232 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000010233 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000010234 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000010235 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +000010236 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010237 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000010238 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010239 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000010240 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000010241 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000010242 case 'R': // LEGACY_REGS
10243 if (VT == MVT::i8)
10244 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
10245 if (VT == MVT::i16)
10246 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
10247 if (VT == MVT::i32 || !Subtarget->is64Bit())
10248 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
10249 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010250 case 'f': // FP Stack registers.
10251 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
10252 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000010253 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010254 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010255 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010256 return std::make_pair(0U, X86::RFP64RegisterClass);
10257 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000010258 case 'y': // MMX_REGS if MMX allowed.
10259 if (!Subtarget->hasMMX()) break;
10260 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000010261 case 'Y': // SSE_REGS if SSE2 allowed
10262 if (!Subtarget->hasSSE2()) break;
10263 // FALL THROUGH.
10264 case 'x': // SSE_REGS if SSE1 allowed
10265 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010266
Owen Anderson825b72b2009-08-11 20:47:22 +000010267 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000010268 default: break;
10269 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000010270 case MVT::f32:
10271 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000010272 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010273 case MVT::f64:
10274 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000010275 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000010276 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000010277 case MVT::v16i8:
10278 case MVT::v8i16:
10279 case MVT::v4i32:
10280 case MVT::v2i64:
10281 case MVT::v4f32:
10282 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000010283 return std::make_pair(0U, X86::VR128RegisterClass);
10284 }
Chris Lattnerad043e82007-04-09 05:11:28 +000010285 break;
10286 }
10287 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010288
Chris Lattnerf76d1802006-07-31 23:26:50 +000010289 // Use the default implementation in TargetLowering to convert the register
10290 // constraint into a member of a register class.
10291 std::pair<unsigned, const TargetRegisterClass*> Res;
10292 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000010293
10294 // Not found as a standard register?
10295 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000010296 // Map st(0) -> st(7) -> ST0
10297 if (Constraint.size() == 7 && Constraint[0] == '{' &&
10298 tolower(Constraint[1]) == 's' &&
10299 tolower(Constraint[2]) == 't' &&
10300 Constraint[3] == '(' &&
10301 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
10302 Constraint[5] == ')' &&
10303 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000010304
Chris Lattner56d77c72009-09-13 22:41:48 +000010305 Res.first = X86::ST0+Constraint[4]-'0';
10306 Res.second = X86::RFP80RegisterClass;
10307 return Res;
10308 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000010309
Chris Lattner56d77c72009-09-13 22:41:48 +000010310 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000010311 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000010312 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000010313 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000010314 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000010315 }
Chris Lattner56d77c72009-09-13 22:41:48 +000010316
10317 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000010318 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000010319 Res.first = X86::EFLAGS;
10320 Res.second = X86::CCRRegisterClass;
10321 return Res;
10322 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000010323
Dale Johannesen330169f2008-11-13 21:52:36 +000010324 // 'A' means EAX + EDX.
10325 if (Constraint == "A") {
10326 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000010327 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000010328 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000010329 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000010330 return Res;
10331 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010332
Chris Lattnerf76d1802006-07-31 23:26:50 +000010333 // Otherwise, check to see if this is a register class of the wrong value
10334 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
10335 // turn into {ax},{dx}.
10336 if (Res.second->hasType(VT))
10337 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010338
Chris Lattnerf76d1802006-07-31 23:26:50 +000010339 // All of the single-register GCC register classes map their values onto
10340 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
10341 // really want an 8-bit or 32-bit register, map to the appropriate register
10342 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000010343 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000010344 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010345 unsigned DestReg = 0;
10346 switch (Res.first) {
10347 default: break;
10348 case X86::AX: DestReg = X86::AL; break;
10349 case X86::DX: DestReg = X86::DL; break;
10350 case X86::CX: DestReg = X86::CL; break;
10351 case X86::BX: DestReg = X86::BL; break;
10352 }
10353 if (DestReg) {
10354 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010355 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010356 }
Owen Anderson825b72b2009-08-11 20:47:22 +000010357 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010358 unsigned DestReg = 0;
10359 switch (Res.first) {
10360 default: break;
10361 case X86::AX: DestReg = X86::EAX; break;
10362 case X86::DX: DestReg = X86::EDX; break;
10363 case X86::CX: DestReg = X86::ECX; break;
10364 case X86::BX: DestReg = X86::EBX; break;
10365 case X86::SI: DestReg = X86::ESI; break;
10366 case X86::DI: DestReg = X86::EDI; break;
10367 case X86::BP: DestReg = X86::EBP; break;
10368 case X86::SP: DestReg = X86::ESP; break;
10369 }
10370 if (DestReg) {
10371 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010372 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010373 }
Owen Anderson825b72b2009-08-11 20:47:22 +000010374 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010375 unsigned DestReg = 0;
10376 switch (Res.first) {
10377 default: break;
10378 case X86::AX: DestReg = X86::RAX; break;
10379 case X86::DX: DestReg = X86::RDX; break;
10380 case X86::CX: DestReg = X86::RCX; break;
10381 case X86::BX: DestReg = X86::RBX; break;
10382 case X86::SI: DestReg = X86::RSI; break;
10383 case X86::DI: DestReg = X86::RDI; break;
10384 case X86::BP: DestReg = X86::RBP; break;
10385 case X86::SP: DestReg = X86::RSP; break;
10386 }
10387 if (DestReg) {
10388 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010389 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010390 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000010391 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000010392 } else if (Res.second == X86::FR32RegisterClass ||
10393 Res.second == X86::FR64RegisterClass ||
10394 Res.second == X86::VR128RegisterClass) {
10395 // Handle references to XMM physical registers that got mapped into the
10396 // wrong class. This can happen with constraints like {xmm0} where the
10397 // target independent register mapper will just pick the first match it can
10398 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000010399 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000010400 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000010401 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000010402 Res.second = X86::FR64RegisterClass;
10403 else if (X86::VR128RegisterClass->hasType(VT))
10404 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000010405 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010406
Chris Lattnerf76d1802006-07-31 23:26:50 +000010407 return Res;
10408}